Skip to content

Commit 1cf2196

Browse files
committed
Merge #27
27: Chop the tests up r=cuviper a=cuviper The test modules were getting huge, and some of its functions were actually a huge amount of code due to macros, causing tests to take a long time just to compile. They are now separated into a few different tests, and the scalar macros especially are now expanded more sparingly in just a few `check()` functions. Test compile times for me went from about 25 seconds to 1.5s in debug mode, and from 300 seconds (!) to about 8s in release mode.
2 parents 01cf35a + 8964eb9 commit 1cf2196

File tree

12 files changed

+657
-610
lines changed

12 files changed

+657
-610
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
Cargo.lock
22
target
3+
*.bk
4+
*.orig

src/bigint.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ use biguint::BigUint;
3232
use UsizePromotion;
3333
use IsizePromotion;
3434

35-
#[cfg(test)]
36-
#[path = "tests/bigint.rs"]
37-
mod bigint_tests;
38-
3935
/// A Sign is a `BigInt`'s composing element.
4036
#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Debug, Hash)]
4137
pub enum Sign {
@@ -1812,3 +1808,53 @@ fn twos_complement<'a, I>(digits: I)
18121808
}
18131809
}
18141810
}
1811+
1812+
1813+
#[test]
1814+
fn test_from_biguint() {
1815+
fn check(inp_s: Sign, inp_n: usize, ans_s: Sign, ans_n: usize) {
1816+
let inp = BigInt::from_biguint(inp_s, FromPrimitive::from_usize(inp_n).unwrap());
1817+
let ans = BigInt {
1818+
sign: ans_s,
1819+
data: FromPrimitive::from_usize(ans_n).unwrap(),
1820+
};
1821+
assert_eq!(inp, ans);
1822+
}
1823+
check(Plus, 1, Plus, 1);
1824+
check(Plus, 0, NoSign, 0);
1825+
check(Minus, 1, Minus, 1);
1826+
check(NoSign, 1, NoSign, 0);
1827+
}
1828+
1829+
#[test]
1830+
fn test_from_slice() {
1831+
fn check(inp_s: Sign, inp_n: u32, ans_s: Sign, ans_n: u32) {
1832+
let inp = BigInt::from_slice(inp_s, &[inp_n]);
1833+
let ans = BigInt {
1834+
sign: ans_s,
1835+
data: FromPrimitive::from_u32(ans_n).unwrap(),
1836+
};
1837+
assert_eq!(inp, ans);
1838+
}
1839+
check(Plus, 1, Plus, 1);
1840+
check(Plus, 0, NoSign, 0);
1841+
check(Minus, 1, Minus, 1);
1842+
check(NoSign, 1, NoSign, 0);
1843+
}
1844+
1845+
#[test]
1846+
fn test_assign_from_slice() {
1847+
fn check(inp_s: Sign, inp_n: u32, ans_s: Sign, ans_n: u32) {
1848+
let mut inp = BigInt::from_slice(Minus, &[2627_u32, 0_u32, 9182_u32, 42_u32]);
1849+
inp.assign_from_slice(inp_s, &[inp_n]);
1850+
let ans = BigInt {
1851+
sign: ans_s,
1852+
data: FromPrimitive::from_u32(ans_n).unwrap(),
1853+
};
1854+
assert_eq!(inp, ans);
1855+
}
1856+
check(Plus, 1, Plus, 1);
1857+
check(Plus, 0, NoSign, 0);
1858+
check(Minus, 1, Minus, 1);
1859+
check(NoSign, 1, NoSign, 0);
1860+
}

src/biguint.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ use UsizePromotion;
3838

3939
use ParseBigIntError;
4040

41-
#[cfg(test)]
42-
#[path = "tests/biguint.rs"]
43-
mod biguint_tests;
44-
4541
/// A big unsigned integer type.
4642
///
4743
/// A `BigUint`-typed value `BigUint { data: vec!(a, b, c) }` represents a number
@@ -2277,3 +2273,32 @@ fn get_radix_base(radix: u32) -> (BigDigit, usize) {
22772273
_ => panic!("Invalid bigdigit size")
22782274
}
22792275
}
2276+
2277+
2278+
#[test]
2279+
fn test_from_slice() {
2280+
fn check(slice: &[BigDigit], data: &[BigDigit]) {
2281+
assert!(BigUint::from_slice(slice).data == data);
2282+
}
2283+
check(&[1], &[1]);
2284+
check(&[0, 0, 0], &[]);
2285+
check(&[1, 2, 0, 0], &[1, 2]);
2286+
check(&[0, 0, 1, 2], &[0, 0, 1, 2]);
2287+
check(&[0, 0, 1, 2, 0, 0], &[0, 0, 1, 2]);
2288+
check(&[-1i32 as BigDigit], &[-1i32 as BigDigit]);
2289+
}
2290+
2291+
#[test]
2292+
fn test_assign_from_slice() {
2293+
fn check(slice: &[BigDigit], data: &[BigDigit]) {
2294+
let mut p = BigUint::from_slice(&[2627_u32, 0_u32, 9182_u32, 42_u32]);
2295+
p.assign_from_slice(slice);
2296+
assert!(p.data == data);
2297+
}
2298+
check(&[1], &[1]);
2299+
check(&[0, 0, 0], &[]);
2300+
check(&[1, 2, 0, 0], &[1, 2]);
2301+
check(&[0, 0, 1, 2], &[0, 0, 1, 2]);
2302+
check(&[0, 0, 1, 2, 0, 0], &[0, 0, 1, 2]);
2303+
check(&[-1i32 as BigDigit], &[-1i32 as BigDigit]);
2304+
}

src/lib.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,6 @@ impl From<ParseIntError> for ParseBigIntError {
127127
}
128128
}
129129

130-
#[cfg(test)]
131-
use std::hash;
132-
133-
#[cfg(test)]
134-
fn hash<T: hash::Hash>(x: &T) -> u64 {
135-
use std::hash::{BuildHasher, Hasher};
136-
use std::collections::hash_map::RandomState;
137-
let mut hasher = <RandomState as BuildHasher>::Hasher::new();
138-
x.hash(&mut hasher);
139-
hasher.finish()
140-
}
141-
142130
#[macro_use]
143131
mod macros;
144132

0 commit comments

Comments
 (0)