-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathck_float64.go
More file actions
232 lines (193 loc) · 6.52 KB
/
ck_float64.go
File metadata and controls
232 lines (193 loc) · 6.52 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
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
/*
Golang test helper library: sztest.
Copyright (C) 2023-2025 Leslie Dancsecs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package sztest
import (
"math"
"strconv"
)
const float64TypeName = "float64"
func float64TypeString(tolerance float64) string {
if tolerance == 0.0 {
return float64TypeName
}
return float64TypeName + "(+/- " +
strconv.FormatFloat(tolerance, 'g', -1, 64) +
")"
}
// Float64f compares got and want within the given tolerance.
//
// The values are considered equal if |got - want| <= tolerance. A tolerance of
// 0.0 requires exact equality. On mismatch, the failure is reported with a
// formatted message built from msgFmt and msgArgs. Returns true if the
// comparison succeeds. NOTE: Values are considered equal if both are
// math.NaN.
func (chk *Chk) Float64f(
got, want, tolerance float64, msgFmt string, msgArgs ...any,
) bool {
if IsFloat64Similar(got, want, tolerance) {
return true
}
chk.t.Helper()
return chk.errChkf(
got, want, float64TypeString(tolerance), msgFmt, msgArgs...,
)
}
// Float64 compares got and want within the given tolerance.
//
// The values are considered equal if |got - want| <= tolerance. A tolerance of
// 0.0 requires exact equality. On mismatch, the failure is reported to the
// underlying testingT and the optional msg values are appended. Returns true
// if the comparison succeeds. NOTE: Values are considered equal if both are
// math.NaN.
func (chk *Chk) Float64(
got, want, tolerance float64, msg ...any,
) bool {
if IsFloat64Similar(got, want, tolerance) {
return true
}
chk.t.Helper()
return chk.errChk(
got, want, float64TypeString(tolerance), msg...,
)
}
// Float64Slicef compares two float64 slices element-wise within the given
// tolerance.
//
// Each pair of elements must satisfy |got[i] - want[i]| <= tolerance. A
// tolerance of 0.0 requires exact equality. Length mismatches or element
// mismatches are reported to the underlying testingT with a formatted message
// built from msgFmt and msgArgs. Returns true if slices are equal within
// tolerance. NOTE: Values are considered equal if both are math.NaN.
func (chk *Chk) Float64Slicef(
got, want []float64, tolerance float64, msgFmt string, msgArgs ...any,
) bool {
l := len(got)
equal := l == len(want)
for i := 0; equal && i < l; i++ {
equal = IsFloat64Similar(got[i], want[i], tolerance)
}
if equal {
return true
}
chk.t.Helper()
return errSlicef(chk,
got, want, float64TypeString(tolerance),
func(a, b float64) bool {
return IsFloat64Similar(a, b, tolerance)
},
msgFmt, msgArgs...,
)
}
// Float64Slice compares two float64 slices element-wise within the given
// tolerance.
//
// Each pair of elements must satisfy |got[i] - want[i]| <= tolerance. A
// tolerance of 0.0 requires exact equality. Length mismatches or element
// mismatches are reported to the underlying testingT. Optional msg values are
// included in the failure output. Returns true if slices are equal within
// tolerance. NOTE: Values are considered equal if both are math.NaN.
func (chk *Chk) Float64Slice(
got, want []float64, tolerance float64, msg ...any,
) bool {
l := len(got)
equal := l == len(want)
for i := 0; equal && i < l; i++ {
equal = IsFloat64Similar(got[i], want[i], tolerance)
}
if equal {
return true
}
chk.t.Helper()
return errSlice(chk,
got, want, float64TypeString(tolerance),
func(a, b float64) bool {
return IsFloat64Similar(a, b, tolerance)
},
msg...,
)
}
// IsFloat64Similar compares two floats to see if they match within the
// specified tolerance.
func IsFloat64Similar(num1, num2, tolerance float64) bool {
if num1 == num2 || math.IsNaN(num1) && math.IsNaN(num2) {
return true
}
return math.Abs(num1-num2) <= tolerance
}
////////////////////////////////////////////////////////////////
// Bounded and Unbounded Ranges.
////////////////////////////////////////////////////////////////
// Float64Boundedf checks that got lies within the bounded interval defined by
// minV and maxV according to the chosen option.
//
// On failure, the test is reported with a formatted message built from msgFmt
// and msgArgs. Returns true if got is within bounds.
func (chk *Chk) Float64Boundedf(
got float64, option BoundedOption, minV, maxV float64,
msgFmt string, msgArgs ...any,
) bool {
inRange, want := inBoundedRange(got, option, minV, maxV)
if inRange {
return true
}
chk.t.Helper()
return chk.errGotWntf(float64TypeName, got, want, msgFmt, msgArgs...)
}
// Float64Bounded checks that got lies within the bounded interval defined by
// minV and maxV according to the chosen option.
//
// On failure, the test is reported with the optional msg values appended.
// Returns true if got is within bounds.
func (chk *Chk) Float64Bounded(
got float64, option BoundedOption, minV, maxV float64, msg ...any,
) bool {
inRange, want := inBoundedRange(got, option, minV, maxV)
if inRange {
return true
}
chk.t.Helper()
return chk.errGotWnt(float64TypeName, got, want, msg...)
}
// Float64Unboundedf checks that got lies within the unbounded interval
// defined by bound and option.
//
// On failure, the test is reported with a formatted message built from msgFmt
// and msgArgs. Returns true if got is within bounds.
func (chk *Chk) Float64Unboundedf(
got float64, option UnboundedOption, bound float64,
msgFmt string, msgArgs ...any,
) bool {
inRange, want := inUnboundedRange(got, option, bound)
if inRange {
return true
}
chk.t.Helper()
return chk.errGotWntf(float64TypeName, got, want, msgFmt, msgArgs...)
}
// Float64Unbounded checks that got lies within the unbounded interval defined
// by bound and option.
//
// On failure, the test is reported with optional msg values appended. Returns
// true if got is within bounds.
func (chk *Chk) Float64Unbounded(
got float64, option UnboundedOption, bound float64, msg ...any,
) bool {
inRange, want := inUnboundedRange(got, option, bound)
if inRange {
return true
}
chk.t.Helper()
return chk.errGotWnt(float64TypeName, got, want, msg...)
}