Skip to content

Commit

Permalink
Add additional tests to increase coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
salt-die committed Nov 29, 2023
1 parent 30022f1 commit 32743e2
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__pycache__/
TODO.md
53 changes: 49 additions & 4 deletions tests/test_region_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,67 @@
def test_disjoint_intersection():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(0, 15, 10, 10))
assert a & b == Region([])
result = Region()
assert a & b == result
assert list(result.rects()) == []
assert result.bbox is None


def test_vertical_intersection():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(0, 5, 10, 10))
assert a & b == Region([Band(5, 10, [0, 10])])
result = Region([Band(5, 10, [0, 10])])
assert a & b == result
assert list(result.rects()) == [Rect(0, 5, 10, 5)]
assert result.bbox == Rect(0, 5, 10, 5)


def test_horizontal_intersection():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(5, 0, 10, 10))
assert a & b == Region([Band(0, 10, [5, 10])])
result = Region([Band(0, 10, [5, 10])])
assert a & b == result
assert list(result.rects()) == [Rect(5, 0, 5, 10)]
assert result.bbox == Rect(5, 0, 5, 10)


def test_offset_intersection():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(5, 5, 10, 10))
assert a & b == Region([Band(5, 10, [5, 10])])
result = Region([Band(5, 10, [5, 10])])
assert a & b == result
assert list(result.rects()) == [Rect(5, 5, 5, 5)]
assert result.bbox == Rect(5, 5, 5, 5)


def test_intersection_branching_1():
a = Region([Band(0, 10, [0, 10]), Band(20, 50, [0, 10])])
b = Region([Band(5, 25, [5, 15]), Band(35, 45, [5, 15])])
result = Region(
[Band(5, 10, [5, 10]), Band(20, 25, [5, 10]), Band(35, 45, [5, 10])]
)
assert a & b == result
assert list(result.rects()) == [
Rect(5, 5, 5, 5),
Rect(5, 20, 5, 5),
Rect(5, 35, 5, 10),
]
assert result.bbox == Rect(5, 5, 5, 40)


def test_intersection_branching_2():
a = Region([Band(0, 10, [0, 10]), Band(40, 50, [0, 10])])
b = Region([Band(20, 30, [0, 10])])
result = Region()
assert a & b == result
assert list(result.rects()) == []
assert result.bbox is None


def test_intersection_branching_3():
a = Region([Band(0, 10, [0, 10]), Band(15, 25, [0, 10])])
b = Region([Band(5, 25, [5, 15])])
result = Region([Band(5, 10, [5, 10]), Band(15, 25, [5, 10])])
assert a & b == result
assert list(result.rects()) == [Rect(5, 5, 5, 5), Rect(5, 15, 5, 10)]
assert result.bbox == Rect(5, 5, 5, 20)
67 changes: 63 additions & 4 deletions tests/test_region_subtraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,81 @@
def test_disjoint_subtraction():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(0, 15, 10, 10))
assert a - b == a
result = a
assert a - b == result
assert list(result.rects()) == [Rect(0, 0, 10, 10)]
assert result.bbox == Rect(0, 0, 10, 10)


def test_vertical_subtraction():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(0, 5, 10, 10))
assert a - b == Region([Band(0, 5, [0, 10])])
result = Region([Band(0, 5, [0, 10])])
assert a - b == result
assert list(result.rects()) == [Rect(0, 0, 10, 5)]
assert result.bbox == Rect(0, 0, 10, 5)


def test_horizontal_subtraction():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(5, 0, 10, 10))
assert a - b == Region([Band(0, 10, [0, 5])])
result = Region([Band(0, 10, [0, 5])])
assert a - b == result
assert list(result.rects()) == [Rect(0, 0, 5, 10)]
assert result.bbox == Rect(0, 0, 5, 10)


def test_offset_subtraction():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(5, 5, 10, 10))
assert a - b == Region([Band(0, 5, [0, 10]), Band(5, 10, [0, 5])])
result = Region([Band(0, 5, [0, 10]), Band(5, 10, [0, 5])])
assert a - b == result
assert list(result.rects()) == [Rect(0, 0, 10, 5), Rect(0, 5, 5, 5)]
assert result.bbox == Rect(0, 0, 10, 10)


def test_subtraction_branching_1():
a = Region([Band(0, 10, [0, 10]), Band(20, 50, [0, 10])])
b = Region([Band(5, 25, [5, 15]), Band(35, 45, [5, 15])])
result = Region(
[
Band(0, 5, [0, 10]),
Band(5, 10, [0, 5]),
Band(20, 25, [0, 5]),
Band(25, 35, [0, 10]),
Band(35, 45, [0, 5]),
Band(45, 50, [0, 10]),
]
)
assert a - b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 5),
Rect(0, 5, 5, 5),
Rect(0, 20, 5, 5),
Rect(0, 25, 10, 10),
Rect(0, 35, 5, 10),
Rect(0, 45, 10, 5),
]
assert result.bbox == Rect(0, 0, 10, 50)


