From d1b3e57df82dda0e0c8ec1edd916a2285a1df35e Mon Sep 17 00:00:00 2001
From: "antran@caltech.edu" <antran@caltech.edu>
Date: Sat, 19 Nov 2022 07:33:54 +0000
Subject: [PATCH] Make test_script.py more helpful

---
 test_script.py | 73 +++++++++++++++++++++++++++++++++++---------------
 1 file changed, 52 insertions(+), 21 deletions(-)

diff --git a/test_script.py b/test_script.py
index 490c904..f7317bb 100644
--- a/test_script.py
+++ b/test_script.py
@@ -1,6 +1,7 @@
 from collections import namedtuple, defaultdict
 import argparse
 import os.path
+from textwrap import wrap
 
 parser = argparse.ArgumentParser(description="Grade Project04a")
 parser.add_argument("-s", "--stage", type=int, choices=[1,2,3,4], required=True, help="Grade this stage")
@@ -12,7 +13,7 @@ VM_SIZE = 768
 
 if not os.path.isfile("log.txt"):
     print("No output from run, did code compile?")
-    exit(1)
+    assert False
 
 def print_page_info(pg, owner, usable):
     print(f"      Page #: {pg} | Owner: {owner} | User-accessible: {usable}")
@@ -29,7 +30,7 @@ def parse_virtual_dump(s: str) :
     if len(result) != VM_SIZE:
         print("FAIL: Virtual memory dump is not the expected size")
         print("      Did the code finish running?")
-        exit(1)
+        assert False
     return result
 
 def confirm_physical_exhausted(pm):
@@ -39,7 +40,7 @@ def confirm_physical_exhausted(pm):
         print("      This error was thrown because your physical memory does")
         print("      not allocate physical frames from 0x0 to 0x200000,")
         print("      which it should if stage 4 was correctly implemented.")
-        exit(1)
+        assert False
 
 def confirm_virtual_allocation(pm):
     # Checks that physical address space is being used up for virtual allocation
@@ -49,7 +50,7 @@ def confirm_virtual_allocation(pm):
             print("      This error was thrown because your physical memory does")
             print("      not allocate physical frames from 0x0 to 0x187000,")
             print("      which it should if stage 3 was correctly implemented.")
-            exit(1)
+            assert False
 
 def confirm_kernel_isolation(vm):
     # Checks that all of the virtual address spaces have it such that
@@ -64,18 +65,18 @@ def confirm_kernel_isolation(vm):
                 print("      block at 0xB8000 did not point to the console or")
                 print("      it was not mapped as usable by the user process.")
                 print_page_info(i, p, u)
-                exit(1)
+                assert False
         elif i < KERNEL_BOUNDARY and u:
             print("FAIL: Kernel memory must not be accessible by user process")
             print("      This error was thrown because your virtual address")
             print("      blocks below the kernel boundary was marked as accessible")
             print("      by the user process.")
             print_page_info(i, p, u)
-            exit(1)
+            assert False
         elif p == "." and u:
             print("FAIL: Unmapped page is accessible")
             print_page_info(i, p, u)
-            exit(1)
+            assert False
 
 def confirm_process_isolation(vm, pid):
     # Check that beyond the kernel memory map, processes only pages mapped
@@ -88,7 +89,7 @@ def confirm_process_isolation(vm, pid):
             print(f"      blocks for the process {pid} below the kernel boundary")
             print(f"      was owned by someone else other than the process {pid}.")
             print_page_info(i, p, u)
-            exit(1)
+            assert False
 
 def confirm_stack_address_end(vm, pid):
     i = VM_SIZE - 1
@@ -99,7 +100,7 @@ def confirm_stack_address_end(vm, pid):
         print(f"      block at MEMSIZE_VIRTUAL - PAGESIZE is not mapped to")
         print(f"      a page owned by process {pid}.")
         print_page_info(i, p, u)
-        exit(1)
+        assert False
 
 def confirm_overlap(vms):
     # Check that there is overlap in virtual address spaces
@@ -115,7 +116,7 @@ def confirm_overlap(vms):
         print("      blocks below the kernel boundary did not overlap with")
         print("      other virtual address blocks of other processes, which it")
         print("      should if stage 4 was completed successfully.")
