Exemplo n.º 1
0
 def test_keys_with_prefix_all_found(self):
     t = TST()
     t.insert("occasion", 2)
     t.insert("occasionally", 2)
     t.insert("occam", 2)
     self.assertEqual(sorted(t.keys_with_prefix("occa")),
                      ["occam", "occasion", "occasionally"])
Exemplo n.º 2
0
 def test_keys_with_prefix_two_found(self):
     t = TST()
     t.insert("one", 1)
     t.insert("two", 2)
     t.delete("one")
     t.insert("three", 3)
     self.assertEqual(sorted(t.keys_with_prefix("t")), ["three", "two"])
Exemplo n.º 3
0
 def test_keys_that_match_pattern_using_dots(self):
     t = TST()
     t.insert("nop", 0)
     t.insert("one", 1)
     t.insert("on", "fire")
     t.insert("fno", "ok")
     self.assertEqual(sorted(t.keys_that_match(".n.")), ["fno", "one"])
Exemplo n.º 4
0
    def test_all_pairs_random_size_and_strings(self):
        t = TST()

        n = random.randint(3, 1000)
        random_pairs = {}

        for _ in range(n):
            key = TestTST.gen_rand_str(random.randint(1, 17))
            random_pairs[key] = key
            t.insert(key, key)

        self.assertEqual(t.all_pairs(), random_pairs)
Exemplo n.º 5
0
    def test_delete_the_only_key(self):
        t = TST()
        t.insert("seven", 7)

        self.assertEqual(t.delete("seven"), 7)
        self.assertTrue(t.is_empty())
        self.assertEqual(t.count(), 0)
        self.assertFalse(t.contains("seven"))
        self.assertIsNone(t.search("seven"))
Exemplo n.º 6
0
    def test_delete_inexistent_key(self):
        t = TST()
        t.insert("first", "1st")

        self.assertIsNone(t.delete("second"))
        self.assertFalse(t.is_empty())
        self.assertEqual(t.count(), 1)
        self.assertTrue(t.contains("first"))
        self.assertEqual(t.search("first"), "1st")
Exemplo n.º 7
0
 def test_insert_one(self):
     t = TST()
     t.insert("one", 97)
     self.assertFalse(t.is_empty())
     self.assertEqual(t.count(), 1)
     self.assertEqual(t.search("one"), 97)
     self.assertTrue(t.contains("one"))
Exemplo n.º 8
0
    def test_keys_with_prefix_empty_prefix(self):
        t = TST()

        n = random.randint(1, 50)
        keys = set()

        for _ in range(n):
            key = TestTST.gen_rand_str(random.randint(1, 11))
            keys.add(key)
            t.insert(key, key)

        kwp = t.keys_with_prefix("")
        kwp_set = set(kwp)
        self.assertEqual(len(kwp), len(
            kwp_set))  # I should not need to check this here!!!
        self.assertEqual(kwp_set, keys)
Exemplo n.º 9
0
    def test_delete_all_random_keys(self):
        t = TST()

        n = random.randint(3, 2000)
        random_pairs = {}

        for _ in range(n):
            key = TestTST.gen_rand_str(random.randint(1, 11))
            random_pairs[key] = key
            t.insert(key, key)

        for k, v in random_pairs.items():
            self.assertEqual(t.delete(k), v)
            self.assertIsNone(t.search(k))
            self.assertFalse(t.contains(k))

        self.assertTrue(t.is_empty())
        self.assertEqual(t.count(), 0)
Exemplo n.º 10
0
 def test_keys_that_match_example_docs(self):
     t = TST()
     t.insert("food", 3)
     t.insert("good", 3)
     t.insert("foodie", 3)
     self.assertEqual(sorted(t.keys_that_match(".ood")), ["food", "good"])
Exemplo n.º 11
0
 def test_keys_that_match_pattern_no_dots(self):
     t = TST()
     t.insert("one", 1)
     t.insert("on", "fire")
     self.assertEqual(t.keys_that_match("on"), ["on"])
Exemplo n.º 12
0
 def test_keys_that_match_tst_empty_pattern_many_dots(self):
     t = TST()
     self.assertEqual(t.keys_that_match("......."), [])
Exemplo n.º 13
0
 def test_keys_that_match_pattern_empty_str(self):
     t = TST()
     self.assertRaises(ValueError, t.keys_that_match, "")
Exemplo n.º 14
0
 def test_all_pairs_empty_tst(self):
     t = TST()
     self.assertEqual(t.all_pairs(), {})
