-
Notifications
You must be signed in to change notification settings - Fork 0
/
bert_test.go
65 lines (53 loc) · 2.25 KB
/
bert_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
package bert
import (
"fmt";
"strings";
"bytes";
"testing";
)
func assertBytesEqual(t *testing.T, message string, expected, actual []byte) {
if !bytes.Equal(expected, actual) {
t.Errorf("%s: '%s' != '%s'", message, expected, actual);
}
}
func testEncode(t *testing.T, message string, val interface{}, expected []byte) {
out := new(bytes.Buffer);
Encode(out, val);
assertBytesEqual(t, message, expected, out.Bytes());
}
func TestEncodeNil(t *testing.T) {
expected := []byte {MAGIC, SMALL_TUPLE, 2, ATOM, 4, 'b', 'e', 'r', 't', ATOM, 3, 'n', 'i', 'l'};
testEncode(t, "Encoding nil failed", nil, expected);
}
func TestEncodeTrue(t *testing.T) {
expected := []byte {MAGIC, SMALL_TUPLE, 2, ATOM, 4, 'b', 'e', 'r', 't', ATOM, 4, 't', 'r', 'u', 'e'};
testEncode(t, "Encoding true failed", true, expected);
}
func TestEncodeFalse(t *testing.T) {
expected := []byte {MAGIC, SMALL_TUPLE, 2, ATOM, 4, 'b', 'e', 'r', 't', ATOM, 5, 'f', 'a', 'l', 's', 'e'};
testEncode(t, "Encoding true failed", false, expected);
}
func TestEncodeFloat(t *testing.T) {
expected := bytes.Add([]byte {MAGIC, FLOAT}, strings.Bytes(fmt.Sprintf("%.20e", float(1.2e3))));
testEncode(t, "Encoding float failed", 1.2e3, expected);
}
func TestEncodeFloat32(t *testing.T) {
expected := bytes.Add([]byte {MAGIC, FLOAT}, strings.Bytes(fmt.Sprintf("%.20e", float32(1.2e3))));
testEncode(t, "Encoding float32 failed", 1.2e3, expected);
}
func TestEncodeFloat64(t *testing.T) {
expected := bytes.Add([]byte {MAGIC, FLOAT}, strings.Bytes(fmt.Sprintf("%.20e", float64(1.2e3))));
testEncode(t, "Encoding float64 failed", 1.2e3, expected);
}
func TestEncodeString(t *testing.T) {
expected := bytes.Add([]byte {MAGIC, BIN, 0, 0, 0, 3}, strings.Bytes("foo"));
testEncode(t, "Encoding string failed", "foo", expected);
}
func TestEncodeArray(t *testing.T) {
expected := []byte {MAGIC, LIST, 0, 0, 0, 2, SMALL_INT, 1, SMALL_INT, 2, NIL};
testEncode(t, "Encoding array failed", []byte {1, 2}, expected);
}
func TestEncodeMap(t *testing.T) {
expected := []byte {MAGIC, SMALL_TUPLE, 3, ATOM, 4, 'b', 'e', 'r', 't', ATOM, 4, 'd', 'i', 'c', 't', LIST, 0, 0, 0, 1, SMALL_TUPLE, 2, SMALL_INT, 1, SMALL_INT, 2, NIL};
testEncode(t, "Encoding map failed", map[byte]byte {1: 2}, expected);
}