def test_correct(self) -> None: self.assertEqual(len(KeyedPQ()), 0) self.assertEqual(len(KeyedPQ([])), 0) dummy = DummyClass() pq: KeyedPQ[DummyClass] = KeyedPQ([('a', 1.0, dummy)]) self.assertEqual(len(pq), 1) item = pq.peek() self.assertEqual(item.key, 'a') self.assertEqual(item.value, 1.0) self.assertIs(item.data, dummy)
def test_incorrect(self) -> None: with self.assertRaises(TypeError): typing.cast(typing.Any, KeyedPQ)([], []) with self.assertRaises(ValueError): KeyedPQ(typing.cast(typing.Any, [tuple()])) with self.assertRaises(ValueError): KeyedPQ(typing.cast(typing.Any, [('a',)])) with self.assertRaises(ValueError): KeyedPQ(typing.cast(typing.Any, [('a', 1.0)])) with self.assertRaises(TypeError): KeyedPQ(typing.cast(typing.Any, [(1.0, 1.0, None)])) with self.assertRaises(TypeError): KeyedPQ(typing.cast(typing.Any, [('a', 'a', None)]))
def test_different_heaps(self) -> None: pq2: KeyedPQ[None] = KeyedPQ() item_a = self.pq.add('a', 0.0, None) item_b = pq2.add('a', 0.0, None) self._assert_not_equal(item_a, item_b)
def test_change_value(self) -> None: # Setup pq: KeyedPQ[None] = KeyedPQ() heap: typing.List[typing.List[float]] = [] heap_lookup_dict: typing.Dict[str, typing.List[float]] = {} for i in range(10000): val = random.random() key = str(i) pq.add(key, val, None) heap_entry = [val] heap_lookup_dict[key] = heap_entry heapq.heappush(heap, heap_entry) for i in range(10000): val = random.random() key = str(i) pq.change_value(key, val) heap_lookup_dict[key][0] = val heapq.heapify(heap) # Test self.assertEqual(pq._export(), [e[0] for e in heap])
def __init__(self, reuse_timer: ReuseTimer, configuration: 'MIND.Configuration') -> None: self._reuse_timer: ReuseTimer = reuse_timer self._d_factor: float = configuration.d_factor self._min_d: Optional[int] = configuration.min_d self._max_d: Optional[int] = configuration.max_d self._pq: KeyedPQ[MIND.State._FileInfo] = KeyedPQ(max_heap=True)
def __init__(self) -> None: # PQ of cached files in this class self.pq: KeyedPQ[OBMA.State._FileInfo] = KeyedPQ(max_heap=True) # Total size of cached files in this class self.total_size: int = 0 # Counts bytes to be evicted (eviction is delayed until this # counter surpasses the eviction candidate's size) self.eviction_counter: int = 0
def _set_priorities(self, ts: int) -> None: old_pq = self._pq eva_of_file_at_time = self._eva_of_file_at_time self._pq = KeyedPQ(( it.key, eva_of_file_at_time(it.data, ts), it.data, ) for it in old_pq) self._last_age_bin = self._age_binner(ts)
def test_others_item_key(self) -> None: pq2: KeyedPQ[None] = KeyedPQ() item2 = pq2.add('a', 0.0, None) self._assert_not_contained(item2) self.pq.add('a', 0.0, None) self._assert_not_contained(item2)
def test_non_equality(self) -> None: pq2: KeyedPQ[None] = KeyedPQ() self.assertTrue(self.pq != pq2) self.assertFalse(self.pq == pq2) self.assertTrue(self.pq != None) self.assertFalse(self.pq == None) self.assertTrue(self.pq != 4.0) self.assertFalse(self.pq == 4.0)
def bench_pop(b: BenchTimer) -> None: s = StringSource() pq: KeyedPQ[None] = KeyedPQ() for _ in range(b.n + 10000): pq.add(next(s), random_01(), None) with b.time() as t: for _ in t: pq.pop() with b.offset() as t: for _ in t: pass
def test_incremental_push_small(self) -> None: for n in itertools.chain.from_iterable(itertools.repeat(i, 1000) for i in range(10)): # Setup pq: KeyedPQ[None] = KeyedPQ() heap: typing.List[float] = [] # Test for i in range(n): val = random.random() pq.add(str(i), val, None) heapq.heappush(heap, val) self.assertEqual(pq._export(), heap)
def test_incremental_push(self) -> None: for n in range(1000): # Setup pq: KeyedPQ[None] = KeyedPQ() heap: typing.List[float] = [] # Test for i in range(n): val = random.random() pq.add(str(i), val, None) heapq.heappush(heap, val) self.assertEqual(pq._export(), heap)
def bench_change_value(b: BenchTimer) -> None: s = StringSource() pq: KeyedPQ[None] = KeyedPQ() for _ in range(10000): pq.add(next(s), random_01(), None) with b.time() as t: for _ in t: key = s.rand_existing() pq.change_value(key, random_01()) with b.offset() as t: for _ in t: key = s.rand_existing() random_01()
def bench_add(b: BenchTimer) -> None: s = StringSource() s_offset = StringSource() pq: KeyedPQ[None] = KeyedPQ() for _ in range(10000): pq.add(next(s), random_01(), None) next(s_offset) with b.time() as t: for _ in t: pq.add(next(s), random_01(), None) with b.offset() as t: for _ in t: next(s_offset) random_01()
def bench_remove(b: BenchTimer) -> None: s = StringSource() s_remove = StringSource() s_offset = StringSource() pq: KeyedPQ[None] = KeyedPQ() for _ in range(b.n + 10000): pq.add(next(s), random_01(), None) next(s_offset) with b.time() as t: for _ in t: key = next(s_remove) del pq[key] with b.offset() as t: for _ in t: key = next(s_offset)
def __init__(self, configuration: 'EVA.Configuration', storage: Storage) -> None: self._classifier: Classifier = Combine(( _ReusedClassifier(self), configuration.classifier, )) self._age_bin_width: int = configuration.age_bin_width self._ewma_factor: float = configuration.ewma_factor self._eva_computation_interval: int = configuration.eva_computation_interval self._storage_size: int = storage.total_bytes self._pq: KeyedPQ[EVA.State._FileInfo] = KeyedPQ() self._age_binner: Binner = LinearBinner(width=self._age_bin_width) self._class_infos: DefaultDict[ Hashable, EVA.State._ClassInfo] = DefaultDict( lambda: EVA.State._ClassInfo(self._age_binner), ) self._accesses_since_eva_computation: int = 0 self._last_eva_computation_ts: int = 0 self._last_age_bin: int = 0
class EndToEndTest(unittest.TestCase): def setUp(self) -> None: self.l: typing.List[float] = [] self.pq: KeyedPQ[None] = KeyedPQ() def _sort_l(self) -> None: self.l.sort() def _set_pq_from_iterable(self, iterable: typing.Iterable[typing.Tuple[str, float, None]]) -> None: self.pq = KeyedPQ(iterable) def test_build_heap(self) -> None: # Setup def iterable() -> typing.Iterable[typing.Tuple[str, float, None]]: for i in range(10000): val = random.random() self.l.append(val) yield (str(i), val, None) self._set_pq_from_iterable(iterable()) self._sort_l() # Test self.assertEqual(len(self.pq), len(self.l)) for i, val in enumerate(addressable_pq_pop_all(self.pq)): self.assertEqual(val, self.l[i]) def test_pop(self) -> None: # Setup for i in range(10000): val = random.random() self.pq.add(str(i), val, None) self.l.append(val) self._sort_l() # Test self.assertEqual(len(self.pq), len(self.l)) for i, val in enumerate(addressable_pq_pop_all(self.pq)): self.assertEqual(val, self.l[i]) def test_change_value(self) -> None: # Setup for i in range(10000): val = random.random() self.pq.add(str(i), val, None) for i in range(10000): val = random.random() self.pq.change_value(str(i), val) self.l.append(val) self._sort_l() # Test self.assertEqual(len(self.pq), len(self.l)) for i, val in enumerate(addressable_pq_pop_all(self.pq)): self.assertEqual(val, self.l[i]) def test_ordered_iter(self) -> None: # Setup for i in range(10000): val = random.random() self.pq.add(str(i), val, None) self.l.append(val) self._sort_l() # Test self.assertEqual(len(self.pq), len(self.l)) for i, it in enumerate(self.pq.ordered_iter()): self.assertEqual(it.value, self.l[i]) self.assertEqual(len(self.pq), len(self.l))
def setUp(self) -> None: self.pq: KeyedPQ[None] = KeyedPQ()
def __init__(self, configuration: 'GreedyDual.Configuration') -> None: self._mode: Mode = configuration.mode self._pq: KeyedPQ[GreedyDual.State._FileInfo] = KeyedPQ() self._threshold: float = 0.0
def __init__(self) -> None: self._pq: KeyedPQ[None] = KeyedPQ()
def test_duplicate_key(self) -> None: with self.assertRaises(KeyError): KeyedPQ([ ('a', 1.0, None), ('a', 3.0, None), ])
def setUp(self) -> None: self.pq: KeyedPQ[None] = KeyedPQ(max_heap=True)
class InvariantTest(unittest.TestCase): NUMBER_OF_ENTRIES = 10000 def setUp(self) -> None: self.pq: KeyedPQ[None] = KeyedPQ() def _set_pq_from_iterable(self, iterable: typing.Iterable[typing.Tuple[str, float, None]]) -> None: self.pq = KeyedPQ(iterable) def test_build_heap_small(self) -> None: # Setup # Test for entry_num in range(100): self._set_pq_from_iterable( (str(i), random.random(), None) for i in range(entry_num) ) self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), entry_num) def test_build_heap(self) -> None: # Setup # Test self._set_pq_from_iterable( (str(i), random.random(), None) for i in range(self.NUMBER_OF_ENTRIES) ) self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), self.NUMBER_OF_ENTRIES) def test_add(self) -> None: # Setup # Test self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), 0) for i in range(self.NUMBER_OF_ENTRIES): val = random.random() self.pq.add(str(i), val, None) self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), i + 1) def test_pop(self) -> None: # Setup for i in range(self.NUMBER_OF_ENTRIES): val = random.random() self.pq.add(str(i), val, None) # Test self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), self.NUMBER_OF_ENTRIES) for i in range(self.NUMBER_OF_ENTRIES): _, _, _ = self.pq.pop() self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), self.NUMBER_OF_ENTRIES - i - 1) def test_change_value(self) -> None: # Setup for i in range(self.NUMBER_OF_ENTRIES): val = random.random() self.pq.add(str(i), val, None) # Test self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), self.NUMBER_OF_ENTRIES) for i in range(self.NUMBER_OF_ENTRIES): val = random.random() self.pq.change_value(str(i), val) self.assertTrue(self.pq._verify_invariants()) def test_delete(self) -> None: # Setup for i in range(self.NUMBER_OF_ENTRIES): val = random.random() self.pq.add(str(i), val, None) # Test self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), self.NUMBER_OF_ENTRIES) for i in range(self.NUMBER_OF_ENTRIES): del self.pq[str(i)] self.assertTrue(self.pq._verify_invariants()) self.assertEqual(len(self.pq), self.NUMBER_OF_ENTRIES - i - 1)
def __init__(self, reuse_timer: ReuseTimer) -> None: self._reuse_timer: ReuseTimer = reuse_timer self._pq: KeyedPQ[None] = KeyedPQ(max_heap=True)
def __init__(self) -> None: self._pq: KeyedPQ[LandlordState._FileInfo] = KeyedPQ() self._rent_threshold: float = 0.0
def setUp(self) -> None: self.l: typing.List[float] = [] self.pq: KeyedPQ[None] = KeyedPQ(max_heap=True)
def setUp(self) -> None: self.pq: KeyedPQ[DummyClass] = KeyedPQ()
def _set_pq_from_iterable(self, iterable: typing.Iterable[typing.Tuple[str, float, None]]) -> None: self.pq = KeyedPQ(iterable, max_heap=True)
def __init__(self) -> None: super(Size.State, self).__init__() self._pq = KeyedPQ(max_heap=True)