Exemplo n.º 15
0
 def test_insert_none_value(self):
     t = TST()
     self.assertRaises(ValueError, t.insert, "key", None)
Exemplo n.º 16
0
 def test_keys_that_match_pattern_using_dots_to_retrieve_all_keys_of_certain_length(
         self):
     t = TST()
     t.insert("zero", 0)
     t.insert("one", 1)
     t.insert("two", 2)
     t.insert("three", 3)
     t.insert("four", 4)
     t.insert("five", 5)
     t.insert("six", 6)
     self.assertEqual(sorted(t.keys_that_match("...")),
                      ["one", "six", "two"])
     self.assertEqual(sorted(t.keys_that_match("....")),
                      ["five", "four", "zero"])
     self.assertEqual(sorted(t.keys_that_match(".....")), ["three"])
Exemplo n.º 17
0
 def test_insert_two(self):
     t = TST()
     t.insert("he", 0)
     t.insert("she", 1)
     self.assertFalse(t.is_empty())
     self.assertEqual(t.count(), 2)
     self.assertEqual(t.search("he"), 0)
     self.assertEqual(t.search("she"), 1)
     self.assertTrue(t.contains("he"))
     self.assertTrue(t.contains("she"))
Exemplo n.º 18
0
 def test_longest_prefix_of_longest_prefix_size_one(self):
     t = TST()
     t.insert("o", 7)
     t.insert("obnoxious", 23)
     self.assertEqual(t.longest_prefix_of("overall"), "o")
Exemplo n.º 19
0
 def test_longest_prefix_of_longest_prefix_size_zero(self):
     t = TST()
     t.insert("obnoxious", 7)
     # obnoxious is NOT even a prefix of over
     self.assertEqual(t.longest_prefix_of("over"), "")
Exemplo n.º 20
0
 def test_longest_prefix_of_empty_tst(self):
     t = TST()
     self.assertEqual(t.longest_prefix_of(TestTST.gen_rand_str(10)), "")
Exemplo n.º 21
0
 def test_longest_prefix_of_query_empty(self):
     t = TST()
     self.assertRaises(ValueError, t.longest_prefix_of, "")
Exemplo n.º 22
0
 def test_longest_prefix_of_query_not_str(self):
     t = TST()
     self.assertRaises(TypeError, t.longest_prefix_of, -0.12)
Exemplo n.º 23
0
 def test_insert_key_not_string(self):
     t = TST()
     self.assertRaises(TypeError, t.insert, 10, 5)
Exemplo n.º 24
0
 def test_longest_prefix_of_longest_prefix_size_of_query(self):
     t = TST()
     t.insert("allen", "first")
     t.insert("allen halloween", "underrated!")
     self.assertEqual(t.longest_prefix_of("allen halloween"),
                      "allen halloween")
Exemplo n.º 25
0
 def test_insert_key_empty_string(self):
     t = TST()
     self.assertRaises(ValueError, t.insert, "", 2)
Exemplo n.º 26
0
 def test_keys_that_match_pattern_not_str(self):
     t = TST()
     self.assertRaises(TypeError, t.keys_that_match, 1 / 2)
Exemplo n.º 27
0
 def test_creation(self):
     t = TST()
     self.assertTrue(t.is_empty())
     self.assertEqual(t.count(), 0)
Exemplo n.º 28
0
 def test_all_pairs_tst_size_1(self):
     t = TST()
     t.insert("the most sadistic", "necro")
     self.assertEqual(t.all_pairs(), {"the most sadistic": "necro"})
Exemplo n.º 29
0
 def test_insert_same_twice_to_update(self):
     t = TST()
     t.insert("seven", 7)
     t.insert("fly away", 11)
     t.insert("fly away", 101)
     t.insert("bandit queen", "Looptroop")
     self.assertFalse(t.is_empty())
     self.assertEqual(t.count(), 3)
     self.assertEqual(t.search("seven"), 7)
     self.assertEqual(t.search("fly away"), 101)
     self.assertEqual(t.search("bandit queen"), "Looptroop")
     self.assertTrue(t.contains("seven"))
     self.assertTrue(t.contains("fly away"))
     self.assertTrue(t.contains("bandit queen"))
Exemplo n.º 30
0
 def test_longest_prefix_of_longest_prefix_size_two(self):
     t = TST()
     t.insert("p", 7)
     t.insert("oa", 23)
     self.assertEqual(t.longest_prefix_of("oak"), "oa")