|
| 1 | +import pytest |
| 2 | +from selfie_lib.ArrayMap import ArrayMap |
| 3 | + |
| 4 | +def assertEmpty(map): |
| 5 | + assert len(map) == 0 |
| 6 | + assert list(map.keys()) == [] |
| 7 | + assert list(map.values()) == [] |
| 8 | + assert list(map.items()) == [] |
| 9 | + with pytest.raises(KeyError): |
| 10 | + _ = map["key"] |
| 11 | + assert map == {} |
| 12 | + assert map == ArrayMap.empty() |
| 13 | + |
| 14 | +def assertSingle(map, key, value): |
| 15 | + assert len(map) == 1 |
| 16 | + assert set(map.keys()) == {key} |
| 17 | + assert list(map.values()) == [value] |
| 18 | + assert set(map.items()) == {(key, value)} |
| 19 | + assert map[key] == value |
| 20 | + with pytest.raises(KeyError): |
| 21 | + _ = map[key + "blah"] |
| 22 | + assert map == {key: value} |
| 23 | + assert map == ArrayMap.empty().plus(key, value) |
| 24 | + |
| 25 | +def assertDouble(map, key1, value1, key2, value2): |
| 26 | + assert len(map) == 2 |
| 27 | + assert set(map.keys()) == {key1, key2} |
| 28 | + assert list(map.values()) == [value1, value2] |
| 29 | + assert set(map.items()) == {(key1, value1), (key2, value2)} |
| 30 | + assert map[key1] == value1 |
| 31 | + assert map[key2] == value2 |
| 32 | + with pytest.raises(KeyError): |
| 33 | + _ = map[key1 + "blah"] |
| 34 | + assert map == {key1: value1, key2: value2} |
| 35 | + assert map == {key2: value2, key1: value1} |
| 36 | + assert map == ArrayMap.empty().plus(key1, value1).plus(key2, value2) |
| 37 | + assert map == ArrayMap.empty().plus(key2, value2).plus(key1, value1) |
| 38 | + |
| 39 | +def assertTriple(map, key1, value1, key2, value2, key3, value3): |
| 40 | + assert len(map) == 3 |
| 41 | + assert set(map.keys()) == {key1, key2, key3} |
| 42 | + assert list(map.values()) == [value1, value2, value3] |
| 43 | + assert set(map.items()) == {(key1, value1), (key2, value2), (key3, value3)} |
| 44 | + assert map[key1] == value1 |
| 45 | + assert map[key2] == value2 |
| 46 | + assert map[key3] == value3 |
| 47 | + with pytest.raises(KeyError): |
| 48 | + _ = map[key1 + "blah"] |
| 49 | + assert map == {key1: value1, key2: value2, key3: value3} |
| 50 | + assert map == ArrayMap.empty().plus(key1, value1).plus(key2, value2).plus(key3, value3) |
| 51 | + |
| 52 | +def test_empty(): |
| 53 | + assertEmpty(ArrayMap.empty()) |
| 54 | + |
| 55 | +def test_single(): |
| 56 | + empty = ArrayMap.empty() |
| 57 | + single = empty.plus("one", "1") |
| 58 | + assertEmpty(empty) |
| 59 | + assertSingle(single, "one", "1") |
| 60 | + |
| 61 | +def test_double(): |
| 62 | + empty = ArrayMap.empty() |
| 63 | + single = empty.plus("one", "1") |
| 64 | + double = single.plus("two", "2") |
| 65 | + assertEmpty(empty) |
| 66 | + assertSingle(single, "one", "1") |
| 67 | + assertDouble(double, "one", "1", "two", "2") |
| 68 | + assertDouble(single.plus("a", "sorted"), "a", "sorted", "one", "1") |
| 69 | + |
| 70 | + with pytest.raises(ValueError) as context: |
| 71 | + single.plus("one", "2") |
| 72 | + assert str(context.value) == "Key already exists" |
| 73 | + |
| 74 | +def test_triple(): |
| 75 | + triple = ArrayMap.empty().plus("1", "one").plus("2", "two").plus("3", "three") |
| 76 | + assertTriple(triple, "1", "one", "2", "two", "3", "three") |
| 77 | + |
| 78 | +def test_multi(): |
| 79 | + test_triple() # Calling another test function directly is unusual but works |
| 80 | + triple = ArrayMap.empty().plus("2", "two").plus("3", "three").plus("1", "one") |
| 81 | + assertTriple(triple, "1", "one", "2", "two", "3", "three") |
| 82 | + triple = ArrayMap.empty().plus("3", "three").plus("1", "one").plus("2", "two") |
| 83 | + assertTriple(triple, "1", "one", "2", "two", "3", "three") |
| 84 | + |
| 85 | +def test_minus_sorted_indices(): |
| 86 | + initial_map = ArrayMap.empty().plus("1", "one").plus("2", "two").plus("3", "three").plus("4", "four") |
| 87 | + modified_map = initial_map.minus_sorted_indices([1, 3]) |
| 88 | + assert len(modified_map) == 2 |
| 89 | + assert list(modified_map.keys()) == ["1", "3"] |
| 90 | + assert list(modified_map.values()) == ["one", "three"] |
| 91 | + with pytest.raises(KeyError): |
| 92 | + _ = modified_map["2"] |
| 93 | + with pytest.raises(KeyError): |
| 94 | + _ = modified_map["4"] |
| 95 | + assert modified_map == {"1": "one", "3": "three"} |
| 96 | + |
| 97 | +def test_plus_with_existing_keys(): |
| 98 | + map_with_duplicates = ArrayMap.empty().plus("a", "alpha").plus("b", "beta") |
| 99 | + with pytest.raises(ValueError): |
| 100 | + map_with_duplicates.plus("a", "new alpha") |
| 101 | + updated_map = map_with_duplicates.plus("c", "gamma") |
| 102 | + assert len(updated_map) == 3 |
| 103 | + assert updated_map["a"] == "alpha" |
| 104 | + assert updated_map["b"] == "beta" |
| 105 | + assert updated_map["c"] == "gamma" |
| 106 | + modified_map = map_with_duplicates.minus_sorted_indices([0]).plus("a", "updated alpha") |
| 107 | + assert len(modified_map) == 2 |
| 108 | + assert modified_map["a"] == "updated alpha" |
| 109 | + assert modified_map["b"] == "beta" |
| 110 | + |
| 111 | +def test_map_length(): |
| 112 | + map = ArrayMap.empty() |
| 113 | + assert len(map) == 0, "Length should be 0 for an empty map" |
| 114 | + map = map.plus("key1", "value1") |
| 115 | + assert len(map) == 1, "Length should be 1 after adding one item" |
| 116 | + map = map.plus("key2", "value2") |
| 117 | + assert len(map) == 2, "Length should be 2 after adding another item" |
| 118 | + map = map.plus("key3", "value3") |
| 119 | + assert len(map) == 3, "Length should be 3 after adding a third item" |
| 120 | + map = map.minus_sorted_indices([1]) |
| 121 | + assert len(map) == 2, "Length should be 2 after removing one item" |
| 122 | + map = map.minus_sorted_indices([0]) |
| 123 | + assert len(map) == 1, "Length should be 1 after removing another item" |
| 124 | + map = map.minus_sorted_indices([0]) |
| 125 | + assert len(map) == 0, "Length should be 0 after removing all items" |
0 commit comments