forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice_test.go
53 lines (43 loc) · 1.58 KB
/
price_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
package xdr_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stellar/go/xdr"
)
func TestPriceInvert(t *testing.T) {
p := xdr.Price{N: 1, D: 2}
p.Invert()
assert.Equal(t, xdr.Price{N: 2, D: 1}, p)
}
func TestPriceEqual(t *testing.T) {
// canonical
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
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) {
// canonical
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
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}
p.Normalize()
assert.Equal(t, xdr.Price{N: 1, D: 4}, p)
// not canonical
p = xdr.Price{N: 500, D: 2000}
p.Normalize()
assert.Equal(t, xdr.Price{N: 1, D: 4}, p)
}