def test_run_three_iterations(self):
    expected_row = [
      True, True, False, True, True, True, True
    ]
    automaton = Engine(30)

    last_row = automaton.run(3)

    self.assertListEqual(last_row, expected_row)
  def test_run_one_iteration(self):
    expected_row = [
      True, True, True
    ]
    automaton = Engine(30)

    last_row = automaton.run(1)

    self.assertListEqual(last_row, expected_row)
  def test_user_provided_iteration_handler_one_iteration(self):
    received_rows = []
    received_iteration = [None]
    def row_handler(row, iteration):
      received_rows.append(row)
      received_iteration[0] = iteration
    automaton = Engine(30)

    automaton.run(1, handler=row_handler)

    self.assertEqual(0, received_iteration[0])
    self.assertEqual(1, len(received_rows), "Row handler was not called")
    self.assertListEqual([True, True, True], received_rows[0])
Exemple #4
0
 def test_rule_90_construction(self):
     expected = [
         [1, 1, 0],
         [1, 0, 0],
         [0, 1, 1],
         [0, 0, 1],
     ]
     rule = Engine.get_rule(90)
     self.assertListEqual(rule, expected)
 def test_rule_90_construction(self):
   expected = [
     [1, 1, 0],
     [1, 0, 0],
     [0, 1, 1],
     [0, 0, 1],
   ]
   rule = Engine.get_rule(90)
   self.assertListEqual(rule, expected)
  def test_tostring(self):
    expected_string = """Rule 30, 10 iterations:
          #          
         ###         
        ##  #        
       ## ####       
      ##  #   #      
     ## #### ###     
    ##  #    #  #    
   ## ####  ######   
  ##  #   ###     #  
 ## #### ##  #   ### 
##  #    # #### ##  #"""
    automaton = Engine(30)
    automaton.run(10)

    output_string = automaton.__str__()

    self.assertEquals(expected_string, output_string)
  def test_automaton_creation_attaches_rule(self):
    expected_rule = [
      [True,  False, False],
      [False, True,  True],
      [False, True,  False],
      [False, False, True],
    ]

    automaton = Engine(30)

    self.assertListEqual(automaton.rule, expected_rule)
  def test_user_provided_iteration_handler_many_iterations(self):
    received_rows = []
    last_iteration = [None]

    def row_handler(row, iteration):
      received_rows.append(row)
      last_iteration[0] = iteration

    automaton = Engine(30)

    automaton.run(10, handler=row_handler)

    self.assertEqual(9, last_iteration[0])
    self.assertEqual(
      10, len(received_rows), 
      "Row handler was not called once for each new iteration output"
    )
    self.assertListEqual(
      [True, True, False, False, True, False, False, False, False, True, False, 
       True, True, True, True, False, True, True, False, False, True], 
      received_rows[9]
    )
  def test_retrieve_iteration(self):
    expected_first_row = [True]
    expected_second_row = [True, True, True]
    expected_third_row = [
      True, True, False, False, True
    ]
    automaton = Engine(30)
    automaton.run(10)

    first_row = automaton.retrieve(0)
    second_row = automaton.retrieve(1)
    third_row = automaton.retrieve(2)

    self.assertListEqual(first_row, expected_first_row)
    self.assertListEqual(second_row, expected_second_row)
    self.assertListEqual(third_row, expected_third_row)