-
Notifications
You must be signed in to change notification settings - Fork 4
/
types_test.go
137 lines (126 loc) · 2.99 KB
/
types_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package types
import (
"encoding/json"
"fmt"
"os"
"testing"
)
func ExampleNullString() {
n := NullString{Valid: true, String: "foo"}
json.NewEncoder(os.Stdout).Encode(n)
// Output: "foo"
}
func TestString(t *testing.T) {
var ns NullString
str := []byte("\"foo\"")
err := json.Unmarshal(str, &ns)
assertNotError(t, err, "")
assertEquals(t, ns.Valid, true)
assertEquals(t, ns.String, "foo")
}
func TestNullString(t *testing.T) {
var ns NullString
str := []byte("null")
err := json.Unmarshal(str, &ns)
assertNotError(t, err, "")
assertEquals(t, ns.Valid, false)
}
func TestStringMarshal(t *testing.T) {
ns := NullString{
Valid: true,
String: "foo bar",
}
b, err := json.Marshal(ns)
assertNotError(t, err, "")
assertEquals(t, string(b), "\"foo bar\"")
}
func TestStringMarshalNull(t *testing.T) {
ns := NullString{
Valid: false,
String: "",
}
b, err := json.Marshal(ns)
assertNotError(t, err, "")
assertEquals(t, string(b), "null")
}
var byteTests = []struct {
in string
err string
out Bits
}{
{"5bit", "", 5 * Bit},
{"5kB", "", 5 * Kilobyte},
{"1.3kB", "", 1300 * Byte},
{"1300B", "", 1300 * Byte},
{"1300", "types: missing unit in input 1300", 0},
}
func TestParseBits(t *testing.T) {
for _, tt := range byteTests {
out, err := ParseBits(tt.in)
hdr := fmt.Sprintf("ParseBits(%q):", tt.in)
if err == nil {
if tt.err != "" {
t.Errorf("%s expected to get error %v, got a valid result", hdr, tt.err)
continue
}
if out != tt.out {
t.Errorf("%s got %v, want %v", hdr, out, tt.out)
}
} else {
if tt.err == "" {
t.Errorf("%s didn't expect error but got %v", hdr, err)
continue
}
if err.Error() != tt.err {
t.Errorf("%s got error %v, want %v", hdr, err, tt.err)
continue
}
}
}
}
func TestBytes(t *testing.T) {
b := 13 * Bit
if b.Bytes() != 1.625 {
t.Errorf("13 bits should be 1.625 bytes, got %v", b.Bytes())
}
}
func TestKilobytes(t *testing.T) {
b := 3*Kilobyte + 10*Byte
if got := b.Kilobytes(); got != 3.01 {
t.Errorf("3.01kB should be 3.01 bytes, got %v", got)
}
}
func TestBitsString(t *testing.T) {
b := 0 * Bit
if got := b.String(); got != "0" {
t.Errorf("0 bits should be 0, got %q", got)
}
b = 7 * Bit
if got := b.String(); got != "7bit" {
t.Errorf("7 bits should be 7bit, got %q", got)
}
b = 9 * Bit
if got := b.String(); got != "1.125B" {
t.Errorf("9 bits should be 1.125B, got %q", got)
}
b = 7380*Kilobyte + 871*Byte
if got := b.String(); got != "7.38MB" {
t.Errorf("25015kB should be 25.015MB, got %q", got)
}
b = 25015 * Kilobyte
if got := b.String(); got != "25.015MB" {
t.Errorf("25015kB should be 25.015MB, got %q", got)
}
b = -25015 * Kilobyte
if got := b.String(); got != "-25.015MB" {
t.Errorf("25015kB should be 25.015MB, got %q", got)
}
b = -123*Megabyte - 15*Kilobyte
if got := b.String(); got != "-123.015MB" {
t.Errorf("should be -123.015MB, got %q", got)
}
b = 1*Exabyte + 15*Petabyte
if got := b.String(); got != "1.015EB" {
t.Errorf("should be 1.015EB, got %q", got)
}
}