Esempio n. 1
0
 def test_recursion(self):
     self.DATA_SIZE = 1000
     seed(0)
     self.data = sample(range(self.DATA_SIZE * 3), k=self.DATA_SIZE)
     self.data.sort()
     recursive_binary_search(self.data, self.data[-1])
     self.assertGreater(RecursionCounter.recursion_count, 9)
Esempio n. 2
0
 def test_search_not_found(self):
     self.DATA_SIZE = 1000
     seed(0)
     self.data = sample(range(self.DATA_SIZE * 3), k=self.DATA_SIZE)
     self.data.sort()
     result = linear_search(self.data, self.DATA_SIZE * 4)
     self.assertFalse(result)
     result = recursive_binary_search(self.data, self.DATA_SIZE * 4)
     self.assertFalse(result)
     result = jump_search(self.data, self.DATA_SIZE * 4)
     self.assertFalse(result)
Esempio n. 3
0
    def test_search_at_middle(self):
        self.DATA_SIZE = 1000
        seed(0)
        self.data = sample(range(self.DATA_SIZE * 3), k=self.DATA_SIZE)
        self.data.sort()

        result = linear_search(self.data, self.data[(self.DATA_SIZE // 2) - 1])
        self.assertTrue(result) 
        
        result = recursive_binary_search(self.data, self.data[(self.DATA_SIZE // 2) - 1])
        self.assertTrue(result) 

        result = jump_search(self.data, self.data[(self.DATA_SIZE // 2) - 1])
        self.assertTrue(result) 
Esempio n. 4
0
    def test_search_at_end(self):
        self.DATA_SIZE = 1000000
        seed(0)
        self.data = sample(range(self.DATA_SIZE * 3), k=self.DATA_SIZE)
        self.data.sort()

        start = time.perf_counter()
        result = recursive_binary_search(self.data, self.data[-1])
        fastest = time.perf_counter() - start
        self.assertTrue(result) 

        start = time.perf_counter()
        result = linear_search(self.data, self.data[-1])
        slowest = time.perf_counter() - start
        self.assertTrue(result)
        self.assertLess(fastest * 10000, slowest * 10000)

        start = time.perf_counter()
        result = jump_search(self.data, self.data[-1])
        fastest = time.perf_counter() - start
        self.assertTrue(result)
        self.assertLess(fastest * 10000, slowest * 10000)