1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 #![allow(non_snake_case)]
14 use self::test::Bencher;
15 use std::collections::HashSet;
16 use unify::{UnifyKey, UnificationTable};
18 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
21 impl UnifyKey for UnitKey {
23 fn index(&self) -> u32 { self.0 }
24 fn from_index(u: u32) -> UnitKey { UnitKey(u) }
25 fn tag(_: Option<UnitKey>) -> &'static str { "UnitKey" }
30 let mut ut: UnificationTable<UnitKey> = UnificationTable::new();
31 let k1 = ut.new_key(());
32 let k2 = ut.new_key(());
33 assert_eq!(ut.unioned(k1, k2), false);
35 assert_eq!(ut.unioned(k1, k2), true);
40 let mut ut: UnificationTable<UnitKey> = UnificationTable::new();
41 let mut keys = Vec::new();
42 const MAX: usize = 1 << 15;
45 keys.push(ut.new_key(()));
55 assert!(ut.unioned(keys[0], keys[i]));
60 fn big_array_bench(b: &mut Bencher) {
61 let mut ut: UnificationTable<UnitKey> = UnificationTable::new();
62 let mut keys = Vec::new();
63 const MAX: usize = 1 << 15;
66 keys.push(ut.new_key(()));
78 assert!(ut.unioned(keys[0], keys[i]));
85 let mut ut: UnificationTable<UnitKey> = UnificationTable::new();
86 let mut keys = Vec::new();
87 const MAX: usize = 1 << 10;
90 let key = ut.new_key(());
94 ut.union(key, keys[i-2]);
99 assert!(!ut.unioned(keys[i-1], keys[i]));
103 assert!(ut.unioned(keys[i-2], keys[i]));
107 #[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
110 impl UnifyKey for IntKey {
111 type Value = Option<i32>;
112 fn index(&self) -> u32 { self.0 }
113 fn from_index(u: u32) -> IntKey { IntKey(u) }
114 fn tag(_: Option<IntKey>) -> &'static str { "IntKey" }
117 /// Test unifying a key whose value is `Some(_)` with a key whose value is `None`.
118 /// Afterwards both should be `Some(_)`.
120 fn unify_key_Some_key_None() {
121 let mut ut: UnificationTable<IntKey> = UnificationTable::new();
122 let k1 = ut.new_key(Some(22));
123 let k2 = ut.new_key(None);
124 assert!(ut.unify_var_var(k1, k2).is_ok());
125 assert_eq!(ut.probe(k2), Some(22));
126 assert_eq!(ut.probe(k1), Some(22));
129 /// Test unifying a key whose value is `None` with a key whose value is `Some(_)`.
130 /// Afterwards both should be `Some(_)`.
132 fn unify_key_None_key_Some() {
133 let mut ut: UnificationTable<IntKey> = UnificationTable::new();
134 let k1 = ut.new_key(Some(22));
135 let k2 = ut.new_key(None);
136 assert!(ut.unify_var_var(k2, k1).is_ok());
137 assert_eq!(ut.probe(k2), Some(22));
138 assert_eq!(ut.probe(k1), Some(22));
141 /// Test unifying a key whose value is `Some(x)` with a key whose value is `Some(y)`.
142 /// This should yield an error.
144 fn unify_key_Some_x_key_Some_y() {
145 let mut ut: UnificationTable<IntKey> = UnificationTable::new();
146 let k1 = ut.new_key(Some(22));
147 let k2 = ut.new_key(Some(23));
148 assert_eq!(ut.unify_var_var(k1, k2), Err((22, 23)));
149 assert_eq!(ut.unify_var_var(k2, k1), Err((23, 22)));
150 assert_eq!(ut.probe(k1), Some(22));
151 assert_eq!(ut.probe(k2), Some(23));
154 /// Test unifying a key whose value is `Some(x)` with a key whose value is `Some(x)`.
155 /// This should be ok.
157 fn unify_key_Some_x_key_Some_x() {
158 let mut ut: UnificationTable<IntKey> = UnificationTable::new();
159 let k1 = ut.new_key(Some(22));
160 let k2 = ut.new_key(Some(22));
161 assert!(ut.unify_var_var(k1, k2).is_ok());
162 assert_eq!(ut.probe(k1), Some(22));
163 assert_eq!(ut.probe(k2), Some(22));
166 /// Test unifying a key whose value is `None` with a value is `x`.
167 /// Afterwards key should be `x`.
169 fn unify_key_None_val() {
170 let mut ut: UnificationTable<IntKey> = UnificationTable::new();
171 let k1 = ut.new_key(None);
172 assert!(ut.unify_var_value(k1, 22).is_ok());
173 assert_eq!(ut.probe(k1), Some(22));
176 /// Test unifying a key whose value is `Some(x)` with the value `y`.
177 /// This should yield an error.
179 fn unify_key_Some_x_val_y() {
180 let mut ut: UnificationTable<IntKey> = UnificationTable::new();
181 let k1 = ut.new_key(Some(22));
182 assert_eq!(ut.unify_var_value(k1, 23), Err((22, 23)));
183 assert_eq!(ut.probe(k1), Some(22));
186 /// Test unifying a key whose value is `Some(x)` with the value `x`.
187 /// This should be ok.
189 fn unify_key_Some_x_val_x() {
190 let mut ut: UnificationTable<IntKey> = UnificationTable::new();
191 let k1 = ut.new_key(Some(22));
192 assert!(ut.unify_var_value(k1, 22).is_ok());
193 assert_eq!(ut.probe(k1), Some(22));