156 lines
3.3 KiB
Go
156 lines
3.3 KiB
Go
package crockford32
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestEncode(t *testing.T) {
|
|
runAll := true
|
|
|
|
type testData struct {
|
|
TestName string
|
|
RunIt bool
|
|
Input []byte
|
|
Expected string
|
|
}
|
|
|
|
tests := []testData{
|
|
{
|
|
TestName: "nil",
|
|
RunIt: false || runAll,
|
|
Input: nil,
|
|
Expected: "",
|
|
},
|
|
{
|
|
TestName: "1 byte",
|
|
RunIt: false || runAll,
|
|
Input: []byte{0b11111},
|
|
Expected: "0Z",
|
|
},
|
|
{
|
|
TestName: "2 bytes",
|
|
RunIt: false || runAll,
|
|
Input: []byte{0b00000011, 0b11111111},
|
|
Expected: "00ZZ",
|
|
},
|
|
{
|
|
TestName: "valid ulid",
|
|
RunIt: false || runAll,
|
|
Input: []byte{1, 141, 178, 57, 150, 88, 1, 148, 253, 194, 250, 47, 252, 192, 65, 211},
|
|
Expected: "01HPS3K5JR06AFVGQT5ZYC0GEK",
|
|
},
|
|
{
|
|
TestName: "quick brown fox",
|
|
RunIt: false || runAll,
|
|
Input: []byte("The quick brown fox jumps over the lazy dog."),
|
|
Expected: "1A6GS90E5TPJRVB41H74VVQDRG6CVVR41N7AVBGECG6YXK5E8G78T3541P62YKS41J6YSSE",
|
|
},
|
|
{
|
|
TestName: "max ULID",
|
|
RunIt: false || runAll,
|
|
Input: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
|
|
Expected: "7ZZZZZZZZZZZZZZZZZZZZZZZZZ",
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.TestName, func(t *testing.T) {
|
|
if !test.RunIt {
|
|
t.SkipNow()
|
|
}
|
|
output := Encode(test.Input)
|
|
if test.Expected != output {
|
|
t.Errorf("expected %v, got %v", test.Expected, output)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDecode(t *testing.T) {
|
|
runAll := true
|
|
|
|
type testData struct {
|
|
TestName string
|
|
RunIt bool
|
|
Input string
|
|
Expected []byte
|
|
Error bool
|
|
}
|
|
|
|
tests := []testData{
|
|
{
|
|
TestName: "nil",
|
|
RunIt: false || runAll,
|
|
Input: "",
|
|
Expected: []byte{},
|
|
Error: false,
|
|
},
|
|
{
|
|
TestName: "invalid character",
|
|
RunIt: false || runAll,
|
|
Input: "ABCDF!@#$%",
|
|
Expected: nil,
|
|
Error: true,
|
|
},
|
|
{
|
|
TestName: "1 byte",
|
|
RunIt: false || runAll,
|
|
Input: "0Z",
|
|
Expected: []byte{0b11111},
|
|
Error: false,
|
|
},
|
|
{
|
|
TestName: "2 bytes",
|
|
RunIt: false || runAll,
|
|
Input: "00ZZ",
|
|
Expected: []byte{0b00000011, 0b11111111},
|
|
Error: false,
|
|
},
|
|
{
|
|
TestName: "valid ulid",
|
|
RunIt: false || runAll,
|
|
Input: "01HPS3K5JR06AFVGQT5ZYC0GEK",
|
|
Expected: []byte{1, 141, 178, 57, 150, 88, 1, 148, 253, 194, 250, 47, 252, 192, 65, 211},
|
|
Error: false,
|
|
},
|
|
{
|
|
TestName: "quick brown fox",
|
|
RunIt: false || runAll,
|
|
Input: "1A6GS90E5TPJRVB41H74VVQDRG6CVVR41N7AVBGECG6YXK5E8G78T3541P62YKS41J6YSSE",
|
|
Expected: []byte("The quick brown fox jumps over the lazy dog."),
|
|
Error: false,
|
|
},
|
|
{
|
|
TestName: "max ULID",
|
|
RunIt: false || runAll,
|
|
Input: "7ZZZZZZZZZZZZZZZZZZZZZZZZZ",
|
|
Expected: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
|
|
Error: false,
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.TestName, func(t *testing.T) {
|
|
if !test.RunIt {
|
|
t.SkipNow()
|
|
}
|
|
output, err := Decode(test.Input)
|
|
if test.Error && err == nil {
|
|
t.Errorf("expected an error")
|
|
} else {
|
|
msg := fmt.Sprintf("expected %b, got %b", test.Expected, output)
|
|
if len(test.Expected) != len(output) {
|
|
t.Fatalf(msg)
|
|
}
|
|
|
|
for i := range test.Expected {
|
|
if test.Expected[i] != output[i] {
|
|
t.Errorf(msg)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|