File: test_PyObjectMapExtras.py

package info (click to toggle)
python-cykhash 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,240 kB
  • sloc: python: 3,954; sh: 90; makefile: 7
file content (189 lines) | stat: -rw-r--r-- 5,786 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import sys
from unittestmock import UnitTestMock

from cykhash.compat import PYPY, assert_if_not_on_PYPY
import pytest
not_on_pypy = pytest.mark.skipif(PYPY, reason="pypy has no refcounting")

from cykhash import PyObjectMap

class TestPyObjectMapMisc(UnitTestMock): 

   def test_from_keys_works(self):
      s=PyObjectMap.fromkeys(["a", "b", "c"], "kkk")
      self.assertEqual(len(s), 3)
      self.assertEqual(s["a"],"kkk") 


@not_on_pypy
class TestRefCounterPyObjectMap(UnitTestMock): 

   def test_map_put_discard_right_refcnts(self):
      a=4200
      s=PyObjectMap()
      old_ref_cnt = sys.getrefcount(a)
      s[a] = a
      self.assertEqual(sys.getrefcount(a), old_ref_cnt+2, msg="first add")
      s[a] = a # shouldn't do anything
      self.assertEqual(sys.getrefcount(a), old_ref_cnt+2, msg="second add")
      s.discard(a)
      self.assertEqual(sys.getrefcount(a), old_ref_cnt, msg="discard")

   def test_map_deallocate_decrefs(self):
      a=4200
      s=PyObjectMap()
      old_ref_cnt = sys.getrefcount(a)
      s[a] = a
      self.assertEqual(sys.getrefcount(a), old_ref_cnt+2)
      del s
      self.assertEqual(sys.getrefcount(a), old_ref_cnt)

   def test_map_interator_right_refcnts(self):
      a=4200
      s=PyObjectMap()
      old_ref_cnt = sys.getrefcount(a)
      s[a] = a
      self.assertEqual(sys.getrefcount(a), old_ref_cnt+2)
      lst = list(s.items())
      # now also lst helds two additional references
      self.assertEqual(sys.getrefcount(a), old_ref_cnt+4)

   def test_first_object_kept(self):
      a,b =float("nan"), float("nan")
      self.assertTrue(a is not b) #prerequisite
      s=PyObjectMap()
      s[a] = a
      s[b] = b
      lst = list(s.items())
      self.assertEqual(len(lst), 1)
      self.assertTrue(a is lst[0][0])

   def test_discard_with_equivalent_object(self):
      a,b =float("nan"), float("nan")
      self.assertTrue(a is not b) #prerequisite
      old_ref_cnt_a = sys.getrefcount(a)
      old_ref_cnt_b = sys.getrefcount(b)
      s=PyObjectMap()
      s[a] = None
      s[b] = None
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a+1)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)
      
      self.assertTrue(b in s)
      self.assertTrue(a in s)
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a+1)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)

      s.discard(b)
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)

      s.discard(b)
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)

      s.discard(a)
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)

      s[b] = None
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b+1)

   def test_rewrite_works(self):
      a,b =float("nan"), float("nan")
      self.assertTrue(a is not b) #prerequisite
      old_ref_cnt_a = sys.getrefcount(a)
      old_ref_cnt_b = sys.getrefcount(b)
      s=PyObjectMap()
      s[a] = a
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a+2)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)
      
      s[b] = b    
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a+1)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b+1)

      del s
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)


   def test_rewrite_works(self):
      a,b =float("nan"), float("nan")
      self.assertTrue(a is not b) #prerequisite
      old_ref_cnt_a = sys.getrefcount(a)
      old_ref_cnt_b = sys.getrefcount(b)
      s=PyObjectMap()
      s[a] = a
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a+2)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)
      
      s[b] = b    
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a+1)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b+1)

      del s
      self.assertEqual(sys.getrefcount(a), old_ref_cnt_a)
      self.assertEqual(sys.getrefcount(b), old_ref_cnt_b)


def test_nan_float():
    nan1 = float("nan")
    nan2 = float("nan")
    assert_if_not_on_PYPY(nan1 is not nan2, reason="nan is singelton in PyPy")
    table = PyObjectMap()
    table[nan1] =  42
    assert table[nan2] == 42


def test_nan_complex():
    nan1 = complex(0, float("nan"))
    nan2 = complex(0, float("nan"))
    assert_if_not_on_PYPY(nan1 is not nan2, reason="nan is singelton in PyPy")
    table = PyObjectMap()
    table[nan1] =  42
    assert table[nan2] == 42


def test_nan_in_tuple():
    nan1 = (float("nan"),)
    nan2 = (float("nan"),)
    assert_if_not_on_PYPY(nan1[0] is not nan2[0], reason="nan is singelton in PyPy")
    table = PyObjectMap()
    table[nan1] =  42
    assert table[nan2] == 42


def test_nan_in_nested_tuple():
    nan1 = (1, (2, (float("nan"),)))
    nan2 = (1, (2, (float("nan"),)))
    other = (1, 2)
    table = PyObjectMap()
    table[nan1] =  42
    assert table[nan2] == 42
    assert other not in table


def test_unique_for_nan_objects_floats():
    table = PyObjectMap(zip([float("nan") for i in range(50)], range(50)))
    assert len(table) == 1


def test_unique_for_nan_objects_complex():
    table = PyObjectMap(zip([complex(float("nan"), 1.0) for i in range(50)], range(50)))
    assert len(table) == 1


def test_unique_for_nan_objects_tuple():
    table = PyObjectMap(zip([(1.0, (float("nan"), 1.0)) for i in range(50)], range(50)))
    assert len(table) == 1


def test_float_complex_int_are_equal_as_objects():
    table = PyObjectMap(zip(range(129), range(129)))
    assert table[5] == 5
    assert table[5.0] == 5
    assert table[5.0+0j] == 5