This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
package langext
|
||||
|
||||
import (
|
||||
"git.blackforestbytes.com/BlackForestBytes/goext/tst"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestEncodeBase62Zero(t *testing.T) {
|
||||
tst.AssertEqual(t, EncodeBase62(0), "0")
|
||||
}
|
||||
|
||||
func TestEncodeBase62Small(t *testing.T) {
|
||||
tst.AssertEqual(t, EncodeBase62(1), "1")
|
||||
tst.AssertEqual(t, EncodeBase62(9), "9")
|
||||
tst.AssertEqual(t, EncodeBase62(10), "A")
|
||||
tst.AssertEqual(t, EncodeBase62(35), "Z")
|
||||
tst.AssertEqual(t, EncodeBase62(36), "a")
|
||||
tst.AssertEqual(t, EncodeBase62(61), "z")
|
||||
tst.AssertEqual(t, EncodeBase62(62), "10")
|
||||
}
|
||||
|
||||
func TestDecodeBase62Empty(t *testing.T) {
|
||||
_, err := DecodeBase62("")
|
||||
if err == nil {
|
||||
t.Errorf("expected error on empty input")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeBase62Invalid(t *testing.T) {
|
||||
_, err := DecodeBase62("foo!bar")
|
||||
if err == nil {
|
||||
t.Errorf("expected error on invalid character")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeBase62Basic(t *testing.T) {
|
||||
v, err := DecodeBase62("0")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
tst.AssertEqual(t, v, uint64(0))
|
||||
|
||||
v, err = DecodeBase62("10")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
tst.AssertEqual(t, v, uint64(62))
|
||||
}
|
||||
|
||||
func TestEncodeDecodeBase62RoundTrip(t *testing.T) {
|
||||
for _, n := range []uint64{0, 1, 61, 62, 100, 12345, 1<<32 - 1, 1 << 40} {
|
||||
s := EncodeBase62(n)
|
||||
v, err := DecodeBase62(s)
|
||||
if err != nil {
|
||||
t.Errorf("decode error for %d (encoded %q): %v", n, s, err)
|
||||
continue
|
||||
}
|
||||
tst.AssertEqual(t, v, n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandBase62Length(t *testing.T) {
|
||||
for _, l := range []int{0, 1, 8, 32, 64} {
|
||||
s := RandBase62(l)
|
||||
tst.AssertEqual(t, len(s), l)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandBase62Alphabet(t *testing.T) {
|
||||
const alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
s := RandBase62(256)
|
||||
for _, r := range s {
|
||||
if !strings.ContainsRune(alphabet, r) {
|
||||
t.Errorf("character %q not in base62 alphabet", string(r))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandBase62Distinct(t *testing.T) {
|
||||
a := RandBase62(32)
|
||||
b := RandBase62(32)
|
||||
if a == b {
|
||||
t.Errorf("two base62 random strings of length 32 should not be equal")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user