| 1 |
use std::{cmp, env, fmt, fs::File, io::Write, path::PathBuf}; |
| 2 |
|
| 3 |
enum UIntCode { |
| 4 |
Term, |
| 5 |
Zero(Box<UIntCode>), |
| 6 |
One(Box<UIntCode>), |
| 7 |
} |
| 8 |
|
| 9 |
enum IntCode { |
| 10 |
Zero, |
| 11 |
Pos(Box<UIntCode>), |
| 12 |
Neg(Box<UIntCode>), |
| 13 |
} |
| 14 |
|
| 15 |
impl fmt::Display for UIntCode { |
| 16 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 17 |
match *self { |
| 18 |
UIntCode::Term => write!(f, "UTerm"), |
| 19 |
UIntCode::Zero(ref inner) => write!(f, "UInt<{}, B0>", inner), |
| 20 |
UIntCode::One(ref inner) => write!(f, "UInt<{}, B1>", inner), |
| 21 |
} |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
impl fmt::Display for IntCode { |
| 26 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 27 |
match *self { |
| 28 |
IntCode::Zero => write!(f, "Z0"), |
| 29 |
IntCode::Pos(ref inner) => write!(f, "PInt<{}>", inner), |
| 30 |
IntCode::Neg(ref inner) => write!(f, "NInt<{}>", inner), |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
fn gen_uint(u: u64) -> UIntCode { |
| 36 |
let mut result = UIntCode::Term; |
| 37 |
let mut x = 1u64 << 63; |
| 38 |
while x > u { |
| 39 |
x >>= 1 |
| 40 |
} |
| 41 |
while x > 0 { |
| 42 |
result = if x & u > 0 { |
| 43 |
UIntCode::One(Box::new(result)) |
| 44 |
} else { |
| 45 |
UIntCode::Zero(Box::new(result)) |
| 46 |
}; |
| 47 |
x >>= 1; |
| 48 |
} |
| 49 |
result |
| 50 |
} |
| 51 |
|
| 52 |
fn gen_int(i: i64) -> IntCode { |
| 53 |
use std::cmp::Ordering::{Equal, Greater, Less}; |
| 54 |
|
| 55 |
match i.cmp(&0) { |
| 56 |
Greater => IntCode::Pos(Box::new(gen_uint(i as u64))), |
| 57 |
Less => IntCode::Neg(Box::new(gen_uint(i.abs() as u64))), |
| 58 |
Equal => IntCode::Zero, |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/// Computes the greatest common divisor of two integers. |
| 63 |
fn gcdi(mut a: i64, mut b: i64) -> i64 { |
| 64 |
a = a.abs(); |
| 65 |
b = b.abs(); |
| 66 |
|
| 67 |
while a != 0 { |
| 68 |
let tmp = b % a; |
| 69 |
b = a; |
| 70 |
a = tmp; |
| 71 |
} |
| 72 |
|
| 73 |
b |
| 74 |
} |
| 75 |
|
| 76 |
fn gcdu(mut a: u64, mut b: u64) -> u64 { |
| 77 |
while a != 0 { |
| 78 |
let tmp = b % a; |
| 79 |
b = a; |
| 80 |
a = tmp; |
| 81 |
} |
| 82 |
|
| 83 |
b |
| 84 |
} |
| 85 |
|
| 86 |
fn sign(i: i64) -> char { |
| 87 |
use std::cmp::Ordering::*; |
| 88 |
match i.cmp(&0) { |
| 89 |
Greater => 'P', |
| 90 |
Less => 'N', |
| 91 |
Equal => '_', |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
struct UIntTest { |
| 96 |
a: u64, |
| 97 |
op: &'static str, |
| 98 |
b: Option<u64>, |
| 99 |
r: u64, |
| 100 |
} |
| 101 |
|
| 102 |
impl fmt::Display for UIntTest { |
| 103 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 104 |
match self.b { |
| 105 |
Some(b) => write!( |
| 106 |
f, |
| 107 |
" |
| 108 |
#[test] |
| 109 |
#[allow(non_snake_case)] |
| 110 |
fn test_{a}_{op}_{b}() {{ |
| 111 |
type A = {gen_a}; |
| 112 |
type B = {gen_b}; |
| 113 |
type U{r} = {result}; |
| 114 |
|
| 115 |
#[allow(non_camel_case_types)] |
| 116 |
type U{a}{op}U{b} = <<A as {op}<B>>::Output as Same<U{r}>>::Output; |
| 117 |
|
| 118 |
assert_eq!(<U{a}{op}U{b} as Unsigned>::to_u64(), <U{r} as Unsigned>::to_u64()); |
| 119 |
}}", |
| 120 |
gen_a = gen_uint(self.a), |
| 121 |
gen_b = gen_uint(b), |
| 122 |
r = self.r, |
| 123 |
result = gen_uint(self.r), |
| 124 |
a = self.a, |
| 125 |
b = b, |
| 126 |
op = self.op |
| 127 |
), |
| 128 |
None => write!( |
| 129 |
f, |
| 130 |
" |
| 131 |
#[test] |
| 132 |
#[allow(non_snake_case)] |
| 133 |
fn test_{a}_{op}() {{ |
| 134 |
type A = {gen_a}; |
| 135 |
type U{r} = {result}; |
| 136 |
|
| 137 |
#[allow(non_camel_case_types)] |
| 138 |
type {op}U{a} = <<A as {op}>::Output as Same<U{r}>>::Output; |
| 139 |
assert_eq!(<{op}U{a} as Unsigned>::to_u64(), <U{r} as Unsigned>::to_u64()); |
| 140 |
}}", |
| 141 |
gen_a = gen_uint(self.a), |
| 142 |
r = self.r, |
| 143 |
result = gen_uint(self.r), |
| 144 |
a = self.a, |
| 145 |
op = self.op |
| 146 |
), |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
fn uint_binary_test(left: u64, operator: &'static str, right: u64, result: u64) -> UIntTest { |
| 152 |
UIntTest { |
| 153 |
a: left, |
| 154 |
op: operator, |
| 155 |
b: Option::Some(right), |
| 156 |
r: result, |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
struct IntBinaryTest { |
| 161 |
a: i64, |
| 162 |
op: &'static str, |
| 163 |
b: i64, |
| 164 |
r: i64, |
| 165 |
} |
| 166 |
|
| 167 |
impl fmt::Display for IntBinaryTest { |
| 168 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 169 |
write!( |
| 170 |
f, |
| 171 |
" |
| 172 |
#[test] |
| 173 |
#[allow(non_snake_case)] |
| 174 |
fn test_{sa}{a}_{op}_{sb}{b}() {{ |
| 175 |
type A = {gen_a}; |
| 176 |
type B = {gen_b}; |
| 177 |
type {sr}{r} = {result}; |
| 178 |
|
| 179 |
#[allow(non_camel_case_types)] |
| 180 |
type {sa}{a}{op}{sb}{b} = <<A as {op}<B>>::Output as Same<{sr}{r}>>::Output; |
| 181 |
|
| 182 |
assert_eq!(<{sa}{a}{op}{sb}{b} as Integer>::to_i64(), <{sr}{r} as Integer>::to_i64()); |
| 183 |
}}", |
| 184 |
gen_a = gen_int(self.a), |
| 185 |
gen_b = gen_int(self.b), |
| 186 |
r = self.r.abs(), |
| 187 |
sr = sign(self.r), |
| 188 |
result = gen_int(self.r), |
| 189 |
a = self.a.abs(), |
| 190 |
b = self.b.abs(), |
| 191 |
sa = sign(self.a), |
| 192 |
sb = sign(self.b), |
| 193 |
op = self.op |
| 194 |
) |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
fn int_binary_test(left: i64, operator: &'static str, right: i64, result: i64) -> IntBinaryTest { |
| 199 |
IntBinaryTest { |
| 200 |
a: left, |
| 201 |
op: operator, |
| 202 |
b: right, |
| 203 |
r: result, |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
struct IntUnaryTest { |
| 208 |
op: &'static str, |
| 209 |
a: i64, |
| 210 |
r: i64, |
| 211 |
} |
| 212 |
|
| 213 |
impl fmt::Display for IntUnaryTest { |
| 214 |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 215 |
write!( |
| 216 |
f, |
| 217 |
" |
| 218 |
#[test] |
| 219 |
#[allow(non_snake_case)] |
| 220 |
fn test_{sa}{a}_{op}() {{ |
| 221 |
type A = {gen_a}; |
| 222 |
type {sr}{r} = {result}; |
| 223 |
|
| 224 |
#[allow(non_camel_case_types)] |
| 225 |
type {op}{sa}{a} = <<A as {op}>::Output as Same<{sr}{r}>>::Output; |
| 226 |
assert_eq!(<{op}{sa}{a} as Integer>::to_i64(), <{sr}{r} as Integer>::to_i64()); |
| 227 |
}}", |
| 228 |
gen_a = gen_int(self.a), |
| 229 |
r = self.r.abs(), |
| 230 |
sr = sign(self.r), |
| 231 |
result = gen_int(self.r), |
| 232 |
a = self.a.abs(), |
| 233 |
sa = sign(self.a), |
| 234 |
op = self.op |
| 235 |
) |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
fn int_unary_test(operator: &'static str, num: i64, result: i64) -> IntUnaryTest { |
| 240 |
IntUnaryTest { |
| 241 |
op: operator, |
| 242 |
a: num, |
| 243 |
r: result, |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
fn uint_cmp_test(a: u64, b: u64) -> String { |
| 248 |
format!( |
| 249 |
" |
| 250 |
#[test] |
| 251 |
#[allow(non_snake_case)] |
| 252 |
fn test_{a}_Cmp_{b}() {{ |
| 253 |
type A = {gen_a}; |
| 254 |
type B = {gen_b}; |
| 255 |
|
| 256 |
#[allow(non_camel_case_types)] |
| 257 |
type U{a}CmpU{b} = <A as Cmp<B>>::Output; |
| 258 |
assert_eq!(<U{a}CmpU{b} as Ord>::to_ordering(), Ordering::{result:?}); |
| 259 |
}}", |
| 260 |
a = a, |
| 261 |
b = b, |
| 262 |
gen_a = gen_uint(a), |
| 263 |
gen_b = gen_uint(b), |
| 264 |
result = a.cmp(&b) |
| 265 |
) |
| 266 |
} |
| 267 |
|
| 268 |
fn int_cmp_test(a: i64, b: i64) -> String { |
| 269 |
format!( |
| 270 |
" |
| 271 |
#[test] |
| 272 |
#[allow(non_snake_case)] |
| 273 |
fn test_{sa}{a}_Cmp_{sb}{b}() {{ |
| 274 |
type A = {gen_a}; |
| 275 |
type B = {gen_b}; |
| 276 |
|
| 277 |
#[allow(non_camel_case_types)] |
| 278 |
type {sa}{a}Cmp{sb}{b} = <A as Cmp<B>>::Output; |
| 279 |
assert_eq!(<{sa}{a}Cmp{sb}{b} as Ord>::to_ordering(), Ordering::{result:?}); |
| 280 |
}}", |
| 281 |
a = a.abs(), |
| 282 |
b = b.abs(), |
| 283 |
sa = sign(a), |
| 284 |
sb = sign(b), |
| 285 |
gen_a = gen_int(a), |
| 286 |
gen_b = gen_int(b), |
| 287 |
result = a.cmp(&b) |
| 288 |
) |
| 289 |
} |
| 290 |
pub fn gen_tests() -> String { |
| 291 |
// will test all permutations of number pairs up to this (and down to its opposite for ints) |
| 292 |
let high: i64 = 5; |
| 293 |
|
| 294 |
let uints = (0u64..high as u64 + 1).flat_map(|a| (a..a + 1).cycle().zip(0..high as u64 + 1)); |
| 295 |
let ints = (-high..high + 1).flat_map(|a| (a..a + 1).cycle().zip(-high..high + 1)); |
| 296 |
|
| 297 |
let mut result = String::new(); |
| 298 |
|
| 299 |
result.push_str( |
| 300 |
" |
| 301 |
use typenum::*; |
| 302 |
use core::ops::*; |
| 303 |
use core::cmp::Ordering; |
| 304 |
", |
| 305 |
); |
| 306 |
// uint operators: |
| 307 |
for (a, b) in uints { |
| 308 |
let mut tests = vec![ |
| 309 |
uint_binary_test(a, "BitAnd", b, a & b), |
| 310 |
uint_binary_test(a, "BitOr", b, a | b), |
| 311 |
uint_binary_test(a, "BitXor", b, a ^ b), |
| 312 |
uint_binary_test(a, "Shl", b, a << b), |
| 313 |
uint_binary_test(a, "Shr", b, a >> b), |
| 314 |
uint_binary_test(a, "Add", b, a + b), |
| 315 |
uint_binary_test(a, "Mul", b, a * b), |
| 316 |
uint_binary_test(a, "Pow", b, a.pow(b as u32)), |
| 317 |
uint_binary_test(a, "Min", b, cmp::min(a, b)), |
| 318 |
uint_binary_test(a, "Max", b, cmp::max(a, b)), |
| 319 |
uint_binary_test(a, "Gcd", b, gcdu(a, b)), |
| 320 |
]; |
| 321 |
if a >= b { |
| 322 |
tests.push(uint_binary_test(a, "Sub", b, a - b)); |
| 323 |
} |
| 324 |
if b != 0 { |
| 325 |
tests.push(uint_binary_test(a, "Div", b, a / b)); |
| 326 |
tests.push(uint_binary_test(a, "Rem", b, a % b)); |
| 327 |
if a % b == 0 { |
| 328 |
tests.push(uint_binary_test(a, "PartialDiv", b, a / b)); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
for test in tests { |
| 333 |
result.push_str(&test.to_string()); |
| 334 |
} |
| 335 |
result.push_str(&uint_cmp_test(a, b)); |
| 336 |
} |
| 337 |
|
| 338 |
// int operators: |
| 339 |
for (a, b) in ints { |
| 340 |
let mut tests = vec![ |
| 341 |
int_binary_test(a, "Add", b, a + b), |
| 342 |
int_binary_test(a, "Sub", b, a - b), |
| 343 |
int_binary_test(a, "Mul", b, a * b), |
| 344 |
int_binary_test(a, "Min", b, cmp::min(a, b)), |
| 345 |
int_binary_test(a, "Max", b, cmp::max(a, b)), |
| 346 |
int_binary_test(a, "Gcd", b, gcdi(a, b)), |
| 347 |
]; |
| 348 |
if b != 0 { |
| 349 |
tests.push(int_binary_test(a, "Div", b, a / b)); |
| 350 |
tests.push(int_binary_test(a, "Rem", b, a % b)); |
| 351 |
if a % b == 0 { |
| 352 |
tests.push(int_binary_test(a, "PartialDiv", b, a / b)); |
| 353 |
} |
| 354 |
} |
| 355 |
if b >= 0 || a.abs() == 1 { |
| 356 |
let result = if b < 0 { |
| 357 |
if a == 1 { |
| 358 |
a |
| 359 |
} else if a == -1 { |
| 360 |
a.pow((-b) as u32) |
| 361 |
} else { |
| 362 |
unreachable!() |
| 363 |
} |
| 364 |
} else { |
| 365 |
a.pow(b as u32) |
| 366 |
}; |
| 367 |
tests.push(int_binary_test(a, "Pow", b, result)); |
| 368 |
} |
| 369 |
for test in tests { |
| 370 |
result.push_str(&test.to_string()); |
| 371 |
} |
| 372 |
result.push_str(&int_cmp_test(a, b)); |
| 373 |
} |
| 374 |
|
| 375 |
// int unary operators: |
| 376 |
for n in -high..high + 1 { |
| 377 |
let tests = vec![ |
| 378 |
int_unary_test("Neg", n, -n), |
| 379 |
int_unary_test("Abs", n, n.abs()), |
| 380 |
]; |
| 381 |
for test in tests { |
| 382 |
result.push_str(&test.to_string()); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
result |
| 387 |
} |
| 388 |
|
| 389 |
#[cfg_attr( |
| 390 |
feature = "no_std", |
| 391 |
deprecated( |
| 392 |
since = "1.3.0", |
| 393 |
note = "the `no_std` flag is no longer necessary and will be removed in the future" |
| 394 |
) |
| 395 |
)] |
| 396 |
pub fn no_std() {} |
| 397 |
|
| 398 |
#[cfg_attr( |
| 399 |
feature = "force_unix_path_separator", |
| 400 |
deprecated( |
| 401 |
since = "1.17.0", |
| 402 |
note = "the `force_unix_path_separator` flag is no longer necessary and will be removed in the future" |
| 403 |
) |
| 404 |
)] |
| 405 |
pub fn force_unix_path_separator() {} |
| 406 |
|
| 407 |
fn main() { |
| 408 |
no_std(); |
| 409 |
force_unix_path_separator(); |
| 410 |
println!("cargo:rerun-if-changed=tests"); |
| 411 |
|
| 412 |
let tests = gen_tests(); |
| 413 |
let out_dir = env::var("OUT_DIR").unwrap(); |
| 414 |
let dest = PathBuf::from(out_dir).join("tests.rs"); |
| 415 |
let mut f = File::create(&dest).unwrap(); |
| 416 |
f.write_all(tests.as_bytes()).unwrap(); |
| 417 |
} |