-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal64p2_test.go
294 lines (250 loc) · 6.95 KB
/
decimal64p2_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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package decimal
import (
"encoding/json"
"testing"
)
func TestNewDecimal64p2(t *testing.T) {
var d Decimal64p2
if d = NewDecimal64p2(0, 0); int64(d) != 0 {
t.Errorf("Expected 0, got: %d", d)
}
if d = NewDecimal64p2(1, 0); int64(d) != 100 {
t.Errorf("Expected 1, got: %d", d)
}
if d = NewDecimal64p2(-1, 0); int64(d) != -100 {
t.Errorf("Expected -1, got: %d", d)
}
if d = NewDecimal64p2(1, 23); int64(d) != 123 {
t.Errorf("Expected 123, got: %d", d)
}
if d = NewDecimal64p2(-1, -23); int64(d) != -123 {
t.Errorf("Expected -123, got: %d", d)
}
if d = NewDecimal64p2(0, -23); int64(d) != -23 {
t.Errorf("Expected -23, got: %d", d)
}
}
func TestParseDecimal64p2(t *testing.T) {
d, err := ParseDecimal64p2("0")
if err != nil {
t.Error(err)
}
if d != 0 {
t.Errorf("Expected 0, got: %v", d)
}
if d, err = ParseDecimal64p2("0.00"); err != nil {
t.Error(err)
} else if d != 0 {
t.Errorf("Expected 0, got: %v", d)
} else if //goland:noinspection GoDfaNilDereference
d.String() != "0" {
t.Errorf("Expected 0, got: %v", d.String())
}
if d, err = ParseDecimal64p2("1.00"); err != nil {
t.Error(err)
} else if d != NewDecimal64p2(1, 0) {
t.Errorf("Expected 1, got: %v", d)
} else if d.String() != "1" {
t.Errorf("Expected 1, got: %v", d.String())
}
if d, err = ParseDecimal64p2("1.23"); err != nil {
t.Error(err)
} else if d != NewDecimal64p2(1, 23) {
t.Errorf("Expected 1.23, got: %d", d)
} else if d.String() != "1.23" {
t.Errorf("Expected 1.23, got: %v", d.String())
}
if d, err = ParseDecimal64p2("0.03"); err != nil {
t.Error(err)
} else if d != NewDecimal64p2(0, 3) {
t.Errorf("Expected 0.03, got: %d", d)
} else if d.String() != "0.03" {
t.Errorf("Expected 0.03, got: %v", d.String())
}
}
func TestDecimal64p2_String(t *testing.T) {
m := NewDecimal64p2(0, 0)
s := m.String()
if s != "0" {
t.Errorf("Expected '0', got '%v'", s)
}
s = NewDecimal64p2(0, 3).String()
if s != "0.03" {
t.Errorf("Expected '0.03', got '%v'", s)
}
s = NewDecimal64p2(0, 23).String()
if s != "0.23" {
t.Errorf("Expected '0.23', got '%v'", s)
}
s = NewDecimal64p2(1, 23).String()
if s != "1.23" {
t.Errorf("Expected '1.23', got '%v'", s)
}
s = NewDecimal64p2(45, 0).String()
if s != "45" {
t.Errorf("Expected '45', got '%v'", s)
}
s = NewDecimal64p2(-45, -67).String()
if s != "-45.67" {
t.Errorf("Expected '-45.67', got '%v'", s)
}
s = NewDecimal64p2FromFloat64(-0.03).String()
if s != "-0.03" {
t.Errorf("Expected '-0.03', got '%v'", s)
}
}
func TestDecimal64p2_IntPart(t *testing.T) {
m := NewDecimal64p2(23, 45)
if m.IntPart() != 23 {
t.Error("m.IntPart() != 23")
}
m = NewDecimal64p2(-23, -45)
if m.IntPart() != -23 {
t.Error("m.IntPart() != -23")
}
}
func TestDecimal64p2_DecimalPart(t *testing.T) {
m := NewDecimal64p2(23, 45)
if m.DecimalPart() != 45 {
t.Errorf("m.DecimalPart() != 45, got: %v", m.DecimalPart())
}
m = NewDecimal64p2(-23, -45)
if m.DecimalPart() != 45 {
t.Errorf("m.DecimalPart() != 45, got: %v", m.DecimalPart())
}
}
func TestNewDecimal64p2FromFloat64(t *testing.T) {
var d Decimal64p2
if d = NewDecimal64p2FromFloat64(0); int64(d) != 0 {
t.Errorf("Expected 0, got: %d", d)
}
if d = NewDecimal64p2FromFloat64(1.23); int64(d) != 123 {
t.Errorf("Expected 123, got: %d", d)
} else if d.DecimalPart() != 23 {
t.Errorf("Decimal part expected to be 23, got: %d", d.DecimalPart())
}
if d = NewDecimal64p2FromFloat64(-1.23); int64(d) != -123 {
t.Errorf("Expected -123, got: %d", d)
} else if d.DecimalPart() != 23 {
t.Errorf("Decimal part expected to be 23, got: %d", d.DecimalPart())
}
if d = NewDecimal64p2FromFloat64(2333.33); int64(d) != 233333 {
t.Errorf("Expected 233333, got: %d", d)
} else if d.DecimalPart() != 33 {
t.Errorf("Decimal part expected to be 33, got: %d", d.DecimalPart())
}
}
func TestNewDecimal64p2FromInt(t *testing.T) {
var d Decimal64p2
if d = NewDecimal64p2FromInt(0); int64(d) != 0 {
t.Errorf("Expected 0, got: %d", d)
} else if d.DecimalPart() != 0 {
t.Errorf("Decimal part expected to be 0, got: %d", d.DecimalPart())
}
if d = NewDecimal64p2FromInt(123); int64(d) != 12300 {
t.Errorf("Expected 12345, got: %d", d)
} else if dp := d.DecimalPart(); dp != 0 {
t.Errorf("Decimal part expected to be 0, got: %d", dp)
}
if d = NewDecimal64p2FromInt(-123); int64(d) != -12300 {
t.Errorf("Expected -12300, got: %d", d)
} else if dp := d.DecimalPart(); dp != 0 {
t.Errorf("Decimal part expected to be 0, got: %d", dp)
}
}
func TestDecimal64p2_AsFloat64(t *testing.T) {
var d Decimal64p2
d = NewDecimal64p2(1, 23)
if d.AsFloat64() != 1.23 {
t.Errorf("Expected 1.23, got: %v", d)
}
d = NewDecimal64p2(1, 05)
if d.AsFloat64() != 1.05 {
t.Errorf("Expected 1.05, got: %v", d)
}
}
func TestDecimal64p2_MarshalJSON(t *testing.T) {
var d1 Decimal64p2
d1 = NewDecimal64p2(1, 23)
if s, err := json.Marshal(d1); err != nil {
t.Error(err)
} else if expected := "123"; string(s) != expected {
t.Errorf("Expected '%v', got: '%v'", expected, string(s))
}
d1 = NewDecimal64p2(-1, -23)
if s, err := json.Marshal(d1); err != nil {
t.Error(err)
} else if expected := "-123"; string(s) != expected {
t.Errorf("Expected '%v', got: '%v'", expected, string(s))
}
}
func TestDecimal64p2_UnmarshalJSON(t *testing.T) {
var d2 Decimal64p2
if err := json.Unmarshal([]byte("1234"), &d2); err != nil {
t.Error(err)
} else if intPart := d2.IntPart(); intPart != 12 {
t.Errorf("Expected 12 for int part, got %d: %s", intPart, d2.String())
} else if decimalPart := d2.DecimalPart(); decimalPart != 34 {
t.Errorf("Expected 0 for decimal part, got %d: %s", decimalPart, d2.String())
}
if err := json.Unmarshal([]byte("1.23"), &d2); err != nil {
t.Error(err)
} else if d2.IntPart() != 1 || d2.DecimalPart() != 23 {
t.Errorf("Expected 1.23, got %v", d2)
}
if err := json.Unmarshal([]byte("-1.23"), &d2); err != nil {
t.Error(err)
} else if d2.IntPart() != -1 || d2.DecimalPart() != 23 {
t.Errorf("Expected -1.23, got %v", d2)
}
}
func TestDecimal64p2_Abs(t *testing.T) {
d1 := NewDecimal64p2FromFloat64(-3.24)
d2 := d1.Abs()
if d2 < 0 {
t.Error("Abs() returned < 0")
}
if d2.IntPart() != 3 {
t.Error("d2.IntPart() != 3")
}
if d2.DecimalPart() != 24 {
t.Error("d2.IntPart() != 24")
}
d1 = NewDecimal64p2FromFloat64(3.24)
d2 = d1.Abs()
if d1 != d2 {
t.Error("d1 != d2")
}
}
func TestNewDecimal64p2_panicPositiveNegative(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("No panic")
}
}()
NewDecimal64p2(1, -1)
}
func TestNewDecimal64p2_panicNegativePositive(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("No panic")
}
}()
NewDecimal64p2(-1, 1)
}
func TestNewDecimal64p2_panicDecimalGreaterPlus99(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("No panic")
}
}()
NewDecimal64p2(1, 100)
}
func TestNewDecimal64p2_panicDecimalLessMinus99(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("No panic")
}
}()
NewDecimal64p2(-1, -100)
}