Skip to content

Commit

Permalink
Add a replay string test.
Browse files Browse the repository at this point in the history
  • Loading branch information
TricksterGuy committed May 22, 2022
1 parent 21f3a93 commit 3f7516e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
12 changes: 7 additions & 5 deletions pylc3/unittests/lc3_unit_test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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))
Expand All @@ -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)
42 changes: 32 additions & 10 deletions pylc3/unittests/lc3_unit_test_case_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = """
Expand Down Expand Up @@ -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 = """
Expand Down Expand Up @@ -1728,19 +1722,47 @@ 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)

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 ----
# -----------------------------------
Expand Down

0 comments on commit 3f7516e

Please sign in to comment.