def test_summary(self): def assert_sum_code(c): code, _ = t.summary() self.assertEqual(code, c) t = Test(list, operation=True) assert_sum_code(t.EXIT_EMPTY) t.add_case(Case('append', [1], 'pop', Result(1))) t.run() assert_sum_code(t.EXIT_PASS) t.add_case(Case('pop', Result(None))) # assert_sum_code(t.EXIT_PENDING) with self.assertRaises(IndexError): t.run() assert_sum_code(t.EXIT_FAIL) t.add_case(Case('append', [1], Result(None))) # assert_sum_code(t.EXIT_PENDING) t.run() assert_sum_code(t.EXIT_FAIL) def f(i): if i == 0: return Case('append', [1], Result(None)) raise ValueError t.add_func(f) with self.assertRaises(ValueError): t.run() assert_sum_code(t.EXIT_GEN_ERR)
def test_check_result(self): t = Test(list, operation=True) t.add_case( Case('append', [1], 'pop', Result(1), 'append', [2], 'append', [3], 'pop', 'pop', Result(2))) t.add_case( Case('append', [1], 'pop', 'append', [2], 'append', [3], 'pop', 'pop', result=list)) t.run()
def g(i): rows = 0 coins = i while coins >= rows: coins -= rows rows += 1 return Case(i, result=rows - 1)
def random_complete_tree(i): vals = randints(i + 2, max_num=i * 100) root = TreeNode.from_iterable(vals) i_first_leaf = int(len(vals) / 2) i_last_left_left = len(vals) - len(vals) % 2 - 1 result = sum(vals[i_last_left_left:i_first_leaf - 1:-2]) return Case(root, result=result)
def r(i): length = randint(1, 20) ints = randints(length, max_num=9, min_num=1) count = [ ints[i] > ints[j] * 2 for i in range(0, length) for j in range(i + 1, length) ].count(True) return Case(ints, result=count)
def r(i): cnt_nums = randint(1, 50) k = randint(1, cnt_nums) items = randints(cnt_nums, unique=True, min_num=MIN_INT, max_num=MAX_INT) num_repeats = randints(cnt_nums, unique=True, min_num=1, max_num=cnt_nums + 1) num_repeats.sort(reverse=True) nums = sum(([item] * repeat for item, repeat in zip(items, num_repeats)), []) shuffle(nums) return Case(nums, k, result=items[:k])
def r(i): cnt_twice = randint(0, i) cnt_nums = cnt_twice + i nums = randints(i, unique=True, max_num=cnt_nums, min_num=1) twice_nums = nums[:cnt_twice] once_nums = nums[cnt_twice:] nums = twice_nums * 2 + once_nums shuffle(nums) return Case(nums, result=twice_nums)
def random_numbers(i): """ :param int i: number of times this function is called starting from 0 :return Case: """ nums1 = sorted(randints(count=i, max_num=i * 100)) nums2 = sorted(randints(count=max(i, 1), max_num=i * 100)) result = float(median(nums1 + nums2)) return Case(nums1, nums2, result=result)
def r(i): cnt_twice = randint(0, i) cnt_nums = cnt_twice + i nums = randints(i, unique=True, max_num=cnt_nums, min_num=1) twice_nums = nums[:cnt_twice] once_nums = nums[cnt_twice:] nums = twice_nums * 2 + once_nums shuffle(nums) missing = list(set(range(1, cnt_nums + 1)) - set(nums)) return Case(nums, result=missing)
def r(i): mid = list('a' * randint(1, i + 1) + 'b' * randint(1, i + 1)) shuffle(mid) mid = ''.join(mid) non_repeating = list(set(ascii_lowercase) - set('ab')) left = randstr(length=randint(0, len(non_repeating)), unique=True, alphabet=non_repeating) right = randstr(length=randint(0, len(non_repeating)), unique=True, alphabet=non_repeating) return Case(left + mid + right, result=len(mid))
def r(i): ops = [] nums = randints(max(1, i), max_num=75) tmp_nums = [] for num in nums: tmp_nums.append(num) tmp_nums.sort() if len(tmp_nums) % 2 == 1: median = tmp_nums[len(tmp_nums) // 2] else: median = float(tmp_nums[len(tmp_nums) // 2] + tmp_nums[len(tmp_nums) // 2 - 1]) / 2 ops.extend(['addNum', [num], 'findMedian', Result(median)]) return Case(*ops)
def _make_case(root): operations = [] # Constructor arguments operations.append([root]) # Methods names and asserted results if root is not None: for val in root.inorder(): operations.extend( ['hasNext', Result(True), 'next', Result(val)]) operations.extend(['hasNext', Result(False)]) return Case(*operations)
def r(i): prefix_chrs = sample(alphabet, randint(0, len(alphabet))) shuffle(prefix_chrs) prefix = ''.join(prefix_chrs) suffix_chrs = list(alphabet - set(prefix_chrs)) if suffix_chrs: strs = [ prefix + suffix_chrs[i] + ''.join(sample(suffix_chrs, randint(0, len(suffix_chrs)))) for i in range(randint(1, len(suffix_chrs))) ] else: strs = [prefix] return Case(strs, result=prefix if len(strs) > 1 else strs[0])
def do_test(target): with Test(target) as test: Case([1, 3], [2], result=2.0) Case([1, 2], [3, 4], result=2.5) @test def random_numbers(i): """ :param int i: number of times this function is called starting from 0 :return Case: """ nums1 = sorted(randints(count=i, max_num=i * 100)) nums2 = sorted(randints(count=max(i, 1), max_num=i * 100)) result = float(median(nums1 + nums2)) return Case(nums1, nums2, result=result) def median(nums): if not nums: raise ValueError nums = sorted(nums) if len(nums) % 2 == 0: return float(nums[int(len(nums) / 2)] + nums[int(len(nums) / 2) - 1]) / 2 return nums[int(len(nums) / 2)]
def r(i): num_cits = randint(0, i) citations = randints(count=num_cits, max_num=num_cits * 2) citations.sort() count = 0 min_cites = MAX_INT for citation in reversed(citations): min_cites = min(citation, min_cites) count += 1 if count > min_cites: count -= 1 break return Case(citations, result=count)
def r(i): cnt_vals = randint(0, i) odd_vals = randints(cnt_vals) even_vals = randints(cnt_vals) vals = [val for pair in zip(odd_vals, even_vals) for val in pair] if even_vals and randbool(): even_vals.pop() vals.pop() root = ListNode.from_iterable(vals) odd_root = ListNode.from_iterable(odd_vals) if odd_root: even_root = ListNode.from_iterable(even_vals) odd_root.end().next = even_root return Case(root, result=odd_root)
def test_process_args(self): c = Case() res = c.process_args([1, 2, 3], False) self.assertEqual(res, Operations((), [Operation(None, [1, 2, 3], True)])) res = c.process_args([['a', 2, 'c'], 'push', [1, 2, 3], 'pop', Result(1), 'count', 'count', 'pop', Result('d'), 'push', [[4, 5]], Result([0]), 'len'], True) self.assertEqual( res, Operations(('a', 2, 'c'), [ Operation('push', [1, 2, 3], False), Operation('pop', collect=True), Operation('count'), Operation('count'), Operation('pop', collect=True), Operation('push', [[4, 5]], True), Operation('len'), ])) STRS = [ ([], r'no args were specified'), ([[1, 2, 3], []], r'expected.*, got \[\]'), ([[1, 2, 3], 'a', [], [1]], r'expected.*, got \[1\]'), (['a', Result(1), Result(2)], r'expected.*, got Result\(2\)'), ([ Result(1), ], r'got Result\(1\)'), ([[1, 2, 3], Result(1)], r'got Result\(1\)'), (['1', Result('b'), [1]], r'got \[1\]'), ] for args, pat in STRS: with self.assertRaisesRegexp(ValueError, pat): c.process_args(args, True) with self.assertRaisesRegexp(ValueError, r'no args'): c.process_args([], True) with self.assertRaisesRegexp(ValueError, r'no method call'): c.process_args([[]], True)
def g(i): rows = gen_pascal_triangle(i) return Case(i, result=rows[-1])
from __future__ import print_function from random import randint from rapidtest import Test, Case, randints, ListNode, randbool from solutions.odd_even_linked_list import Solution with Test(Solution) as test: Case(ListNode.from_iterable([1, 2, 3, 4, 5, 6, 7, 8]), result=ListNode.from_iterable([1, 3, 5, 7, 2, 4, 6, 8])) Case(ListNode.from_iterable([]), result=ListNode.from_iterable([])) Case(ListNode.from_iterable([1]), result=ListNode.from_iterable([1])) Case(ListNode.from_iterable([1, 2]), result=ListNode.from_iterable([1, 2])) @test def r(i): cnt_vals = randint(0, i) odd_vals = randints(cnt_vals) even_vals = randints(cnt_vals) vals = [val for pair in zip(odd_vals, even_vals) for val in pair] if even_vals and randbool(): even_vals.pop() vals.pop() root = ListNode.from_iterable(vals) odd_root = ListNode.from_iterable(odd_vals) if odd_root: even_root = ListNode.from_iterable(even_vals) odd_root.end().next = even_root
from rapidtest import Test, Case, randints, randstr, Result, unordered from itertools import combinations, chain, permutations from random import randint, shuffle from string import ascii_lowercase with Test('Solution.java', post_proc=unordered) as t: Case('word', result=['word', '1ord', 'w1rd', 'wo1d', 'wor1', '2rd', 'w2d', 'wo2', '1o1d', '1or1', 'w1r1', '1o2', '2r1', '3d', 'w3', '4']) Case('', result=['']) Case('a', result=['a', '1']) Case('1', result=['1', '1'])
from rapidtest import Test, Case, randints, randstr, Result, unordered from itertools import combinations, chain, permutations from random import randint, shuffle from string import ascii_lowercase with Test('Solution.java', post_proc=unordered) as t: Case("++++", result=["--++", "+--+", "++--"]) Case("+++", result=["--+", "+--"]) Case("++", result=["--"]) Case("--", result=[]) Case("+-+", result=[]) Case("+", result=[]) Case("", result=[]) Case("+-+-", result=[]) Case("+-++-", result=['+----'])
from rapidtest import Test, Case, randints, randstr, Result, unordered from itertools import combinations, chain, permutations from random import randint, shuffle from string import ascii_lowercase with Test('Solution.java', post_proc=unordered) as t: Case(['area', 'lead', 'wall', 'lady', 'ball'], result=[['wall', 'area', 'lead', 'lady'], ['ball', 'area', 'lead', 'lady']]) Case(['abat', 'baba', 'atan', 'atal'], result=[['baba', 'abat', 'baba', 'atan'], ['baba', 'abat', 'baba', 'atal']]) Case(['a'], result=[[ 'a', ]]) Case(['ab', 'ba'], result=[[ 'ab', 'ba', ], [ 'ba', 'ab', ]]) Case(['abc', 'bca', 'cab'], result=[[ 'abc', 'bca', 'cab',
def r(i): n = randint(1, i + 1) nums = randints(n) k = randint(1, n) maxes = [max(nums[i:i + k]) for i in range(n - k + 1)] return Case(nums, k, result=maxes)
from rapidtest import Test, Case, randints, randstr from itertools import combinations, chain, permutations from random import randint, shuffle from string import ascii_lowercase with Test('Solution.java') as t: Case([1], 1, result=[1]) Case([1, 2, 3, 2, 1], 1, result=[1, 2, 3, 2, 1]) Case([1, 2, 3, 2, 1], 5, result=[3]) Case([3, 2, 1, 0, 1], 5, result=[3]) Case([1, 0, 1, 2, 3], 5, result=[3]) Case([1, 3, -1, -3, 5, 3, 6, 7], 3, result=[3, 3, 5, 5, 6, 7]) Case([-1, 0, -1, -2, -3], 5, result=[0]) @t def r(i): n = randint(1, i + 1) nums = randints(n) k = randint(1, n) maxes = [max(nums[i:i + k]) for i in range(n - k + 1)] return Case(nums, k, result=maxes)
from rapidtest import Test, Case, TreeNode from solutions.binary_tree_postorder_traversal import Solution with Test(Solution) as test: Case(TreeNode.from_string('[1,null,2,3]'), result=[3, 2, 1]) Case(TreeNode.from_string('[]'), result=[]) Case(TreeNode.from_string('[1]'), result=[1]) Case(TreeNode.from_string('[1,2]'), result=[2, 1]) Case(TreeNode.from_string('[1,2]'), result=[2, 1]) Case(TreeNode.from_string( '[1,2,null,4,5,null,6,2,null,6,8,4,null,1,2,4,null,6,8,0,9,null,7,5,4,null,3,null,2,3]' ), result=[ 3, 0, 9, 1, 7, 2, 6, 5, 4, 4, 8, 6, 4, 3, 6, 2, 8, 4, 2, 5, 2, 1 ])
def r(i): mid = randstr(length=min(i, 26), unique=True, alphabet=ascii_lowercase) left = randstr(length=randint(0, i), alphabet=mid) right = randstr(length=randint(0, i), alphabet=mid) return Case(left + mid + right, result=len(mid))
from rapidtest import Test, Case, randints, randstr from itertools import combinations, chain, permutations from random import randint from string import ascii_lowercase with Test('Solution.java') as t: Case('abcabcbb', result=len('abc')) Case('bbbbb', result=len('b')) Case('pwwkew', result=len('wke')) @t def r(i): mid = randstr(length=min(i, 26), unique=True, alphabet=ascii_lowercase) left = randstr(length=randint(0, i), alphabet=mid) right = randstr(length=randint(0, i), alphabet=mid) return Case(left + mid + right, result=len(mid))
def all_palindromes(i): while True: p = next(_gen) if p and not (len(p) != 1 and p.startswith('0')): return Case(int(p), result=True)
def negative(i): return Case(-i - 1, result=False)
def random(i): x = randint(0, MAX_INT) return Case(x, result=_is_palindrome(x))