Example #1
0
def time(store):
    tm = TreeMap()
    start = time_ns()
    for i in store:
        tm.put(i, i)
    end = time_ns()
    return end - start
Example #2
0
 def make(null_values):
     tm = TreeMap()
     for each in range(self.TEST_SIZE):
         key = self.KEYS[each]
         value = ((None if each == 0 else self.VALUES[each])
                  if null_values else self.VALUES[each])
         tm.put(key, value)
     return tm
def test():
    map = TreeMap()
    assert not map.contains_value('haoranpeng')
    assert not map.contains_value(None)

    map.put('a', None)
    map.put('b', 'haoranpeng')

    assert map.contains_value('haoranpeng')
    assert map.contains_value(None)
Example #4
0
def test():
    m = TreeMap()
    m.put(1, 1)
    m.put(2, 2)
    m.put(3, 3)
    m2 = m.sub_map(2, 2)
    with pytest.raises(KeyError):
        m2.first_key()
    with pytest.raises(KeyError):
        m2.last_key()
    m3 = m.sub_map(2, 3)
    assert m3.first_key() == 2
    assert m3.last_key() == 2
Example #5
0
def test_get():
    m = TreeMap()
    # permitsNullKeys=False, permitsNullValues=True, usesIdentity=False
    put(m, 'A', True, None)
    put(m, 'A', False, True)
    put(m, 'B', True, None)
    put(m, 'A', False, False)

    try:
        m.get(None)
        pytest.fail('did not reject None key')
    except TypeError:
        pass
    except Exception as e:
        print('unexpected exception')
        raise e

    try:
        m.put(None, True)
        pytest.fail('did not reject None key')
    except TypeError:
        pass
    except Exception as e:
        print('unexpected exception')
        raise e

    try:
        put(m, 'C', None, None)
        put(m, 'C', True, None)
        put(m, 'C', None, True)
    except Exception as e:
        print('unexpected exception')
        raise e
Example #6
0
def test_map():
    map = TreeMap()
    for each in range(TEST_SIZE):
        map.put(KEYS[each], VALUES[each])
    keys = map.key_set().to_list()
    keys.sort()
    for each in range(TEST_SIZE):
        assert keys[each] == KEYS[each]
    values = map.values().to_list()
    values.sort()
    for each in range(TEST_SIZE):
        assert values[each] == VALUES[each]
    entries = map.entry_set().to_list()
    entries.sort(key=lambda e: e.get_key())
    for each in range(TEST_SIZE):
        assert (entries[each].get_key() == KEYS[each]
                and entries[each].get_value() == VALUES[each])
Example #7
0
def test():
    m1 = TreeMap(none_at_end)
    assert m1.put('a', 'a') is None
    assert m1.put('b', 'b') is None
    assert m1.put('c', 'c') is None
    assert m1.put(None, 'd') is None

    m2 = TreeMap(none_at_end)
    m2.put_all(m1)

    assert m1.last_key() is None
    assert m1.get(m1.last_key()) == 'd'
    assert m1.remove(m1.last_key()) == 'd'
    assert m1.last_key() == 'c'

    assert m2.entry_set().to_string() == '[a=a, b=b, c=c, None=d]'

    m3 = m2.tail_map('b')

    assert m3.last_key() is None
    assert m3.get(m3.last_key()) == 'd'
    assert m3.remove(m3.last_key()) == 'd'
    assert m3.last_key() == 'c'
Example #8
0
    def test_navigable_map_removers(self, m):
        views = [
            m,
            m.head_map(99, True),
            m.tail_map(-99, False),
            m.sub_map(-99, 99, True, False)
        ]

        def f1(m, k, v):
            assert m.remove(k) == v

        def f2(m, k, v):
            assert m.descending_map().remove(k) == v

        def f3(m, k, v):
            assert m.descending_map().head_map(-86, False).remove(k) == v

        def f4(m, k, v):
            assert m.descending_map().tail_map(86, True).remove(k) == v

        def f5(m, k, v):
            assert m.head_map(86, True).remove(k) == v

        def f6(m, k, v):
            assert m.tail_map(-86, True).remove(k) == v

        def f7(m, k, v):
            assert m.sub_map(-86, 86, False, True).remove(k) == v

        def f8(m, k, v):
            assert m.key_set().remove(k)

        def f9(m, k, v):
            assert m.navigable_key_set().head_set(86, True).remove(k)

        def f10(m, k, v):
            assert m.navigable_key_set().tail_set(-86, False).remove(k)

        def f11(m, k, v):
            assert m.navigable_key_set().sub_set(-86, 86, True,
                                                 False).remove(k)

        def f12(m, k, v):
            assert m.descending_key_set().head_set(-86, False).remove(k)

        def f13(m, k, v):
            assert m.descending_key_set().tail_set(86, True).remove(k)

        def f14(m, k, v):
            assert m.descending_key_set().sub_set(86, -86, True,
                                                  False).remove(k)

        removers = [
            f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14
        ]

        for view in views:
            for remover in removers:
                m.clear()
                self.equal_maps(m, TreeMap())
                assert m.put(1, 2) is None
                assert m.size() == 1
                remover(view, 1, 2)
                self.equal_maps(m, TreeMap())
