def _generic_test(self, num_trees, num_its, num_distinct_items, new_tree_ave_len, weights): for key_type, alg, updator, key in itertools.product(_key_types, _algs, _StressTest._updators, _keys): key_type = _StressTest.updator_key_type(updator, key_type) r_ = _StressTest.updator_r(updator, num_distinct_items) print(key_type, alg, key, updator) with leak_detector.check_obj_count(100): trees = [SortedDict(key_type = key_type, alg = alg, key = key, updator = updator) \ for _ in range(num_trees)] dicts = [dict() for _ in range(num_trees)] for _ in range(num_its): i = random.randrange(0, num_trees) choice = weighted_choice(weights) if choice == 0: len_ = int(random.expovariate(1. / new_tree_ave_len)) create_choice = weighted_choice([1] * 2) if create_choice == 0: items = [(r_(), r_()) for _ in range(len_)] dicts[i] = dict(items) trees[i] = SortedDict( dicts[i].items(), key_type = key_type, alg = alg, key = key, updator = updator) elif create_choice == 1: keys = [r_() for _ in range(len_)] value = r_() dicts[i] = SortedDict.fromkeys(keys, value) trees[i] = SortedDict.fromkeys( keys, value, key_type = key_type, alg = alg, updator = updator) else: self.assertTrue(False, create_choice) self._check_tree_ok(r_, trees[i], dicts[i]) continue tree, dict_ = trees[i], dicts[i] for k in tree: self.assertIn(k, dict_) self.assertEqual(tree[k], dict_[k]) key_ = r_() if choice == 1: default = r_() self.assertEqual( tree.get(key_, default), dict_.get(key_, default), (tree.get(key_, default), dict_.get(key_, default))) self.assertEqual(key_ in tree, key_ in dict_, (key, tree, dict_)) if key_ in tree: self.assertIn(key_, dict_) self.assertEqual(tree[key_], dict_[key_], (tree[key_], dict_[key_])) b, e = r_(), r_() self.assertEqual( tree[b: e], tuple((v for (k, v) in tree.items() if b <= k < e))) elif choice == 2: val = r_() tree[key_] = val dict_[key_] = val self.assertIn(key_, tree) elif choice == 3: if key_ in tree: self.assertIn(key_, dict_) del tree[key_] del dict_[key_] else: self.assertFalse(key in dict_) else: self.assertTrue(False, choice) for i, tree, dict_ in zip(range(num_trees), trees, dicts): self._check_tree_ok(r_, tree, dict_)
def _generic_test(self, num_trees, num_its, num_distinct_items, new_tree_ave_len, weights): for key_type, alg, updator, key in itertools.product(_key_types, _algs, _StressTest._updators, _keys): key_type = _StressTest.updator_key_type(updator, key_type) r_ = _StressTest.updator_r(updator, num_distinct_items) print(key_type, alg, key, updator) with leak_detector.check_obj_count(100): trees = [SortedSet(key_type = key_type, alg = alg, key = key, updator = updator) \ for _ in range(num_trees)] sets = [set() for _ in range(num_trees)] for _ in range(num_its): i = random.randrange(0, num_trees) choice = weighted_choice(weights) if choice == 0: len_ = int(random.expovariate(1. / new_tree_ave_len)) items = [r_() for _ in range(len_)] trees[i] = SortedSet(items, key_type = key_type, alg = alg, key = key, updator = updator) sets[i] = set(items) self._check_tree_ok(r_, trees[i], sets[i], num_distinct_items, new_tree_ave_len) continue tree, set_ = trees[i], sets[i] item = r_() if choice == 1: self.assertEqual(item in tree, item in set_) elif choice == 2: tree.add(item) set_.add(item) elif choice == 3: erase_choice = weighted_choice([1] * 5) if erase_choice == 0: if item in tree: self.assertIn(item, set_) tree.remove(item) set_.remove(item) else: self.assertNotIn(item, set_) elif erase_choice == 1: stop = r_() for i in tree: if item <= i < stop: set_.remove(i) tree.remove(item, stop) elif erase_choice == 2: for i in tree: if item <= i: set_.remove(i) tree.remove(item, None) elif erase_choice == 3: stop = r_() for i in tree: if i < stop: set_.remove(i) tree.remove(None, stop) else: set_.clear() tree.remove(None, None) elif choice == 4: len_ = int(random.expovariate(1. / new_tree_ave_len)) items = [r_() for _ in range(len_)] tree = SortedSet(items, key_type = key_type, alg = alg, key = key, updator = updator) set_ = set(items) ext_merge_choice = weighted_choice([1] * 4) if ext_merge_choice == 0: trees[i] |= tree sets[i] |= set_ elif ext_merge_choice == 1: trees[i] &= tree sets[i] &= set_ elif ext_merge_choice == 2: trees[i] -= tree sets[i] -= set_ elif ext_merge_choice == 3: trees[i] ^= tree sets[i] ^= set_ else: self.assertTrue(False, ext_merge_choice) self._check_tree_ok(r_, trees[i], sets[i], num_distinct_items, new_tree_ave_len) elif choice == 5: tree = trees[i].copy() self.assertEqual(tree, trees[i]) trees[i] = tree else: self.assertTrue(False, choice) for tree, set_ in zip(trees, sets): self._check_tree_ok(r_, tree, set_, num_distinct_items, new_tree_ave_len) tree = set_ = trees = sets = item = items = None