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
|
functor Test (I: INTEGER) =
struct
fun foreach (l, f) = List.app f l
val m = concat ["Int", Int.toString (valOf I.precision)]
val _ = print (concat ["Testing ", m, "\n"])
val nums =
[valOf I.maxInt,
I.- (valOf I.maxInt, I.fromInt 1)]
@ (List.foldl
(fn (i, ac) =>
case SOME (I.fromInt i) handle Overflow => NONE of
NONE => ac
| SOME i => i :: ac)
[]
[100, 10, 5, 2, 1, 0, ~1, ~2, ~5, ~10, ~100])
@ [I.+ (I.fromInt 1, valOf I.minInt),
valOf I.minInt]
fun err msg = print (concat [m, ": ", concat msg, "\n"])
datatype z = datatype StringCvt.radix
val _ =
foreach
(nums, fn i =>
foreach
([("toString", I.toString, LargeInt.toString),
("fmt BIN", I.fmt BIN, LargeInt.fmt BIN),
("fmt OCT", I.fmt OCT, LargeInt.fmt OCT),
("fmt DEC", I.fmt DEC, LargeInt.fmt DEC),
("fmt HEX", I.fmt HEX, LargeInt.fmt HEX)],
fn (name, f, f') =>
let
val s = f i
val s' = f' (I.toLarge i) handle Overflow => "Overflow"
in
if s = s'
then ()
else err [name, " ", s, " <> ", name, " ", s']
end))
structure Answer =
struct
datatype t =
Div
| Int of I.int
| Overflow
val toString =
fn Div => "Div"
| Int i => I.toString i
| Overflow => "Overflow"
fun run (f: unit -> I.int): t =
Int (f ())
handle General.Div => Div
| General.Overflow => Overflow
val equals: t * t -> bool = op =
end
val _ =
foreach
(nums, fn i =>
let
val a1 = Answer.Int i
val a2 = Answer.run (fn () => I.fromLarge (I.toLarge i))
in
if Answer.equals (a1, a2)
then ()
else err ["fromLarge (toLarge ", I.toString i, ") = ",
Answer.toString a2]
end)
val _ =
foreach
([("abs", I.abs, LargeInt.abs),
("~", I.~, LargeInt.~),
("fromString o toString",
valOf o I.fromString o I.toString,
valOf o LargeInt.fromString o LargeInt.toString)],
fn (name, f, f') =>
foreach
(nums, fn i =>
let
val a = Answer.run (fn () => f i)
val a' = Answer.run (fn () => I.fromLarge (f' (I.toLarge i)))
in
if Answer.equals (a, a')
then ()
else err [name, " ", I.toString i,
" = ", Answer.toString a,
" <> ", Answer.toString a']
end))
val _ =
foreach
(nums, fn i =>
foreach
([("BIN", BIN), ("OCT", OCT), ("DEC", DEC), ("HEX", HEX)],
fn (rName, r) =>
let
val i' = valOf (StringCvt.scanString (I.scan r) (I.fmt r i))
in
if i = i'
then ()
else err ["scan ", rName, " ", I.toString i, " = ", I.toString i']
end))
val _ =
foreach
([("sign", I.sign, LargeInt.sign),
("toInt", I.toInt, LargeInt.toInt)],
fn (name, f, f') =>
foreach
(nums, fn i =>
let
val a = Answer.run (fn () => I.fromInt (f i))
val a' = Answer.run (fn () => I.fromInt (f' (I.toLarge i)))
in
if Answer.equals (a, a')
then ()
else err [name, " ", I.toString i,
" = ", Answer.toString a,
" <> ", Answer.toString a']
end))
val _ =
foreach
([("+", I.+, LargeInt.+),
("-", I.-, LargeInt.-),
("*", I.*, LargeInt.* ),
("div", I.div, LargeInt.div),
("max", I.max, LargeInt.max),
("min", I.min, LargeInt.min),
("mod", I.mod, LargeInt.mod),
("quot", I.quot, LargeInt.quot),
("rem", I.rem, LargeInt.rem)],
fn (name,
f: I.int * I.int -> I.int,
f': LargeInt.int * LargeInt.int -> LargeInt.int) =>
foreach
(nums, fn i: I.int =>
foreach
(nums, fn j: I.int =>
let
val a = Answer.run (fn () => f (i, j))
val a' = Answer.run (fn () =>
I.fromLarge (f' (I.toLarge i, I.toLarge j)))
in
if Answer.equals (a, a')
then ()
else err [I.toString i, " ", name, " ", I.toString j,
" = ", Answer.toString a, " <> ", Answer.toString a']
end)))
val _ =
foreach
([(">", I.>, LargeInt.>),
(">=", I.>=, LargeInt.>=),
("<", I.<, LargeInt.<),
("<=", I.<=, LargeInt.<=),
("sameSign", I.sameSign, LargeInt.sameSign)],
fn (name, f, f') =>
foreach
(nums, fn i: I.int =>
foreach
(nums, fn j: I.int =>
let
val b = f (i, j)
val b' = f' (I.toLarge i, I.toLarge j)
in
if b = b'
then ()
else err [I.toString i, " ", name, " ", I.toString j,
" = ", Bool.toString b, " <> ", Bool.toString b']
end)))
structure Order =
struct
datatype t = datatype order
val equals: t * t -> bool = op =
val toString =
fn EQUAL => "EQUAL"
| GREATER => "GREATER"
| LESS => "LESS"
end
val _ =
foreach
(nums, fn i =>
foreach
(nums, fn j =>
let
val ord = I.compare (i, j)
val ord' = LargeInt.compare (I.toLarge i, I.toLarge j)
in
if Order.equals (ord, ord')
then ()
else err ["compare (", I.toString i, ", ",
I.toString j, ") = ",
Order.toString ord,
" <> ",
Order.toString ord']
end))
end
structure S = Test (Int2)
structure S = Test (Int3)
structure S = Test (Int4)
structure S = Test (Int7)
structure S = Test (Int8)
structure S = Test (Int9)
structure S = Test (Int13)
structure S = Test (Int16)
structure S = Test (Int17)
structure S = Test (Int20)
structure S = Test (Int25)
structure S = Test (Int30)
structure S = Test (Int31)
structure S = Test (Int32)
structure S = Test (Int64)
|