Example #1
0
 def test_stats(self):
     d = SparseDict({1: 'a', 'b': 2, ('c',): [3, 4]})
     stats = d._stats()
     self.assertIsInstance(stats, dict, stats)
     for key in ["block_size", "num_blocks", "max_items", "num_items", "num_deleted",
                 "consider_shrink", "disable_resize", "string_lookup"]:
         self.assertIn(key, stats, key)
Example #2
0
 def test_shrink_to_static(self):
     d = SparseDict()
     d[0] = 0
     static_size = d.__sizeof__()
     for i in xrange(100):
         d[i] = i
     for i in xrange(100):
         del d[i]
     d[0] = 0
     self.assertEqual(d.__sizeof__(), static_size)
Example #3
0
    def test_resize_reentrancy(self):
        d = SparseDict()
        class Key(int):
            resize = False
            def __hash__(self):
                if self.resize:
                    d.resize(128)
                return int.__hash__(self)

        d[Key(0)] = 0
        Key.resize = True
        self.assertRaises(RuntimeError, lambda: d.resize(64))
Example #4
0
 def test_dict_repr(self):
     d = SparseDict({1: 10, "a": "ABC"})
     self.assertIsInstance(repr(d), str)
     r = repr(d.viewitems())
     self.assertIsInstance(r, str)
     self.assertTrue(
         r == "SparseDict_Items([('a', 'ABC'), (1, 10)])" or r == "SparseDict_Items([(1, 10), ('a', 'ABC')])"
     )
     r = repr(d.viewkeys())
     self.assertIsInstance(r, str)
     self.assertTrue(r == "SparseDict_Keys(['a', 1])" or r == "SparseDict_Keys([1, 'a'])")
     r = repr(d.viewvalues())
     self.assertIsInstance(r, str)
     self.assertTrue(r == "SparseDict_Values(['ABC', 10])" or r == "SparseDict_Values([10, 'ABC'])")
Example #5
0
 def _stress(self, items):
     # needs improvement
     for i in xrange(len(items)):
         d = SparseDict()
         items_part = set(items[:i])
         for item in items_part:
             d[item] = item
         self.assertEqual(set(d), items_part)
         self.assertEqual(set(d.iterkeys()), items_part)
         self.assertEqual(set(d.itervalues()), items_part)
         self.assertEqual(set(d.keys()), items_part)
         self.assertEqual(set(d.values()), items_part)
         for item in items_part:
             del d[item]
         self.assertEqual(len(d), 0)
         self.assertEqual(d, {})
         d[0] = 0
         self.assertEqual(len(d), 1)
         self.assertEqual(d, {0: 0})
Example #6
0
 def test_dict_keys(self):
     d = SparseDict({1: 10, "a": "ABC"})
     keys = d.viewkeys()
     self.assertEqual(len(keys), 2)
     self.assertEqual(set(keys), set([1, "a"]))
     self.assertEqual(keys, set([1, "a"]))
     self.assertNotEqual(keys, set([1, "a", "b"]))
     self.assertNotEqual(keys, set([1, "b"]))
     self.assertNotEqual(keys, set([1]))
     self.assertNotEqual(keys, 42)
     self.assertIn(1, keys)
     self.assertIn("a", keys)
     self.assertNotIn(10, keys)
     self.assertNotIn("Z", keys)
     self.assertEqual(d.viewkeys(), d.viewkeys())
     e = SparseDict({1: 11, "a": "def"})
     self.assertEqual(d.viewkeys(), e.viewkeys())
     del e["a"]
     self.assertNotEqual(d.viewkeys(), e.viewkeys())
Example #7
0
 def test_dict_items(self):
     d = SparseDict({1: 10, "a": "ABC"})
     items = d.viewitems()
     self.assertEqual(len(items), 2)
     self.assertEqual(set(items), set([(1, 10), ("a", "ABC")]))
     self.assertEqual(items, set([(1, 10), ("a", "ABC")]))
     self.assertNotEqual(items, set([(1, 10), ("a", "ABC"), "junk"]))
     self.assertNotEqual(items, set([(1, 10), ("a", "def")]))
     self.assertNotEqual(items, set([(1, 10)]))
     self.assertNotEqual(items, 42)
     self.assertIn((1, 10), items)
     self.assertIn(("a", "ABC"), items)
     self.assertNotIn((1, 11), items)
     self.assertNotIn(1, items)
     self.assertNotIn((), items)
     self.assertNotIn((1,), items)
     self.assertNotIn((1, 2, 3), items)
     self.assertEqual(d.viewitems(), d.viewitems())
     e = d.copy()
     self.assertEqual(d.viewitems(), e.viewitems())
     e["a"] = "def"
     self.assertNotEqual(d.viewitems(), e.viewitems())
