def assert_predictions_match_cloned_learner(test: TestCase, y1, y2): y1, y2 = list(y1[:100]), list(y2[:100]) # limit amount of comparisons for perf and put in list test.assertListEqual( y1, y2, ( "Calling `predict` after fitting the same set of data to a cloned learner should " "yield the same result" ), )
def test_auto_complete(): redis = Redis(decode_responses=True) redis.flushall() test_case = TestCase() ac = AutoComplete(redis) ac.feed('张艺兴', 5000) ac.feed('张艺谋', 3000) ac.feed('张三', 500) test_case.assertListEqual(['张艺兴', '张艺谋', '张三'], ac.hint('张')) test_case.assertListEqual(['张艺兴', '张艺谋'], ac.hint('张艺'))
def api_has_expected_paths(test_case: unittest.TestCase, api_name: str, expected_paths: List[str]) -> List[str]: """ Test that an AWS API Gateway REST API has certain paths. :param test_case: Instance of a unittest test case. This object is used to make assertions. :param api_name: The name of the REST API. :param expected_paths: List of paths which should exist in the API. :return: The paths found in the API Gateway REST API. """ api_id = APIGateway.rest_api_exists(test_case, api_name) resources = apigateway.get_resources(restApiId=api_id) paths = [path.get('path') for path in resources.get('items')] paths.sort() test_case.assertListEqual(expected_paths, paths) return paths
def test_sns(): test_case = TestCase() redis = Redis(decode_responses=True) redis.flushall() bingo = SocialRelationship(redis, 'Bingo') iris = SocialRelationship(redis, 'Iris') bingo.follow('Iris') bingo.follow('GitHub') bingo.follow('Apple') iris.follow('Bingo') iris.follow('GitHub') assert bingo.is_following('Iris') is True assert iris.is_following('Bingo') is True assert bingo.count_following() == 3 test_case.assertListEqual(['GitHub'], bingo.get_common_following('Iris'))
def assert_dir_files(test: unittest.TestCase, directory: str, contents: dict) -> None: """ Will check if directory contains all the given files with the given content Args: test (unittest.TestCase): The testcase to use directory (str): given directory to check contents (dict): mapping 'filename -> content' Returns: flag indicating all files are as expected """ for file, content in contents.items(): current_file = os.path.join(directory, file) test.assertTrue( os.path.isfile(current_file), 'File: "' + file + '" is expected to exist (' + directory + ")") with open(current_file, mode='r') as f: test.assertListEqual(f.read().splitlines(), content)
def test_ranking(): test_case = TestCase() redis = Redis(decode_responses=True) redis.flushall() ranking = Ranking(redis) ranking.incr('bingo', 5) ranking.incr('iris', 3) ranking.incr('lily', 4) ranking.incr('helen', 6) test_case.assertListEqual([('helen', 6.0), ('bingo', 5.0)], ranking.get_today_top_n(n=2, with_scores=True)) test_case.assertListEqual([('helen', 6.0), ('bingo', 5.0)], ranking.get_week_top_n(n=2, with_scores=True)) test_case.assertListEqual([('helen', 6.0), ('bingo', 5.0), ('lily', 4.0)], ranking.get_month_top_n(n=3, with_scores=True))
def test_paginate(): test_case = TestCase() redis = Redis(decode_responses=True) redis.flushall() topics = Paginate(redis, 'user-topics') for i in range(24): topics.add(i) # 取总数 assert topics.get_size() == 24 # 以每页5条数据的方式取出第1页数据 test_case.assertListEqual(['23', '22', '21', '20', '19'], topics.get_page(1, 5)) # 以每页10条数据的方式取出第1页数据 test_case.assertListEqual( ['23', '22', '21', '20', '19', '18', '17', '16', '15', '14'], topics.get_page(1, 10)) # 以每页5条数据的方式取出第5页数据 test_case.assertListEqual(['3', '2', '1', '0'], topics.get_page(5, 5))
zeros.append(i) else: product *= n if len(zeros) == 0: for i in range(len(nums)): nums[i] = int(product / nums[i]) elif len(zeros) == 1: for i in range(len(nums)): if nums[i] != 0: nums[i] = 0 else: nums[i] = product else: nums = [0 for i in nums] return nums if __name__ == '__main__': test = TestCase() test.assertListEqual(productExceptSelf([1, 2, 3, 4]), [24, 12, 8, 6]) test.assertListEqual(productExceptSelf([]), []) test.assertListEqual(productExceptSelf([0, 1, 2]), [2, 0, 0]) test.assertListEqual(productExceptSelf([0, 0]), [0, 0]) test.assertListEqual(productExceptSelf2([1, 2, 3, 4]), [24, 12, 8, 6]) test.assertListEqual(productExceptSelf2([]), []) test.assertListEqual(productExceptSelf2([0, 1, 2]), [2, 0, 0]) test.assertListEqual(productExceptSelf2([0, 0]), [0, 0])
Minimize the total number of operations. ''' from unittest import TestCase from typing import List class Solution: def moveZeros(self, nums: List[int]) -> None: cur = 0 # current zero position for i in range(len(nums)): if nums[i] != 0: nums[cur] = nums[i] cur += 1 while (cur < len(nums)): nums[cur] = 0 cur += 1 if __name__ == "__main__": nums = [0, 1, 0, 3, 12] solution = Solution() solution.moveZeros(nums) assert (len(nums) == 5) assert (nums[0] == 1) assert (nums[2] == 12) assert (nums[4] == 0) test_case = TestCase() test_case.assertListEqual(nums, [1, 3, 12, 0, 0])
if product == 1: return True if path.get(product) is None: path[product] = True else: return False lst = split_num(product) def split_num2(n: int) -> List[int]: return [int(i) for i in str(n)] def split_num(n: int) -> List[int]: ret = [] while n >= 1: ret.insert(0, n % 10) n = int(n / 10) return ret if __name__ == "__main__": test_case = TestCase() test_case.assertTrue(isHappy(7)) test_case.assertFalse(isHappy(8)) test_case.assertTrue(isHappy(19)) test_case.assertListEqual(split_num(76), [7, 6]) test_case.assertListEqual(split_num2(76), [7, 6])
def test_precisionN(self): myprecisionN = precisionN(self.myref, self.mysys1) correctPrecisionN = [(1, 1.0), (2, 0.5)] TestCase.assertListEqual(self, myprecisionN, correctPrecisionN)