Ejemplo n.º 1
0
 def assert_unbounded_above(self, rng: Range):
     self.assertFalse(rng.has_upper_bound())
     with self.assertRaises(ValueError):
         rng.upper_endpoint()
     # pylint: disable=pointless-statement
     with self.assertRaises(AssertionError):
         rng.upper_bound_type
Ejemplo n.º 2
0
 def test_range_set_equality(self) -> None:
     self.assertEqual(
         ImmutableRangeSet.builder()  # type: ignore
         .add(Range.at_most(2)).add(Range.at_least(5)).build(),
         ImmutableRangeSet.builder()  # type: ignore
         .add(Range.at_least(5)).add(Range.at_most(2)).build(),
     )
Ejemplo n.º 3
0
 def test_intersection_empty(self):
     rng = Range.closed_open(3, 3)
     self.assertEqual(rng, rng.intersection(rng))
     with self.assertRaises(ValueError):
         rng.intersection(Range.open(3, 5))
     with self.assertRaises(ValueError):
         rng.intersection(Range.closed(0, 2))
Ejemplo n.º 4
0
 def test_ignores_smaller_sharing_lower_bound(self):
     range_set = RangeSet.create_mutable()
     range_set.add(Range.closed(1, 6))
     range_set.add(Range.closed(1, 4))
     self._test_invariants(range_set)
     self.assertEqual(tuple([Range.closed(1, 6)]),
                      tuple(range_set.as_ranges()))
Ejemplo n.º 5
0
 def test_fill_hole_exactly(self):
     range_set = RangeSet.create_mutable()
     range_set.add(Range.closed_open(1, 3))
     range_set.add(Range.closed_open(4, 6))
     range_set.add(Range.closed_open(3, 4))
     self._test_invariants(range_set)
     self.assertTrue(Range.closed_open(1, 6) in range_set.as_ranges())
Ejemplo n.º 6
0
 def test_range_containing1(self):
     range_set = RangeSet.create_mutable()
     range_set.add(Range.closed(3, 10))
     self.assertEqual(Range.closed(3, 10), range_set.range_containing(5))
     self.assertTrue(5 in range_set)
     self.assertIsNone(range_set.range_containing(1))
     self.assertFalse(1 in range_set)
Ejemplo n.º 7
0
 def test_range_clear(self) -> None:
     range_set: MutableRangeSet[int] = RangeSet.create_mutable()
     range_set.add_all(
         [Range.at_most(2),
          Range.open_closed(5, 8),
          Range.at_least(10)])
     range_set.clear()
     self.assertEqual(0, len(range_set.as_ranges()))
Ejemplo n.º 8
0
 def test_check_usable_in_set(self) -> None:
     range_set = ImmutableSet.of([
         Range.open_closed(0, 1),
         Range.open_closed(0, 1),
         Range.at_most(1),
         Range.at_most(1),
     ])
     self.assertEqual(2, len(range_set))
Ejemplo n.º 9
0
 def test_fill_hole_with_overlap(self):
     range_set = RangeSet.create_mutable()
     range_set.add(Range.closed_open(1, 3))
     range_set.add(Range.closed_open(4, 6))
     range_set.add(Range.closed_open(2, 5))
     self._test_invariants(range_set)
     self.assertEqual(tuple([Range.closed_open(1, 6)]),
                      tuple(range_set.as_ranges()))
Ejemplo n.º 10
0
 def test_add_all(self):
     range_set = RangeSet.create_mutable()
     range_set.add(Range.closed(3, 10))
     range_set.add_all(
         [Range.open(1, 3),
          Range.closed(5, 8),
          Range.closed(9, 11)])
     self.assertEqual(tuple(range_set.as_ranges()),
                      tuple([Range.open_closed(1, 11)]))
Ejemplo n.º 11
0
 def test_len(self):
     self.assertEqual(0, len(RangeSet.create_mutable()))
     self.assertEqual(
         1, len(RangeSet.create_mutable().add(Range.closed(1, 2))))
     self.assertEqual(
         2,
         len(RangeSet.create_mutable().add(Range.closed(1, 2)).add(
             Range.open(3, 4))),
     )
Ejemplo n.º 12
0
 def test_lookup(self):
     range_map = (ImmutableRangeMap.builder().put(Range.closed(
         0, 2), "foo").put(Range.open_closed(6, 8), "bar").build())
     self.assertEqual("foo", range_map[0])
     self.assertEqual("foo", range_map[1])
     self.assertEqual("foo", range_map[2])
     self.assertEqual(None, range_map[6])
     self.assertEqual("bar", range_map[7])
     self.assertEqual("bar", range_map[8])
     self.assertEqual(None, range_map[9])
