-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
68 lines (55 loc) · 1.57 KB
/
util_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
package main
import (
"bytes"
"math/big"
"testing"
)
// Verify hash conversion
func TestHash(t *testing.T) {
address := "127.0.0.1:4232"
expected := hash(address)
haStr := string(expected)
actual := []byte(haStr)
if bytes.Compare(actual, expected) != 0 {
t.Errorf("Want: %x; Got: %x\n", expected, actual)
}
actual = []byte("sdsdsjasdjkgasjhdgjhasvdjhasvdjhvsajhdvjhasvdjhsvdjhvshjdvhjsvdjhs")
if bytes.Compare(actual, expected) == 0 {
t.Errorf("Want: %x; Got: %x should be different.\n", expected, actual)
}
}
func TestMsgDataSerialization(t *testing.T) {
data := make(map[string]interface{})
data["Key"] = "Name"
data["Value"] = "chord"
m := &msg{make(map[string]interface{})}
m.content["data"] = data
b, _ := m.Marshal()
m = &msg{}
d, _ := m.Unmarshal(b)
dm := d["data"].(map[string]interface{})
if dm["Value"] != "chord" {
t.Errorf("Want %s; Got: %s", data["Value"], dm["Value"])
}
}
// test if there is any difference between
// between bytes comparison and big int comparison
func TestIdMath(t *testing.T) {
x := hash("132")
a := hash("123")
b := hash("chord")
xBigInt := new(big.Int).SetBytes(x)
aBigInt := new(big.Int).SetBytes(a)
bBigInt := new(big.Int).SetBytes(b)
actual := bytes.Compare(x, a) | bytes.Compare(x, b) | bytes.Compare(a, b) // 1 | 1 | -1
expected := (xBigInt.Cmp(aBigInt) | xBigInt.Cmp(bBigInt) | aBigInt.Cmp(bBigInt)) // -1
// Test equals
if actual != expected {
t.Errorf("Got: %d; Want: %d", actual, expected)
}
// Test not equal
actual &= 1
if actual == expected {
t.Errorf("Got: %d; Want: %d", actual, expected)
}
}