def test_bad_arguments(): with pytest.raises(TypeError): Range(1, "to") # nowhere ... with pytest.raises(TypeError): Range("1", "to", 5) with pytest.raises(ValueError): Range(1, "BAD DIRECTION", 3)
def test_changing_range(): a = Array("1234") a.range = Range(3, "downto", 0) assert a.range == Range(3, "downto", 0) with pytest.raises(TypeError): a.range = range(10) with pytest.raises(ValueError): a.range = Range(7, "downto", 0)
def test_both_construction(): a = Array("1234", Range(-2, 1)) assert a.left == -2 assert a.direction == "to" assert a.right == 1 with pytest.raises(ValueError): Array("1234", Range(0, 1))
def test_bad_construction(): with pytest.raises(TypeError): Array() with pytest.raises(TypeError): Array(value=1) with pytest.raises(TypeError): Array(range=Range(0, 1)) with pytest.raises(ValueError): Array(value="1234", range=Range(0, 1)) with pytest.raises(TypeError): Array(value="1234", range=object())
def test_null_range(): r = Range(1, "downto", 4) assert r.left == 1 assert r.direction == "downto" assert r.right == 4 assert len(r) == 0 assert list(r) == [] assert list(reversed(r)) == [] with pytest.raises(IndexError): r[0] assert 2 not in r with pytest.raises(ValueError): r.index(4) assert r.count(4) == 0
def test_indexing(): a = Array("1234", Range(8, "to", 11)) assert a[8] == "1" with pytest.raises(IndexError): a[0] a[11] = False assert a[11] is False b = Array("1234", Range(10, "downto", 7)) assert b[8] == "3" with pytest.raises(IndexError): b[-2] b[8] = 9 assert b[8] == 9
def test_array_concat(): l = Array("01ZX", Range(0, 'to', 3)) p = Array("1101") r = concat(l, p) assert r == Array("01ZX1101") rconcat_called = None class SpecialArray(Array): def __rconcat__(self, other): nonlocal rconcat_called rconcat_called = 3 return super().__rconcat__(other) q = SpecialArray("ABC") r2 = concat(q, l) assert r2 == Array("ABC01ZX") r3 = concat(l, q) assert r3 == Array("01ZXABC") assert rconcat_called == 3 with pytest.raises(TypeError): concat(l, "nope") with pytest.raises(TypeError): concat("nope", l)
def test_conversions(): t = range(10, 1, -1) r = Range.from_range(t) assert r.left == 10 assert r.right == 2 assert r.direction == "downto" assert r.to_range() == t
def test_array_concat(): l = Array("01ZX", Range(0, "to", 3)) p = Array("1101") r = concat(l, p) assert r == Array("01ZX1101") with pytest.raises(TypeError): concat(l, "nope") with pytest.raises(TypeError): concat("nope", l)
def test_logic_array_or(): l = LogicArray("0011XZ") p = LogicArray("011010", Range(-9, "downto", -14)) assert (l | p) == LogicArray("01111X") with pytest.raises(TypeError): l | object() with pytest.raises(TypeError): object() | l with pytest.raises(ValueError): LogicArray("") | LogicArray("01")
def test_logic_array_constructor(): LogicArray([False, 1, "X", Logic("Z")]) l = LogicArray("01XZ") assert all(isinstance(v, Logic) for v in l) with pytest.raises(ValueError): LogicArray([object()]) assert LogicArray(0) == LogicArray("0") assert LogicArray(0xA7) == LogicArray("10100111") assert LogicArray(-1) == LogicArray("1") assert LogicArray(10, Range(5, "downto", 0)) == LogicArray("001010") assert LogicArray(-2, Range(5, "downto", 0)) == LogicArray("111110") with pytest.raises(ValueError): LogicArray(10, Range(1, "to", 3)) with pytest.raises(TypeError): LogicArray(object()) with pytest.raises(ValueError): LogicArray("101010", Range(0, 'to', 0))
def test_equality(): assert Array("1234", Range(1, 4)) == Array("1234", Range(1, 4)) assert Array("1234", Range(1, 4)) == Array("1234", Range(0, -3)) assert Array("1234", Range(1, 4)) != Array("4321", Range(1, 4)) assert Array("1234") != Array("12") assert Array("1234") != "1234" assert Array("1234") != 8
def test_downto_range(): r = Range(4, "downto", -3) assert r.left == 4 assert r.direction == "downto" assert r.right == -3 assert len(r) == 8 assert list(r) == [4, 3, 2, 1, 0, -1, -2, -3] assert list(reversed(r)) == [-3, -2, -1, 0, 1, 2, 3, 4] assert r[0] == 4 assert r[7] == -3 with pytest.raises(IndexError): r[8] assert r[3:7] == Range(1, "downto", -2) assert 0 in r assert 10 not in r assert r.index(2) == 2 with pytest.raises(ValueError): r.index(9) assert r.count(4) == 1 assert r.count(10) == 0
def test_to_range(): r = Range(1, "to", 8) assert r.left == 1 assert r.direction == "to" assert r.right == 8 assert len(r) == 8 assert list(r) == [1, 2, 3, 4, 5, 6, 7, 8] assert list(reversed(r)) == [8, 7, 6, 5, 4, 3, 2, 1] assert r[0] == 1 assert r[7] == 8 with pytest.raises(IndexError): r[8] assert r[3:7] == Range(4, "to", 7) assert 8 in r assert 10 not in r assert r.index(7) == 6 with pytest.raises(ValueError): r.index(9) assert r.count(4) == 1 assert r.count(10) == 0
def test_equality(): assert Range(7, "downto", -7) == Range(7, "downto", -7) assert Range(7, "downto", -7) != Range(0, "to", 8) assert Range(1, "to", 0) == Range(8, "to", -8) # null ranges are all equal? assert Range(1, "to", 4) != 789
def test_length(): a = Array(range=Range(1, 6)) assert len(a) == 6
def test_range(): r = Range(-2, 8) a = Array(range=r) assert a.range == r
def test_bad_getitem(): with pytest.raises(TypeError): Range(10, "downto", 4)["8"]
def test_range(): r = Range(-2, 1) a = Array(value="0123", range=r) assert a.range == r
def test_use_in_set(): assert len({Range(1, "to", 8), Range(1, "to", 8)}) == 1 assert len({Range(1, "to", 8), Range(8, "downto", 1)}) == 2
def test_bad_direction(): with pytest.raises(ValueError): Range(1, "nope", 8)
def test_uppercase_in_direction(): r = Range(1, "TO", 8) assert r.direction == "to"
def test_repr(): r = Range(5, "to", 9) assert eval(repr(r)) == r
def test_slice_direction_mismatch(): a = Array([1, 2, 3, 4], Range(10, 'downto', 7)) with pytest.raises(IndexError): a[7:9] with pytest.raises(IndexError): a[9:10] = ['a', 'b']
def test_slice_direction_mismatch(): a = Array([1, 2, 3, 4], Range(10, "downto", 7)) with pytest.raises(IndexError): a[7:9] with pytest.raises(IndexError): a[9:10] = ["a", "b"]
def test_logic_array_concat(): l = LogicArray("01ZX", Range(0, "to", 3)) p = LogicArray("1101") assert concat(l, p) == LogicArray("01ZX1101") with pytest.raises(TypeError): concat(l, "nope")
def test_other_constructors(): assert Range(1, 8) == Range(1, "to", 8) assert Range(3, -4) == Range(3, "downto", -4) assert Range(left=1, right=8) == Range(1, "to", 8) assert Range(left=3, right=-4) == Range(3, "downto", -4)
def test_bad_bound(): with pytest.raises(TypeError): Range(object(), "to", 8)
def test_range_only_construction(): a = Array(range=Range(1, -2)) assert a.left == 1 assert a.direction == 'downto' assert a.right == -2 assert all(v is None for v in a)
def test_bad_step(): with pytest.raises(ValueError): Range.from_range(range(10, 5, -2))