Ejemplo n.º 13
0
 def test_temporary_exception_on_connected_range_keys(self):
     with self.assertRaisesRegex(
             ValueError,
             "Some range keys are connected or overlapping. Overlapping keys "
             "will never be supported. Support for connected keys is tracked in "
             "https://github.com/isi-vista/vistautils/issues/37",
     ):
         (ImmutableRangeMap.builder().put(Range.open(0, 2),
                                          0).put(Range.closed(2, 3),
                                                 1).build())
Ejemplo n.º 14
0
 def test_all(self):
     rng = Range.all()
     # how to check for largest negative value? -sys.maxsize -1 should probably work,
     # but as far as I can tell there is no guarantee. For now we check for a large negative
     # value
     self.assertTrue(-2**30 in rng)
     self.assertTrue(sys.maxsize in rng)
     self.assert_unbounded_below(rng)
     self.assert_unbounded_above(rng)
     self.assertFalse(rng.is_empty())
     self.assertEqual("(-\u221e..+\u221e)", str(rng))
     self.assertTrue(rng is Range.all())
Ejemplo n.º 15
0
 def test_integer(self):
     params = Parameters.from_mapping({"test_int": 5})
     self.assertEqual(5, params.integer("test_int"))
     self.assertEqual(2, params.integer("not_appearing", default=2))
     with self.assertRaisesRegex(ParameterError,
                                 "Invalid value for integer parameter"):
         params.integer("test_int", valid_range=Range.closed(1, 3))
     with self.assertRaisesRegex(ParameterError,
                                 "Invalid value for integer parameter"):
         params.integer("not_appearing",
                        default=2,
                        valid_range=Range.closed(10, 20))
Ejemplo n.º 16
0
    def test_float(self):
        params = Parameters.from_mapping({"test_float": 5.5})
        self.assertEqual(5.5, params.floating_point("test_float"))
        self.assertEqual(
            5.5,
            params.floating_point("test_float", valid_range=Range.open(5, 6)))

        with self.assertRaisesRegex(
                ParameterError,
                "For parameter test_float, expected a float in the range \\(0.0..1.0\\) but got 5.5",
        ):
            params.floating_point("test_float",
                                  valid_range=Range.open(0.0, 1.0))
Ejemplo n.º 17
0
    def test_add_many_pairs(self):
        for a_low in range(0, 6):
            for a_high in range(0, 6):
                if a_low > a_high:
                    continue
                a_ranges = [
                    Range.closed(a_low, a_high),
                    Range.open_closed(a_low, a_high),
                    Range.closed_open(a_low, a_high),
                ]
                if a_low != a_high:
                    a_ranges.append(Range.open(a_low, a_high))

                for b_low in range(0, 6):
                    for b_high in range(0, 6):
                        if b_low > b_high:
                            continue
                        b_ranges = [
                            Range.closed(b_low, b_high),
                            Range.open_closed(b_low, b_high),
                            Range.closed_open(b_low, b_high),
                        ]
                        if b_low != b_high:
                            b_ranges.append(Range.open(b_low, b_high))
                        for a_range in a_ranges:
                            for b_range in b_ranges:
                                self._pair_test(a_range, b_range)
Ejemplo n.º 18
0
    def test_pickling(self):
        empty_rangemap = immutablerangemap({})
        ranges = (Range.closed(0, 2), Range.closed(5,
                                                   29), Range.closed(35, 39))
        values = ("foo", "bar", "meep")
        rangemap = immutablerangemap(zip(ranges, values))

        self.assertEqual(empty_rangemap,
                         pickle.loads(pickle.dumps(empty_rangemap)))
        self.assertEqual(rangemap, pickle.loads(pickle.dumps(rangemap)))

        self.assertEqual(empty_rangemap.__reduce__(),
                         (immutablerangemap, ((), )))
        self.assertEqual(rangemap.__reduce__(),
                         (immutablerangemap, (tuple(zip(ranges, values)), )))
