def test_Day9Example3OutputsLargeMiddleNumber(self): intcode = [104,1125899906842624,99] exampleComputer = IntcodeComputer(intcode) with patch('builtins.print') as p: exampleComputer.run() p.assert_called_with(1125899906842624) self.assertEqual(1125899906842624,exampleComputer.outputs[0])
def test_opcode5OnNotZeroJump(self): intcode = [5, 7, 1, 1, 4, 4, 6, 99] testComputer = IntcodeComputer(intcode) testComputer.run() self.assertEqual(len(testComputer.log), 2) self.assertEqual(testComputer.log[0], f'opcode 5 at position 0 processed') self.assertEqual(testComputer.log[1], f'opcode 99, exiting')
def test_runningOpcodes_1positionMode_and_99MessageLog(self): intcode = [1, 1, 1, 1, 99] testComputer = IntcodeComputer(intcode) testComputer.run() self.assertEqual(2, len(testComputer.log)) self.assertEqual('opcode 1 at position 0 processed',testComputer.log[0]) self.assertEqual('opcode 99, exiting',testComputer.log[1])
def test_runningOpcode3WithInput5IntcodeChanges(self): intcode = [3, 1, 99] userInput = 5 testComputer = IntcodeComputer(intcode) with patch('builtins.input', lambda *args: userInput): testComputer.run() self.assertEqual([3, 5, 99],testComputer.intcode)
def test_Day9Example2Outputs16digitNumber(self): intcode = [1102,34915192,34915192,7,4,7,99,0] exampleComputer = IntcodeComputer(intcode) with patch('builtins.print') as p: exampleComputer.run() p.assert_called_with(1219070632396864) self.assertEqual(16,len(str(exampleComputer.outputs[0])))
def test_parameterCis0_1_2(self): intcode = [801, 99] testComputer = IntcodeComputer(intcode) with self.assertRaises(ValueError) as e: testComputer.run() self.assertEqual(f'Parameter C should be 0, 1 or 2. Not 8', str(e.exception))
def test_opcode6ImediateModeJump(self): intcode = [1106, 0, 7, 4, 0, 4, 0, 99] testComputer = IntcodeComputer(intcode) testComputer.run() self.assertEqual(len(testComputer.log), 2) self.assertEqual(testComputer.log[0], f'opcode 6 at position 0 processed') self.assertEqual(testComputer.log[1], f'opcode 99, exiting')
def test_opcode6OnZeroJump(self): intcode = [6, 6, 3, 7, 4, 4, 0, 99] testComputer = IntcodeComputer(intcode) testComputer.run() self.assertEqual(len(testComputer.log), 2) self.assertEqual(testComputer.log[0], f'opcode 6 at position 0 processed') self.assertEqual(testComputer.log[1], f'opcode 99, exiting')
def test_Day5Part2Example6Input1Output1FromList(self): intcode = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1] exampleComputer = IntcodeComputer(intcode) userInputNonZero = 1 with patch('builtins.print') as p: with patch('builtins.input', lambda *args: userInputNonZero): exampleComputer.run() self.assertEqual(1, exampleComputer.outputs[0])
def test_opcode6OnNotZeroDoNothing(self): intcode = [6, 6, 7, 1, 4, 4, 1, 99] testComputer = IntcodeComputer(intcode) testComputer.run() self.assertEqual(len(testComputer.log), 3) self.assertEqual(testComputer.log[0], f'opcode 6 at position 0 processed') self.assertEqual(testComputer.log[1], f'opcode 1 at position 3 processed') self.assertEqual(testComputer.log[2], f'opcode 99, exiting')
def test_immediateModeOpcode3ValueErrorParameterA(self): intcode = [103, 1, 99] testComputer = IntcodeComputer(intcode) with self.assertRaises(ValueError) as e: userInput = 4 with patch('builtins.input', lambda *args: userInput): testComputer.run() self.assertEqual('Parameters that an instruction writes to will never be in immediate mode.', str(e.exception))
def test_runningOpcode3input5_opcode3input3InputList(self): intcode = [3, 0, 3, 1, 99] inputs = [5, 3] testComputer = IntcodeComputer(intcode) testComputer.inputs = inputs testComputer.run() self.assertEqual([],testComputer.inputs) self.assertEqual([5, 3, 3, 1, 99],testComputer.intcode)
def test_immediateModeOpcode1ValueErrorParameter3(self): intcode = [10001, 0, 0, 99] testComputer = IntcodeComputer(intcode) with self.assertRaises(ValueError) as e: testComputer.run() self.assertEqual( 'Parameters that an instruction writes to will never be in immediate mode.', str(e.exception))
def test_Day5Part2Example5Input0Output0FromList(self): intcode = [3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9] exampleComputer = IntcodeComputer(intcode) userInputZero = 0 with patch('builtins.print') as p: with patch('builtins.input', lambda *args: userInputZero): exampleComputer.run() self.assertEqual(0, exampleComputer.outputs[0])
def test_day5Example1FromList(self): intcode = [3, 0, 4, 0, 99] inputValue = 567 exampleComputer = IntcodeComputer(intcode) with patch('builtins.print') as p: with patch('builtins.input', lambda *args: inputValue): exampleComputer.run() self.assertEqual(inputValue,exampleComputer.outputs[0],f'day 5 example 1 from list should take an input and output it')
def test_Day5Part2Example4Input8Output1FromList(self): intcode = [3, 3, 1107, -1, 8, 3, 4, 3, 99] exampleComputer = IntcodeComputer(intcode) userInputLess = 5 with patch('builtins.print') as p: with patch('builtins.input', lambda *args: userInputLess): exampleComputer.run() self.assertEqual(1, exampleComputer.outputs[0])
def test_Day5Part2Example2Input5Output0FromList(self): intcode = [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8] exampleComputer = IntcodeComputer(intcode) userInputLess = 5 with patch('builtins.print') as p: with patch('builtins.input', lambda *args: userInputLess): exampleComputer.run() self.assertEqual(1, exampleComputer.outputs[0])
def test_takingStepUpperBoundPassedChangesIntcode(self): intcode = [] testComputer = IntcodeComputer(intcode) message = testComputer.takeStep(25) self.assertEqual(25,len(testComputer.intcode)) for i in range(0,25): with self.subTest(intcodeIndex= i): self.assertEqual(0,testComputer.intcode[i])
def test_day2Example1FromList(self): intcode = [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50] exampleComputer = IntcodeComputer(intcode) exampleComputer.run() self.assertEqual(70, exampleComputer.intcode[3], f'day 2 example 1 from list should have 70 at pos 3') self.assertEqual( 3500, exampleComputer.intcode[0], f'day 2 example 1 from list should have 3500 at pos 0')
def test_runningOpcode3WithInput5MessageLog(self): intcode = [3, 1, 99] userInput = 5 testComputer = IntcodeComputer(intcode) with patch('builtins.input', lambda *args: userInput): testComputer.run() self.assertEqual(2, len(testComputer.log)) self.assertEqual('opcode 3 at position 0 processed',testComputer.log[0]) self.assertEqual('opcode 99, exiting',testComputer.log[1])
def test_opcode6ImediateModeDontJump(self): intcode = [1106, 1, 7, 3, 0, 3, 0, 99] testComputer = IntcodeComputer(intcode) testComputer.inputs = [1, 5] testComputer.run() self.assertEqual(len(testComputer.log), 4) self.assertEqual(testComputer.log[0], f'opcode 6 at position 0 processed') self.assertEqual(testComputer.log[1], f'opcode 3 at position 3 processed') self.assertEqual(testComputer.log[2], f'opcode 3 at position 5 processed') self.assertEqual(testComputer.log[3], f'opcode 99, exiting')
def test_opcode3AutomaticModePausesIntcomputerTillInputIsgiven(self): intcode = [3, 1, 99] automaticTestComputer = IntcodeComputer(intcode, True) automaticTestComputer.run() self.assertFalse(automaticTestComputer.running) automaticTestComputer.automaticModeTakeInputAndUnpauze(5) self.assertEqual([3, 5, 99], automaticTestComputer.intcode) self.assertEqual(3, len(automaticTestComputer.log)) self.assertEqual('pausing for input at position 0', automaticTestComputer.log[0]) self.assertEqual('opcode 3 at position 0 processed', automaticTestComputer.log[1]) self.assertEqual('opcode 99, exiting', automaticTestComputer.log[2])
class Opcode99(unittest.TestCase): def setUp(self): intcode = [99, 99, 85] self.testComputer = IntcodeComputer(intcode) self.referenceMessage = 'opcode 99, exiting' def tearDown(self): self.testComputer = None def test_opcode99stopsTheComputer(self): self.testComputer.run() self.assertEqual(1,len(self.testComputer.log)) def test_opcode99logsTheMessage(self): self.testComputer.run() self.assertEqual(self.referenceMessage,self.testComputer.log[0])
class OpcodeUnknown(unittest.TestCase): def setUp(self): intcode = [11145, 91, 85] self.testComputer = IntcodeComputer(intcode) self.referenceMessage = 'Uknown opcode encountered, 45 at position 0, exiting' def tearDown(self): self.testComputer = None def test_takingStepUnknownOpcodePrintsMessage(self): with patch('builtins.print') as p: self.testComputer.takeStep() p.assert_called_with('unknown opcode 45 encountered at position 0') def test_takingStepUnknownOpcodeLogsMessage(self): with patch('builtins.print') as p: logMessage = self.testComputer.takeStep() self.assertEqual(self.referenceMessage, logMessage) def test_runningUnknownOpcodeStopsTheIntcodeComputer(self): with patch('builtins.print') as p: self.testComputer.run() self.assertEqual(1, len(self.testComputer.log)) def test_runningUnkownOpcodeLogsTheMessage(self): with patch('builtins.print') as p: self.testComputer.run() self.assertEqual(self.referenceMessage, self.testComputer.log[0])
def __init__(self, intcodeList): self.brain = Computer(intcodeList, automaticMode=True) self.screen = Map() screenDictionary = { 0: ' ', 1: 'W', 2: 'B', 3: '=', 4: 'O', 16: 'X', 34: 'X' } self.screenRepresentation = Representation('screen', screenDictionary) self.score = 0 self.ballX = 0 self.paddleX = 0
def test_takingStepOpcode2with_2_0_0_3ReturnsMessage(self): intcode = [2,0,0,3] testComputer = IntcodeComputer(intcode) message = testComputer.takeStep() self.assertEqual('opcode 2 at position 0 processed',message)
def test_takingStepOpcode2with_2_1_1_1ChangesIntcode(self): intcode = [2,1,1,1] testComputer = IntcodeComputer(intcode) testComputer.takeStep() self.assertEqual([2,1,1,1],testComputer.intcode)
def test_running1101_5_7_99ChangesIntcode(self): intcode = [1101,5,7,3,99] testComputer = IntcodeComputer(intcode) testComputer.run() self.assertEqual([1101, 5, 7, 12, 99],testComputer.intcode)
def test_step22201_2_2_1IntcodeChanges(self): intcode = [22201,2,2,1] testComputer = IntcodeComputer(intcode) testComputer.takeStep() self.assertEqual([22201, 4, 2, 1], testComputer.intcode)
def test_running1001_0_120_3_99ChangesIntcode(self): intcode = [1001, 0, 120, 3, 99] testComputer = IntcodeComputer(intcode) testComputer.run() self.assertEqual([1001, 0, 120, 1121, 99],testComputer.intcode)