-        exit(1)
+        assert False
 
 def confirm_frequency(vcs):
     if vcs[0] >= vcs[1] or vcs[1] >= vcs[2] or vcs[2] >= vcs[3]:
@@ -123,7 +124,7 @@ def confirm_frequency(vcs):
         print("      This error was thrown because your virtual address blocks")
         print("      allocated for PID 1, 2, 3, 4 did not have it such that")
         print("      count1 < count2 < count3 < count4")
-        exit(1)
+        assert False
 
 def count_physical_pages_owners(pm):
     m = dict()
@@ -147,21 +148,27 @@ def confirm_counts(pcount, vmc, pid, delta):
         print(f"WARNING: number of pages mapped for virtual ({pcount[pid] - delta}) and physical ({vmc}) do not match for process {pid}")
         print(f"   NOTE: This may fail if you have implemented a later stage than the one you are testing for")
 
-# make NO_SLOWDOWN=1 TICK_LIMIT=100 run
-with open("log.txt") as f:
-    lines = [line.rstrip("\n") for line in f.readlines()]
+def read_log_file():
+    with open("log.txt") as f:
+        return [line.rstrip("\n") for line in f.readlines()]
+
+lines = read_log_file()
+try:        
+    # parsing
+    if (lines[0] != "Starting WeensyOS"
+        or lines[1] != "PHYSICAL MEMORY DUMP"
+        or lines[3] != "VIRTUAL MEMORY FOR PROCESS 1"
+        or lines[5] != "VIRTUAL MEMORY FOR PROCESS 2"
+        or lines[7] != "VIRTUAL MEMORY FOR PROCESS 3"
+        or lines[9] != "VIRTUAL MEMORY FOR PROCESS 4"):
+        print("FAIL: Testing output mismatch, did you leave in debugging log_printf in your code?")
+        assert False
     
     # parsing
-    assert lines[0] == "Starting WeensyOS"
-    assert lines[1] == "PHYSICAL MEMORY DUMP"
     pm = parse_physical_dump(lines[2])
-    assert lines[3] == "VIRTUAL MEMORY FOR PROCESS 1"
     vm1 = parse_virtual_dump(lines[4])
-    assert lines[5] == "VIRTUAL MEMORY FOR PROCESS 2"
     vm2 = parse_virtual_dump(lines[6])
-    assert lines[7] == "VIRTUAL MEMORY FOR PROCESS 3"
     vm3 = parse_virtual_dump(lines[8])
-    assert lines[9] == "VIRTUAL MEMORY FOR PROCESS 4"
     vm4 = parse_virtual_dump(lines[10])
 
     pcount = count_physical_pages_owners(pm)
@@ -170,6 +177,8 @@ with open("log.txt") as f:
     vm3c = count_virtual_pages(vm3, "3")
     vm4c = count_virtual_pages(vm4, "4")
 
+    print(f"Checking for stage {args.stage} completion:")
+
     if args.stage == 1:
         confirm_counts(pcount, vm1c, "1", 0)
         confirm_counts(pcount, vm2c, "2", 0)
@@ -212,4 +221,26 @@ with open("log.txt") as f:
         confirm_stack_address_end(vm4, "4")
         # confirm_frequency([vm1c, vm2c, vm3c, vm4c])
     
-    print(f"Tests up to stage {args.stage} passed!")
\ No newline at end of file
+    print(f"Tests up to stage {args.stage} passed!")
+except AssertionError:
+    print("Tests failed!")
+    # Dump out the memory view
+    print(lines[0])
+    print(lines[1])
+    for line in wrap(lines[2],64):
+        print(line)
+    print(lines[3])
+    for line in wrap(lines[4],128):
+        print(line)
+    print(lines[5])
+    for line in wrap(lines[6],128):
+        print(line)
+    print(lines[7])
+    for line in wrap(lines[8],128):
+        print(line)
+    print(lines[9])
+    for line in wrap(lines[10],128):
+        print(line)
+    exit(1)
+except BaseException as e:
+    print(f"Unknown exception: {e}")
\ No newline at end of file
-- 
GitLab