Ejemplo n.º 19
0
class HorizontalAxisOfObject(Generic[_ObjectT], AxisFunction[_ObjectT]):
    _object: _ObjectT = attrib()
    _index: int = attrib(validator=in_(Range.closed(0, 1)))

    def to_concrete_axis(
        self, axes_info: Optional[AxesInfo[_ObjectT]]  # pylint:disable=unused-argument
    ) -> GeonAxis:
        if not isinstance(self._object, HasAxes):
            raise RuntimeError(
                "Can only instantiate an axis function if the object is of a "
                "concrete type (e.g. perception or situation object)"
            )
        horizontal_axes = tuple(
            axis
            for axis in self._object.axes.all_axes
            if not axis.aligned_to_gravitational
        )
        return horizontal_axes[self._index]  # pylint:disable=invalid-sequence-index

    def copy_remapping_objects(
        self, object_map: Mapping[_ObjectT, _ObjectToT]
    ) -> "HorizontalAxisOfObject[_ObjectToT]":
        return HorizontalAxisOfObject(object_map[self._object], index=self._index)

    def __repr__(self, object_map: Optional[Mapping[_ObjectT, str]] = None) -> str:
        handle = object_map[self._object] if object_map else self._object
        return f"HorizontalAxisOfObject(_object={handle}, _index={str(self._index)})"
Ejemplo n.º 20
0
    def test_intersection_de_facto_empty(self):
        rng = Range.open(3, 4)
        self.assertEqual(rng, rng.intersection(rng))
        self.assertEqual(Range.open_closed(3, 3),
                         rng.intersection(Range.at_most(3)))
        self.assertEqual(Range.closed_open(4, 4),
                         rng.intersection(Range.at_least(4)))

        with self.assertRaises(ValueError):
            rng.intersection(Range.less_than(3))
        with self.assertRaises(ValueError):
            rng.intersection(Range.greater_than(4))

        rng2 = Range.closed(3, 4)
        self.assertEqual(Range.open_closed(4, 4),
                         rng2.intersection(Range.greater_than(4)))
Ejemplo n.º 21
0
 class Hypothesis:
     pattern_template: PerceptionGraphTemplate = attrib(
         validator=instance_of(PerceptionGraphTemplate))
     association_score: float = attrib(validator=instance_of(float),
                                       default=0)
     probability: float = attrib(validator=in_(Range.open(0, 1)), default=0)
     observation_count: int = attrib(default=1)
Ejemplo n.º 22
0
    def test_closed(self):
        rng = Range.closed(5, 7)
        self.check_contains(rng)
        self.assertTrue(rng.has_lower_bound())
        self.assertEqual(5, rng.lower_endpoint)
        self.assertEqual(BoundType.closed(), rng.lower_bound_type)
        self.assertTrue(rng.has_upper_bound())
        self.assertEqual(7, rng.upper_endpoint)
        self.assertEqual(BoundType.closed(), rng.upper_bound_type)
        self.assertFalse(rng.is_empty())
        self.assertEqual("[5..7]", str(rng))
        # this is legal and should raise no exception
        Range.closed(4, 4)

        with self.assertRaises(ValueError):
            Range.closed(8, 4)
Ejemplo n.º 23
0
    def test_open(self):
        rng = Range.open(4, 8)
        self.check_contains(rng)
        self.assertTrue(rng.has_lower_bound())
        self.assertEqual(4, rng.lower_endpoint)
        self.assertEqual(BoundType.open(), rng.lower_bound_type)
        self.assertTrue(rng.has_upper_bound())
        self.assertEqual(8, rng.upper_endpoint)
        self.assertEqual(BoundType.open(), rng.upper_bound_type)
        self.assertFalse(rng.is_empty())
        self.assertEqual("(4..8)", str(rng))

        with self.assertRaises(ValueError):
            Range.open(8, 4)

        with self.assertRaises(ValueError):
            Range.open(4, 4)
Ejemplo n.º 24
0
 def test_range_enclosing_range(self) -> None:
     range_set: MutableRangeSet[int] = RangeSet.create_mutable()
     range_set.add_all(
         [Range.at_most(2),
          Range.open_closed(5, 8),
          Range.at_least(10)])
     self.assertEqual(None,
                      range_set.range_enclosing_range(Range.closed(2, 3)))
     self.assertEqual(Range.at_most(2),
                      range_set.range_enclosing_range(Range.open(-1, 0)))
     self.assertEqual(
         Range.open_closed(5, 8),
         range_set.range_enclosing_range(Range.closed_open(6, 7)),
     )
     self.assertEqual(None,
                      range_set.range_enclosing_range(Range.closed(5, 8)))
