Skip to content

Commit e9658ac

Browse files
committed
Add basic test cases for some of the imported crates
- ordered-float - permutohedron - rustc-hash - hashbrown
1 parent 82573d9 commit e9658ac

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

src/main.rs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@ type UnitResult = Result<(), Box<dyn std::error::Error>>;
44

55
fn main() -> UnitResult {
66
run_proconio();
7+
run_ordered_float();
78
run_modtype()?;
89
run_modtype_derive();
910
run_ascii()?;
1011
run_bitset_fixed();
12+
run_permutohedron();
1113
run_superslice();
1214
run_itertools();
15+
run_rustc_hash();
16+
run_hashbrown();
17+
// run_smallvec();
18+
// run_arrayvec();
19+
// run_im();
20+
// run_im_rc();
21+
// run_num();
1322
run_rand_family()?;
1423
run_sfmt()?;
1524
run_regex()?;
@@ -55,6 +64,46 @@ fn test_proconio() {
5564
}
5665

5766
// ordered-float
67+
fn run_ordered_float() {
68+
use ordered_float::OrderedFloat;
69+
use rustc_hash::FxHasher;
70+
use std::f64::{INFINITY, NAN};
71+
use std::hash::{Hash, Hasher};
72+
73+
let mut v = [
74+
8.20, -5.83, -0.21, 3.44, -7.12, 3.39, -0.72, -1.07, 9.36, NAN,
75+
5.16, -2.81, 1.02, -8.67, 5.77, -1.24, 0.44, 9.91, -7.06, INFINITY,
76+
-3.93, 5.82, 9.64, -8.04, -4.53,
77+
]
78+
.iter()
79+
.map(|&n| OrderedFloat(n))
80+
.collect::<Vec<_>>();
81+
82+
assert_eq!(v.iter().min(), Some(&OrderedFloat(-8.67)));
83+
assert_eq!(v.iter().max(), Some(&OrderedFloat(NAN)));
84+
85+
v.sort_unstable();
86+
87+
let size = v.len();
88+
assert_eq!(v[0], OrderedFloat(-8.67));
89+
assert_eq!(v[size - 2], OrderedFloat(INFINITY));
90+
assert_eq!(v[size - 1], OrderedFloat(NAN));
91+
92+
let mut hasher = FxHasher::default();
93+
v[0].hash(&mut hasher);
94+
println!("hash for {} is {}", v[0], hasher.finish());
95+
96+
v.pop(); // NAN
97+
v.pop(); // INFINITY
98+
99+
let s = v.iter().map::<f64, _>(|&n| n.into()).sum::<f64>();
100+
assert!(10.91 < s && s < 10.92);
101+
}
102+
103+
#[test]
104+
fn test_ordered_float() {
105+
run_ordered_float();
106+
}
58107

59108
// modtype
60109
// these codes were taken from examples at https://github.com/qryxip/modtype/tree/master/examples
@@ -231,6 +280,32 @@ fn test_bitset_fixed() {
231280
}
232281

233282
// permutohedron
283+
fn run_permutohedron() {
284+
use permutohedron::Heap;
285+
286+
let mut data = vec![1, 2, 3];
287+
288+
let mut permutations = Heap::new(&mut data).collect::<Vec<_>>();
289+
assert_eq!(permutations.len(), 6);
290+
291+
permutations.sort_unstable();
292+
assert_eq!(
293+
permutations,
294+
[
295+
[1, 2, 3],
296+
[1, 3, 2],
297+
[2, 1, 3],
298+
[2, 3, 1],
299+
[3, 1, 2],
300+
[3, 2, 1]
301+
]
302+
);
303+
}
304+
305+
#[test]
306+
fn test_permutohedron() {
307+
run_permutohedron();
308+
}
234309

235310
// superslice
236311
fn run_superslice() {
@@ -262,8 +337,40 @@ fn test_itertools() {
262337
}
263338

264339
// rustc-hash
340+
fn run_rustc_hash() {
341+
use rustc_hash::FxHashMap;
342+
343+
let mut map = [('c', "Cindy"), ('a', "Alice"), ('b', "Bob")]
344+
.iter()
345+
.map(|(c, s)| (*c, s.to_string()))
346+
.collect::<FxHashMap<_, _>>();
347+
map.entry('d').or_insert("Denis".to_string());
348+
map.insert('a', "Alexa".to_string());
349+
assert_eq!(map.len(), 4);
350+
}
351+
352+
#[test]
353+
fn test_rustc_hash() {
354+
run_rustc_hash();
355+
}
265356

266357
// hashbrown
358+
fn run_hashbrown() {
359+
use hashbrown::HashMap;
360+
361+
let mut map = [('c', "Cindy"), ('a', "Alice"), ('b', "Bob")]
362+
.iter()
363+
.map(|(c, s)| (*c, s.to_string()))
364+
.collect::<HashMap<_, _>>();
365+
map.entry('d').or_insert("Denis".to_string());
366+
map.insert('a', "Alexa".to_string());
367+
assert_eq!(map.len(), 4);
368+
}
369+
370+
#[test]
371+
fn test_hashbrown() {
372+
run_hashbrown();
373+
}
267374

268375
// smallvec
269376
// arrayvec

0 commit comments

Comments
 (0)