Exemplo n.º 1
0
 def test(self):
     self.assertEqual(sum_array(None), 0)
     self.assertEqual(sum_array([]), 0)
     self.assertEqual(sum_array([3]), 0)
     self.assertEqual(sum_array([-3]), 0)
     self.assertEqual(sum_array([3, 5]), 0)
     self.assertEqual(sum_array([-3, -5]), 0)
     self.assertEqual(sum_array([6, 2, 1, 8, 10]), 16)
     self.assertEqual(sum_array([6, 0, 1, 10, 10]), 17)
     self.assertEqual(sum_array([-6, -20, -1, -10, -12]), -28)
     self.assertEqual(sum_array([-6, 20, -1, 10, -12]), 3)
Exemplo n.º 2
0
    def test_rand(self):
        from random import randint
        from functools import reduce
        sol = lambda arr: 0 if not arr or len(arr) < 3 else reduce(
            lambda a, b: a - b,
            reduce(
                lambda a, b: [
                    a[0] + b, b if a[1] > b else a[1], b if a[2] < b else a[2]
                ], arr, [0, 9999999999999, -9999999999999]))

        for _ in range(40):
            arr = [(-1)**randint(0, 1) * randint(1, 10**randint(1, 3))
                   for q in range(randint(1, 45))]
            self.assertEqual(sum_array(arr[:]), sol(arr),
                             "It should work for random inputs too")
Exemplo n.º 3
0
def test_only_one_element():
    assert sum_array([3]) == 0
    assert sum_array([-3]) == 0
Exemplo n.º 4
0
def test_none_or_empty():
    assert sum_array(None) == 0
    assert sum_array([]) == 0
Exemplo n.º 5
0
def test_real_tests():
    assert sum_array([6, 2, 1, 8, 10]) == 16
    assert sum_array([6, 0, 1, 10, 10]) == 17
    assert sum_array([-6, -20, -1, -10, -12]) == -28
    assert sum_array([-6, 20, -1, 10, -12]) == 3
Exemplo n.º 6
0
def test_only_two_element():
    assert sum_array([3, 5]) == 0
    assert sum_array([-3, -5]) == 0
Exemplo n.º 7
0
def test_sum_array():
    assert sum_array([]) == 0
    assert sum_array([1, 2, 3]) == 6
    assert sum_array([1.1, 2.2, 3.3]) == 6.6
    assert sum_array([4, 5, 6]) == 15
    assert sum_array(range(101)) == 5050
Exemplo n.º 8
0
def test_output():
    assert sum_array([]) == 0
    assert sum_array([1]) == 1
    assert sum_array([1,2]) == 3
    assert sum_array([1,10,100]) == 111
    assert sum_array([1.1,2.2,3.3]) == 6.6
Exemplo n.º 9
0
def test_float():
    assert str(type(sum_array([1.1,2])))[1:-1] == "type 'float'"
Exemplo n.º 10
0
def test_int():
    assert str(type(sum_array([1,2])))[1:-1] == "type 'int'"
Exemplo n.º 11
0
 def test(self):
     self.assertEqual(sum_array([]), 0)
     self.assertEqual(sum_array([1, 2, 3]), 6)
     self.assertEqual(sum_array([1.1, 2.2, 3.3]), 6.6)
     self.assertEqual(sum_array([4, 5, 6]), 15)
     self.assertEqual(sum_array(range(101)), 5050)