added supporting files for assignment-1

This commit is contained in:
karthikmurakonda 2022-08-18 11:26:01 +05:30
parent 2da447a4fd
commit d130a8756e
20 changed files with 249 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,113 @@
#!/bin/python
import os
import shutil
import sys
import subprocess
from threading import Timer
def evaluate(submitted_program):
jarfile = "./emulator.jar"
testcases_directory = "./test_cases/"
l = len(submitted_program.split("/"))
program_name = submitted_program.split("/")[l-1].split("_")[0].split(".")[0]
total_marks = 0
scored_marks = 0
for cur_file in os.listdir(testcases_directory):
if program_name in cur_file and ".input" in cur_file:
total_marks = total_marks + 1
#find starting and ending address
expected_file = open(testcases_directory + cur_file.split(".")[0] + ".expected")
starting_address = "x"
ending_address = "x"
memory_required = False
for line in expected_file:
if memory_required == True:
if len(line.split(":")) > 1:
if starting_address == "x":
starting_address = line.split(":")[0].split()[0]
else:
ending_address = line.split(":")[0].split()[0]
if "Main Memory Contents" in line:
memory_required = True
expected_file.close()
if starting_address == "x":
starting_address = "0"
ending_address = "0"
if ending_address == "x":
ending_address = starting_address
#create new assembly file based on test case
new_assembly_file = open("./tmp.asm", 'w')
input_file = open(testcases_directory + cur_file)
for line in input_file:
new_assembly_file.write(line)
input_file.close()
text_encountered = False
submitted_file = open(submitted_program)
for line in submitted_file:
if ".text" in line:
text_encountered = True
if text_encountered == True:
new_assembly_file.write(line)
submitted_file.close()
new_assembly_file.close()
# spawn emulator
stdout_file = open("./tmp.output", 'w')
popen_args = ["java", "-Xmx1g", "-jar", jarfile, "./tmp.asm", starting_address, ending_address]
proc = subprocess.Popen(popen_args, stdout = stdout_file, stderr = stdout_file)
timer = Timer(5, proc.kill)
try:
timer.start()
stdout, stderr = proc.communicate()
finally:
timer.cancel()
stdout_file.close()
# evaluate against expected output
expected_file = open(testcases_directory + cur_file.split(".")[0] + ".expected")
result_file = open("./tmp.output")
expected_line = expected_file.readline()
first_line_found = False
evaluation = True
for line in result_file:
if first_line_found == True and line != expected_line:
evaluation = False
break
if expected_line == line:
first_line_found = True
expected_line = expected_file.readline()
if expected_line == None or expected_line == "":
break
if first_line_found == False:
evaluation = False
expected_file.close()
result_file.close()
if evaluation == True:
scored_marks = scored_marks + 1
print(cur_file + "\t: PASS!")
else:
print(cur_file + "\t: Fail")
#debug print
debug = False
if debug == True:
print("testcase = " + cur_file)
print("\n output expected to contain = ")
expected_file = open(testcases_directory + cur_file.split(".")[0] + ".expected")
for line in expected_file:
print(line)
expected_file.close()
print("\n obtained output = ")
output_file = open("./tmp.output")
for line in output_file:
print(line)
output_file.close()
os.remove("./tmp.asm")
os.remove("./tmp.output")
return [total_marks, scored_marks]

View File

@ -0,0 +1,16 @@
.data
a:
70
80
40
20
10
30
50
60
n:
8
.text
// your code here
// you may change the numbers in the array, and the size of the array; but allow the name of the array to remain as 'a', and size as 'n'
// remove these comments!

View File

@ -0,0 +1,8 @@
.data
a:
10
.text
// your code here to check if the number 'a' is even or odd. If yes, write 1 to x10. Else write -1.
// you may change the value of 'a'
// remove these comments!

View File

@ -0,0 +1,7 @@
.data
n:
10
.text
// your code here to get the first 'n' Fibonacci numbers
// you may change the value of 'n'
// remove these comments!

View File

@ -0,0 +1,7 @@
.data
a:
10
.text
// your code here to check if 'a' is a palindrome
// you may change the value of 'a'
// remove these comments!

View File

@ -0,0 +1,8 @@
.data
a:
10
.text
// your code here to check if the number 'a' is prime. If yes, write 1 to x10. Else write -1.
// you may change the value of 'a'
// remove these comments!

View File

@ -0,0 +1,10 @@
Main Memory Contents:
0 : 80
1 : 70
2 : 60
3 : 50
4 : 40
5 : 30
6 : 20
7 : 10

View File

@ -0,0 +1,12 @@
.data
a:
70
80
40
20
10
30
50
60
n:
8

View File

@ -0,0 +1 @@
x10 : -1

View File

@ -0,0 +1,3 @@
.data
a:
10

View File

@ -0,0 +1,12 @@
Main Memory Contents:
65526 : 34
65527 : 21
65528 : 13
65529 : 8
65530 : 5
65531 : 3
65532 : 2
65533 : 1
65534 : 1
65535 : 0

View File

@ -0,0 +1,3 @@
.data
n:
10

View File

@ -0,0 +1 @@
x10 : 1

View File

@ -0,0 +1,3 @@
.data
a:
12321

View File

@ -0,0 +1 @@
x10 : -1

View File

@ -0,0 +1,3 @@
.data
a:
10

View File

@ -0,0 +1,6 @@
#!/bin/python
import sys
from evaluate import evaluate
evaluate(sys.argv[1])

View File

@ -0,0 +1,35 @@
#!/bin/python
import sys
import os
import zipfile
from evaluate import evaluate
import shutil
zip_file = sys.argv[1]
l = len(zip_file.split("/"))
print("Students :\t" + zip_file.split("/")[l-1].split("_")[0] + " and " + zip_file.split("_")[1].split(".")[0])
print("")
submissions_temp_dir = "./submissions/"
if not os.path.exists(submissions_temp_dir):
os.mkdir(submissions_temp_dir)
zip_ref = zipfile.ZipFile(zip_file, 'r')
zip_ref.extractall(submissions_temp_dir)
zip_ref.close()
total_marks = 0
scored_marks = 0
for asm_file in os.listdir(submissions_temp_dir):
if ".asm in file":
[t,s] = evaluate(submissions_temp_dir + asm_file)
print(asm_file + ";\tscore = " + str(s) + " out of " + str(t))
total_marks = total_marks + t
scored_marks = scored_marks + s
shutil.rmtree(submissions_temp_dir)
print("total score = " + str(scored_marks) + " out of " + str(total_marks))