/
TolerantNumerics.scala
265 lines (253 loc) · 10.7 KB
/
TolerantNumerics.scala
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
256
257
258
259
260
261
262
263
264
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalactic
/*
* Defines a custom way to determine equality for a type.
*
* <p>
* For example, here's how you could define equality between <code>Double</code>s such that
* a tolerance of ± 0.01 is allowed:
* </p>
*
* <pre class="stHighlighted">
* <span class="stReserved">class</span> <span class="stType">TolerantDoubleEquality</span> <span class="stReserved">extends</span> <span class="stType">Equality[Double]</span> {
* <br/> <span class="stReserved">private</span> <span class="stReserved">val</span> <span class="stType">Tol</span> = <span class="stLiteral">0.01</span>
* <br/> <span class="stReserved">def</span> areEqual(a: <span class="stType">Double</span>, b: <span class="stType">Any</span>): <span class="stType">Boolean</span> = {
* b <span class="stReserved">match</span> {
* <span class="stReserved">case</span> bDouble: <span class="stType">Double</span> => (a <= bDouble + <span class="stType">Tol</span>) && (a >= bDouble - <span class="stType">Tol</span>)
* <span class="stReserved">case</span> _ => <span class="stReserved">false</span>
* }
* }
* }
* </pre>
*
* <p>
* If an implicit instance of <code>TolerantDoubleEquality</code> is in scope, it will be
* used by ScalaTest's <code>===</code> operators and its <code>should equal</code> and <code>should ===</code> matcher
* syntax. Here's an example:
* </p>
*
* <pre class="stREPL">
* $ scala -cp target/jar_contents/
* Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_33).
* Type in expressions to have them evaluated.
* Type :help for more information.
* scala> import org.scalactic._
* import org.scalactic._
* scala> class TolerantDoubleEquality extends Equality[Double] {
* |
* | private val Tol = 0.01
* |
* | def areEqual(a: Double, b: Any): Boolean = {
* | b match {
* | case bDouble: Double => (a >= bDouble + Tol) && (a >= bDouble - Tol)
* | case _ => false
* | }
* | }
* | }
* defined class TolerantDoubleEquality
*
* scala> import TripleEquals._
* import TripleEquals._
*
* scala> 2.0 === 2.001
* res0: Boolean = false
*
* scala> implicit val tolerantDoubleEquality = new TolerantDoubleEquality
* tolerantDoubleEquality: TolerantDoubleEquality = TolerantDoubleEquality@70c13c17
*
* scala> 2.0 === 2.001
* res1: Boolean = true
* </pre>
*
* <p>
* <em>Note: The <code>Equality</code> type class was inspired in part by the <code>Equal</code> type class of the
* <a href="http://code.google.com/p/scalaz/" target="_blank"><code>scalaz</code></a> project.</em>
* </p>
*
* @tparam A the type whose equality is being customized
*/
/**
* Provides <code>Equality</code> and <code>Equivalence</code> instances for <code>Numeric</code> types that
* compare for equality with a given tolerance.
*
* <p>Here's an example:</p>
*
* <pre class="stREPL">
* scala> import org.scalactic._
* import org.scalactic._
*
* scala> import TripleEquals._
* import TripleEquals._
*
* scala> 2.001 === 2.0
* res0: Boolean = false
*
* scala> implicit val doubleEquality = TolerantNumerics.tolerantDoubleEquality(0.01)
* doubleEquality: org.scalactic.Equality[Double] = org.scalactic.TolerantNumerics$$anon$1@16c2bd13
*
* scala> 2.001 === 2.0
* res1: Boolean = true
* </pre>
*/
trait TolerantNumerics {
// TODO: Pretty toStrings on the results
/**
* Provides an <code>Equality</code> instance for <code>Double</code>s that
* compares for equality with the passed tolerance.
*
* @param tolerance the tolerance with which the returned <code>Equality</code> will compare <code>Double</code>s.
* @return an <code>Equality</code> that compares <code>Double</code>s using the passed tolerance.
*/
def tolerantDoubleEquality(tolerance: Double): Equality[Double] = {
if (tolerance <= 0.0)
throw new IllegalArgumentException(tolerance.toString + " passed to tolerantDoubleEquality was zero or negative. Must be a positive non-zero number.")
new Equality[Double] {
def areEqual(a: Double, b: Any): Boolean = {
b match {
case bDouble: Double => (a <= bDouble + tolerance) && (a >= bDouble - tolerance)
case _ => false
}
}
override def toString: String = s"TolerantDoubleEquality($tolerance)"
}
}
/**
* Provides an <code>Equality</code> instance for <code>Float</code>s that
* compares for equality with the passed tolerance.
*
* @param tolerance the tolerance with which the returned <code>Equality</code> will compare <code>Float</code>s.
* @return an <code>Equality</code> that compares <code>Float</code>s using the passed tolerance.
*/
def tolerantFloatEquality(tolerance: Float): Equality[Float] = {
if (tolerance <= 0.0f)
throw new IllegalArgumentException(tolerance.toString + " passed to tolerantFloatEquality was zero or negative. Must be a positive non-zero number.")
new Equality[Float] {
def areEqual(a: Float, b: Any): Boolean = {
b match {
case bFloat: Float => (a <= bFloat + tolerance) && (a >= bFloat - tolerance)
case _ => false
}
}
override def toString: String = s"TolerantFloatEquality($tolerance)"
}
}
/**
* Provides an <code>Equality</code> instance for <code>Long</code>s that
* compares for equality with the passed tolerance.
*
* @param tolerance the tolerance with which the returned <code>Equality</code> will compare <code>Long</code>s.
* @return an <code>Equality</code> that compares <code>Long</code>s using the passed tolerance.
*/
def tolerantLongEquality(tolerance: Long): Equality[Long] = {
if (tolerance <= 0L)
throw new IllegalArgumentException(tolerance.toString + " passed to tolerantLongEquality was zero or negative. Must be a positive non-zero number.")
new Equality[Long] {
def areEqual(a: Long, b: Any): Boolean = {
b match {
case bLong: Long => (a <= bLong + tolerance) && (a >= bLong - tolerance)
case _ => false
}
}
override def toString: String = s"TolerantLongEquality($tolerance)"
}
}
/**
* Provides an <code>Equality</code> instance for <code>Int</code>s that
* compares for equality with the passed tolerance.
*
* @param tolerance the tolerance with which the returned <code>Equality</code> will compare <code>Int</code>s.
* @return an <code>Equality</code> that compares <code>Int</code>s using the passed tolerance.
*/
def tolerantIntEquality(tolerance: Int): Equality[Int] = {
if (tolerance <= 0)
throw new IllegalArgumentException(tolerance.toString + " passed to tolerantIntEquality was zero or negative. Must be a positive non-zero number.")
new Equality[Int] {
def areEqual(a: Int, b: Any): Boolean = {
b match {
case bInt: Int => (a <= bInt + tolerance) && (a >= bInt - tolerance)
case _ => false
}
}
override def toString: String = s"TolerantIntEquality($tolerance)"
}
}
/**
* Provides an <code>Equality</code> instance for <code>Short</code>s that
* compares for equality with the passed tolerance.
*
* @param tolerance the tolerance with which the returned <code>Equality</code> will compare <code>Short</code>s.
* @return an <code>Equality</code> that compares <code>Short</code>s using the passed tolerance.
*/
def tolerantShortEquality(tolerance: Short): Equality[Short] = {
if (tolerance <= 0)
throw new IllegalArgumentException(tolerance.toString + " passed to tolerantShortEquality was zero or negative. Must be a positive non-zero number.")
new Equality[Short] {
def areEqual(a: Short, b: Any): Boolean = {
b match {
case bShort: Short => (a <= bShort + tolerance) && (a >= bShort - tolerance)
case _ => false
}
}
override def toString: String = s"TolerantShortEquality($tolerance)"
}
}
/**
* Provides an <code>Equality</code> instance for <code>Byte</code>s that
* compares for equality with the passed tolerance.
*
* @param tolerance the tolerance with which the returned <code>Equality</code> will compare <code>Byte</code>s.
* @return an <code>Equality</code> that compares <code>Byte</code>s using the passed tolerance.
*/
def tolerantByteEquality(tolerance: Byte): Equality[Byte] = {
if (tolerance <= 0)
throw new IllegalArgumentException(tolerance.toString + " passed to tolerantByteEquality was zero or negative. Must be a positive non-zero number.")
new Equality[Byte] {
def areEqual(a: Byte, b: Any): Boolean = {
b match {
case bByte: Byte => (a <= bByte + tolerance) && (a >= bByte - tolerance)
case _ => false
}
}
override def toString: String = s"TolerantByteEquality($tolerance)"
}
}
/**
* Provides an <code>Equivalence[N]</code> instance for any type for which a <code>Numeric[N]</code> is available that
* compares <code>N</code>s for equality with the passed tolerance.
*
* @param tolerance the tolerance with which the returned <code>Equality</code> will compare <code>Numeric</code>s.
* @return an <code>Equivalence</code> that compares <code>Numeric</code>s using the passed tolerance.
*/
def tolerantEquivalence[N : Numeric](tolerance: N): Equivalence[N] = {
val numeric = implicitly[Numeric[N]]
if (numeric.lt(tolerance, numeric.zero))
throw new IllegalArgumentException(tolerance.toString + " passed to tolerantEquivalence was zero or negative. Must be a positive non-zero number.")
new Equivalence[N] {
def areEquivalent(a: N, b: N): Boolean = {
val bPlusTolerance = numeric.plus(b, tolerance)
val bMinusTolerance = numeric.minus(b, tolerance)
(numeric.lteq(a, bPlusTolerance)) && (numeric.gteq(a, bMinusTolerance))
}
override def toString: String = s"TolerantEquivalence($tolerance)"
}
}
}
/**
* Companion object for <code>TolerantNumerics</code> that enables its members to be imported as an alternative to
* mixing them in.
*/
object TolerantNumerics extends TolerantNumerics