aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/array.py2
-rw-r--r--src/util/range.py4
-rw-r--r--src/util/test_array.py12
-rw-r--r--src/util/test_range.py6
4 files changed, 21 insertions, 3 deletions
diff --git a/src/util/array.py b/src/util/array.py
index 7dd0357..bb4eee3 100644
--- a/src/util/array.py
+++ b/src/util/array.py
@@ -1,5 +1,5 @@
def insert_position(x, xs, is_reversed: bool) -> int:
for i, y in enumerate(xs):
- if is_reversed and x > y or not is_reversed and x < y:
+ if is_reversed and x >= y or not is_reversed and x <= y:
return i
return len(xs)
diff --git a/src/util/range.py b/src/util/range.py
index c75232c..bd4b27e 100644
--- a/src/util/range.py
+++ b/src/util/range.py
@@ -1,10 +1,10 @@
-from typing import NamedTuple
+from typing import NamedTuple, List
class Range(NamedTuple):
start: int
length: int
-def from_indexes(indexes):
+def from_indexes(indexes: List[int]) -> List[Range]:
ranges = []
curr_range_start = 0
curr_range_len = 0
diff --git a/src/util/test_array.py b/src/util/test_array.py
new file mode 100644
index 0000000..38759b9
--- /dev/null
+++ b/src/util/test_array.py
@@ -0,0 +1,12 @@
+from array import insert_position
+
+def test_insert_position():
+ assert insert_position(0, [], False) == 0
+ assert insert_position(1, [1, 2, 3], False) == 0
+ assert insert_position(2, [1, 2, 3], False) == 1
+ assert insert_position(3, [1, 2, 3], False) == 2
+ assert insert_position(8, [1, 2, 3], False) == 3
+ assert insert_position(8, [3, 2, 1], True) == 0
+ assert insert_position(3, [3, 2, 1], True) == 0
+ assert insert_position(2, [3, 2, 1], True) == 1
+ assert insert_position(1, [3, 2, 1], True) == 2
diff --git a/src/util/test_range.py b/src/util/test_range.py
new file mode 100644
index 0000000..0bd909b
--- /dev/null
+++ b/src/util/test_range.py
@@ -0,0 +1,6 @@
+from range import from_indexes, Range
+
+def test_from_indexes():
+ assert from_indexes([]) == []
+ assert from_indexes([1]) == [Range(1, 1)]
+ assert from_indexes([9, 6, 0, 10]) == [Range(0, 1), Range(6, 1), Range(9, 2)]