Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
myui committed Jan 10, 2025
1 parent a61973e commit 0c1fa0d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/utils/test_collections.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

from rtrec.utils.collections import SortedSet
from typing import List

def test_sorted_set_init_empty():
"""Test initialization of an empty SortedSet."""
Expand Down Expand Up @@ -67,3 +68,50 @@ def test_sorted_set_repr():
"""Test the string representation of the SortedSet."""
s = SortedSet([3, 1, 4])
assert repr(s) == "SortedSet([1, 3, 4])"

def test_add_unique_elements():
sset = SortedSet()
assert sset.add(10) == 0 # Adding first element
assert sset.add(5) == 0 # Adding element at the start
assert sset.add(15) == 2 # Adding element at the end
assert len(sset) == 3
assert list(sset) == [5, 10, 15]

def test_add_duplicate_elements():
sset = SortedSet([10, 20, 30])
assert sset.add(20) == 1 # Element already exists, return its index
assert len(sset) == 3 # Length should not increase
assert list(sset) == [10, 20, 30]

def test_index_existing_elements():
sset = SortedSet([10, 20, 30])
assert sset.index(10) == 0 # Check index of first element
assert sset.index(20) == 1 # Check index of middle element
assert sset.index(30) == 2 # Check index of last element

def test_index_non_existing_elements():
sset = SortedSet([10, 20, 30])
assert sset.index(15) == -2 # Not found, insertion point is 1
assert sset.index(5) == -1 # Not found, insertion point is 0
assert sset.index(35) == -4 # Not found, insertion point is 3

def test_add_and_index_combined():
sset = SortedSet()
assert sset.add(50) == 0
assert sset.index(50) == 0
assert sset.add(30) == 0
assert sset.index(30) == 0
assert sset.add(70) == 2
assert sset.index(70) == 2
assert len(sset) == 3
assert list(sset) == [30, 50, 70]

def test_len_and_contains():
sset = SortedSet([5, 10, 15])
assert len(sset) == 3
assert 10 in sset
assert 20 not in sset

def test_iter():
sset = SortedSet([10, 20, 30])
assert list(iter(sset)) == [10, 20, 30]

0 comments on commit 0c1fa0d

Please sign in to comment.