| 1 |
use std::{env, fmt, fs, io, path}; |
| 2 |
|
| 3 |
use super::{gen_int, gen_uint}; |
| 4 |
|
| 5 |
/// Computes the greatest common divisor of two integers. |
| 6 |
fn gcdi(mut a: i64, mut b: i64) -> i64 { |
| 7 |
a = a.abs(); |
| 8 |
b = b.abs(); |
| 9 |
|
| 10 |
while a != 0 { |
| 11 |
let tmp = b % a; |
| 12 |
b = a; |
| 13 |
a = tmp; |
| 14 |
} |
| 15 |
|
| 16 |
b |
| 17 |
} |
| 18 |
|
| 19 |
fn gcdu(mut a: u64, mut b: u64) -> u64 { |
| 20 |
while a != 0 { |
| 21 |
let tmp = b % a; |
| 22 |
b = a; |
| 23 |
a = tmp; |
| 24 |
} |
| 25 |
|
| 26 |
b |
| 27 |
} |
| 28 |
|
| 29 |
fn sign(i: i64) -> char { |
| 30 |
use std::cmp::Ordering::*; |
| 31 |
match i.cmp(&0) { |
| 32 |
Greater => 'P', |
| 33 |
Less => 'N', |
| 34 |
Equal => '_', |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
struct UIntTest { |
| 39 |
a: u64, |
| 40 |
op: &'static str, |
| 41 |
b: Option<u64>, |
| 42 |
r: u64, |
| 43 |
} |
| 44 |
|
| 45 |
impl fmt::Display for UIntTest { |
| 46 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 47 |
match self.b { |
| 48 |
Some(b) => write!( |
| 49 |
f, |
| 50 |
" |
| 51 |
#[test] |
| 52 |
#[allow(non_snake_case)] |
| 53 |
fn test_{a}_{op}_{b}() {{ |
| 54 |
type A = {gen_a}; |
| 55 |
type B = {gen_b}; |
| 56 |
type U{r} = {result}; |
| 57 |
|
| 58 |
#[allow(non_camel_case_types)] |
| 59 |
type U{a}{op}U{b} = <<A as {op}<B>>::Output as Same<U{r}>>::Output; |
| 60 |
|
| 61 |
assert_eq!(<U{a}{op}U{b} as Unsigned>::to_u64(), <U{r} as Unsigned>::to_u64()); |
| 62 |
}}", |
| 63 |
gen_a = gen_uint(self.a), |
| 64 |
gen_b = gen_uint(b), |
| 65 |
r = self.r, |
| 66 |
result = gen_uint(self.r), |
| 67 |
a = self.a, |
| 68 |
b = b, |
| 69 |
op = self.op |
| 70 |
), |
| 71 |
None => write!( |
| 72 |
f, |
| 73 |
" |
| 74 |
#[test] |
| 75 |
#[allow(non_snake_case)] |
| 76 |
fn test_{a}_{op}() {{ |
| 77 |
type A = {gen_a}; |
| 78 |
type U{r} = {result}; |
| 79 |
|
| 80 |
#[allow(non_camel_case_types)] |
| 81 |
type {op}U{a} = <<A as {op}>::Output as Same<U{r}>>::Output; |
| 82 |
assert_eq!(<{op}U{a} as Unsigned>::to_u64(), <U{r} as Unsigned>::to_u64()); |
| 83 |
}}", |
| 84 |
gen_a = gen_uint(self.a), |
| 85 |
r = self.r, |
| 86 |
result = gen_uint(self.r), |
| 87 |
a = self.a, |
| 88 |
op = self.op |
| 89 |
), |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
fn uint_binary_test(left: u64, operator: &'static str, right: u64, result: u64) -> UIntTest { |
| 95 |
UIntTest { |
| 96 |
a: left, |
| 97 |
op: operator, |
| 98 |
b: Option::Some(right), |
| 99 |
r: result, |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// fn uint_unary_test(op: &'static str, a: u64, result: u64) -> UIntTest { |
| 104 |
// UIntTest { a: a, op: op, b: Option::None, r: result } |
| 105 |
// } |
| 106 |
|
| 107 |
struct IntBinaryTest { |
| 108 |
a: i64, |
| 109 |
op: &'static str, |
| 110 |
b: i64, |
| 111 |
r: i64, |
| 112 |
} |
| 113 |
|
| 114 |
impl fmt::Display for IntBinaryTest { |
| 115 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 116 |
write!( |
| 117 |
f, |
| 118 |
" |
| 119 |
#[test] |
| 120 |
#[allow(non_snake_case)] |
| 121 |
fn test_{sa}{a}_{op}_{sb}{b}() {{ |
| 122 |
type A = {gen_a}; |
| 123 |
type B = {gen_b}; |
| 124 |
type {sr}{r} = {result}; |
| 125 |
|
| 126 |
#[allow(non_camel_case_types)] |
| 127 |
type {sa}{a}{op}{sb}{b} = <<A as {op}<B>>::Output as Same<{sr}{r}>>::Output; |
| 128 |
|
| 129 |
assert_eq!(<{sa}{a}{op}{sb}{b} as Integer>::to_i64(), <{sr}{r} as Integer>::to_i64()); |
| 130 |
}}", |
| 131 |
gen_a = gen_int(self.a), |
| 132 |
gen_b = gen_int(self.b), |
| 133 |
r = self.r.abs(), |
| 134 |
sr = sign(self.r), |
| 135 |
result = gen_int(self.r), |
| 136 |
a = self.a.abs(), |
| 137 |
b = self.b.abs(), |
| 138 |
sa = sign(self.a), |
| 139 |
sb = sign(self.b), |
| 140 |
op = self.op |
| 141 |
) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
fn int_binary_test(left: i64, operator: &'static str, right: i64, result: i64) -> IntBinaryTest { |
| 146 |
IntBinaryTest { |
| 147 |
a: left, |
| 148 |
op: operator, |
| 149 |
b: right, |
| 150 |
r: result, |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
struct IntUnaryTest { |
| 155 |
op: &'static str, |
| 156 |
a: i64, |
| 157 |
r: i64, |
| 158 |
} |
| 159 |
|
| 160 |
impl fmt::Display for IntUnaryTest { |
| 161 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 162 |
write!( |
| 163 |
f, |
| 164 |
" |
| 165 |
#[test] |
| 166 |
#[allow(non_snake_case)] |
| 167 |
fn test_{sa}{a}_{op}() {{ |
| 168 |
type A = {gen_a}; |
| 169 |
type {sr}{r} = {result}; |
| 170 |
|
| 171 |
#[allow(non_camel_case_types)] |
| 172 |
type {op}{sa}{a} = <<A as {op}>::Output as Same<{sr}{r}>>::Output; |
| 173 |
assert_eq!(<{op}{sa}{a} as Integer>::to_i64(), <{sr}{r} as Integer>::to_i64()); |
| 174 |
}}", |
| 175 |
gen_a = gen_int(self.a), |
| 176 |
r = self.r.abs(), |
| 177 |
sr = sign(self.r), |
| 178 |
result = gen_int(self.r), |
| 179 |
a = self.a.abs(), |
| 180 |
sa = sign(self.a), |
| 181 |
op = self.op |
| 182 |
) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
fn int_unary_test(operator: &'static str, num: i64, result: i64) -> IntUnaryTest { |
| 187 |
IntUnaryTest { |
| 188 |
op: operator, |
| 189 |
a: num, |
| 190 |
r: result, |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
fn uint_cmp_test(a: u64, b: u64) -> String { |
| 195 |
format!( |
| 196 |
" |
| 197 |
#[test] |
| 198 |
#[allow(non_snake_case)] |
| 199 |
fn test_{a}_Cmp_{b}() {{ |
| 200 |
type A = {gen_a}; |
| 201 |
type B = {gen_b}; |
| 202 |
|
| 203 |
#[allow(non_camel_case_types)] |
| 204 |
type U{a}CmpU{b} = <A as Cmp<B>>::Output; |
| 205 |
assert_eq!(<U{a}CmpU{b} as Ord>::to_ordering(), Ordering::{result:?}); |
| 206 |
}}", |
| 207 |
a = a, |
| 208 |
b = b, |
| 209 |
gen_a = gen_uint(a), |
| 210 |
gen_b = gen_uint(b), |
| 211 |
result = a.cmp(&b) |
| 212 |
) |
| 213 |
} |
| 214 |
|
| 215 |
fn int_cmp_test(a: i64, b: i64) -> String { |
| 216 |
format!( |
| 217 |
" |
| 218 |
#[test] |
| 219 |
#[allow(non_snake_case)] |
| 220 |
fn test_{sa}{a}_Cmp_{sb}{b}() {{ |
| 221 |
type A = {gen_a}; |
| 222 |
type B = {gen_b}; |
| 223 |
|
| 224 |
#[allow(non_camel_case_types)] |
| 225 |
type {sa}{a}Cmp{sb}{b} = <A as Cmp<B>>::Output; |
| 226 |
assert_eq!(<{sa}{a}Cmp{sb}{b} as Ord>::to_ordering(), Ordering::{result:?}); |
| 227 |
}}", |
| 228 |
a = a.abs(), |
| 229 |
b = b.abs(), |
| 230 |
sa = sign(a), |
| 231 |
sb = sign(b), |
| 232 |
gen_a = gen_int(a), |
| 233 |
gen_b = gen_int(b), |
| 234 |
result = a.cmp(&b) |
| 235 |
) |
| 236 |
} |
| 237 |
|
| 238 |
// Allow for rustc 1.22 compatibility. |
| 239 |
#[allow(bare_trait_objects)] |
| 240 |
pub fn build_tests() -> Result<(), Box<::std::error::Error>> { |
| 241 |
// will test all permutations of number pairs up to this (and down to its opposite for ints) |
| 242 |
let high: i64 = 5; |
| 243 |
|
| 244 |
let uints = (0u64..high as u64 + 1).flat_map(|a| (a..a + 1).cycle().zip(0..high as u64 + 1)); |
| 245 |
let ints = (-high..high + 1).flat_map(|a| (a..a + 1).cycle().zip(-high..high + 1)); |
| 246 |
|
| 247 |
let out_dir = env::var("OUT_DIR")?; |
| 248 |
let dest = path::Path::new(&out_dir).join("tests.rs"); |
| 249 |
let f = fs::File::create(&dest)?; |
| 250 |
let mut writer = io::BufWriter::new(&f); |
| 251 |
use std::io::Write; |
| 252 |
writer.write_all( |
| 253 |
b" |
| 254 |
extern crate typenum; |
| 255 |
|
| 256 |
use std::ops::*; |
| 257 |
use std::cmp::Ordering; |
| 258 |
use typenum::*; |
| 259 |
", |
| 260 |
)?; |
| 261 |
use std::cmp; |
| 262 |
// uint operators: |
| 263 |
for (a, b) in uints { |
| 264 |
write!(writer, "{}", uint_binary_test(a, "BitAnd", b, a & b))?; |
| 265 |
write!(writer, "{}", uint_binary_test(a, "BitOr", b, a | b))?; |
| 266 |
write!(writer, "{}", uint_binary_test(a, "BitXor", b, a ^ b))?; |
| 267 |
write!(writer, "{}", uint_binary_test(a, "Shl", b, a << b))?; |
| 268 |
write!(writer, "{}", uint_binary_test(a, "Shr", b, a >> b))?; |
| 269 |
write!(writer, "{}", uint_binary_test(a, "Add", b, a + b))?; |
| 270 |
write!(writer, "{}", uint_binary_test(a, "Min", b, cmp::min(a, b)))?; |
| 271 |
write!(writer, "{}", uint_binary_test(a, "Max", b, cmp::max(a, b)))?; |
| 272 |
write!(writer, "{}", uint_binary_test(a, "Gcd", b, gcdu(a, b)))?; |
| 273 |
if a >= b { |
| 274 |
write!(writer, "{}", uint_binary_test(a, "Sub", b, a - b))?; |
| 275 |
} |
| 276 |
write!(writer, "{}", uint_binary_test(a, "Mul", b, a * b))?; |
| 277 |
if b != 0 { |
| 278 |
write!(writer, "{}", uint_binary_test(a, "Div", b, a / b))?; |
| 279 |
write!(writer, "{}", uint_binary_test(a, "Rem", b, a % b))?; |
| 280 |
if a % b == 0 { |
| 281 |
write!(writer, "{}", uint_binary_test(a, "PartialDiv", b, a / b))?; |
| 282 |
} |
| 283 |
} |
| 284 |
write!(writer, "{}", uint_binary_test(a, "Pow", b, a.pow(b as u32)))?; |
| 285 |
write!(writer, "{}", uint_cmp_test(a, b))?; |
| 286 |
} |
| 287 |
// int operators: |
| 288 |
for (a, b) in ints { |
| 289 |
write!(writer, "{}", int_binary_test(a, "Add", b, a + b))?; |
| 290 |
write!(writer, "{}", int_binary_test(a, "Sub", b, a - b))?; |
| 291 |
write!(writer, "{}", int_binary_test(a, "Mul", b, a * b))?; |
| 292 |
write!(writer, "{}", int_binary_test(a, "Min", b, cmp::min(a, b)))?; |
| 293 |
write!(writer, "{}", int_binary_test(a, "Max", b, cmp::max(a, b)))?; |
| 294 |
write!(writer, "{}", int_binary_test(a, "Gcd", b, gcdi(a, b)))?; |
| 295 |
if b != 0 { |
| 296 |
write!(writer, "{}", int_binary_test(a, "Div", b, a / b))?; |
| 297 |
write!(writer, "{}", int_binary_test(a, "Rem", b, a % b))?; |
| 298 |
if a % b == 0 { |
| 299 |
write!(writer, "{}", int_binary_test(a, "PartialDiv", b, a / b))?; |
| 300 |
} |
| 301 |
} |
| 302 |
if b >= 0 || a.abs() == 1 { |
| 303 |
let result = if b < 0 { |
| 304 |
if a == 1 { |
| 305 |
a |
| 306 |
} else if a == -1 { |
| 307 |
a.pow((-b) as u32) |
| 308 |
} else { |
| 309 |
unreachable!() |
| 310 |
} |
| 311 |
} else { |
| 312 |
a.pow(b as u32) |
| 313 |
}; |
| 314 |
write!(writer, "{}", int_binary_test(a, "Pow", b, result))?; |
| 315 |
} |
| 316 |
write!(writer, "{}", int_cmp_test(a, b))?; |
| 317 |
} |
| 318 |
|
| 319 |
// int unary operators: |
| 320 |
for n in -high..high + 1 { |
| 321 |
write!(writer, "{}", int_unary_test("Neg", n, -n))?; |
| 322 |
write!(writer, "{}", int_unary_test("Abs", n, n.abs()))?; |
| 323 |
} |
| 324 |
|
| 325 |
writer.flush()?; |
| 326 |
|
| 327 |
Ok(()) |
| 328 |
} |