def test_subtraction_branching_2():
a = Region([Band(0, 10, [0, 10]), Band(40, 50, [0, 10])])
b = Region([Band(20, 30, [0, 10])])
result = a
assert a - b == result
assert list(result.rects()) == [Rect(0, 0, 10, 10), Rect(0, 40, 10, 10)]
assert result.bbox == Rect(0, 0, 10, 50)


def test_subtraction_branching_3():
a = Region([Band(0, 10, [0, 10]), Band(15, 25, [0, 10])])
b = Region([Band(5, 25, [5, 15])])
result = Region([Band(0, 5, [0, 10]), Band(5, 10, [0, 5]), Band(15, 25, [0, 5])])
assert a - b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 5),
Rect(0, 5, 5, 5),
Rect(0, 15, 5, 10),
]
assert result.bbox == Rect(0, 0, 10, 25)
94 changes: 89 additions & 5 deletions tests/test_region_union.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,108 @@
def test_disjoint_union():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(0, 15, 10, 10))
assert a + b == Region([Band(0, 10, [0, 10]), Band(15, 25, [0, 10])])
result = Region([Band(0, 10, [0, 10]), Band(15, 25, [0, 10])])
assert a + b == result
assert a | b == result
assert list(result.rects()) == [Rect(0, 0, 10, 10), Rect(0, 15, 10, 10)]
assert result.bbox == Rect(0, 0, 10, 25)


def test_vertical_union():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(0, 5, 10, 10))
assert a + b == Region([Band(0, 15, [0, 10])])
result = Region([Band(0, 15, [0, 10])])
assert a + b == result
assert a | b == result
assert list(result.rects()) == [Rect(0, 0, 10, 15)]
assert result.bbox == Rect(0, 0, 10, 15)


def test_horizontal_union():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(5, 0, 10, 10))
assert a + b == Region([Band(0, 10, [0, 15])])
result = Region([Band(0, 10, [0, 15])])
assert a + b == result
assert a | b == result
assert list(result.rects()) == [Rect(0, 0, 15, 10)]
assert result.bbox == Rect(0, 0, 15, 10)


def test_offset_union():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(5, 5, 10, 10))
assert a + b == Region(
[Band(0, 5, [0, 10]), Band(5, 10, [0, 15]), Band(10, 15, [5, 15])]
result = Region([Band(0, 5, [0, 10]), Band(5, 10, [0, 15]), Band(10, 15, [5, 15])])
assert a + b == result
assert a | b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 5),
Rect(0, 5, 15, 5),
Rect(5, 10, 10, 5),
]
assert result.bbox == Rect(0, 0, 15, 15)


def test_union_branching_1():
a = Region([Band(0, 10, [0, 10]), Band(20, 50, [0, 10])])
b = Region([Band(5, 25, [5, 15]), Band(35, 45, [5, 15])])
result = Region(
[
Band(0, 5, [0, 10]),
Band(5, 10, [0, 15]),
Band(10, 20, [5, 15]),
Band(20, 25, [0, 15]),
Band(25, 35, [0, 10]),
Band(35, 45, [0, 15]),
Band(45, 50, [0, 10]),
]
)
assert a + b == result
assert a | b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 5),
Rect(0, 5, 15, 5),
Rect(5, 10, 10, 10),
Rect(0, 20, 15, 5),
Rect(0, 25, 10, 10),
Rect(0, 35, 15, 10),
Rect(0, 45, 10, 5),
]
assert result.bbox == Rect(0, 0, 15, 50)


def test_union_branching_2():
a = Region([Band(0, 10, [0, 10]), Band(40, 50, [0, 10])])
b = Region([Band(20, 30, [0, 10])])
result = Region(
[Band(0, 10, [0, 10]), Band(20, 30, [0, 10]), Band(40, 50, [0, 10])]
)
assert a + b == result
assert a | b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 10),
Rect(0, 20, 10, 10),
Rect(0, 40, 10, 10),
]
assert result.bbox == Rect(0, 0, 10, 50)


