-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinteger_test.go
62 lines (53 loc) · 913 Bytes
/
integer_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
package snmp
import (
"bytes"
"encoding/asn1"
"testing"
)
func compEncoding(i int) bool {
b1, _ := asn1.Marshal(i)
b2, _ := Int(i).Encode()
return bytes.Equal(b1, b2)
}
func compDecoding(b []byte) bool {
i := 0
asn1.Unmarshal(b, &i)
dataType, _, _ := decode(bytes.NewReader(b))
return i == int(dataType.(Int))
}
func TestIntegerEncoding(t *testing.T) {
cases := [...]int{
0,
-127,
127,
128,
-128,
12345,
-12345,
1,
-1,
}
for _, testCase := range cases {
if !compEncoding(testCase) {
t.Errorf("encoding failed for %d", testCase)
}
}
}
func TestIntegerDecoding(t *testing.T) {
cases := [...][]byte{
{2, 1, 0},
{2, 1, 129},
{2, 1, 127},
{2, 2, 0, 128},
{2, 1, 128},
{2, 2, 48, 57},
{2, 2, 207, 199},
{2, 1, 1},
{2, 1, 255},
}
for _, testCase := range cases {
if !compDecoding(testCase) {
t.Errorf("encoding failed for %d", testCase)
}
}
}