-
Notifications
You must be signed in to change notification settings - Fork 2
/
sortingTest.py
75 lines (58 loc) · 2.46 KB
/
sortingTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python2.7
"""Sorting Simulation
This script is meant to simulate testing + grading of a LC3 programming Assignment
Assignment: sort a list of (student_id, grades) in place.
Note: sorted by grade in descending order
List Entries:
student_id: uint8_t (unique)
grade: uint8_t in [0,100]
Student Program Starts at x3000
Grade List starts at x3200
"""
import pylc3
import unittest
from LC3Helper import *
import random
import grader
# Helper Functions
def upperHalf(x):
return (x << 8) & 0xFF00
def lowerHalf(x):
return x & 0xFF
class SortingTest(unittest.TestCase):
#The Setup class is provided by unittest and is run before each test
def setUp(self):
self.sim = pylc3.simulator()
self.sim.load("sortscores.obj")
# print("Done Initializing Simulator")
def test_ProvidedInDocument(self):
gradeList = [upperHalf(23) | lowerHalf(78), \
upperHalf(10) | lowerHalf(91), \
upperHalf(56) | lowerHalf(5), \
upperHalf(2) | lowerHalf(87)]
self.sim.mem[0x3200:(0x3200 + len(gradeList))] = gradeList
self.sim.run()
progOutList = self.sim.mem[0x3200:(0x3200 + len(gradeList))]
#Python Sorts are guaranteed to be stable, so we can check if their output is correctly sorted
# in just one line :) (We dont care whether the student implemented stable vs unstable sort)
# Note, mem returns a pylc3.mem object, so we need to cast it to a list to print
Expected = progOutList # Sorted Sorts the list in place
sorted(Expected, key = lambda grade : grade & 0xFF, reverse=True)
self.assertEqual(Expected, progOutList)
# Generate A random Test
def test_random_1(self):
numStudents = 50
gradeList = [] #Declare a list
#Create the Grade list
for i in range(numStudents):
gradeList.append( upperHalf(i + 1) | lowerHalf(random.randint(0,100)))
#Populate the simulator memory and run
self.sim.mem[0x3200:(0x3200 + len(gradeList))] = gradeList
self.sim.run()
progOutList = self.sim.mem[0x3200:(0x3200 + len(gradeList))]
#Python Sorts are guaranteed to be stable, so we can check if their output is correctly sorted
# in just one line :) (We dont care whether the student implemented stable vs unstable sort)
Expected = progOutList # Sorted Sorts the list in place
sorted(Expected, key = lambda grade : grade & 0xFF, reverse=True)
self.assertEqual(Expected, progOutList)
grader.doTest(SortingTest)