Exemplo n.º 1
0
		def get_test_automaton():
			words = "he her hers his she hi him man himan".split()

			t = Trie();
			for w in words:
				t.add_word(w, w)

			t.make_automaton()

			return t
Exemplo n.º 2
0
    def testExistShouldDetectAddedWords(self):
        t = Trie()
        t.add_word('python', 'value')
        t.add_word('ada', 'value')

        self.assertTrue(t.exists('python'))
        self.assertTrue(t.exists('ada'))
Exemplo n.º 3
0
    def testAddingExistingWordShouldReplaceAssociatedValue(self):
        t = Trie()
        t.add_word('python', 'value')
        self.assertEqual(len(t), 1)
        self.assertEqual(t.get('python'), 'value')

        t.add_word('python', 'other')
        self.assertEqual(len(t), 1)
        self.assertEqual(t.get('python'), 'other')
Exemplo n.º 4
0
	def testExistShouldDetectAddedWords(self):
		t = Trie()
		t.add_word('python', 'value')
		t.add_word('ada', 'value')

		self.assertTrue(t.exists('python'))
		self.assertTrue(t.exists('ada'))
Exemplo n.º 5
0
	def testAddingExistingWordShouldReplaceAssociatedValue(self):
		t = Trie()
		t.add_word('python', 'value')
		self.assertEqual(len(t), 1)
		self.assertEqual(t.get('python'), 'value')

		t.add_word('python', 'other')
		self.assertEqual(len(t), 1)
		self.assertEqual(t.get('python'), 'other')
Exemplo n.º 6
0
        def get_test_automaton():
            words = "he her hers his she hi him man himan".split()

            t = Trie()
            for w in words:
                t.add_word(w, w)

            t.make_automaton()

            return t
Exemplo n.º 7
0
 def testEmptyTrieShouldNotContainsAnyWords(self):
     t = Trie()
     self.assertEqual(len(t), 0)
Exemplo n.º 8
0
    def testClearShouldRemoveEveryting(self):
        t = Trie()

        t.add_word('python', 1)
        t.add_word('ada', 2)
        t.add_word('perl', 3)
        t.add_word('pascal', 4)
        t.add_word('php', 5)

        self.assertEqual(len(t), 5)
        self.assertEqual(len(list(t.items())), 5)

        t.clear()

        self.assertEqual(len(t), 0)
        self.assertEqual(len(list(t.items())), 0)
Exemplo n.º 9
0
	def testValuesShouldReturnAllValuesAlreadyAddedToTheTrie(self):
		t = Trie()

		t.add_word('python', 1)
		t.add_word('ada', 2)
		t.add_word('perl', 3)
		t.add_word('pascal', 4)
		t.add_word('php', 5)

		result = list(t.values())
		self.assertEquals(len(result), 5)
		self.assertIn(1, result)
		self.assertIn(2, result)
		self.assertIn(3, result)
		self.assertIn(4, result)
		self.assertIn(5, result)
Exemplo n.º 10
0
    def testMatchShouldDetecAllPrefixesIncludingWord(self):
        t = Trie()
        t.add_word('python', 'value')
        t.add_word('ada', 'value')

        self.assertTrue(t.match('a'))
        self.assertTrue(t.match('ad'))
        self.assertTrue(t.match('ada'))

        self.assertTrue(t.match('p'))
        self.assertTrue(t.match('py'))
        self.assertTrue(t.match('pyt'))
        self.assertTrue(t.match('pyth'))
        self.assertTrue(t.match('pytho'))
        self.assertTrue(t.match('python'))
Exemplo n.º 11
0
 def testGetUnknowWordWithoutDefaultValueShouldRaiseException(self):
     t = Trie()
     with self.assertRaises(KeyError):
         t.get('python')
Exemplo n.º 12
0
	def testGetUnknowWordWithDefaultValueShouldReturnDefault(self):
		t = Trie()
		self.assertEqual(t.get('python', 'default'), 'default')
Exemplo n.º 13
0
	def testGetUnknowWordWithoutDefaultValueShouldRaiseException(self):
		t = Trie()
		with self.assertRaises(KeyError):
			t.get('python')
Exemplo n.º 14
0
	def testAddedWordShouldBeCountedAndAvailableForRetrieval(self):
		t = Trie()
		t.add_word('python', 'value')
		self.assertEqual(len(t), 1)
		self.assertEqual(t.get('python'), 'value')