Ejemplo n.º 25
0
 def test_greater_than(self):
     rng = Range.greater_than(5)
     self.assertFalse(5 in rng)
     self.assertTrue(6 in rng)
     self.assertTrue(sys.maxsize in rng)
     self.assertTrue(rng.has_lower_bound())
     self.assertEqual(5, rng.lower_endpoint)
     self.assertEqual(BoundType.open(), rng.lower_bound_type)
     self.assert_unbounded_above(rng)
     self.assertFalse(rng.is_empty())
     self.assertEqual("(5..+\u221e)", str(rng))
Ejemplo n.º 26
0
 def test_at_least(self):
     rng = Range.at_least(6)
     self.assertFalse(5 in rng)
     self.assertTrue(6 in rng)
     self.assertTrue(sys.maxsize in rng)
     self.assertTrue(rng.has_lower_bound())
     self.assertEqual(6, rng.lower_endpoint)
     self.assertEqual(BoundType.closed(), rng.lower_bound_type)
     self.assert_unbounded_above(rng)
     self.assertFalse(rng.is_empty())
     self.assertEqual("[6..+\u221e)", str(rng))
Ejemplo n.º 27
0
 def test_closed_open(self):
     rng = Range.closed_open(5, 8)
     self.check_contains(rng)
     self.assertTrue(rng.has_lower_bound())
     self.assertEqual(5, rng.lower_endpoint)
     self.assertEqual(BoundType.closed(), rng.lower_bound_type)
     self.assertTrue(rng.has_upper_bound())
     self.assertEqual(8, rng.upper_endpoint)
     self.assertEqual(BoundType.open(), rng.upper_bound_type)
     self.assertFalse(rng.is_empty())
     self.assertEqual("[5..8)", str(rng))
Ejemplo n.º 28
0
 def test_open_closed(self):
     rng = Range.open_closed(4, 7)
     self.check_contains(rng)
     self.assertTrue(rng.has_lower_bound())
     self.assertEqual(4, rng.lower_endpoint)
     self.assertEqual(BoundType.open(), rng.lower_bound_type)
     self.assertTrue(rng.has_upper_bound())
     self.assertEqual(7, rng.upper_endpoint)
     self.assertEqual(BoundType.closed(), rng.upper_bound_type)
     self.assertFalse(rng.is_empty())
     self.assertEqual("(4..7]", str(rng))
Ejemplo n.º 29
0
 def test_empty_1(self):
     rng = Range.closed_open(4, 4)
     self.assertFalse(3 in rng)
     self.assertFalse(4 in rng)
     self.assertFalse(5 in rng)
     self.assertTrue(rng.has_lower_bound())
     self.assertEqual(4, rng.lower_endpoint)
     self.assertEqual(BoundType.closed(), rng.lower_bound_type)
     self.assertTrue(rng.has_upper_bound())
     self.assertEqual(4, rng.upper_endpoint)
     self.assertEqual(BoundType.open(), rng.upper_bound_type)
     self.assertTrue(rng.is_empty())
     self.assertEqual("[4..4)", str(rng))
Ejemplo n.º 30
0
    def test_pickling(self):
        empty_mutable_rangeset = MutableRangeSet.create_mutable()
        empty_immutable_rangeset = ImmutableRangeSet.builder().build()
        ranges = (Range.closed(0, 2), Range.closed(5,
                                                   29), Range.closed(35, 39))
        mutable_rangeset = MutableRangeSet.create_mutable().add_all(ranges)
        immutable_rangeset = ImmutableRangeSet.builder().add_all(
            ranges).build()

        self.assertEqual(empty_mutable_rangeset,
                         pickle.loads(pickle.dumps(empty_mutable_rangeset)))
        self.assertEqual(empty_immutable_rangeset,
                         pickle.loads(pickle.dumps(empty_immutable_rangeset)))
        self.assertEqual(mutable_rangeset,
                         pickle.loads(pickle.dumps(mutable_rangeset)))
        self.assertEqual(immutable_rangeset,
                         pickle.loads(pickle.dumps(immutable_rangeset)))

        self.assertEqual(empty_mutable_rangeset.__getstate__(), ())
        self.assertEqual(empty_immutable_rangeset.__getstate__(), ())

        self.assertEqual(mutable_rangeset.__getstate__(), ranges)
        self.assertEqual(immutable_rangeset.__getstate__(), ranges)