aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/hash_set_ut.pyx
blob: e2d3dfd5c7b7e90342203e4af9fff7e138ad2ce5 (plain) (blame)
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
# cython: c_string_type=str, c_string_encoding=utf8 
 
from util.generic.hash_set cimport THashSet 
from util.generic.string cimport TString 
 
import pytest 
import unittest 
 
from cython.operator cimport dereference as deref 
 
 
class TestHashSet(unittest.TestCase):
 
    def test_simple_constructor_equality_operator(self): 
        cdef THashSet[int] c1 
        c1.insert(1) 
        assert c1.size() == 1 
        c1.insert(2) 
        c1.insert(2) 
        c1.insert(2) 
        c1.insert(2) 
        assert c1.size() == 2 
        assert c1.contains(2) 
        assert not c1.contains(5) 
        cdef THashSet[int] c2 = c1 
        assert c1 == c2 
        c1.insert(3) 
        assert c1 != c2 
        c1.erase(3) 
        assert c1 == c2 
 
    def test_insert_erase(self): 
        cdef THashSet[TString] tmp 
        self.assertTrue(tmp.insert("one").second) 
        self.assertFalse(tmp.insert("one").second) 
        self.assertTrue(tmp.insert("two").second) 
        cdef TString one = "one" 
        cdef TString two = "two" 
        self.assertEqual(tmp.erase(one), 1) 
        self.assertEqual(tmp.erase(two), 1) 
        self.assertEqual(tmp.size(), 0) 
        self.assertTrue(tmp.empty()) 
 
    def test_iterators_and_find(self): 
        cdef THashSet[TString] tmp 
        self.assertTrue(tmp.begin() == tmp.end()) 
        self.assertTrue(tmp.find("1") == tmp.end()) 
        tmp.insert("1") 
        self.assertTrue(tmp.begin() != tmp.end()) 
        cdef THashSet[TString].iterator it = tmp.find("1") 
        self.assertTrue(it != tmp.end()) 
        self.assertEqual(deref(it), "1")