github.com/andy2046/gopie@v0.7.0/pkg/base58/base58_test.go (about)

     1  package base58_test
     2  
     3  import (
     4  	. "github.com/andy2046/gopie/pkg/base58"
     5  	"math"
     6  	"math/rand"
     7  	"testing"
     8  	"time"
     9  )
    10  
    11  func TestEncode(t *testing.T) {
    12  	testcases := map[uint64]string{
    13  		0:              "1",
    14  		32:             "Z",
    15  		57:             "z",
    16  		math.MaxUint8:  "5Q",
    17  		math.MaxUint16: "LUv",
    18  		math.MaxUint32: "7YXq9G",
    19  		math.MaxUint64: "jpXCZedGfVQ",
    20  	}
    21  	for k, v := range testcases {
    22  		r := Encode(k)
    23  		if v != r {
    24  			t.Errorf("expected %s got %s", v, r)
    25  		}
    26  	}
    27  }
    28  
    29  func TestDecode(t *testing.T) {
    30  	_, err := Decode("")
    31  	if err == nil {
    32  		t.Fail()
    33  	}
    34  	_, err = Decode("0")
    35  	if err == nil {
    36  		t.Fail()
    37  	}
    38  
    39  	testcases := map[uint64]string{
    40  		0:              "1",
    41  		32:             "Z",
    42  		57:             "z",
    43  		math.MaxUint8:  "5Q",
    44  		math.MaxUint16: "LUv",
    45  		math.MaxUint32: "7YXq9G",
    46  		math.MaxUint64: "jpXCZedGfVQ",
    47  	}
    48  	for k, v := range testcases {
    49  		r, err := Decode(v)
    50  		if err != nil {
    51  			t.Fatal(err)
    52  		}
    53  		if k != r {
    54  			t.Errorf("expected %d got %d", k, r)
    55  		}
    56  	}
    57  }
    58  
    59  func TestReverse(t *testing.T) {
    60  	testcases := map[string]string{
    61  		"":    "",
    62  		"1":   "1",
    63  		"ABC": "CBA",
    64  		"xyz": "zyx",
    65  	}
    66  	for k, v := range testcases {
    67  		r := []byte(k)
    68  		Reverse(r)
    69  		if v != string(r) {
    70  			t.Errorf("expected %s got %s", v, string(r))
    71  		}
    72  	}
    73  }
    74  
    75  func BenchmarkEncode(b *testing.B) {
    76  	s := rand.New(rand.NewSource(time.Now().UnixNano()))
    77  
    78  	b.ReportAllocs()
    79  	b.ResetTimer()
    80  	for i := 0; i < b.N; i++ {
    81  		Encode(uint64(s.Int63()))
    82  	}
    83  }
    84  
    85  func BenchmarkDecode(b *testing.B) {
    86  	arr := []string{"1", "Z", "z", "5Q", "LUv", "7YXq9G", "jpXCZedGfVQ"}
    87  	ln := len(arr)
    88  	s := rand.New(rand.NewSource(time.Now().UnixNano()))
    89  
    90  	b.ReportAllocs()
    91  	b.ResetTimer()
    92  	for i := 0; i < b.N; i++ {
    93  		_, err := Decode(arr[s.Intn(ln)])
    94  		if err != nil {
    95  			b.Fatal(err)
    96  		}
    97  	}
    98  }