CMU 15-112 Spring 2019: Fundamentals of Programming and Computer Science
Check 1.12


  1. Check 1.12
    Add a test case to the function testTensDigit that makes the test function fail on the given function, tensDigit.
    def tensDigit(n): # Returns just the tens digit of the given number. return n // 10 def testTensDigit(): print("testing tensDigit...", end="") assert(tensDigit(12) == 1) # add your own test case here! print("passed!") testTensDigit() import sys def set_certificate(certificate_div_id, certificate): document[certificate_div_id].textContent = certificate def get_student_code(student_code_div_id): raw_student_code = document[student_code_div_id].textContent return window.patchCodeToCheckTimeout(raw_student_code, 'check_timeout();'); class captureIO: def __init__(self): self.captured = [] def get_output(self): out = "" for c in self.captured: out += str(c) return out def write(self, data): self.captured.append(data) def flush(self): pass def extract_student_function(student_code): extracted_function = "" adding = False for line in student_code.splitlines(): if adding: if len(line) > 0: if line[0].isspace(): extracted_function += line + "\n" else: break if line == "def testTensDigit():": extracted_function += line + "\n" adding = True return extracted_function def make_certificate(student_code_div_id, certificate_div_id): student_code = get_student_code(student_code_div_id) certificate = [] output = "" capture = captureIO() sys.stdout = capture sys.stderr = capture # Make sure that the tests crash on the first run try: exec(student_code) output = "no error" except AssertionError: output = "assertion error" except: output = "another error" certificate.append((output, type(output))) # Bring in the correct function to the namespace for the second run goodTensDigit = """\ def tensDigit(n): n = abs(n) return (n // 10) % 10 """ output = "" try: exec(goodTensDigit + extract_student_function(student_code)) testTensDigit() output = "no error" except AssertionError: output = "assertion error" except: output = "another error" certificate.append((output, type(output))) set_certificate(certificate_div_id, str(certificate))

  2. Back to notes