~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/tool/algo/test/test_multiweakdict.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import py, gc
 
2
from pypy.tool.algo.multiweakdict import MultiWeakKeyDictionary
 
3
 
 
4
 
 
5
class A(object):
 
6
    pass
 
7
 
 
8
 
 
9
def test_simple():
 
10
    d = MultiWeakKeyDictionary()
 
11
    a1 = A()
 
12
    a2 = A()
 
13
    a3 = A()
 
14
    d[a1, a2] = 12
 
15
    d[a1,] = 1
 
16
    d[a2,] = 2
 
17
    d[a2, a1] = 21
 
18
    d[a2, a2] = 22
 
19
    d[()] = 0
 
20
    assert d[a1, a2] == 12
 
21
    assert d[a1,] == 1
 
22
    assert d[a2,] == 2
 
23
    assert d[a2, a1] == 21
 
24
    assert d[a2, a2] == 22
 
25
    assert d[()] == 0
 
26
    assert dict.fromkeys(d.keys()) == {(a1, a2): None,
 
27
                                       (a1,): None,
 
28
                                       (a2,): None,
 
29
                                       (a2, a1): None,
 
30
                                       (a2, a2): None,
 
31
                                       (): None}
 
32
    del d[a2,]
 
33
    assert dict.fromkeys(d.keys()) == {(a1, a2): None,
 
34
                                       (a1,): None,
 
35
                                       (a2, a1): None,
 
36
                                       (a2, a2): None,
 
37
                                       (): None}
 
38
    assert d[a1, a2] == 12
 
39
    assert d[a1,] == 1
 
40
    assert d[a2, a1] == 21
 
41
    assert d[a2, a2] == 22
 
42
    assert d[()] == 0
 
43
    py.test.raises(KeyError, "d[a2,]")
 
44
 
 
45
    del a1
 
46
    locals()   # obscure fix for CPython -- make sure a1 is no longer in
 
47
               # the cached f_locals of the frame
 
48
    gc.collect()   # less obscure fix for other Python implementations
 
49
    assert dict.fromkeys(d.keys()) == {(a2, a2): None,
 
50
                                       (): None}
 
51
    assert d[a2, a2] == 22
 
52
    assert d[()] == 0