def test_ordering(self): self.assertEqual( ("a", "b", "c"), tuple( ImmutableSet.builder(order_key=lambda x: x).add_all( ["b", "c", "a"]).build()), )
def test_enclosed(self): range_map: ImmutableRangeMap[int, str] = (ImmutableRangeMap.builder().put( Range.closed(0, 2), "foo").put(Range.open_closed(6, 8), "bar").put( Range.open(12, 14), "meep").build()) self.assertEqual( ImmutableSet.of(["foo", "bar", "meep"]), range_map.get_enclosed_by(Range.closed(-1, 15)), ) self.assertEqual(ImmutableSet.of(["foo"]), range_map.get_enclosed_by(Range.closed(0, 6))) self.assertEqual(ImmutableSet.empty(), range_map.get_enclosed_by(Range.closed(5, 5)))
def test_empty_singleton(self): empty1 = immutableset() empty2 = immutableset() self.assertIs(empty1, empty2) empty3 = ImmutableSet.builder().build() self.assertIs(empty1, empty3) empty4 = immutableset([]) self.assertIs(empty1, empty4)
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))
def _test_encloses(self, range_set: RangeSet[int]): self.assertTrue(range_set.encloses_all(ImmutableSet.empty())) for query_range in TestRangeSet.QUERY_RANGES: expected_to_enclose = any( x.encloses(query_range) for x in range_set.as_ranges()) self.assertEqual(expected_to_enclose, range_set.encloses(query_range)) self.assertEqual(expected_to_enclose, range_set.encloses_all([query_range]))
def test_empty(self): empty = immutableset() self.assertEqual(0, len(empty)) empty2 = immutableset([]) self.assertEqual(0, len(empty2)) self.assertEqual(empty, empty2) empty3 = ImmutableSet.builder().build() self.assertEqual(0, len(empty3)) self.assertEqual(empty, empty3)
def test_zip_bytes(self): tmp_dir = Path(tempfile.mkdtemp()) with KeyValueSink.zip_bytes_sink(tmp_dir / "test.zip") as zip_sink: zip_sink.put("hello", "world".encode("utf-8")) zip_sink.put("foo", "bar".encode("utf-8")) with KeyValueSource.zip_bytes_source(tmp_dir / "test.zip") as zip_source: self.assertIsNotNone(zip_source.keys()) self.assertEqual(ImmutableSet.of(["hello", "foo"]), zip_source.keys()) self.assertEqual("world".encode("utf-8"), zip_source["hello"]) self.assertEqual("bar".encode("utf-8"), zip_source["foo"]) self.assertIsNone(zip_source.get("not-there", None)) self.assertEqual( "moo".encode("utf-8"), zip_source.get("not-there", "moo".encode("utf-8"))) with self.assertRaises(KeyError): # pylint: disable=pointless-statement zip_source["not-there"] # test adding to an existing zip with KeyValueSink.zip_bytes_sink(tmp_dir / "test.zip", overwrite=False) as zip_sink: zip_sink.put("meep", "lalala".encode("utf-8")) with KeyValueSource.zip_bytes_source(tmp_dir / "test.zip") as zip_source: self.assertIsNotNone(zip_source.keys()) self.assertEqual(ImmutableSet.of(["hello", "foo", "meep"]), zip_source.keys()) self.assertEqual("world".encode("utf-8"), zip_source["hello"]) self.assertEqual("bar".encode("utf-8"), zip_source["foo"]) self.assertEqual("lalala".encode("utf-8"), zip_source["meep"]) self.assertIsNone(zip_source.get("not-there", None)) self.assertEqual( "moo".encode("utf-8"), zip_source.get("not-there", "moo".encode("utf-8"))) with self.assertRaises(KeyError): # pylint: disable=pointless-statement zip_source["not-there"] shutil.rmtree(str(tmp_dir))
def _pair_test(self, a: Range[int], b: Range[int]) -> None: range_set: MutableRangeSet[int] = RangeSet.create_mutable() range_set.add(a) range_set.add(b) if a.is_empty() and b.is_empty(): self.assertTrue(range_set.is_empty()) self.assertFalse(range_set.as_ranges()) elif a.is_empty(): self.assertTrue(b in range_set.as_ranges()) elif b.is_empty(): self.assertTrue(a in range_set.as_ranges()) elif a.is_connected(b): self.assertEqual(tuple(range_set.as_ranges()), tuple([a.span(b)])) else: if a.lower_endpoint < b.lower_endpoint: self.assertEqual(tuple(range_set.as_ranges()), tuple([a, b])) else: self.assertEqual(ImmutableSet.of([a, b]), ImmutableSet.of(range_set.as_ranges()))
def __init__( self, *, source: Optional["ImmutableMultiDict[KT2,VT2]"] = None, order_key: Callable[[VT2], Any] = None, ) -> None: self._dict: MutableMapping[ KT2, ImmutableSet.Builder[VT2]] = defaultdict( lambda: ImmutableSet.builder(order_key=order_key)) self._source = source self._dirty = False
def test_singleton_index(self): s = ImmutableSet.of([1]) self.assertEqual(1, s[0]) self.assertEqual(1, s[-1]) with self.assertRaises(IndexError): s[2] with self.assertRaises(IndexError): s[-2] with self.assertRaises(IndexError): s[25] with self.assertRaises(IndexError): s[-25]
def test_cannot_init(self): with self.assertRaises(TypeError): # noinspection PyArgumentList ImmutableSet([1, 2, 3])
def test_type_testing(self): ImmutableSet.of([1, 2, 3], check_top_type_matches=int) with self.assertRaises(TypeError): ImmutableSet.of([1, 2, "three"], check_top_type_matches=int) string_set = ImmutableSet.of(["one", "two", "three"], check_top_type_matches=str) # this is fine ImmutableSet.of(string_set, check_top_type_matches=str) with self.assertRaises(TypeError): ImmutableSet.of(string_set, check_top_type_matches=int) # we want to check that type checking still works when the original immutable set wasn't # typed checked up front unchecked_string_set = ImmutableSet.of(["one", "two", "three"]) ImmutableSet.of(unchecked_string_set, check_top_type_matches=str) with self.assertRaises(TypeError): ImmutableSet.of(unchecked_string_set, check_top_type_matches=int)
def __getitem__(self, k: KT) -> ImmutableSet[VT]: return self._dict.get(k, ImmutableSet.empty())
def test_of(self): x = ImmutableSetMultiDict.of({1: [2, 2, 3], 4: [5, 6]}) self.assertEqual(ImmutableSet.of([2, 3]), x[1]) y = immutablesetmultidict([(1, 2), (1, 2), (1, 3), (4, 5), (4, 6)]) self.assertEqual(immutableset([2, 3]), y[1])
def test_ranges_enclosed_by_out_of_bounds(self) -> None: self.assertEqual( ImmutableSet.empty(), RangeSet.create_mutable() # type: ignore .add(Range.closed(0, 10)).ranges_enclosed_by(Range.at_least(20)), )