-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_test.go
101 lines (75 loc) · 2.82 KB
/
int_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
package xo
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDigitOf(t *testing.T) {
assert := assert.New(t)
assert.Equal(0, DigitOf(0, 1))
assert.Equal(5, DigitOf(5, 1))
assert.Equal(1, DigitOf(11, 1))
assert.Equal(5, DigitOf(51, 2))
assert.Equal(8, DigitOf(128, 1))
assert.Equal(2, DigitOf(128, 2))
assert.Equal(1, DigitOf(128, 3))
}
func TestParseDecimalStringIntoUint64(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
uintNumber, err := ParseUint64FromDecimalString("0.01", 2)
require.NoError(err)
assert.Equal(uint64(1), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("0.11", 2)
require.NoError(err)
assert.Equal(uint64(11), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("0.1111", 2)
require.NoError(err)
assert.Equal(uint64(11), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("10.00", 2)
require.NoError(err)
assert.Equal(uint64(1000), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("10.01", 2)
require.NoError(err)
assert.Equal(uint64(1001), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("10.11", 2)
require.NoError(err)
assert.Equal(uint64(1011), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("10.0000", 4)
require.NoError(err)
assert.Equal(uint64(100000), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("10.01", 4)
require.NoError(err)
assert.Equal(uint64(100100), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("10.1101", 4)
require.NoError(err)
assert.Equal(uint64(101101), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("1000000.0000001", 2)
require.NoError(err)
assert.Equal(uint64(100000000), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("1000000.01", 2)
require.NoError(err)
assert.Equal(uint64(100000001), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("0", 2)
require.NoError(err)
assert.Equal(uint64(0), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("0000000", 2)
require.NoError(err)
assert.Equal(uint64(0), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("0.00000", 2)
require.NoError(err)
assert.Equal(uint64(0), uintNumber)
uintNumber, err = ParseUint64FromDecimalString("00000.00000", 2)
require.NoError(err)
assert.Equal(uint64(0), uintNumber)
}
func TestConvertUint64ToDecimalString(t *testing.T) {
assert := assert.New(t)
assert.Equal("0.00", ConvertUint64ToDecimalString(0, 2))
assert.Equal("0", ConvertUint64ToDecimalString(0, -1))
assert.Equal("19.90", ConvertUint64ToDecimalString(1990, 2))
assert.Equal("19.9", ConvertUint64ToDecimalString(1990, -1))
assert.Equal("19.99", ConvertUint64ToDecimalString(1999, 2))
assert.Equal("1.00", ConvertUint64ToDecimalString(100, 2))
assert.Equal("1", ConvertUint64ToDecimalString(100, -1))
}