This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_extra_test.go
255 lines (227 loc) · 5.13 KB
/
int_extra_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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test extra functionality
package gmp
import (
"math/rand"
"testing"
"time"
)
type binaryFun func(a, b int64) bool
type ternaryFun func(a, b, c int64) bool
var random *rand.Rand
var perFuncTests []int
func init() {
random = rand.New(rand.NewSource(time.Now().UnixNano()))
perFuncTests = random.Perm(50)
}
func randoms(num int, bits int) <-chan int64 {
channel := make(chan int64)
go func(out chan<- int64) {
for _, i := range random.Perm(num) {
ran := random.Int63n(1<<uint(bits-1) - 1)
if i&1 == 1 {
ran = -ran
}
out <- ran
}
close(out)
}(channel)
return channel
}
func absi(i int64) int64 {
if i < 0 {
return -i
}
return i
}
func cmpi(a, b int64) int {
if a < b {
return -1
}
if a > b {
return 1
}
return 0
}
func testBinary(t *testing.T, name string, fun binaryFun, maxBits int) {
small := [...]int64{0, 1, -1, 2, -2, 3, -3}
for _, x := range small {
for _, y := range small {
if !fun(x, y) {
t.Errorf("%s failed for %v and %v", name, x, y)
}
}
}
for x := range randoms(10, maxBits) {
for y := range randoms(10, maxBits) {
if !fun(x, y) {
t.Errorf("%s failed for %v and %v", name, x, y)
}
}
}
}
func testTernary(t *testing.T, name string, fun ternaryFun) {
small := [...]int64{0, 1, -1, 2, -2, 3, -3}
for _, x := range small {
for _, y := range small {
for _, z := range small {
if !fun(x, y, z) {
t.Errorf("%s failed for %v, %v and %v", name, x, y, z)
}
}
}
}
num, bits := 6, 30
for x := range randoms(num, bits) {
for y := range randoms(num, bits) {
for z := range randoms(num, bits) {
if !fun(x, y, z) {
t.Errorf("%s failed for %v, %v and %v", name, x, y, z)
}
}
}
}
}
func TestSwap(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
b := NewInt(j)
a.Swap(b)
return i == b.Int64() && j == a.Int64()
}
testBinary(t, "Swap", fun, 64)
}
func TestAddMul(t *testing.T) {
fun := func(i, j, k int64) bool {
a := NewInt(i)
return a.AddMul(NewInt(j), NewInt(k)).Int64() == i+j*k
}
testTernary(t, "AddMul", fun)
}
func TestAddUint32(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
k := uint32(j)
return a.AddUint32(a, k).Int64() == i+int64(k)
}
testBinary(t, "AddUint32", fun, 62)
}
func TestSubUint32(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
k := uint32(j)
return a.SubUint32(a, k).Int64() == i-int64(k)
}
testBinary(t, "SubUint32", fun, 62)
}
func TestUint32Sub(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
k := uint32(j)
return a.Uint32Sub(k, a).Int64() == int64(k)-i
}
testBinary(t, "Uint32Sub", fun, 62)
}
func TestMulUint32(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
k := uint32(j)
return a.MulUint32(a, k).Int64() == i*int64(k)
}
testBinary(t, "MulUint32", fun, 30)
}
func TestMulInt32(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
k := int32(j)
return a.MulInt32(a, k).Int64() == i*int64(k)
}
testBinary(t, "MulInt32", fun, 30)
}
func TestAddMulUint32(t *testing.T) {
fun := func(i, j, k int64) bool {
a := NewInt(i)
n := uint32(k)
return a.AddMulUint32(NewInt(j), n).Int64() == i+j*int64(n)
}
testTernary(t, "AddMulUint32", fun)
}
func TestSubMul(t *testing.T) {
fun := func(i, j, k int64) bool {
a := NewInt(i)
return a.SubMul(NewInt(j), NewInt(k)).Int64() == i-j*k
}
testTernary(t, "SubMul", fun)
}
func TestSubMulUint32(t *testing.T) {
fun := func(i, j, k int64) bool {
a := NewInt(i)
n := uint32(k)
return a.SubMulUint32(NewInt(j), n).Int64() == i-j*int64(n)
}
testTernary(t, "SubMulUint32", fun)
}
func TestCmpUint32(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
k := uint32(j)
return a.CmpUint32(k) == cmpi(i, int64(k))
}
testBinary(t, "CmpUint32", fun, 64)
}
func TestCmpInt32(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
k := int32(j)
return a.CmpInt32(k) == cmpi(i, int64(k))
}
testBinary(t, "CmpInt32", fun, 64)
}
func TestCmpAbs(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
return a.CmpAbs(NewInt(j)) == cmpi(absi(i), absi(j))
}
testBinary(t, "CmpAbs", fun, 64)
}
func TestCmpAbsUint32(t *testing.T) {
fun := func(i, j int64) bool {
a := NewInt(i)
k := uint32(j)
return a.CmpAbsUint32(k) == cmpi(absi(i), int64(k))
}
testBinary(t, "CmpAbsUint32", fun, 64)
}
func TestUint32(t *testing.T) {
for _ = range perFuncTests {
var n = uint32(random.Int63())
if NewInt(int64(n)).Uint32() != n {
t.Errorf("Uint32 failed for %v", n)
}
}
}
func TestInt32(t *testing.T) {
for _ = range perFuncTests {
var n = int32(random.Int63())
if NewInt(int64(n)).Int32() != n {
t.Errorf("Int32 failed for %v", n)
}
}
}
func TestSqrt(t *testing.T) {
a := NewInt(1)
aSquared := NewInt(1)
ten := NewInt(10)
hundred := NewInt(100)
root := new(Int)
for _ = range perFuncTests {
root := root.Sqrt(aSquared)
if root.Cmp(a) != 0 {
t.Errorf("Sqrt failed got %d expecting %d", root, a)
}
a.Mul(a, ten)
aSquared.Mul(aSquared, hundred)
}
}