-
Notifications
You must be signed in to change notification settings - Fork 46
/
fraction_test.go
166 lines (160 loc) · 3.87 KB
/
fraction_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package weave
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)
func TestFractionUnmarshalJSON(t *testing.T) {
cases := map[string]struct {
raw string
wantFrac Fraction
wantErr bool
}{
"zero": {
raw: `"0"`,
wantFrac: Fraction{Denominator: 1},
wantErr: false,
},
"integer human format number": {
raw: `"4"`,
wantFrac: Fraction{Numerator: 4, Denominator: 1},
wantErr: false,
},
"zero numerator": {
raw: `"0/123"`,
wantFrac: Fraction{Denominator: 123},
wantErr: false,
},
"zero numerator and denominator": {
raw: `"0/1"`,
wantFrac: Fraction{Denominator: 1},
wantErr: false,
},
"human readable format": {
raw: `"1/2"`,
wantFrac: Fraction{Numerator: 1, Denominator: 2},
wantErr: false,
},
"human readable format, too many separators": {
raw: `"1/2/3"`,
wantErr: true,
},
"human readable format, floating point number": {
raw: `"1/3.3"`,
wantErr: true,
},
"human readable format, signed number": {
raw: `"-1"`,
wantErr: true,
},
"verbose format": {
raw: `{"numerator": 1, "denominator": 2}`,
wantFrac: Fraction{Numerator: 1, Denominator: 2},
wantErr: false,
},
"verbose format only denominator": {
raw: `{"denominator": 2}`,
wantFrac: Fraction{Numerator: 0, Denominator: 2},
wantErr: false,
},
"verbose format only numerator": {
raw: `{"numerator": 2}`,
wantFrac: Fraction{Numerator: 2, Denominator: 0},
wantErr: false,
},
"random string characters": {
raw: `"asdlkhsdalhksda"`,
wantErr: true,
},
"number is not acceptable": {
raw: `12345`,
wantErr: true,
},
"whitespace is irrelevant for human format": {
raw: `"\t 3 / \t 2 "`,
wantFrac: Fraction{Numerator: 3, Denominator: 2},
wantErr: false,
},
}
for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
var got Fraction
err := json.Unmarshal([]byte(tc.raw), &got)
gotErr := err != nil
if tc.wantErr != gotErr {
t.Fatalf("want error=%v, got %v", tc.wantErr, err)
}
if err == nil && !reflect.DeepEqual(got, tc.wantFrac) {
t.Fatalf("want %+v, got %+v", tc.wantFrac, got)
}
})
}
}
func TestFractionMarshalJSON(t *testing.T) {
f := Fraction{Numerator: 4, Denominator: 5}
b, err := json.Marshal(f)
if err != nil {
t.Fatal(err)
}
const want = `{"numerator":4,"denominator":5}`
if !bytes.Equal(b, []byte(want)) {
t.Fatalf("unexpected JSON format: %q", b)
}
}
func TestFractionCompare(t *testing.T) {
cases := map[string]struct {
a, b Fraction
Want int
}{
"two equal values": {
a: Fraction{Numerator: 1, Denominator: 2},
b: Fraction{Numerator: 2, Denominator: 4},
Want: 0,
},
"a greater than b": {
a: Fraction{Numerator: 3, Denominator: 5},
b: Fraction{Numerator: 2, Denominator: 4},
Want: 1,
},
"b greater than a": {
a: Fraction{Numerator: 3, Denominator: 5},
b: Fraction{Numerator: 3, Denominator: 4},
Want: -1,
},
"a is zero": {
a: Fraction{Numerator: 0, Denominator: 2},
b: Fraction{Numerator: 3, Denominator: 4},
Want: -1,
},
"a is zero value": {
a: Fraction{Numerator: 0, Denominator: 0},
b: Fraction{Numerator: 3, Denominator: 4},
Want: -1,
},
"b is zero": {
a: Fraction{Numerator: 1, Denominator: 2},
b: Fraction{Numerator: 0, Denominator: 3},
Want: 1,
},
"b is zero value": {
a: Fraction{Numerator: 1, Denominator: 2},
b: Fraction{Numerator: 0, Denominator: 0},
Want: 1,
},
"a and b are zero": {
a: Fraction{Numerator: 0, Denominator: 123},
b: Fraction{Numerator: 0, Denominator: 0},
Want: 0,
},
}
for testName, tc := range cases {
t.Run(testName, func(t *testing.T) {
got := tc.a.Compare(tc.b)
if got != tc.Want {
t.Logf("%v compare %v", tc.a, tc.b)
t.Fatalf("want %d, got %d", tc.Want, got)
}
})
}
}