Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
stones_tests.py 1.29 KiB
from unitgrade.framework import Report, UTestCase
from unitgrade.evaluate import evaluate_report_student
import stones
from stones import maximum_stones
# A fancy helper function to generate nicer-looking titles.
def trlist(x):
s = str(list(x))
if len(s) > 30:
s = s[:30] + "...]"
return s
class Stones(UTestCase):
""" Test of the Stones function """
def stest(self, W, stone_weights): # Helper function.
N = maximum_stones(W, stone_weights)
self.title = f"stones({W}, {trlist(stone_weights)}) = {N} ?"
self.assertEqualC(N)
def test_basecase(self):
""" Test the stones-example given in the homework """
N = maximum_stones(15, [2, 5, 3, 1, 8, 4, 5, 7])
self.assertEqual(N, 5) # Test that we can collect 5 stones.
def test_stones1(self):
self.stest(4, [4]) # One stone weighing 4 kg.
def test_stones2(self):
self.stest(4, [1, 4]) # should also give 1
def test_stones3(self):
self.stest(4, [4, 1]) # should also give 1
def test_stones4(self):
self.stest(13, [2, 5, 3, 1, 8, 4, 5, 7])
class StoneReport(Report):
title = "02105 week 2: Stone collection"
questions = [(Stones, 10),]
pack_imports = [stones]
if __name__ == "__main__":
evaluate_report_student(StoneReport())