class SolutionTests(unittest.TestCase): def setUp(self): self.sol = Solution() def test_twoSum_ValidTarget_ReturnsAnswer(self): self.assertEqual(self.sol.twoSum([2, 7, 11, 15], 9), [0, 1]) def test_twoSum_InvalidTarget_RaisesException(self): with self.assertRaises(Exception): self.sol.twoSum([2, 7, 11, 15], 999)
def test_2_sum(self): ts = Solution() a1,a2,a3 = [2,7,11,15],[3,2,4],[3,3] t1,t2,t3 = 9,6,6 r1,r2,r3 = [0,1],[1,2],[0,1] self.assertEqual(ts.twoSum(a1,t1),r1) self.assertEqual(ts.twoSum(a2,t2),r2) self.assertEqual(ts.twoSum(a3,t3),r3)
def test_fails(self): """ This test is supposed to fail """ ts = Solution() a1,a2,a3 = [2,7,11,15],[3,2,4],[3,3] t1,t2,t3 = 9,6,6 r1,r2,r3 = [0,1],[0,2],[0,1] self.assertEqual(ts.twoSum(a1,t1),r1) self.assertEqual(ts.twoSum(a2,t2),r2) self.assertEqual(ts.twoSum(a3,t3),r3)
def test_sum(self): """ Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. """ nums = [2, 7, 11, 15] target = 9 expected = [0, 1] sol = Solution() result = sol.twoSum(nums, target) self.assertListEqual(result, expected)
class TestTwoSum(unittest.TestCase): def setUp(self): self.solution = Solution() def test_empty(self): self.assertEqual(self.solution.two_sum([], 0), False) def test_case_one(self): self.assertEqual(self.solution.two_sum([5, 6, 3, 2], 9), [1, 2]) def test_case_two(self): self.assertEqual(self.solution.two_sum([5, 5], 10), [0, 1]) def test_case_three(self): self.assertEqual(self.solution.two_sum([3, 4, 5, 8], 11), [0, 3]) def test_case_four(self): self.assertEqual(self.solution.two_sum([3, 2, 1, 4], 3), [1, 2])
def test_lists(self): """ Test the output when given the following lists: nums = [2,7,11,15], target = 9 nums = [3,2,4], target = 6 nums = [3,3], target = 6 """ test_one = Solution.twoSum([2, 7, 11, 15], 9) test_two = Solution.twoSum([3, 2, 4], 6) test_three = Solution.twoSum([3, 3], 6) test_one.sort() test_two.sort() test_three.sort() self.assertEqual(test_one, [0, 1]) self.assertEqual(test_two, [1, 2]) self.assertEqual(test_three, [0, 1])
def test_5(self): self.assertEqual(Solution().two_sum_bict([3,5,-4,8,11,1,-1,6],10), [11,-1])
def test_sums_to_9(self): s = Solution() self.assertEqual(s.twoSum([2, 7, 11, 15], 9), [0, 1])
def test_sums_to_6(self): s = Solution() self.assertEqual(s.twoSum([3, 2, 4], 6), [1, 2])
import pytest from two_sum import Solution1, Solution2, Solution solution_list = [Solution1(), Solution2(), Solution()] # testcases: List[Tuple[inp, out]] # use `set` to indicate the expected output may be multiple data = [ ( ([1, 2, 4, 6, 10], 8), [1, 3], ), ( ([2, 3, 4], 6), [0, 2], ), ( ([-1, 0], -1), [0, 1], ), ( ([0] * 13010 + [2, 3] + [9] * 13010, 5), [13010, 13011], ), ] @pytest.fixture(params=solution_list) def sln(request): """Solution fixture"""
def test_sums_to_6_(self): s = Solution() self.assertEqual(s.twoSum([3, 3], 6), [0, 1])
def test_two_sum_1(self): nums = [2, 7, 11, 15] target = 9 result = Solution().twoSum(nums, target) self.assertEqual(result, [0, 1])
def test_two_sum(self): solution = Solution() self.assertEqual(solution.twoSum([2, 7, 11, 15], 9), [0, 1]) self.assertEqual(solution.twoSum([3, 2, 4], 6), [1, 2]) self.assertEqual(solution.twoSum([3, 3], 6), [0, 1])
def setUp(self): self.solution = Solution()
def test_two_sum_2(self): nums = [-3, 4, 3, 90] target = 0 result = Solution().twoSum(nums, target) self.assertEqual(result, [0, 2])
def test_1(self): self.assertEqual(Solution().two_sum(([3,5,-4,8,11,1,-1,6],10), [-1, 11]))
def test_two_sum(): assert Solution().two_sum([2, 7, 11, 15], 9) == [0, 1] assert Solution().two_sum([1, 3, 5, 7], 8) == [1, 2] assert Solution().two_sum([2, 7, 11, 13, 11], 22) == [2, 4] assert Solution().two_sum([3, 2, 3], 6) == [0, 2]
def test_2(self): self.assertEqual(Solution().two_sum([3,5,-4,8,11,1,-1,6,1,5,-9,-4,-6.6,8],19), [8, 11])
def test_solution(nums, target): assert Solution().two_sum(nums, target)
def setUpClass(cls): cls.solution = Solution()