forked from iost-official/go-iost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed_test.go
73 lines (63 loc) · 1.98 KB
/
fixed_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
package common
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestFixed_ToString(t *testing.T) {
f := Fixed{-9223372036854775808, 4, nil}
f.ToString()
assert.Equal(t, f.Err, errOverflow)
f = Fixed{100, 0, nil}
s := f.ToString()
assert.Equal(t, s, "100")
}
func TestNewFixed(t *testing.T) {
f, err := NewFixed("-323.49494", 12)
assert.Equal(t, err, nil)
assert.Equal(t, f.Value, int64(-323494940000000))
_, err = NewFixed("-9223372036854775808", 0)
assert.Equal(t, err, errOverflow)
_, err = NewFixed("-323.49494", 40)
assert.Equal(t, err, errOverflow)
_, err = NewFixed("323.494.94", 10)
assert.Equal(t, err, errDoubleDot)
}
func TestFixed_Multiply(t *testing.T) {
f1 := Fixed{-9223372036854775807, 4, nil}
f2 := Fixed{-9223372036854775807, 4, nil}
f1.Multiply(&f2)
assert.Equal(t, f1.Err, errOverflow)
}
func TestFixed_Times(t *testing.T) {
f1 := Fixed{-9223372036854775807, 4, nil}
f1.Times(3)
assert.Equal(t, f1.Err, errOverflow)
}
func TestFixed_Marshal(t *testing.T) {
f, err := UnmarshalFixed((&Fixed{1230, 2, nil}).Marshal())
assert.Equal(t, nil, err)
assert.Equal(t, 2, f.Decimal)
assert.Equal(t, nil, f.Err)
assert.Equal(t, int64(1230), f.Value)
}
func TestFixed_Compare(t *testing.T) {
checkComp := func(a string, b string, result bool) {
fa, err := NewFixed(a, -1)
assert.Equal(t, err, nil)
fb, err := NewFixed(b, -1)
assert.Equal(t, err, nil)
assert.Equal(t, fa.LessThan(fb), result)
}
// covers every branch in 'LessThan'
// naive case
checkComp("1.0", "999", true)
// 1152921504606846976: 2**60
checkComp("-1.00000001", "1152921504606846976", true)
checkComp("-1.00000001", "-1152921504606846976", false)
checkComp("-1152921504606846976", "-1.00000001", true)
checkComp("0.00000000", "1152921504606846976", true)
checkComp("0.00000000", "-1152921504606846976", false)
checkComp("1152921504606846976", "-1.00000001", false)
checkComp("1152921504606846976", "1.00000001", false)
checkComp("1.00000001", "1152921504606846976", true)
}