// FILE NOT TESTED extern crate snarl; use snarl::UnionFind; #[test] fn test_single() { let mut union_find = UnionFind::new(1); assert_eq!(union_find.components(), 1); assert_eq!(union_find.representative(0), 0); union_find.finalize(); assert_eq!(union_find.components(), 1); assert_eq!(union_find.component(0), 0); } #[test] fn test_separate_pair() { let mut union_find = UnionFind::new(2); assert_eq!(union_find.components(), 2); assert_eq!(union_find.representative(0), 0); assert_eq!(union_find.representative(1), 1); union_find.finalize(); assert_eq!(union_find.components(), 2); assert_eq!(union_find.component(0), 0); assert_eq!(union_find.component(1), 1); } #[test] fn test_merged_pair() { let mut union_find = UnionFind::new(2); assert_eq!(union_find.components(), 2); assert_eq!(union_find.representative(0), 0); assert_eq!(union_find.representative(1), 1); union_find.merge(1, 0); assert_eq!(union_find.components(), 1); assert_eq!(union_find.representative(0), 1); assert_eq!(union_find.representative(1), 1); union_find.finalize(); assert_eq!(union_find.components(), 1); assert_eq!(union_find.component(0), 0); assert_eq!(union_find.component(1), 0); } #[test] fn test_merged_many() { let elements = 8; let mut union_find = UnionFind::new(elements); assert_eq!(union_find.components(), elements); for element in 0..elements { assert_eq!(union_find.representative(element), element); } for element in 0..(elements / 2) { union_find.merge(element, elements - 1 - element); } for element in 0..(elements / 2 - 1) { union_find.merge(element, element + 1); } assert_eq!(union_find.components(), 1); for element in 0..elements { assert_eq!(union_find.representative(element), 7); } union_find.finalize(); assert_eq!(union_find.components(), 1); for element in 0..elements { assert_eq!(union_find.component(element), 0); } }