Beispiel #1
0
 def test_repackArrayNode(self):
     """
     When array nodes fall below 8 children, they're repacked into
     bitmapped nodes.
     """
     d = frozendict()
     vals = 'abcdefghijklmnopq'
     for i in range(17):
         d = d.withPair(i, vals[i])
     for i in range(10):
         d = d.without(i)
     di = FrozenDictInspector(d)
     self.assertEqual(bitcount(di.root.bitmap), 7)
     self.assertEqual(di.root.kind, "BitmapIndexedNode")
     self.assertEqual(d, frozendict(zip(itertools.count(10), list("klmnopq"))))
Beispiel #2
0
 def test_repackArrayNode(self):
     """
     When array nodes fall below 8 children, they're repacked into
     bitmapped nodes.
     """
     d = frozendict()
     vals = 'abcdefghijklmnopq'
     for i in range(17):
         d = d.withPair(i, vals[i])
     for i in range(10):
         d = d.without(i)
     di = FrozenDictInspector(d)
     self.assertEqual(bitcount(di.root.bitmap), 7)
     self.assertEqual(di.root.kind, "BitmapIndexedNode")
     self.assertEqual(d,
                      frozendict(zip(itertools.count(10), list("klmnopq"))))
Beispiel #3
0
    def test_assocFromEmptyInternals(self):
        """
        Adding an association to the empty frozendict creates a frozendict
        containing a bitmap-indexed node, which contains only the requested pair.

        Furthermore, only one bit is set in the bitmap, it's in the rightmost
        region, and is correctly positioned for the key's hash.
        """
        k, v = ('stuff', 42)
        d = frozendict()
        d2 = d.withPair(k, v)
        di = FrozenDictInspector(d2)
        self.assertEqual(di.root.kind, 'BitmapIndexedNode')
        self.assertEqual(bitcount(di.root.bitmap), 1)
        self.assertNotEqual(di.root.bitmap & bitpos(hash(k), 0), 0)
        i = index(di.root.bitmap, bitpos(hash(k), 0))
        self.assertEqual(di.root.array[2*i], k)
        self.assertEqual(di.root.array[2*i+1], v)
Beispiel #4
0
    def test_assocFromEmptyInternals(self):
        """
        Adding an association to the empty frozendict creates a frozendict
        containing a bitmap-indexed node, which contains only the requested pair.

        Furthermore, only one bit is set in the bitmap, it's in the rightmost
        region, and is correctly positioned for the key's hash.
        """
        k, v = ('stuff', 42)
        d = frozendict()
        d2 = d.withPair(k, v)
        di = FrozenDictInspector(d2)
        self.assertEqual(di.root.kind, 'BitmapIndexedNode')
        self.assertEqual(bitcount(di.root.bitmap), 1)
        self.assertNotEqual(di.root.bitmap & bitpos(hash(k), 0), 0)
        i = index(di.root.bitmap, bitpos(hash(k), 0))
        self.assertEqual(di.root.array[2 * i], k)
        self.assertEqual(di.root.array[2 * i + 1], v)
Beispiel #5
0
    def test_nearlyFullNode(self):
        """
        Up to 15 entries can go into a single bitmap-indexed node.
        """

        vals = 'abcdefghijklmnop'
        d = frozendict()
        #integers hash to themselves, so no collisions here
        for i in range(16):
            d = d.withPair(i, vals[i])
        self.assertEqual(len(d), 16)
        self.assertEqual(set(d.keys()), set(range(16)))
        self.assertEqual(set(d.values()), set(vals))
        self.assertEqual(set(d.items()), set(zip(range(16), vals)))
        di = FrozenDictInspector(d)
        self.assertEqual(di.root.kind, 'BitmapIndexedNode')
        self.assertEqual(di.count, 16)
        self.assertEqual(bitcount(di.root.bitmap), 16)
        self.assertEqual(len(di.root.array), 32)
        self.assertEqual(set(di.root.array[::2]), set(range(16)))
        self.assertEqual(set(di.root.array[1::2]), set(vals))
Beispiel #6
0
    def test_nearlyFullNode(self):
        """
        Up to 15 entries can go into a single bitmap-indexed node.
        """

        vals = 'abcdefghijklmnop'
        d = frozendict()
        #integers hash to themselves, so no collisions here
        for i in range(16):
            d = d.withPair(i, vals[i])
        self.assertEqual(len(d), 16)
        self.assertEqual(set(d.keys()), set(range(16)))
        self.assertEqual(set(d.values()), set(vals))
        self.assertEqual(set(d.items()), set(zip(range(16), vals)))
        di = FrozenDictInspector(d)
        self.assertEqual(di.root.kind, 'BitmapIndexedNode')
        self.assertEqual(di.count, 16)
        self.assertEqual(bitcount(di.root.bitmap), 16)
        self.assertEqual(len(di.root.array), 32)
        self.assertEqual(set(di.root.array[::2]), set(range(16)))
        self.assertEqual(set(di.root.array[1::2]), set(vals))