Exemplo n.º 15
0
	def testClearShouldRemoveEveryting(self):
		t = Trie()

		t.add_word('python', 1)
		t.add_word('ada', 2)
		t.add_word('perl', 3)
		t.add_word('pascal', 4)
		t.add_word('php', 5)

		self.assertEqual(len(t), 5)
		self.assertEqual(len(list(t.items())), 5)

		t.clear()

		self.assertEqual(len(t), 0)
		self.assertEqual(len(list(t.items())), 0)
Exemplo n.º 16
0
 def testAddedWordShouldBeCountedAndAvailableForRetrieval(self):
     t = Trie()
     t.add_word('python', 'value')
     self.assertEqual(len(t), 1)
     self.assertEqual(t.get('python'), 'value')
Exemplo n.º 17
0
	def testExistShouldReturnFailOnUnknownWord(self):
		t = Trie()
		t.add_word('python', 'value')

		self.assertFalse(t.exists('ada'))
Exemplo n.º 18
0
	def testMatchShouldDetecAllPrefixesIncludingWord(self):
		t = Trie()
		t.add_word('python', 'value')
		t.add_word('ada', 'value')

		self.assertTrue(t.match('a'))
		self.assertTrue(t.match('ad'))
		self.assertTrue(t.match('ada'))

		self.assertTrue(t.match('p'))
		self.assertTrue(t.match('py'))
		self.assertTrue(t.match('pyt'))
		self.assertTrue(t.match('pyth'))
		self.assertTrue(t.match('pytho'))
		self.assertTrue(t.match('python'))
Exemplo n.º 19
0
 def testGetUnknowWordWithDefaultValueShouldReturnDefault(self):
     t = Trie()
     self.assertEqual(t.get('python', 'default'), 'default')
Exemplo n.º 20
0
	def testKeysShouldReturnAllKeysAlreadyAddedToTheTrie(self):
		t = Trie()

		t.add_word('python', 1)
		t.add_word('ada', 2)
		t.add_word('perl', 3)
		t.add_word('pascal', 4)
		t.add_word('php', 5)

		result = list(t.keys())
		self.assertEquals(len(result), 5)
		self.assertIn('python',result)
		self.assertIn('ada',   result)
		self.assertIn('perl',  result)
		self.assertIn('pascal',result)
		self.assertIn('php',   result)
Exemplo n.º 21
0
    def testExistShouldReturnFailOnUnknownWord(self):
        t = Trie()
        t.add_word('python', 'value')

        self.assertFalse(t.exists('ada'))
Exemplo n.º 22
0
    def testValuesShouldReturnAllValuesAlreadyAddedToTheTrie(self):
        t = Trie()

        t.add_word('python', 1)
        t.add_word('ada', 2)
        t.add_word('perl', 3)
        t.add_word('pascal', 4)
        t.add_word('php', 5)

        result = list(t.values())
        self.assertEquals(len(result), 5)
        self.assertIn(1, result)
        self.assertIn(2, result)
        self.assertIn(3, result)
        self.assertIn(4, result)
        self.assertIn(5, result)
Exemplo n.º 23
0
    def testKeysShouldReturnAllKeysAlreadyAddedToTheTrie(self):
        t = Trie()

        t.add_word('python', 1)
        t.add_word('ada', 2)
        t.add_word('perl', 3)
        t.add_word('pascal', 4)
        t.add_word('php', 5)

        result = list(t.keys())
        self.assertEquals(len(result), 5)
        self.assertIn('python', result)
        self.assertIn('ada', result)
        self.assertIn('perl', result)
        self.assertIn('pascal', result)
        self.assertIn('php', result)
Exemplo n.º 24
0
    def testItemsShouldReturnAllItemsAlreadyAddedToTheTrie(self):
        t = Trie()

        t.add_word('python', 1)
        t.add_word('ada', 2)
        t.add_word('perl', 3)
        t.add_word('pascal', 4)
        t.add_word('php', 5)

        result = list(t.items())
        self.assertEqual(len(result), 5)
        self.assertIn(('python', 1), result)
        self.assertIn(('ada', 2), result)
        self.assertIn(('perl', 3), result)
        self.assertIn(('pascal', 4), result)
        self.assertIn(('php', 5), result)