-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathprice_test.go
More file actions
131 lines (108 loc) · 4.4 KB
/
Copy pathprice_test.go
File metadata and controls
131 lines (108 loc) · 4.4 KB
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
package xdr_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stellar/go-stellar-sdk/xdr"
)
func makeAssertBoolNoError(t *testing.T) (func(bool, error), func(bool, error)) {
t.Helper()
assertTrueNoError := func(res bool, err error) {
t.Helper()
assert.NoError(t, err)
assert.True(t, res)
}
assertFalseNoError := func(res bool, err error) {
t.Helper()
assert.NoError(t, err)
assert.False(t, res)
}
return assertTrueNoError, assertFalseNoError
}
func TestPriceInvert(t *testing.T) {
p := xdr.Price{N: 1, D: 2}
assert.NoError(t, p.TryInvert())
assert.Equal(t, xdr.Price{N: 2, D: 1}, p)
// Using deprecated Price.Invert() method
p = xdr.Price{N: 1, D: 2}
p.Invert()
assert.Equal(t, xdr.Price{N: 2, D: 1}, p)
}
func TestPriceEqual(t *testing.T) {
assertTrueNoError, assertFalseNoError := makeAssertBoolNoError(t)
// canonical
assertTrueNoError(xdr.Price{N: 1, D: 2}.TryEqual(xdr.Price{N: 1, D: 2}))
assertFalseNoError(xdr.Price{N: 1, D: 2}.TryEqual(xdr.Price{N: 2, D: 3}))
// not canonical
assertTrueNoError(xdr.Price{N: 1, D: 2}.TryEqual(xdr.Price{N: 5, D: 10}))
assertTrueNoError(xdr.Price{N: 5, D: 10}.TryEqual(xdr.Price{N: 1, D: 2}))
assertTrueNoError(xdr.Price{N: 5, D: 10}.TryEqual(xdr.Price{N: 50, D: 100}))
assertFalseNoError(xdr.Price{N: 1, D: 3}.TryEqual(xdr.Price{N: 5, D: 10}))
assertFalseNoError(xdr.Price{N: 5, D: 10}.TryEqual(xdr.Price{N: 1, D: 3}))
assertFalseNoError(xdr.Price{N: 5, D: 15}.TryEqual(xdr.Price{N: 50, D: 100}))
// canonical using deprecated Price.Equal() method
assert.True(t, xdr.Price{N: 1, D: 2}.Equal(xdr.Price{N: 1, D: 2}))
assert.False(t, xdr.Price{N: 1, D: 2}.Equal(xdr.Price{N: 2, D: 3}))
// not canonical using deprecated Price.Equal() method
assert.True(t, xdr.Price{N: 1, D: 2}.Equal(xdr.Price{N: 5, D: 10}))
assert.True(t, xdr.Price{N: 5, D: 10}.Equal(xdr.Price{N: 1, D: 2}))
assert.True(t, xdr.Price{N: 5, D: 10}.Equal(xdr.Price{N: 50, D: 100}))
assert.False(t, xdr.Price{N: 1, D: 3}.Equal(xdr.Price{N: 5, D: 10}))
assert.False(t, xdr.Price{N: 5, D: 10}.Equal(xdr.Price{N: 1, D: 3}))
assert.False(t, xdr.Price{N: 5, D: 15}.Equal(xdr.Price{N: 50, D: 100}))
}
func TestPriceCheaper(t *testing.T) {
assertTrueNoError, assertFalseNoError := makeAssertBoolNoError(t)
// canonical
assertTrueNoError(xdr.Price{N: 1, D: 4}.TryCheaper(xdr.Price{N: 1, D: 3}))
assertFalseNoError(xdr.Price{N: 1, D: 3}.TryCheaper(xdr.Price{N: 1, D: 4}))
assertFalseNoError(xdr.Price{N: 1, D: 4}.TryCheaper(xdr.Price{N: 1, D: 4}))
// not canonical
assertTrueNoError(xdr.Price{N: 10, D: 40}.TryCheaper(xdr.Price{N: 3, D: 9}))
assertFalseNoError(xdr.Price{N: 3, D: 9}.TryCheaper(xdr.Price{N: 10, D: 40}))
assertFalseNoError(xdr.Price{N: 10, D: 40}.TryCheaper(xdr.Price{N: 10, D: 40}))
// canonical using deprecated Price.Cheaper() method
assert.True(t, xdr.Price{N: 1, D: 4}.Cheaper(xdr.Price{N: 1, D: 3}))
assert.False(t, xdr.Price{N: 1, D: 3}.Cheaper(xdr.Price{N: 1, D: 4}))
assert.False(t, xdr.Price{N: 1, D: 4}.Cheaper(xdr.Price{N: 1, D: 4}))
// not canonical using deprecated Price.Cheaper() method
assert.True(t, xdr.Price{N: 10, D: 40}.Cheaper(xdr.Price{N: 3, D: 9}))
assert.False(t, xdr.Price{N: 3, D: 9}.Cheaper(xdr.Price{N: 10, D: 40}))
assert.False(t, xdr.Price{N: 10, D: 40}.Cheaper(xdr.Price{N: 10, D: 40}))
}
func TestNormalize(t *testing.T) {
// canonical
p := xdr.Price{N: 1, D: 4}
assert.NoError(t, p.TryNormalize())
assert.Equal(t, xdr.Price{N: 1, D: 4}, p)
// not canonical
p = xdr.Price{N: 500, D: 2000}
assert.NoError(t, p.TryNormalize())
assert.Equal(t, xdr.Price{N: 1, D: 4}, p)
// canonical using deprecated Price.Normalize() method
p = xdr.Price{N: 1, D: 4}
p.Normalize()
assert.Equal(t, xdr.Price{N: 1, D: 4}, p)
// not canonical using deprecated Price.Normalize() method
p = xdr.Price{N: 500, D: 2000}
p.Normalize()
assert.Equal(t, xdr.Price{N: 1, D: 4}, p)
}
func TestInvalidPrices(t *testing.T) {
negativePrices := []xdr.Price{{N: -1, D: 4}, {N: 1, D: -4}, {N: -1, D: -4}}
zeroPrices := []xdr.Price{{N: 0, D: 4}, {N: 1, D: 0}, {N: 0, D: 0}}
errorOnInvalid := func(p xdr.Price) {
assert.Error(t, p.Validate())
assert.Error(t, p.TryNormalize())
assert.Error(t, p.TryInvert())
_, err := p.TryEqual(xdr.Price{N: 1, D: 4})
assert.Error(t, err)
_, err = p.TryCheaper(xdr.Price{N: 1, D: 4})
assert.Error(t, err)
}
for _, p := range negativePrices {
errorOnInvalid(p)
}
for _, p := range zeroPrices {
errorOnInvalid(p)
}
}