Example #8
0
 def __init__(self, *args, **kwargs):
     SparseDict.__init__(self, *args, **kwargs)
     self.a = 'aval' # to test unpickling
     self.b = 'bval'
Example #9
0
    def test_track_dynamic(self):
        # Test GC-optimization of dynamically-created dicts
        class MyObject(object):
            pass
        x, y, z, w, o = 1.5, "a", (1, object()), [], MyObject()

        d = SparseDict()
        self._not_tracked(d)
        d[1] = "a"
        self._not_tracked(d)
        d[y] = 2
        self._not_tracked(d)
        d[z] = 3
        self._not_tracked(d)
        self._not_tracked(d.copy())
        d[4] = w
        self._tracked(d)
        self._tracked(d.copy())
        # Unlike dict, we don't have a _PyDict_MaybeUntrack hook
        # d[4] = None
        # self._not_tracked(d)
        # self._not_tracked(d.copy())

        # dd isn't tracked right now, but it may mutate and therefore d
        # which contains it must be tracked.
        d = SparseDict()
        dd = SparseDict()
        d[1] = dd
        self._not_tracked(dd)
        self._tracked(d)
        dd[1] = d
        self._tracked(dd)

        d = SparseDict.fromkeys([x, y, z])
        self._not_tracked(d)
        dd = SparseDict()
        dd.update(d)
        self._not_tracked(dd)
        d = SparseDict.fromkeys([x, y, z, o])
        self._tracked(d)
        dd = SparseDict()
        dd.update(d)
        self._tracked(dd)

        d = SparseDict(x=x, y=y, z=z)
        self._not_tracked(d)
        d = SparseDict(x=x, y=y, z=z, w=w)
        self._tracked(d)
        d = SparseDict()
        d.update(x=x, y=y, z=z)
        self._not_tracked(d)
        d.update(w=w)
        self._tracked(d)

        d = SparseDict([(x, y), (z, 1)])
        self._not_tracked(d)
        d = SparseDict([(x, y), (z, w)])
        self._tracked(d)
        d = SparseDict()
        d.update([(x, y), (z, 1)])
        self._not_tracked(d)
        d.update([(x, y), (z, w)])
        self._tracked(d)
Example #10
0
 def test_string_rehash(self):
     d = dict(('\x00' * i, i) for i in xrange(100))
     sd = SparseDict(d)
     sd.resize(200)
     self.assertEqual(d, sd)
Example #11
0
    def test_keys_set_operations(self):
        d1 = SparseDict({"a": 1, "b": 2})
        d2 = SparseDict({"b": 3, "c": 2})
        d3 = SparseDict({"d": 4, "e": 5})
        self.assertEqual(d1.viewkeys() & d1.viewkeys(), set(["a", "b"]))
        self.assertEqual(d1.viewkeys() & d2.viewkeys(), set(["b"]))
        self.assertEqual(d1.viewkeys() & d3.viewkeys(), set())
        self.assertEqual(d1.viewkeys() & set(d1.viewkeys()), set(["a", "b"]))
        self.assertEqual(d1.viewkeys() & set(d2.viewkeys()), set(["b"]))
        self.assertEqual(d1.viewkeys() & set(d3.viewkeys()), set())

        self.assertEqual(d1.viewkeys() | d1.viewkeys(), set(["a", "b"]))
        self.assertEqual(d1.viewkeys() | d2.viewkeys(), set(["a", "b", "c"]))
        self.assertEqual(d1.viewkeys() | d3.viewkeys(), set(["a", "b", "d", "e"]))
        self.assertEqual(d1.viewkeys() | set(d1.viewkeys()), set(["a", "b"]))
        self.assertEqual(d1.viewkeys() | set(d2.viewkeys()), set(["a", "b", "c"]))
        self.assertEqual(d1.viewkeys() | set(d3.viewkeys()), set(["a", "b", "d", "e"]))

        self.assertEqual(d1.viewkeys() ^ d1.viewkeys(), set())
        self.assertEqual(d1.viewkeys() ^ d2.viewkeys(), set(["a", "c"]))
        self.assertEqual(d1.viewkeys() ^ d3.viewkeys(), set(["a", "b", "d", "e"]))
        self.assertEqual(d1.viewkeys() ^ set(d1.viewkeys()), set())
        self.assertEqual(d1.viewkeys() ^ set(d2.viewkeys()), set(["a", "c"]))
        self.assertEqual(d1.viewkeys() ^ set(d3.viewkeys()), set(["a", "b", "d", "e"]))

        self.assertEqual(d1.viewkeys() - d1.viewkeys(), set())
        self.assertEqual(d1.viewkeys() - d2.viewkeys(), set(["a"]))
        self.assertEqual(d1.viewkeys() - d3.viewkeys(), set(["a", "b"]))
        self.assertEqual(d1.viewkeys() - set(d1.viewkeys()), set())
        self.assertEqual(d1.viewkeys() - set(d2.viewkeys()), set(["a"]))
        self.assertEqual(d1.viewkeys() - set(d3.viewkeys()), set(["a", "b"]))

        self.assertFalse(d1.viewkeys().isdisjoint(d2.viewkeys()))
        self.assertFalse(d1.viewkeys().isdisjoint(set(d2.viewkeys())))
        self.assertTrue(d1.viewkeys().isdisjoint(d3.viewkeys()))
        self.assertTrue(d1.viewkeys().isdisjoint(set(d3.viewkeys())))
