Beispiel #1
0
    def test_dict_weakref(self):
        # Create one ImmutableDict
        self.assertEqual(ImmutableDict._get_instance_count(), 0)
        d = ImmutableDict(foo=1, bar=2)
        self.assertEqual(ImmutableDict._get_instance_count(), 1)

        # Store in a weakref.WeakValueDictionary
        wvd = weakref.WeakValueDictionary()
        wvd[1] = d
        self.assertEqual(ImmutableDict._get_instance_count(), 1)
        self.assertEqual(len(wvd), 1)
        self.assertTrue(wvd[1] is d)

        # Release the only reference to the ImmutableDict
        d = None
        self.assertEqual(ImmutableDict._get_instance_count(), 0)
        # Check that it is no longer in wkd
        self.assertEqual(len(wvd), 0)
        with self.assertRaises(KeyError):
            wvd[1]

        # Create a new ImmutableDict, check that it not magically appears in
        # wkd
        d = ImmutableDict(foo=1, bar=2)
        self.assertEqual(ImmutableDict._get_instance_count(), 1)
        self.assertEqual(len(wvd), 0)
        with self.assertRaises(KeyError):
            wvd[1]
Beispiel #2
0
class StressTestImmutableDict(unittest.TestCase):
    def test_stress(self):
        r = random.Random(123)

        class X:
            pass

        x = X()

        objects = [
            x,
            x,
            True,
            False,
            None,
            "\uf111",
            -4.5,
            -4.501,
            -4.502,
            4.5,
        ]
        objects.extend(range(-50, 50))
        objects.extend(str(r.random()) for i in range(100))
        objects.append(unittest)

        dicts = list((ImmutableDict(), {}) for i in range(10))

        for i in range(1_000_000):
            # Mutate actions
            idx = random.randrange(0, len(dicts))
            idict, pydict = dicts[idx]

            action = random.randint(0, 2)
            if action == 0:
                key, value = random.choice(objects), random.choice(objects)
                idict = idict.set(key, value)
                pydict[key] = value
            elif action == 1:
                key = random.choice(objects)
                idict = idict.discard(key)
                pydict.pop(key, None)
            elif action == 2:
                update = {
                    random.choice(objects): random.choice(objects)
                    for i in range(random.randint(0, 5))
                }
                idict = idict.update(update)
                pydict.update(update)

            dicts[idx] = idict, pydict

            # Verify actions
            idict, pydict = random.choice(dicts)
            action = random.randint(0, 13)
            if action < 5:
                key = random.choice(objects)
                self.assertEqual(idict.get(key, -999), pydict.get(key, -999))
            elif action < 10:
                key = random.choice(objects)
                try:
                    value = idict[key]
                except KeyError:
                    value = KeyError
                self.assertEqual(value, pydict.get(key, KeyError))
            elif action == 10:
                self.assertTrue(compareUnsortedUniqueSequences(idict, pydict))
            elif action == 11:
                self.assertTrue(
                    compareUnsortedUniqueSequences(idict.keys(), pydict)
                )
            elif action == 12:
                self.assertTrue(
                    compareUnsortedSequences(idict.values(), pydict.values())
                )
            elif action == 13:
                self.assertTrue(
                    compareUnsortedUniqueSequences(
                        idict.items(), pydict.items()
                    )
                )

        for idict, pydict in dicts:
            self.assertEqual(dict(idict), pydict)
            self.assertTrue(idict is ImmutableDict(pydict))

        # check for memory leaks
        del X, action, dicts, i, idict, idx, key, objects, r, pydict, value
        gc.collect()
        self.assertEqual(ImmutableDict._get_instance_count(), 0)
def immutables_count():
    return (ImmutableDict._get_instance_count() +
            ImmutableList._get_instance_count())