github.com/scottcagno/storage@v1.8.0/pkg/util/testing.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io"
     7  	"log"
     8  	"net/http"
     9  	"os"
    10  	"reflect"
    11  	"runtime"
    12  	"strconv"
    13  	"strings"
    14  	"testing"
    15  	"text/tabwriter"
    16  )
    17  
    18  func Trace() string {
    19  	pc := make([]uintptr, 10) // at least 1 entry needed
    20  	runtime.Callers(3, pc)
    21  	f := runtime.FuncForPC(pc[0])
    22  	file, line := f.FileLine(pc[0])
    23  	sfile := strings.Split(file, "/")
    24  	sname := strings.Split(f.Name(), "/")
    25  	return fmt.Sprintf("[%s:%d %s]", sfile[len(sfile)-1], line, sname[len(sname)-1])
    26  }
    27  
    28  func BtoKB(b uint64) uint64 {
    29  	return b / 1024
    30  }
    31  
    32  func BtoMB(b uint64) uint64 {
    33  	return b / 1024 / 1024
    34  }
    35  
    36  func BtoGB(b uint64) uint64 {
    37  	return b / 1024 / 1024 / 1024
    38  }
    39  
    40  func PrintStats(mem runtime.MemStats) {
    41  	runtime.ReadMemStats(&mem)
    42  	fmt.Printf("\t[MEASURMENT]\t[BYTES]\t\t[KB]\t\t[MB]\t[GC=%d]\n", mem.NumGC)
    43  	fmt.Printf("\tmem.Alloc:\t\t%d\t%d\t\t%d\n", mem.Alloc, BtoKB(mem.Alloc), BtoMB(mem.Alloc))
    44  	fmt.Printf("\tmem.TotalAlloc:\t%d\t%d\t\t%d\n", mem.TotalAlloc, BtoKB(mem.TotalAlloc), BtoMB(mem.TotalAlloc))
    45  	fmt.Printf("\tmem.HeapAlloc:\t%d\t%d\t\t%d\n", mem.HeapAlloc, BtoKB(mem.HeapAlloc), BtoMB(mem.HeapAlloc))
    46  	fmt.Printf("\t-----\n\n")
    47  }
    48  
    49  func PrintStatsTab(mem runtime.MemStats) {
    50  	runtime.ReadMemStats(&mem)
    51  	w := new(tabwriter.Writer)
    52  	w.Init(os.Stdout, 5, 4, 4, ' ', tabwriter.AlignRight)
    53  	fmt.Fprintln(w, "Alloc\tTotalAlloc\tHeapAlloc\tNumGC\t")
    54  	fmt.Fprintf(w, "%v\t%v\t%v\t%v\t\n", mem.Alloc, mem.TotalAlloc, mem.HeapAlloc, mem.NumGC)
    55  	fmt.Fprintln(w, "-----\t-----\t-----\t-----\t")
    56  	w.Flush()
    57  }
    58  
    59  func DEBUG(format string, v ...interface{}) {
    60  	log.Printf(Trace()+" "+format, v...)
    61  }
    62  
    63  func AssertExpected(t *testing.T, expected, got interface{}) bool {
    64  	if !reflect.DeepEqual(expected, got) {
    65  		t.Errorf("error, expected: %v, got: %v\n", expected, got)
    66  		return false
    67  	}
    68  	return true
    69  }
    70  
    71  func AssertLen(t *testing.T, expected, got interface{}) bool {
    72  	return AssertExpected(t, expected, got)
    73  }
    74  
    75  func AssertEqual(t *testing.T, expected, got interface{}) bool {
    76  	return AssertExpected(t, expected, got)
    77  }
    78  
    79  func AssertTrue(t *testing.T, got interface{}) bool {
    80  	return AssertExpected(t, true, got)
    81  }
    82  
    83  func AssertError(t *testing.T, got interface{}) bool {
    84  	return AssertExpected(t, got, got)
    85  }
    86  
    87  func AssertNoError(t *testing.T, got interface{}) bool {
    88  	return AssertExpected(t, nil, got)
    89  }
    90  
    91  func AssertNil(t *testing.T, got interface{}) bool {
    92  	return AssertExpected(t, nil, got)
    93  }
    94  
    95  func AssertNotNil(t *testing.T, got interface{}) bool {
    96  	return got != nil
    97  }
    98  
    99  func GetListOfRandomWordsHttp(num int) []string {
   100  	host := "https://random-word-api.herokuapp.com"
   101  	var api string
   102  	if num == -1 {
   103  		api = "/all"
   104  	} else {
   105  		api = "/word?number=" + strconv.Itoa(num)
   106  	}
   107  	resp, err := http.Get(host + api)
   108  	if err != nil {
   109  		log.Panic(err)
   110  	}
   111  	defer resp.Body.Close()
   112  	body, err := io.ReadAll(resp.Body)
   113  	if err != nil {
   114  		log.Panic(err)
   115  	}
   116  	var resplist []string
   117  	err = json.Unmarshal(body, &resplist)
   118  	if err != nil {
   119  		log.Panic(err)
   120  	}
   121  	return resplist
   122  }