-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathuint160_test.go
82 lines (67 loc) · 1.76 KB
/
uint160_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
80
81
82
package common
import (
"encoding/hex"
"encoding/json"
"testing"
)
// NKN address: NKNPkGps7i6yye6W2qSVsQUgiyqxWHEoTRby
// ToHexString(): 867ff0ca31905faec269949de37d27773ba7394e
const NKNADDRESS = "\"NKNPkGps7i6yye6W2qSVsQUgiyqxWHEoTRby\""
const NKNADDRESS_HEX = "867ff0ca31905faec269949de37d27773ba7394e"
func TestMarshalJSON(t *testing.T) {
// Create instance of Uint160
f := new(Uint160)
dat, err := hex.DecodeString(NKNADDRESS_HEX)
if err != nil {
t.Fatal(err)
}
f.SetBytes(dat)
// Expected value after Marshalling
expected := []byte(NKNADDRESS)
bytes, err := json.Marshal(f)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !bytesEqual(bytes, expected) {
t.Fatalf("Expected %v, got %v", expected, bytes)
}
bytes, err = json.Marshal(*f)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !bytesEqual(bytes, expected) {
t.Fatalf("Expected %v, got %v", expected, bytes)
}
// TODO: If possible, create a scenario where ToAddress returns an error
// and verify that MarshalJSON also returns this error.
}
func TestUnmarshalJSON(t *testing.T) {
// Input for unmarshalling
input := []byte(NKNADDRESS)
f := new(Uint160)
err := f.UnmarshalJSON(input)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Verify that f has been updated correctly
expected, err := hex.DecodeString(NKNADDRESS_HEX)
if err != nil {
t.Fatal(err)
}
if !bytesEqual(f.ToArray(), expected) {
t.Fatalf("Expected %v, got %v", expected, f.ToArray())
}
// TODO: If possible, create a scenario where ToScriptHash returns an error
// and verify that UnmarshalJSON also returns this error.
}
func bytesEqual(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}