Example #9
0
    def test_main(self):
        m = TreeMap()
        self.test_navigable_map_removers(m)
        self.test_navigable_map(m)
        self.test_navigable_map(m.head_map(6, False))
        self.test_navigable_map(m.head_map(5, True))
        self.test_navigable_map(m.tail_map(0, False))
        self.test_navigable_map(m.tail_map(1, True))
        self.test_navigable_map(m.sub_map(1, 6, True, False))
        self.test_navigable_map(m.sub_map(0, 5, False, True))

        self.check_functional_invariants(m)

        m.clear()

        assert m.put(3333, 77777) is None
        assert m.put(9134, 74982) is None
        assert m.get(9134) == 74982
        assert m.put(9134, 1382) == 74982
        assert m.get(9134) == 1382
        assert m.size() == 2
        self.check_functional_invariants(m)
Example #10
0
__author__ = 'Haoran Peng'
__email__ = '*****@*****.**'
__license__ = 'GPL-2.0'

class Comparable(object):
    def __lt__(self, other):
        return hash(self) < hash(other)

    def __eq__(self, other):
        return hash(self) == hash(other)

    def __hash__(self):
        return super().__hash__()

TEST_SIZE = 100
entry_data = [[Comparable() for _ in range(TEST_SIZE)],
              [object() for _ in range(TEST_SIZE)]]
map = TreeMap()

def test():
    for i in range(TEST_SIZE):
        map.put(entry_data[0][i], entry_data[1][i])
    for e in map.entry_set():
        key = e.get_key()
        value = e.get_value()
        expected_entry_hash_code = hash(key) ^ hash(value)
        assert hash(e) == expected_entry_hash_code


def test():
    m = TreeMap(none_low)

    m.put('a', 'A')
    m.put('b', 'B')
    m.put('c', 'C')

    assert m.to_string() == '{a=A, b=B, c=C}'
    assert m.head_map('b').to_string() == '{a=A}'
    assert m.tail_map('b').to_string() == '{b=B, c=C}'
    assert m.head_map(None).to_string() == '{}'
    assert m.tail_map(None).to_string() == '{a=A, b=B, c=C}'

    m = TreeMap(none_high)

    m.put('a', 'A')
    m.put('b', 'B')
    m.put('c', 'C')

    assert m.to_string() == '{a=A, b=B, c=C}'
    assert m.head_map('b').to_string() == '{a=A}'
    assert m.tail_map('b').to_string() == '{b=B, c=C}'
    assert m.head_map(None).to_string() == '{a=A, b=B, c=C}'
    assert m.tail_map(None).to_string() == '{}'

    m.put(None, 'NONE')

    assert m.to_string() == '{a=A, b=B, c=C, None=NONE}'
    assert m.head_map('b').to_string() == '{a=A}'
    assert m.tail_map('b').to_string() == '{b=B, c=C, None=NONE}'
    assert m.head_map(None).to_string() == '{a=A, b=B, c=C}'
    assert m.tail_map(None).to_string() == '{None=NONE}'
Example #12
0
#!/usr/bin/env python
import pytest
from pytreemap import TreeMap

__author__ = 'Haoran Peng'
__email__ = '*****@*****.**'
__license__ = 'GPL-2.0'

m = TreeMap()
for i in range(18):
    m.put(i, i)


@pytest.mark.parametrize('c', [m.values(), m.key_set(), m.entry_set()])
def test(c):
    it = iter(c)
    with pytest.raises(StopIteration):
        while True:
            next(it)
Example #13
0
def test():
    comparable = TreeMap()
    with pytest.raises(TypeError):
        comparable.put(None, 'anything')
    comparable.put('test', 'anything')
    with pytest.raises(TypeError):
        comparable.put(None, 'anything')

    def case_insensitive_order(a, b):
        x, y = a.lower(), b.lower()
        if x < y:
            return -1
        elif x > y:
            return 1
        else:
            return 0

    comparator = TreeMap(case_insensitive_order)
    with pytest.raises(Exception):
        comparator.put(None, 'anything')
    comparator.put('test', 'anything')
    with pytest.raises(Exception):
        comparator.put(None, 'anything')
    comparator.clear()
    with pytest.raises(Exception):
        comparator.put(object(), 'anything')