def test_board_with_no_lines(self): board1 = visual_to_board(""" * * * * * * * * * """) self.assertEqual(squares(board1), {1: 0, 2: 0}) board2 = visual_to_board(""" * """) self.assertEqual(squares(board2), {})
def test_three(self): result = squares(3) expected = { 1: 1, 2: 4, 3: 9 } self.assertEqual(result, expected)
def processImage(img, debug, threshold): if threshold is None: ch = clockhandler(img, debug=debug) else: ch = clockhandler(img, debug=debug, threshold=threshold) if len(ch.getwatchcircle()) == 0: sq = squares(img, debug=debug) sq._process_square()
def test_board_with_no_lines_forming_squares(self): board1 = visual_to_board(""" *-* | * * """) self.assertEqual(squares(board1), {1: 0}) board2 = visual_to_board(""" *-*-*-* | | * * *-* | | * *-*-* | | | * * *-* """) self.assertEqual(squares(board2), {1: 0, 2: 0, 3: 0})
def test_identifies_squares_of_size_1(self): board1 = visual_to_board(""" *-*-* | *-*-* | | | * *-* """) self.assertEqual(squares(board1), {1: 1, 2: 0}) board2 = visual_to_board(""" * * * * | | | * *-*-* | | | *-*-*-* | | | *-* * * """) self.assertEqual(squares(board2), {1: 3, 2: 0, 3: 0})
def test_idenfities_full_board_squares(self): board1 = visual_to_board(""" *-*-*-* | | * * * * | | * * * * | | *-*-*-* """) self.assertEqual(squares(board1), {1: 0, 2: 0, 3: 1}) board2 = visual_to_board(""" *-*-* | | * * * | | *-*-* """) self.assertEqual(squares(board2), {1: 0, 2: 1})
def recurse_phi(eventscatter, start, wedge, theta, acc, partition=4, recurse='half'): """ Takes an event of (list of SpacePoint objects) a starting phi (in radians), a starting wedge (in radians), a constant theta angle (in radians), and a percent change between trials (as a decimal) that the function should stop at. Returns a tuple with the sum of squared distances as the first element and the best phi value that gives the least squares for a given theta as the second element. The argument partition takes an integer. It is the number of partitions the test should use. The argument recurse takes an integer 1 <= recurse <= partition. Its default is 'half', meaning half of the partition number, rounding up. """ partition = float(partition) startsquare = sqr.squares(eventscatter,normgen(theta,start)) if startsquare < 1: return (startsquare,start) phitrials = [] for i in range(int(partition)): phitrials.append((start - (wedge/2.0)) + ((wedge/partition)*i)) squaresresults = [] for angle in phitrials: squaresresults.append(sqr.squares(eventscatter,normgen(theta,angle))) pairs = zip(squaresresults,phitrials) # for each tuple in this list, # index 0 in the sum of squares and index 1 is the value of the # variable phi that gives that sum (for a given theta). pairs.sort() if math.fabs(startsquare - pairs[0][0]) / startsquare < acc and math.fabs(startsquare - pairs[1][0]) / startsquare < acc: return pairs[0] if recurse == 'half': splitnum = int(math.ceil(partition/2.0)) elif recurse == 'all': splitnum = int(partition) else: splitnum = int(recurse) split_result = [] for i in range(int(splitnum)): split_result.append(recurse_phi(eventscatter,pairs[i][1],(2 * (wedge/partition)),theta,acc,partition,recurse)) split_result.sort() return split_result[0]
def test_squares(): assert squares(2, 5) == [2, 4, 16, 256, 65536] assert squares(3, 3) == [3, 9, 81] assert squares(5, 3) == [5, 25, 625] assert squares(10, 4) == [10, 100, 10000, 100000000] assert squares(2, 0) == [] assert squares(2, -4) == []
def main(): IPs = getIPs() GeoData = getGeo(IPs) data = map(GeoData) squares(data)
squares.py: def squares(start, stop): for i in range(start, stop): print i**2 if __name__ == `__main__`: # ...run automated tests... calc-squares: #! /usr/bin/env python import squares squares.squares(0, 10)
def test_zero(self): result = squares(0) expected = {} self.assertEqual(result, expected)
def test_one(self): result = squares(1) expected = {1: 1} self.assertEqual(result, expected)
def test_neg_number(self): result = squares(-1) expected = {} self.assertEqual(result, expected)
def test_squares_return_list_that_hes_not_last_element_equel_to_limit_squared( ): expected_result = 25 result = squares.squares(5) assert expected_result != result
def test_squares_return_list_that_has_lenght_equal_to_limit(): expected_length = 5 result = squares.squares(5) assert len(result) == expected_length
def test_squares_return_lista_that_hes_first_element_equel_to_zero(): result = squares.squares(5) assert result[0] == 0