def test_bmh_search(self):
        self.assertEqual(-1, bmh_search('', ''))
        self.assertEqual(-1, bmh_search('', 'x'))
        self.assertEqual(-1, bmh_search('axxxx', ''))
        self.assertEqual(-1, bmh_search('axxxx', ' '))
        self.assertEqual(0, bmh_search('axxxx', 'a'))
        self.assertEqual(1, bmh_search('xaxxx', 'a'))
        self.assertEqual(2, bmh_search('xxaxx', 'a'))
        self.assertEqual(3, bmh_search('xxxax', 'a'))
        self.assertEqual(4, bmh_search('xxxxa', 'a'))
        self.assertEqual(0, bmh_search('abcxxx', 'abc'))
        self.assertEqual(1, bmh_search('xabcxx', 'abc'))
        self.assertEqual(2, bmh_search('xxabcx', 'abc'))
        self.assertEqual(3, bmh_search('xxxabc', 'abc'))
        self.assertEqual(0, bmh_search('a', 'a'))

        # generate pattern which matches
        for i in range(1000):
            # s = random_strings(12, 20)
            s = random_string(80)
            pattern = random_substring(s, 15)
            print(s, '---', pattern)
            print(s.find(pattern, bmh_search(s, pattern)))
            self.assertEqual(s.find(pattern), bmh_search(s, pattern))

        # generate random pattern
        for i in range(1000):
            # s = random_strings(12, 20)
            s = random_string(80)
            pattern = random_string(15)
            print(s, '---', pattern)
            print(s.find(pattern, bmh_search(s, pattern)))
            self.assertEqual(s.find(pattern), bmh_search(s, pattern))
Example #2
0
    def test_something(self):
        self.assertEqual(True, True)

        # create palindrome
        for _ in range(10000):
            p = create_palindrome()
            print(p)
            self.assertEqual(is_palindrome(p), True)

        # create non-palindrome
        for _ in range(10000):
            p = create_palindrome() + random_string_len_n(1)
            if all_same(p):
                continue
            print(p)
            self.assertEqual(is_palindrome(p), False)

        # random strings
        for _ in range(10000):
            s = random_string(20)
            print(s)
            self.assertEqual(is_palindrome(s), s == s[::-1])
Example #3
0
    def test_something(self):
        self.assertEqual(length([]), 0)
        self.assertEqual(length([1]), 1)
        self.assertEqual(length([1, 2]), 2)
        self.assertEqual(length([1, 2, 3]), 3)
        for _ in range(1000):
            arr = random_int_array(20, 100)
            # print(arr)
            self.assertEqual(length(arr), len(arr))

        self.assertEqual(same([]), [])
        self.assertEqual(same([1]), [1])
        self.assertEqual(same([1, 2]), [1, 2])
        self.assertEqual(same([1, 2, 3]), [1, 2, 3])
        for _ in range(1000):
            arr = random_int_array(20, 100)
            # print(arr)
            self.assertEqual(same(arr), arr)

        self.assertEqual(reverse([]), [])
        self.assertEqual(reverse([1]), [1])
        self.assertEqual(reverse([1, 2]), [2, 1])
        self.assertEqual(reverse([1, 2, 3]), [3, 2, 1])
        for _ in range(1000):
            arr = random_int_array(20, 100)
            # print(arr)
            arr1 = list(arr)
            arr1.reverse()
            self.assertEqual(reverse(arr), arr1)

        self.assertEqual(nth_element([], 1), None)
        self.assertEqual(nth_element([1, 2, 3], -1), None)
        self.assertEqual(nth_element([1, 2, 3], 3), None)
        self.assertEqual(nth_element([1, 2, 3], 0), 1)
        self.assertEqual(nth_element([1, 2, 3], 1), 2)
        self.assertEqual(nth_element([1, 2, 3], 2), 3)
        for _ in range(1000):
            arr = random_int_array(20, 100)
            if arr:
                i = randrange(len(arr))
                # print(arr, i)
                self.assertEqual(nth_element(arr, i), arr[i])

        self.assertEqual(repeat('ha', -1), '')
        self.assertEqual(repeat('ha', 0), '')
        self.assertEqual(repeat('ha', 1), 'ha')
        self.assertEqual(repeat('ha', 2), 'haha')
        self.assertEqual(repeat('ha', 3), 'hahaha')
        for _ in range(1000):
            s = random_string(10)
            i = randrange(10)
            # print(s, i)
            self.assertEqual(repeat(s, i), s * i)

        self.assertEqual(pop_in_list([], 0), [])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 0), [2, 3, 4, 5])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 1), [1, 3, 4, 5])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 2), [1, 2, 4, 5])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 3), [1, 2, 3, 5])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 4), [1, 2, 3, 4])
        self.assertEqual(pop_in_list([1, 2, 3, 4, 5], 5), [1, 2, 3, 4, 5])
Example #4
0
def create_palindrome():
    s = random_string(15)
    if choice([True, False]):
        return s + s[::-1]
    else:
        return s + random_string(1) + s[::-1]