-
Notifications
You must be signed in to change notification settings - Fork 103
/
quorem_test.go
144 lines (130 loc) · 3.46 KB
/
quorem_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
// Copyright 2023 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.
// Verify that quoRem satisfies the identity
// quo = x div y such that
// rem = x - y*quo with 0 <= rem < |y|
// See doc for math/big.Int.DivMod.
package main
import (
"math/big"
"testing"
"robpike.io/ivy/exec"
"robpike.io/ivy/value"
)
type pair struct {
x, y int
}
var quoRemTests = []pair{
// We run all the tests with all four signs for 5, 3.
// The correct results are:
// 5,3 -> quo 1 rem 2
// -5,3 -> quo -2 rem 1
// 5,-3 -> quo -1 rem 2
// -5,-3 -> quo 2 rem 1
{5, 3},
{-5, 3},
{5, -3},
{-5, -3},
// Now check that they work with remainder 0.
// 5,5 -> quo 1 rem 0
// -5,5 -> quo -2 rem 0
// 5,-5 -> quo -1 rem 0
// -5,-5 -> quo 2 rem 0
{5, 5},
{-5, 5},
{5, -5},
{-5, -5},
}
func TestQuoRem(t *testing.T) {
c := exec.NewContext(&testConf)
for _, test := range quoRemTests {
verifyQuoRemInt(t, c, test.x, test.y)
verifyQuoRemBigInt(t, c, test.x, test.y)
verifyQuoRemBigRat(t, c, test.x, test.y)
verifyQuoRemBigFloat(t, c, test.x, test.y)
}
}
func verifyQuoRemInt(t *testing.T, c value.Context, x, y int) {
t.Helper()
quoV, remV := value.QuoRem("test", c, value.Int(x), value.Int(y))
quo := int(quoV.(value.Int))
rem := int(remV.(value.Int))
absY := y
if y < 0 {
absY = -y
}
if rem < 0 || absY <= rem {
t.Errorf("Int %d QuoRem %d = %d,%d (remainder out of range)", x, y, quo, rem)
}
expect := x - y*quo
if rem != expect {
t.Errorf("Int %d QuoRem %d = %d,%d yielding %d", x, y, quo, rem, expect)
}
}
func bigInt(x int64) value.Value {
return value.BigInt{Int: big.NewInt(x)}
}
func bigRat(x, y int64) value.Value {
return value.BigRat{Rat: big.NewRat(x, y)}
}
func bigFloat(x float64) value.Value {
return value.BigFloat{Float: big.NewFloat(x)}
}
func verifyQuoRemBigInt(t *testing.T, c value.Context, X, Y int) {
t.Helper()
x, y := int64(X), int64(Y)
quoV, remV := value.QuoRem("test", c, bigInt(x), bigInt(y))
// For our tests, we get ints back.
quo := int64(quoV.(value.Int))
rem := int64(remV.(value.Int))
absY := y
if y < 0 {
absY = -y
}
if rem < 0 || absY <= rem {
t.Errorf("BigInt %d QuoRem %d = %d,%d (remainder out of range)", x, y, quo, rem)
}
expect := x - y*quo
if rem != expect {
t.Errorf("BigInt %d QuoRem %d = %d,%d yielding %d", x, y, quo, rem, expect)
}
}
func verifyQuoRemBigRat(t *testing.T, c value.Context, X, Y int) {
t.Helper()
x, y := int64(X), int64(Y)
quoV, remV := value.QuoRem("test", c, bigRat(x, 1), bigRat(y, 1))
// For our tests, we get ints back.
quo := int64(quoV.(value.Int))
rem := int64(remV.(value.Int))
absY := y
if y < 0 {
absY = -y
}
if rem < 0 || absY <= rem {
t.Errorf("BigRat %d QuoRem %d = %d,%d (remainder out of range)", x, y, quo, rem)
}
expect := x - y*quo
if rem != expect {
t.Errorf("BigRat %d QuoRem %d = %d,%d yielding %d", x, y, quo, rem, expect)
}
}
func verifyQuoRemBigFloat(t *testing.T, c value.Context, X, Y int) {
t.Helper()
x, y := float64(X), float64(Y)
quoV, remV := value.QuoRem("test", c, bigFloat(x), bigFloat(y))
// For our tests, we get ints back.
quo := float64(quoV.(value.Int))
rem := float64(remV.(value.Int))
absY := y
if y < 0 {
absY = -y
}
if rem < 0 || absY <= rem {
t.Errorf("BigFloat %g QuoRem %g = %g,%g (remainder out of range)", x, y, quo, rem)
}
expect := x - y*quo
if rem != expect {
t.Errorf("BigFloat %g QuoRem %g = %g,%g yielding %g", x, y, quo, rem, expect)
}
}