From 9541d8a0cbc2e2122b21a7898ef70e2bf299240e Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 3 Jun 2019 12:46:48 -0700 Subject: [PATCH] scala gradescope grader --- .../scala/grading/ExampleGraderScala.scala | 28 ++++++++++ src/test/scala/grading/GradedTest.scala | 51 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 src/test/scala/grading/ExampleGraderScala.scala create mode 100644 src/test/scala/grading/GradedTest.scala diff --git a/src/test/scala/grading/ExampleGraderScala.scala b/src/test/scala/grading/ExampleGraderScala.scala new file mode 100644 index 00000000..0394e864 --- /dev/null +++ b/src/test/scala/grading/ExampleGraderScala.scala @@ -0,0 +1,28 @@ +package dinocpu + +/** Example definition of graded test, runs a few tests from lab 1 + * + */ +object examplegrader { + def main(args: Array[String]): Unit = { + + //each test set appears as one test on gradescope, contains at least one InstTests + val sets = List[GradedTestSet]( + new GradedTestSet("Single Cycle Add",List[CPUTestCase](InstTests.nameMap("add1")),10,true,"single-cycle"), + new GradedTestSet("R-type single cycle",InstTests.rtype,10,true,"single-cycle"), + new GradedTestSet("R-type multicycle",InstTests.rtypeMultiCycle,10,true,"single-cycle") + ) + //json string to gradescope + var json: String = s"""{ "tests":[""" + var results = new scala.collection.mutable.ArrayBuffer[String]() + //runs each test in each set and appends result to json string + for(set <- sets){ + val result = set.runTests() + results += result.toJsonStr() + } + json += results.mkString(",") + //for now just print result + json += "]}" + print(json) + } +} \ No newline at end of file diff --git a/src/test/scala/grading/GradedTest.scala b/src/test/scala/grading/GradedTest.scala new file mode 100644 index 00000000..7226f8b9 --- /dev/null +++ b/src/test/scala/grading/GradedTest.scala @@ -0,0 +1,51 @@ +package dinocpu + +/** Defines a set of tests to run for grading, to appear as one test on gradescope + * + */ +class GradedTestSet(setName: String, tests: List[CPUTestCase], maxScore: Double, partialCredit: Boolean, cpu:String, branchPredictor:String = "") { + var testToRun: List[CPUTestCase] = tests + var totalScore: Double = maxScore + var pc: Boolean = partialCredit + var name: String = setName + var cpuType: String = cpu + var bp: String = branchPredictor + + /** runs all the tests defined in the set and computes the resulting score + * + * @return a GradedTestResult containing the output of the test + */ + def runTests(): GradedTestResult ={ + val result = new GradedTestResult(this) + for (test <- testToRun) { + if(CPUTesterDriver(test, cpuType, bp)){ + result.score += totalScore / testToRun.length + result.output += s"Passed ${test.binary}${test.extraName}\\n" + } else { + result.output += s"Failed ${test.binary}${test.extraName} \\n" + } + } + result.score = (result.score * 100).round / 100.0 + + if(result.score != totalScore && !pc) + result.score = 0 + return result + } +} + +/** Wraps the result of a graded test, to have a field requires for gradescope + * @param tests corresponding graded test of this result + */ +class GradedTestResult(tests: GradedTestSet) { + var score: Double = 0.0 + var name: String = tests.name + var max_score: Double = tests.totalScore + var output: String = "" + + /** returns JSON formated string containing result of test + * + */ + def toJsonStr(): String = { + return f"""{ "score": $score, "max_score": $max_score, "name": "$name", "output": "$output" }""" + } +} \ No newline at end of file