Example #12
0
 def test_dict_values(self):
     d = SparseDict({1: 10, "a": "ABC"})
     values = d.viewvalues()
     self.assertEqual(set(values), set([10, "ABC"]))
     self.assertEqual(len(values), 2)
Example #13
0
 def test_dict_mixed_keys_items(self):
     d = SparseDict({(1, 1): 11, (2, 2): 22})
     e = SparseDict({1: 1, 2: 2})
     self.assertEqual(d.viewkeys(), e.viewitems())
     self.assertNotEqual(d.viewitems(), e.viewkeys())
Example #14
0
    def test_items_set_operations(self):
        d1 = SparseDict({"a": 1, "b": 2})
        d2 = SparseDict({"a": 2, "b": 2})
        d3 = SparseDict({"d": 4, "e": 5})
        self.assertEqual(d1.viewitems() & d1.viewitems(), set([("a", 1), ("b", 2)]))
        self.assertEqual(d1.viewitems() & d2.viewitems(), set([("b", 2)]))
        self.assertEqual(d1.viewitems() & d3.viewitems(), set())
        self.assertEqual(d1.viewitems() & set(d1.viewitems()), set([("a", 1), ("b", 2)]))
        self.assertEqual(d1.viewitems() & set(d2.viewitems()), set([("b", 2)]))
        self.assertEqual(d1.viewitems() & set(d3.viewitems()), set())

        self.assertEqual(d1.viewitems() | d1.viewitems(), set([("a", 1), ("b", 2)]))
        self.assertEqual(d1.viewitems() | d2.viewitems(), set([("a", 1), ("a", 2), ("b", 2)]))
        self.assertEqual(d1.viewitems() | d3.viewitems(), set([("a", 1), ("b", 2), ("d", 4), ("e", 5)]))
        self.assertEqual(d1.viewitems() | set(d1.viewitems()), set([("a", 1), ("b", 2)]))
        self.assertEqual(d1.viewitems() | set(d2.viewitems()), set([("a", 1), ("a", 2), ("b", 2)]))
        self.assertEqual(d1.viewitems() | set(d3.viewitems()), set([("a", 1), ("b", 2), ("d", 4), ("e", 5)]))

        self.assertEqual(d1.viewitems() ^ d1.viewitems(), set())
        self.assertEqual(d1.viewitems() ^ d2.viewitems(), set([("a", 1), ("a", 2)]))
        self.assertEqual(d1.viewitems() ^ d3.viewitems(), set([("a", 1), ("b", 2), ("d", 4), ("e", 5)]))

        self.assertEqual(d1.viewitems() - d1.viewitems(), set())
        self.assertEqual(d1.viewitems() - d2.viewitems(), set([("a", 1)]))
        self.assertEqual(d1.viewitems() - d3.viewitems(), set([("a", 1), ("b", 2)]))

        self.assertFalse(d1.viewitems().isdisjoint(d2.viewitems()))
        self.assertFalse(d1.viewitems().isdisjoint(set(d2.viewitems())))
        self.assertTrue(d1.viewitems().isdisjoint(d3.viewitems()))
        self.assertTrue(d1.viewitems().isdisjoint(set(d3.viewitems())))