Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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])
Ejemplo n.º 3
0
 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")
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
def setmeup(n):
    print(n)
    s = dict()
    p = PyObjectMap()

    for i in range(n):
        s[i] = i
        p[i] = i
    return (s, p)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
def insert_pyobjectmap(maps):
    p = PyObjectMap()
    n = len(maps[0])
    for i in range(n):
        p[i] = i
Ejemplo n.º 12
0
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
Ejemplo n.º 13
0
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
Ejemplo n.º 14
0
def test_unique_for_nan_objects_floats():
    table = PyObjectMap(zip([float("nan") for i in range(50)], range(50)))
    assert len(table) == 1
Ejemplo n.º 15
0
def insert_pyobjectmap_preallocated(maps):
    n = len(maps[1])
    p = PyObjectMap(int(1.3 * n))
    for i in range(n):
        p[i] = i
Ejemplo n.º 16
0
def pyobjectset_add_preallocated(bufs):
    n = len(bufs[0])
    p = PyObjectMap(int(1.3 * n))
    for i in range(n):
        p[i] = i
Ejemplo n.º 17
0
 def test_from_keys_works(self):
     s = PyObjectMap.fromkeys(["a", "b", "c"], "kkk")
     self.assertEqual(len(s), 3)
     self.assertEqual(s["a"], "kkk")