def test_union_branching_3():
a = Region([Band(0, 10, [0, 10]), Band(15, 25, [0, 10])])
b = Region([Band(5, 25, [5, 15])])
result = Region(
[
Band(0, 5, [0, 10]),
Band(5, 10, [0, 15]),
Band(10, 15, [5, 15]),
Band(15, 25, [0, 15]),
]
)
assert a + b == result
assert a | b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 5),
Rect(0, 5, 15, 5),
Rect(5, 10, 10, 5),
Rect(0, 15, 15, 10),
]
assert result.bbox == Rect(0, 0, 15, 25)
93 changes: 89 additions & 4 deletions tests/test_region_xor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,109 @@
def test_disjoint_xor():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(0, 15, 10, 10))
assert a ^ b == Region([Band(0, 10, [0, 10]), Band(15, 25, [0, 10])])
result = Region([Band(0, 10, [0, 10]), Band(15, 25, [0, 10])])
assert a ^ b == result
assert list(result.rects()) == [Rect(0, 0, 10, 10), Rect(0, 15, 10, 10)]
assert result.bbox == Rect(0, 0, 10, 25)


def test_vertical_xor():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(0, 5, 10, 10))
assert a ^ b == Region([Band(0, 5, [0, 10]), Band(10, 15, [0, 10])])
result = Region([Band(0, 5, [0, 10]), Band(10, 15, [0, 10])])
assert a ^ b == result
assert list(result.rects()) == [Rect(0, 0, 10, 5), Rect(0, 10, 10, 5)]
assert result.bbox == Rect(0, 0, 10, 15)


def test_horizontal_xor():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(5, 0, 10, 10))
assert a ^ b == Region([Band(0, 10, [0, 5, 10, 15])])
result = Region([Band(0, 10, [0, 5, 10, 15])])
assert a ^ b == result
assert list(result.rects()) == [Rect(0, 0, 5, 10), Rect(10, 0, 5, 10)]
assert result.bbox == Rect(0, 0, 15, 10)


def test_offset_xor():
a = Region.from_rect(Rect(0, 0, 10, 10))
b = Region.from_rect(Rect(5, 5, 10, 10))
assert a ^ b == Region(
result = Region(
[Band(0, 5, [0, 10]), Band(5, 10, [0, 5, 10, 15]), Band(10, 15, [5, 15])]
)
assert a ^ b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 5),
Rect(0, 5, 5, 5),
Rect(10, 5, 5, 5),
Rect(5, 10, 10, 5),
]
assert result.bbox == Rect(0, 0, 15, 15)


def test_xor_branching_1():
a = Region([Band(0, 10, [0, 10]), Band(20, 50, [0, 10])])
b = Region([Band(5, 25, [5, 15]), Band(35, 45, [5, 15])])
result = Region(
[
Band(0, 5, [0, 10]),
Band(5, 10, [0, 5, 10, 15]),
Band(10, 20, [5, 15]),
Band(20, 25, [0, 5, 10, 15]),
Band(25, 35, [0, 10]),
Band(35, 45, [0, 5, 10, 15]),
Band(45, 50, [0, 10]),
]
)
assert a ^ b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 5),
Rect(0, 5, 5, 5),
Rect(10, 5, 5, 5),
Rect(5, 10, 10, 10),
Rect(0, 20, 5, 5),
Rect(10, 20, 5, 5),
Rect(0, 25, 10, 10),
Rect(0, 35, 5, 10),
Rect(10, 35, 5, 10),
Rect(0, 45, 10, 5),
]
assert result.bbox == Rect(0, 0, 15, 50)


def test_xor_branching_2():
a = Region([Band(0, 10, [0, 10]), Band(40, 50, [0, 10])])
b = Region([Band(20, 30, [0, 10])])
result = Region(
[Band(0, 10, [0, 10]), Band(20, 30, [0, 10]), Band(40, 50, [0, 10])]
)
assert a ^ b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 10),
Rect(0, 20, 10, 10),
Rect(0, 40, 10, 10),
]
assert result.bbox == Rect(0, 0, 10, 50)


def test_xor_branching_3():
a = Region([Band(0, 10, [0, 10]), Band(15, 25, [0, 10])])
b = Region([Band(5, 25, [5, 15])])
result = Region(
[
Band(0, 5, [0, 10]),
Band(5, 10, [0, 5, 10, 15]),
Band(10, 15, [5, 15]),
Band(15, 25, [0, 5, 10, 15]),
]
)
assert a ^ b == result
assert list(result.rects()) == [
Rect(0, 0, 10, 5),
Rect(0, 5, 5, 5),
Rect(10, 5, 5, 5),
Rect(5, 10, 10, 5),
Rect(0, 15, 5, 10),
Rect(10, 15, 5, 10),
]
assert result.bbox == Rect(0, 0, 15, 25)

0 comments on commit 32743e2

Please sign in to comment.