-
Notifications
You must be signed in to change notification settings - Fork 0
/
bijectiv_test.go
79 lines (63 loc) · 1.48 KB
/
bijectiv_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Bijectiv - https://github.com/leonardoeloy/bijectiv
// Copyright 2015 Leonardo Eloy
package bijectiv
import (
"testing"
)
func TestNew(t *testing.T) {
b := New()
if b == nil {
t.Fatal("Received nil instance")
}
if b.Alphabet != default_alphabet {
t.Errorf("Expected default alphabet [%s], but got [%s]", default_alphabet, b.Alphabet)
t.Fail()
}
if b.Base != len(default_alphabet) {
t.Errorf("Expected base to be [%d], but got [%d]", len(default_alphabet), b.Base)
t.FailNow()
}
}
func TestNewWithAlphabet(t *testing.T) {
alpha := "ABCD"
b := NewAlphabet(alpha)
if b == nil {
t.Fatal("Received nil instance")
}
if b.Alphabet != alpha {
t.Errorf("Expected default alphabet [%s], but got [%s]", alpha, b.Alphabet)
t.Fail()
}
if b.Base != len(alpha) {
t.Errorf("Expected base to be [%d], but got [%d]", len(alpha), b.Base)
t.Fail()
}
}
func TestEncode(t *testing.T) {
b := New()
value := 100
n := b.Encode(value)
expected := "bM"
if n != expected {
t.Errorf("Expected encoding of [%d] to be [%s], got [%s]", value, expected, n)
t.Fail()
}
}
func TestDencode(t *testing.T) {
b := New()
value := "bM"
n := b.Decode(value)
expected := 100
if n != expected {
t.Errorf("Expected decoding of [%s] to be [%d], got [%d]", value, expected, n)
t.Fail()
}
}
func TestReturnFirstLetterIfZero(t *testing.T) {
b := New()
n := b.Encode(0)
if n != b.Alphabet[:0] {
t.Errorf("Expected encoding of [0] to be [%s], got [%s]", b.Alphabet[:0], n)
t.Fail()
}
}