forked from regen-network/gocuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_test.go
107 lines (89 loc) · 2.39 KB
/
value_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
package gocuke
import (
"fmt"
"math/big"
"testing"
"github.com/cockroachdb/apd/v3"
"github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
"pgregory.net/rapid"
)
func TestValues(t *testing.T) {
NewRunner(t, &valuesSuite{}).Path("features/values.feature").
ShortTags("not @long").
Run()
}
type valuesSuite struct {
TestingT
orig interface{}
str string
parsed interface{}
}
func (s *valuesSuite) IGetBackTheOriginalValue() {
assert.DeepEqual(s, s.orig, s.parsed, decComparer, bigIntComparer)
}
var decComparer = cmp.Comparer(func(x, y *apd.Decimal) bool {
return x.Cmp(y) == 0
})
var bigIntComparer = cmp.Comparer(func(x, y *big.Int) bool {
return x.Cmp(y) == 0
})
func (s *valuesSuite) AnyInt64String(t *rapid.T) {
s.AnInt64(rapid.Int64().AsAny().Draw(t, "orig").(int64))
}
func (s *valuesSuite) WhenIConvertItToAnInt64() {
s.parsed = toInt64(s, s.str)
}
var decGen = rapid.Custom(func(t *rapid.T) *apd.Decimal {
nBytes := rapid.IntRange(1, 16).AsAny().Draw(t, "nBytes").(int)
bytes := make([]byte, nBytes)
for i := 0; i < nBytes; i++ {
bytes[i] = rapid.Byte().AsAny().Draw(t, fmt.Sprintf("byte%d", i)).(byte)
}
coeff := &apd.BigInt{}
coeff.SetBytes(bytes)
neg := rapid.Bool().AsAny().Draw(t, "neg").(bool)
if neg {
coeff = coeff.Neg(coeff)
}
exp := rapid.Int32Range(-5000, 5000).AsAny().Draw(t, "exp").(int32)
return apd.NewWithBigInt(coeff, exp)
})
func (s *valuesSuite) AnyDecimalString(t *rapid.T) {
s.ADecimal(decGen.AsAny().Draw(t, "x").(*apd.Decimal))
}
func (s *valuesSuite) WhenIConvertItToADecimal() {
s.parsed = toDecimal(s, s.str)
}
var bigIntGen = rapid.Custom(func(t *rapid.T) *big.Int {
nBytes := rapid.IntRange(1, 16).AsAny().Draw(t, "nBytes").(int)
bytes := make([]byte, nBytes)
for i := 0; i < nBytes; i++ {
bytes[i] = rapid.Byte().AsAny().Draw(t, fmt.Sprintf("byte%d", i)).(byte)
}
x := &big.Int{}
x.SetBytes(bytes)
neg := rapid.Bool().AsAny().Draw(t, "neg").(bool)
if neg {
x = x.Neg(x)
}
return x
})
func (s *valuesSuite) AnyBigIntegerString(t *rapid.T) {
s.ABigInteger(bigIntGen.AsAny().Draw(t, "x").(*big.Int))
}
func (s *valuesSuite) WhenIConvertItToABigInteger() {
s.parsed = toBigInt(s, s.str)
}
func (s *valuesSuite) AnInt64(a int64) {
s.orig = a
s.str = fmt.Sprintf("%d", a)
}
func (s *valuesSuite) ADecimal(a *apd.Decimal) {
s.orig = a
s.str = a.String()
}
func (s *valuesSuite) ABigInteger(a *big.Int) {
s.orig = a
s.str = a.String()
}