Пример #1
0
def test_int2float_equal_when_same_keys_and_values():
    a = Int2Float()
    b = Int2Float()
    for i in range(1, 7, 1):
        a[i] = 100 + i
        b[i] = 100 + i
    assert a == b
Пример #2
0
def test_int2float_not_equal_when_same_size_same_keys_but_different_values():
    a = Int2Float()
    b = Int2Float()
    for i in range(1, 7, 1):
        a[i] = 100 + i
        b[i] = 100 + i
    b[6] = 200
    assert a != b
Пример #3
0
def test_int2float_not_equal_when_different_size():
    a = Int2Float()
    b = Int2Float()
    for i in range(1, 7, 1):
        a[i] = 100 + i
        b[i] = 100 + i
    b[7] = 107
    assert a != b
Пример #4
0
def test_int2float_repr_when_empty_and_default_kwarg():
    int2float_map = Int2Float(default=100)
    m = re.match(
        r'\<cdatastructs.hashmap.Int2Float: object at 0x[0-9a-fA-F]+, '
        r'used 0, default 100.0\>',
        repr(int2float_map))
    assert m is not None
Пример #5
0
def test_int2float_from_ptr(int2float_map):
    int2float_map[1] = 101
    int2float_map[2] = 102

    addr = int2float_map.buffer_ptr
    int2float_new_map = Int2Float.from_ptr(addr)

    assert int2float_new_map[1] == 101.0
    assert int2float_new_map[2] == 102.0
Пример #6
0
def test_int2float_pickle_dumps_loads_when_default():
    int2float_map = Int2Float(default=0)

    for i in (1, 2, 3, 4, 5):
        int2float_map[i] = 100 + i

    data = pickle.dumps(int2float_map)
    new = pickle.loads(data)

    assert int2float_map[6] == 0
    assert new[6] == 0
    assert new == int2float_map
    assert set(new.keys()) == {1, 2, 3, 4, 5, 6}
    assert set(new.values()) == {101.0, 102.0, 103.0, 104.0, 105.0, 0}
Пример #7
0
def test_int2float_new_when_prealloc_size_kwarg():
    int2float_map = Int2Float(prealloc_size=1024)

    class Int2FloatHashTable_t(ctypes.Structure):

        _fields_ = [
            ('size', ctypes.c_size_t),
            ('current_size', ctypes.c_size_t),
            ('table_size', ctypes.c_size_t),
            ('readonly', ctypes.c_bool),
        ]

    t = Int2FloatHashTable_t.from_address(int2float_map.buffer_ptr)

    assert t.size == 1024
    assert t.current_size == 0
Пример #8
0
def test_int2float_getitem_more_keys_does_not_exist_and_default_kwarg():

    class Int2FloatItem_t(ctypes.Structure):

        _fields_ = [
            ('key', ctypes.c_ulonglong),
            ('value', ctypes.c_double),
            ('status', ctypes.c_int),
        ]

    class Int2FloatHashTable_t(ctypes.Structure):

        _fields_ = [
            ('size', ctypes.c_size_t),
            ('current_size', ctypes.c_size_t),
            ('table_size', ctypes.c_size_t),
            ('readonly', ctypes.c_bool),
        ]

    int2float_map = Int2Float(default=100)
    t = Int2FloatHashTable_t.from_address(int2float_map.buffer_ptr)

    assert int2float_map.buffer_size == (
        ctypes.sizeof(Int2FloatHashTable_t)
        + (ctypes.sizeof(Int2FloatItem_t) * t.table_size))
    assert t.size == 8
    assert t.current_size == 0

    for i in range(16):
        int2float_map[i]

    t = Int2FloatHashTable_t.from_address(int2float_map.buffer_ptr)

    assert int2float_map.buffer_size == (
        ctypes.sizeof(Int2FloatHashTable_t)
        + (ctypes.sizeof(Int2FloatItem_t) * t.table_size))
    assert len(int2float_map) == 16
    assert set(int2float_map.items()) == {
        (0, 100.0), (1, 100.0), (2, 100.0), (3, 100.0),
        (4, 100.0), (5, 100.0), (6, 100.0), (7, 100.0),
        (8, 100.0), (9, 100.0), (10, 100.0), (11, 100.0),
        (12, 100.0), (13, 100.0), (14, 100.0), (15, 100.0),
    }
    assert t.size == 16
    assert t.current_size == 16
Пример #9
0
def test_int2float_new_fail_when_invalid_default_kwarg_type():
    with pytest.raises(TypeError, match="'default' must be a float"):
        Int2Float(default='8')
Пример #10
0
def test_int2float_from_raw_data_fail_when_inconsistent_args(args, exc, msg):
    with pytest.raises(exc, match=msg):
        Int2Float._from_raw_data(*args)
Пример #11
0
def test_int2float_getitem_key_does_not_exist_and_default_kwarg_is_none():
    int2float_map = Int2Float(default=None)
    with pytest.raises(KeyError) as exc_info:
        int2float_map[1]
    assert "'1'" in str(exc_info)
Пример #12
0
def test_int2float_new_fail_when_invalid_initializer(other, exc, msg):
    with pytest.raises(exc, match=msg):
        Int2Float(other)
Пример #13
0
def test_int2float_new_when_initializer_is_mapping():
    int2float_map = Int2Float({1: 101, 2: 102})
    assert set(int2float_map.items()) == {(1, 101.0), (2, 102.0)}
Пример #14
0
def test_int2float_new_when_initializer_is_iterable(initializer):
    int2float_map = Int2Float(initializer)
    assert set(int2float_map.items()) == {(1, 101.0), (2, 102.0)}
Пример #15
0
def int2float_map():
    return Int2Float()
Пример #16
0
def test_int2float_equal_when_empty():
    a = Int2Float()
    b = Int2Float()
    assert a == b
Пример #17
0
def test_int2float_getitem_key_does_not_exist_and_default_kwarg():
    int2float_map = Int2Float(default=100)
    assert isinstance(int2float_map[1], float)
    assert int2float_map[1] == 100.0
    assert len(int2float_map) == 1