diff --git a/pylc3/unittests/lc3_unit_test_case.py b/pylc3/unittests/lc3_unit_test_case.py index 71f0d02..01d458e 100644 --- a/pylc3/unittests/lc3_unit_test_case.py +++ b/pylc3/unittests/lc3_unit_test_case.py @@ -1340,7 +1340,7 @@ def runCode(self, max_executions=DEFAULT_MAX_EXECUTIONS): for id in range(8): self.registers[id] = self.state.get_register(id) self.state.run(max_executions) - self.replay_msg = self._generateReplay() + self.replay_msg = self.generateReplayMessage() def _assertShortEqual(self, expected, actual, name, msg, level=AssertionType.soft, internal=False): """Helper to assert if two 16 bit values are equal.""" @@ -1994,7 +1994,7 @@ def trap_list(traps): self._internalAssert(name or 'trap calls made', len(self.expected_traps) == len(made_calls) and not missing_calls and not unknown_calls, status_message, level=level) - def _generateHeader(self, datablob): + def generateReplayHeader(self, datablob): header = six.BytesIO() header.write(b'lc-3') header.write(struct.pack('=I', REPLAY_STRING_VERSION_MAJOR)) @@ -2009,12 +2009,14 @@ def _generateHeader(self, datablob): header.close() return headerblob - - def _generateReplay(self): + def generateReplayString(self): preblob = self.preconditions.encode() postblob = self.postconditions.encode() datablob = preblob + postblob if self.enable_compression: datablob = zlib.compress(datablob, level = 9) - blob = base64.b64encode(self._generateHeader(datablob) + datablob) + return base64.b64encode(self._generateHeader(datablob) + datablob) + + def generateReplayMessage(self): + blob = self.generateReplayString() return "\nReplay string to emulate this test case in complx below:\n\n%s\n\nPlease include the FULL OUTPUT in text form (not a screenshot) from this autograder in questions to TA's/piazza\nframework v%d.%d\n" % (blob, REPLAY_STRING_VERSION_MAJOR, REPLAY_STRING_VERSION_MINOR) diff --git a/pylc3/unittests/lc3_unit_test_case_test.py b/pylc3/unittests/lc3_unit_test_case_test.py index 3c244de..944ffc7 100644 --- a/pylc3/unittests/lc3_unit_test_case_test.py +++ b/pylc3/unittests/lc3_unit_test_case_test.py @@ -32,12 +32,6 @@ def form_failure_message(): def tearDownClass(cls): pass - def generateReplay(self): - preblob = self.preconditions.encode() - postblob = self.postconditions.encode() - datablob = preblob + postblob - return base64.b64encode(self._generateHeader(datablob) + zlib.compress(datablob, level = 9 if self.enable_compression else 0)) - def loadCode(self, snippet): # This function is test only, Only use loadAsmFile for student code. self.state.loadCode(snippet) @@ -1506,7 +1500,7 @@ def testReplayStringPassByRegs(self): self.assertEqual(blob, expected_blob) print(self.preconditions.encode()) print(self.postconditions.encode()) - print(self.generateReplay()) + print(self.generateReplayString()) def testReplayString(self): snippet = """ @@ -1611,7 +1605,7 @@ def splitBlob(blob): print(self.preconditions.encode()) print(self.postconditions.encode()) - print(self.generateReplay()) + print(self.generateReplayString()) def testVerificationStringPassByRegs(self): snippet = """ @@ -1728,7 +1722,7 @@ def splitBlob(blob): def testGenerateHeaderCompression(self): self.asm_filename = 'test.asm' self.enable_compression = True - headerblob = self._generateHeader(b'') + headerblob = self.generateReplayHeader(b'') expected = b'lc-3\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x08\x00\x00\x00test.asm' self.assertEqual(headerblob, expected) @@ -1736,11 +1730,39 @@ def testGenerateHeaderCompression(self): def testGenerateHeaderNoCompression(self): self.asm_filename = 'test.asm' self.enable_compression = False - headerblob = self._generateHeader(b'') + headerblob = self.generateReplayHeader(b'') expected = b'lc-3\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00test.asm' self.assertEqual(headerblob, expected) + def testGenerateReplayString(self): + snippet = """ + .orig x3000 + TATA RET + .end + """ + self.loadCode(snippet) + self.asm_filename = 'test' + self.callSubroutine("TATA", {0: 3, 4: 5}) + + blob = self.preconditions.encode() + expected_preblob = b'\x07\x00\x80\x00\x00\x10\x19\x04\x00\x00\x00TATA\n\x00\x00\x00\x00\x00\x03\x00\x04\x00\x05\x00\x05\x00\xfe\xca\x06\x00\x00\xf0\x07\x00\x00\x80\xff' + self.assertEqual(blob, expected_preblob) + + blob = self.postconditions.encode() + expected_postblob = b'\xff' + self.assertEqual(blob, expected_postblob) + + header = self.generateReplayHeader(expected_preblob + expected_postblob) + expected_header = b'lc-3\x01\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00\x16\xd5A\x0b\x00\x04\x00\x00\x00test' + self.assertEqual(header, expected_header) + + expected_data = b'lc-3\x01\x00\x00\x00\x00\x00\x00\x00)\x00\x00\x00\x16\xd5A\x0b\x00\x04\x00\x00\x00test\x07\x00\x80\x00\x00\x10\x19\x04\x00\x00\x00TATA\n\x00\x00\x00\x00\x00\x03\x00\x04\x00\x05\x00\x05\x00\xfe\xca\x06\x00\x00\xf0\x07\x00\x00\x80\xff\xff' + replay = self.generateReplayString() + + self.assertEqual(base64.b64decode(replay), expected_data) + self.assertEqual(replay, base64.b64encode(expected_data)) + # ----------------------------------- # ---- Internal tests begin here ---- # -----------------------------------