Ejemplo n.º 1
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))
Ejemplo n.º 2
0
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)