<s> import math <EOL> import os <EOL> import sys <EOL> import unittest <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . abspath ( '<STR_LIT:..>' ) ) <EOL> import polymaze as pmz <EOL> try : <EOL> _assertCountEqual = unittest . TestCase . assertCountEqual <EOL> except AttributeError : <EOL> _assertCountEqual = unittest . TestCase . assertItemsEqual <EOL> class TestComponentShapeImplementations ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . shape_neighborhoods = [ supershape_with_neighbors ( ss ) <EOL> for _ , ss in pmz . SUPERSHAPES_DICT . items ( ) ] <EOL> def test_neighbors_have_each_others_indexes ( self ) : <EOL> """<STR_LIT>""" <EOL> for neighborhood in self . shape_neighborhoods : <EOL> for shape in neighborhood . shapes ( ) : <EOL> self_index = shape . index ( ) <EOL> for n_index , neighbor in shape . neighbors ( ) : <EOL> if neighbor is None : <EOL> continue <EOL> self . assertIn ( self_index , neighbor . _edge_data , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( neighborhood . supershape_name ( ) , <EOL> shape . name ( ) , shape . index ( ) , <EOL> neighbor . name ( ) , neighbor . index ( ) ) ) <EOL> def test_opposing_edge_vertexes_match ( self ) : <EOL> """<STR_LIT>""" <EOL> for neighborhood in self . shape_neighborhoods : <EOL> for shape in neighborhood . shapes ( ) : <EOL> s_index = shape . index ( ) <EOL> for n_index , neighbor in shape . neighbors ( ) : <EOL> if neighbor is None : <EOL> continue <EOL> s_edge_data = shape . _edge_data [ n_index ] <EOL> n_edge_data = neighbor . _edge_data [ s_index ] <EOL> self_vertexes = [ s_edge_data [ '<STR_LIT>' ] , <EOL> s_edge_data [ '<STR_LIT>' ] ] <EOL> n_vertexes = [ n_edge_data [ '<STR_LIT>' ] , <EOL> n_edge_data [ '<STR_LIT>' ] ] <EOL> self_vertexes . sort ( ) <EOL> n_vertexes . sort ( ) <EOL> for self_vertex , n_vertex in zip ( self_vertexes , n_vertexes ) : <EOL> self_vertex = complex ( * self_vertex ) <EOL> n_vertex = complex ( * n_vertex ) <EOL> ss_name = neighborhood . supershape_name ( ) <EOL> self . assertAlmostEqual ( self_vertex , n_vertex , msg = <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' . format ( ss_name , <EOL> shape . name ( ) , <EOL> shape . index ( ) , <EOL> self_vertex , <EOL> neighbor . name ( ) , <EOL> n_index , n_vertex ) ) <EOL> def test_internally_stored_vertexes_are_in_order_around_perimeter ( self ) : <EOL> """<STR_LIT>""" <EOL> for neighborhood in self . shape_neighborhoods : <EOL> for shape in neighborhood . shapes ( ) : <EOL> edge_count = len ( tuple ( shape . n_indexes ( ) ) ) <EOL> for i , n_index in enumerate ( shape . n_indexes ( ) ) : <EOL> next_i = ( i + <NUM_LIT:1> ) % edge_count <EOL> next_n_index = tuple ( shape . n_indexes ( ) ) [ next_i ] <EOL> edge_data = shape . _edge_data [ n_index ] <EOL> next_data = shape . _edge_data [ next_n_index ] <EOL> a , b = ( edge_data [ '<STR_LIT>' ] , <EOL> edge_data [ '<STR_LIT>' ] ) <EOL> c , d = ( next_data [ '<STR_LIT>' ] , <EOL> next_data [ '<STR_LIT>' ] ) <EOL> a , b = complex ( * a ) , complex ( * b ) <EOL> c , d = complex ( * c ) , complex ( * d ) <EOL> self . assertNotAlmostEqual ( a , d ) <EOL> self . assertAlmostEqual ( b , c ) <EOL> def test_avg_area_is_correct ( self ) : <EOL> triangle_area = math . sin ( math . pi / <NUM_LIT> ) / <NUM_LIT> <EOL> hexagon_area = <NUM_LIT> * <NUM_LIT:3> ** <NUM_LIT:0.5> <EOL> name_and_avg_areas_spec = { '<STR_LIT>' : <NUM_LIT:1.0> , <EOL> '<STR_LIT>' : triangle_area , <EOL> '<STR_LIT>' : hexagon_area } <EOL> tested_count = <NUM_LIT:0> <EOL> for neighborhood in self . shape_neighborhoods : <EOL> ss = neighborhood . _supershape <EOL> if ss . name ( ) not in name_and_avg_areas_spec : <EOL> continue <EOL> self . assertAlmostEqual ( ss . avg_area ( ) , <EOL> name_and_avg_areas_spec [ ss . name ( ) ] ) <EOL> tested_count += <NUM_LIT:1> <EOL> self . assertEqual ( tested_count , len ( name_and_avg_areas_spec ) ) <EOL> class TestComponentShape ( unittest . TestCase ) : <EOL> def test_index_returns_same_index_provided_on_creation ( self ) : <EOL> index_spec = ( <NUM_LIT:10> , <NUM_LIT:20> ) <EOL> shape = generic_shape ( index = index_spec ) <EOL> self . assertEqual ( shape . index ( ) , index_spec ) <EOL> def test_creation_takes_ownership_of_all_unowned_edges ( self ) : <EOL> shape = generic_shape ( ) <EOL> owned_indexes = ( n_index for n_index , edge in shape . edges ( ) ) <EOL> owned_indexes_spec = shape . n_indexes ( ) <EOL> _assertCountEqual ( self , owned_indexes , owned_indexes_spec ) <EOL> def test_creation_does_not_take_ownership_of_claimed_edges ( self ) : <EOL> shape = generic_shape ( ) <EOL> a_neighbor_index = tuple ( shape . n_indexes ( ) ) [ <NUM_LIT:0> ] <EOL> neighbor = shape . _grid . create ( a_neighbor_index ) <EOL> self . assertIn ( a_neighbor_index , shape . _owned_edges ) <EOL> self . assertNotIn ( shape . index ( ) , neighbor . _owned_edges ) <EOL> def test_grab_edges_does_not_include_edges_owned_by_self_or_neighbor ( self ) : <EOL> main_shape = generic_shape ( ) <EOL> n_index = tuple ( main_shape . n_indexes ( ) ) [ <NUM_LIT:0> ] <EOL> neighbor = main_shape . _grid . create ( n_index ) <EOL> for shape in ( main_shape , neighbor ) : <EOL> self . assertFalse ( shape . _grab_edges ( shape . _owned_edges ) ) <EOL> def test_give_away_edges_moves_ownership_of_edges_to_neighbors ( self ) : <EOL> index = ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> original_owner = generic_shape ( index = index ) <EOL> grid = original_owner . grid ( ) <EOL> n_index = tuple ( original_owner . n_indexes ( ) ) [ <NUM_LIT:0> ] <EOL> neighbor = grid . create ( n_index ) <EOL> self . assertIn ( n_index , original_owner . _owned_edges ) <EOL> self . assertNotIn ( index , neighbor . _owned_edges ) <EOL> original_owner . _give_away_edges ( ) <EOL> self . assertNotIn ( n_index , original_owner . _owned_edges ) <EOL> self . assertIn ( index , neighbor . _owned_edges ) <EOL> def test_edge_returns_an_edge_for_each_neighbor_index ( self ) : <EOL> shape = generic_shape ( ) <EOL> for n_index in shape . n_indexes ( ) : <EOL> self . assertIsNotNone ( shape . edge ( n_index ) ) <EOL> def test_neighbors_generates_the_index_and_neighbor_for_each_n_index ( self ) : <EOL> shape = generic_shape ( ) <EOL> indexed_neighbors_spec = list ( ) <EOL> for n_index in shape . n_indexes ( ) : <EOL> neighbor = shape . _grid . create ( n_index ) <EOL> indexed_neighbors_spec . append ( ( n_index , neighbor ) ) <EOL> indexed_neighbors = list ( shape . neighbors ( ) ) <EOL> _assertCountEqual ( self , indexed_neighbors , indexed_neighbors_spec ) <EOL> def test_neighbors_generates_index_and_None_for_empty_neighbors ( self ) : <EOL> shape = generic_shape ( ) <EOL> for n_index , neighbor in shape . neighbors ( ) : <EOL> self . assertIn ( n_index , shape . n_indexes ( ) ) <EOL> self . assertIsNone ( neighbor ) <EOL> class TestEdge ( unittest . TestCase ) : <EOL> def test_endpoints_returns_endpoints ( self ) : <EOL> """<STR_LIT>""" <EOL> index = ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> shape = generic_shape ( index = index ) <EOL> n_index = tuple ( shape . n_indexes ( ) ) [ <NUM_LIT:0> ] <EOL> edge = shape . edge ( n_index ) <EOL> end_1_spec = shape . _edge_data [ n_index ] [ '<STR_LIT>' ] <EOL> end_2_spec = shape . _edge_data [ n_index ] [ '<STR_LIT>' ] <EOL> ends_spec = ( end_1_spec , end_2_spec ) <EOL> ends = tuple ( edge . endpoints ( ) ) <EOL> _assertCountEqual ( self , ends , ends_spec ) <EOL> def test_endpoints_returns_endpoints_from_indicated_shape ( self ) : <EOL> """<STR_LIT>""" <EOL> main_index = ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> main_shape = generic_shape ( index = main_index ) <EOL> n_index = tuple ( main_shape . n_indexes ( ) ) [ <NUM_LIT:0> ] <EOL> neighbor = main_shape . _grid . create ( n_index ) <EOL> edge = main_shape . edge ( n_index ) <EOL> for shape , other in ( ( main_shape , neighbor ) , ( neighbor , main_shape ) ) : <EOL> shape_end_1 , shape_end_2 = edge . endpoints ( shape . index ( ) ) <EOL> n_end_1 , n_end_2 = edge . endpoints ( other . index ( ) ) <EOL> shape_end_1_spec = shape . _edge_data [ other . index ( ) ] [ '<STR_LIT>' ] <EOL> shape_end_2_spec = shape . _edge_data [ other . index ( ) ] [ '<STR_LIT>' ] <EOL> n_end_1_spec = other . _edge_data [ shape . index ( ) ] [ '<STR_LIT>' ] <EOL> n_end_2_spec = other . _edge_data [ shape . index ( ) ] [ '<STR_LIT>' ] <EOL> self . assertIs ( shape_end_1 , shape_end_1_spec ) <EOL> self . assertIs ( shape_end_2 , shape_end_2_spec ) <EOL> self . assertIs ( n_end_1 , n_end_1_spec ) <EOL> self . assertIs ( n_end_2 , n_end_2_spec ) <EOL> def test_endpoints_raises_valueerror_for_non_neighbor_index ( self ) : <EOL> some_index = ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> shape = generic_shape ( index = some_index ) <EOL> non_neighbor_index = ( <NUM_LIT:1000> , <NUM_LIT> ) <EOL> self . assertNotIn ( non_neighbor_index , shape . n_indexes ( ) ) <EOL> _ , any_edge = tuple ( shape . edges ( ) ) [ <NUM_LIT:0> ] <EOL> self . assertRaises ( ValueError , <EOL> any_edge . endpoints , * ( non_neighbor_index , ) ) <EOL> def generic_shape ( supershape = None , index = None ) : <EOL> grid = pmz . PolyGrid ( supershape = supershape ) <EOL> index = index or ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> shape = grid . create ( index ) <EOL> return shape <EOL> def supershape_with_neighbors ( ss ) : <EOL> """<STR_LIT>""" <EOL> grid = pmz . PolyGrid ( supershape = ss ) <EOL> for comp_index , comp_data in ss . components ( ) . items ( ) : <EOL> for n_index in comp_data [ '<STR_LIT>' ] : <EOL> if grid . get ( n_index ) is None : <EOL> grid . create ( n_index ) <EOL> return grid <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
