|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for the collections module.""" |
| 5 | + |
| 6 | + |
| 7 | +from typing import Self |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from frequenz.core.collections import Interval, LessThanComparable |
| 12 | + |
| 13 | + |
| 14 | +class CustomComparable: |
| 15 | + """A custom comparable class.""" |
| 16 | + |
| 17 | + def __init__(self, value: int) -> None: |
| 18 | + """Initialize this instance.""" |
| 19 | + self.value = value |
| 20 | + |
| 21 | + def __lt__(self, other: Self) -> bool: |
| 22 | + """Return whether this instance is less than other.""" |
| 23 | + return self.value < other.value |
| 24 | + |
| 25 | + def __eq__(self, other: object) -> bool: |
| 26 | + """Return whether this instance is equal to other.""" |
| 27 | + if not isinstance(other, CustomComparable): |
| 28 | + return False |
| 29 | + return self.value == other.value |
| 30 | + |
| 31 | + def __repr__(self) -> str: |
| 32 | + """Return a string representation of this instance.""" |
| 33 | + return str(self.value) |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "start, end", |
| 38 | + [ |
| 39 | + (10.0, -100.0), |
| 40 | + (CustomComparable(10), CustomComparable(-100)), |
| 41 | + ], |
| 42 | +) |
| 43 | +def test_invalid_range(start: LessThanComparable, end: LessThanComparable) -> None: |
| 44 | + """Test if the interval has an invalid range.""" |
| 45 | + with pytest.raises( |
| 46 | + ValueError, |
| 47 | + match=rf"The start \({start}\) can't be bigger than end \({end}\)", |
| 48 | + ): |
| 49 | + Interval(start, end) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize( |
| 53 | + "start, end, within, at_start, at_end, before_start, after_end", |
| 54 | + [ |
| 55 | + (10.0, 100.0, 50.0, 10.0, 100.0, 9.0, 101.0), |
| 56 | + ( |
| 57 | + CustomComparable(10), |
| 58 | + CustomComparable(100), |
| 59 | + CustomComparable(50), |
| 60 | + CustomComparable(10), |
| 61 | + CustomComparable(100), |
| 62 | + CustomComparable(9), |
| 63 | + CustomComparable(101), |
| 64 | + ), |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_interval_contains( # pylint: disable=too-many-arguments |
| 68 | + start: LessThanComparable, |
| 69 | + end: LessThanComparable, |
| 70 | + within: LessThanComparable, |
| 71 | + at_start: LessThanComparable, |
| 72 | + at_end: LessThanComparable, |
| 73 | + before_start: LessThanComparable, |
| 74 | + after_end: LessThanComparable, |
| 75 | +) -> None: |
| 76 | + """Test if a value is within the interval.""" |
| 77 | + interval = Interval(start=start, end=end) |
| 78 | + assert within in interval # within |
| 79 | + assert at_start in interval # at start |
| 80 | + assert at_end in interval # at end |
| 81 | + assert before_start not in interval # before start |
| 82 | + assert after_end not in interval # after end |
| 83 | + |
| 84 | + |
| 85 | +@pytest.mark.parametrize( |
| 86 | + "end, within, at_end, after_end", |
| 87 | + [ |
| 88 | + (100.0, 50.0, 100.0, 101.0), |
| 89 | + ( |
| 90 | + CustomComparable(100), |
| 91 | + CustomComparable(50), |
| 92 | + CustomComparable(100), |
| 93 | + CustomComparable(101), |
| 94 | + ), |
| 95 | + ], |
| 96 | +) |
| 97 | +def test_interval_contains_no_start( |
| 98 | + end: LessThanComparable, |
| 99 | + within: LessThanComparable, |
| 100 | + at_end: LessThanComparable, |
| 101 | + after_end: LessThanComparable, |
| 102 | +) -> None: |
| 103 | + """Test if a value is within the interval with no start.""" |
| 104 | + interval_no_start = Interval(start=None, end=end) |
| 105 | + assert within in interval_no_start # within end |
| 106 | + assert at_end in interval_no_start # at end |
| 107 | + assert after_end not in interval_no_start # after end |
| 108 | + |
| 109 | + |
| 110 | +@pytest.mark.parametrize( |
| 111 | + "start, within, at_start, before_start", |
| 112 | + [ |
| 113 | + (10.0, 50.0, 10.0, 9.0), |
| 114 | + ( |
| 115 | + CustomComparable(10), |
| 116 | + CustomComparable(50), |
| 117 | + CustomComparable(10), |
| 118 | + CustomComparable(9), |
| 119 | + ), |
| 120 | + ], |
| 121 | +) |
| 122 | +def test_interval_contains_no_end( |
| 123 | + start: LessThanComparable, |
| 124 | + within: LessThanComparable, |
| 125 | + at_start: LessThanComparable, |
| 126 | + before_start: LessThanComparable, |
| 127 | +) -> None: |
| 128 | + """Test if a value is within the interval with no end.""" |
| 129 | + interval_no_end = Interval(start=start, end=None) |
| 130 | + assert within in interval_no_end # within start |
| 131 | + assert at_start in interval_no_end # at start |
| 132 | + assert before_start not in interval_no_end # before start |
| 133 | + |
| 134 | + |
| 135 | +@pytest.mark.parametrize( |
| 136 | + "value", |
| 137 | + [ |
| 138 | + 50.0, |
| 139 | + 10.0, |
| 140 | + -10.0, |
| 141 | + CustomComparable(50), |
| 142 | + CustomComparable(10), |
| 143 | + CustomComparable(-10), |
| 144 | + ], |
| 145 | +) |
| 146 | +def test_interval_contains_unbound(value: LessThanComparable) -> None: |
| 147 | + """Test if a value is within the interval with no bounds.""" |
| 148 | + interval_no_bounds: Interval[LessThanComparable | None] = Interval( |
| 149 | + start=None, end=None |
| 150 | + ) |
| 151 | + assert value in interval_no_bounds # any value within bounds |
0 commit comments