Skip to content
Snippets Groups Projects
Commit 2d2e037a authored by Morten Hannemose's avatar Morten Hannemose
Browse files

Removed future files

parent 270b5b75
Branches master
No related tags found
No related merge requests found
Showing
with 0 additions and 516 deletions
from unitgrade import Report
import cp
from unitgrade import UTestCase
from cp.ex02.taylor import evaluate_taylor
class HelloWorld2(UTestCase):
def test_say_hello(self):
from cp.ex00 import say_hello
evaluate_taylor(1)
self.assertEqual(0,0)
class Week07Tests(Report): #240 total.
title = "Tests for week 07"
version = 0.1
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
pack_imports = [cp]
individual_imports = []
questions = [
(HelloWorld2, 10),
]
if __name__ == '__main__':
from unitgrade import evaluate_report_student
evaluate_report_student(Week07Tests())
from unitgrade import Report
import cp
from unitgrade import UTestCase
from cp.ex02.taylor import evaluate_taylor
from cp.ex02.fibonacci import fibonacci_number
class HelloWorld2(UTestCase):
def test_say_hello(self):
from cp.ex00 import say_hello
evaluate_taylor(1)
self.assertEqual(0,0)
class Week08Tests(Report):
title = "Tests for week 08"
version = 0.1
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
pack_imports = [cp]
individual_imports = []
questions = [(HelloWorld2, 10),
]
if __name__ == '__main__':
from unitgrade import evaluate_report_student
evaluate_report_student(Week08Tests())
from unitgrade import Report, UTestCase
import unittest
from unittest.mock import patch
import cp
import io
class TestRectangleArea(UTestCase):
def test_area(self):
from cp.ex09.rectangle import area, Rectangle
r = Rectangle()
r.width = 10
r.height = 4
self.assertEqual(area(r), 40)
class TestMakeARectangle(UTestCase):
def test_make_a_rectangle(self):
from cp.ex09.rectangle import make_a_rectangle, Rectangle, Point
rectangle = make_a_rectangle(0, 0, 4, 5)
self.assertIsInstance(rectangle, Rectangle)
rectangle = make_a_rectangle(9, 2, 3, 7)
self.assertIsInstance(rectangle, Rectangle)
self.assertIsInstance(rectangle.corner, Point)
self.assertEqual(rectangle.corner.x, 9)
self.assertEqual(rectangle.corner.y, 2)
self.assertEqual(rectangle.width, 3)
self.assertEqual(rectangle.height, 7)
def test_rectangle(self):
from cp.ex09.rectangle import make_a_rectangle, Rectangle, Point, area
rectangle = make_a_rectangle(5, 3, 1, 1)
self.assertEqual(area(rectangle), 1)
rectangle = make_a_rectangle(0, 0, 20, 10)
self.assertEqual(area(rectangle), 200)
class TestSplitRectangle(UTestCase):
def setUp(self):
from cp.ex09.rectangle import make_a_rectangle
self.rectangle = make_a_rectangle(0, 0, 4, 6)
def test_split_horizontally(self):
from cp.ex09.rectangle import split_rectangle
split = split_rectangle(self.rectangle, True)
self.assertEqual(len(split), 2)
left_rectangle = split[0]
right_rectangle = split[1]
self.assertEqual(left_rectangle.width, 2)
self.assertEqual(left_rectangle.height, 6)
self.assertEqual(right_rectangle.width, 2)
self.assertEqual(right_rectangle.height, 6)
def test_split_vertically(self):
from cp.ex09.rectangle import split_rectangle
split = split_rectangle(self.rectangle, False)
self.assertEqual(len(split), 2)
top_rectangle = split[0]
bottom_rectangle = split[1]
self.assertEqual(top_rectangle.width, 4)
self.assertEqual(top_rectangle.height, 3)
self.assertEqual(bottom_rectangle.width, 4)
self.assertEqual(bottom_rectangle.height, 3)
class TestRectangleInception(UTestCase):
def test_inception_areas(self):
from cp.ex09.rectangle import make_a_rectangle, rectangle_inception, area
r = make_a_rectangle(2, 4, 12, 16)
rs = rectangle_inception(r, 4)
areas = [area(r_) for r_ in rs]
self.assertEqual(len(areas), 5)
self.assertEqual(areas, [96.0, 48.0, 24.0, 12.0, 12.0])
def test_inception_location(self):
from cp.ex09.rectangle import make_a_rectangle, Rectangle, rectangle_inception
rs = rectangle_inception(make_a_rectangle(2, 4, 12, 16), 4)
r = rs[-1]
self.assertIsInstance(r, Rectangle)
self.assertEqual(r.corner.x, 11)
self.assertEqual(r.corner.y, 16)
self.assertEqual(r.width, 3)
self.assertEqual(r.height, 4)
class TestMakeVector(UTestCase):
def test_make_vector(self):
from cp.ex09.vector import Vector, make_vector
v1 = make_vector(2,3)
self.assertIsInstance(v1, Vector, msg="Must return a Vector instance.")
self.assertEqual(v1.x, 2)
self.assertEqual(v1.y, 3)
@unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
def test_print_vector(self, mock_stdout):
from cp.ex09.vector import make_vector, print_vector
v1 = make_vector(2, 3)
print_vector(v1)
out = mock_stdout.getvalue().strip()
self.assertEqual(out, "(2, 3)")
class TestVectorOperations(UTestCase):
def test_dot_product(self):
from cp.ex09.vector import make_vector, dot
v1 = make_vector(2,3)
v2 = make_vector(4,5)
self.assertEqual(dot(v1, v2), 23)
def test_scale(self):
from cp.ex09.vector import make_vector, scale
v = make_vector(2,3)
s = 2
v_scaled = scale(s, v)
self.assertEqual(v_scaled.x, 4)
self.assertEqual(v_scaled.y, 6)
def test_add(self):
from cp.ex09.vector import make_vector, add
v1 = make_vector(2,3)
v2 = make_vector(4,5)
v_sum = add(v1, v2)
self.assertEqual(v_sum.x, 6)
self.assertEqual(v_sum.y, 8)
def test_subtract(self):
from cp.ex09.vector import make_vector, sub
v1 = make_vector(2,3)
v2 = make_vector(4,5)
v_sub = sub(v1, v2)
self.assertEqual(v_sub.x, -2)
self.assertEqual(v_sub.y, -2)
def test_hat(self):
from cp.ex09.vector import make_vector, hat
v = make_vector(2,3)
v_hat = hat(v)
self.assertEqual(v_hat.x, -3)
self.assertEqual(v_hat.y, 2)
class TestLineSegmentMethods(UTestCase):
def test_make_line_segment(self):
from cp.ex09.vector import make_vector, make_line_segment, LineSegment
p = make_vector(1, 2)
q = make_vector(3, 4)
l = make_line_segment(p, q)
self.assertIsInstance(l, LineSegment)
def test_point_on_line(self):
from cp.ex09.vector import make_vector, make_line_segment, Vector, point_on_line
p = make_vector(1, 2)
q = make_vector(3, 4)
l = make_line_segment(p, q)
self.assertIsInstance(point_on_line(l, 0), Vector)
self.assertEqual(point_on_line(l, 0).x, p.x)
self.assertEqual(point_on_line(l, 0).y, p.y)
self.assertEqual(point_on_line(l, 1).x, q.x)
self.assertEqual(point_on_line(l, 1).y, q.y)
def test_point_on_line_midpoint(self):
from cp.ex09.vector import make_vector, make_line_segment, point_on_line
p = make_vector(1, 2)
q = make_vector(3, 4)
l = make_line_segment(p, q)
self.assertEqual(point_on_line(l, .5).x, (p.x+q.x)/2)
self.assertEqual(point_on_line(l, .5).y, (p.y+q.y)/2)
class SquareWithLocationTests(UTestCase):
def test_make_square(self):
from cp.ex09.vector import SquareWithPosition, make_square_with_position
sq = make_square_with_position(1, 2, 3)
self.assertIsInstance(sq, SquareWithPosition)
def test_square_to_lines(self):
from cp.ex09.vector import make_square_with_position, square_to_lines, LineSegment, point_on_line
sq = make_square_with_position(1, 2, 3)
lines = square_to_lines(sq)
self.assertIsInstance(lines, list, msg="Must return a list")
self.assertEqual(len(lines), 4, msg="Must return 4 lines")
for l in lines:
self.assertIsInstance(l, LineSegment, msg="Must return 4 line segments")
tpl = lambda v: (v.x, v.y)
lmd = lambda l: tuple( set( (tpl(point_on_line(l, 0) ), tpl(point_on_line(l, 1) )) ) )
l2 = list( set( [lmd(l) for l in lines] ) )
self.assertEqual(l2[0], ((4,5), (1, 5)), msg="Problem with first line. It should connect points (4,5) -- (1, 5)")
self.assertEqual(l2[1], ((1, 2), (4, 2)))
self.assertEqual(l2[2], ((1, 2), (1, 5)))
self.assertEqual(l2[3], ((4, 5), (4, 2)))
class TestIntersection(UTestCase):
def test_do_they_intersect(self):
from cp.ex09.vector import make_vector, make_line_segment, do_they_intersect
l1 = make_line_segment(make_vector(1, 1), make_vector(4, 1))
l2 = make_line_segment(make_vector(2, -2), make_vector(2, 3))
self.assertTrue( do_they_intersect(l1, l2), msg="These lines should intersect")
self.assertTrue( do_they_intersect(l2, l1), msg="Reverse order; lines should still intersect")
l3 = make_line_segment(make_vector(2, -2), make_vector(-1, 3))
self.assertFalse(do_they_intersect(l1, l3), msg="These lines should not intersect")
self.assertFalse(do_they_intersect(l3, l1), msg="Reverse order; lines should still not intersect")
def test_intersect(self):
from cp.ex09.vector import make_vector, make_line_segment, intersect
l1 = make_line_segment(make_vector(1, 1), make_vector(4, 1))
l2 = make_line_segment(make_vector(2, -2), make_vector(2, 3))
isect = intersect(l1, l2)
self.assertAlmostEqual(isect.x,2)
self.assertAlmostEqual(isect.y, 1)
class GetAllIntersections(UTestCase):
def test_get_intersections_none(self):
from cp.ex09.vector import make_line_segment
from cp.ex09.vector import make_vector, make_square_with_position, get_intersections
sq = make_square_with_position(1, 1, 6)
l = make_line_segment(make_vector(-1, 2), make_vector(0, 0.5))
intersections = get_intersections(sq, l)
self.assertIsInstance(intersections, list)
self.assertEqual(len(intersections), 0)
def test_get_intersections_one(self):
from cp.ex09.vector import make_vector, make_square_with_position, get_intersections, make_line_segment
sq = make_square_with_position(1, 1, 6)
l = make_line_segment(make_vector(-1, 2), make_vector(4, 3))
intersections = get_intersections(sq, l)
self.assertEqual(len(intersections), 1)
self.assertAlmostEqual(intersections[0].x, 1)
self.assertAlmostEqual(intersections[0].y, 2.4)
def test_get_intersections_two(self):
from cp.ex09.vector import make_line_segment, make_vector, make_square_with_position, get_intersections
sq = make_square_with_position(1, 1, 6)
l = make_line_segment(make_vector(-1, 2), make_vector(9, 4))
ls = get_intersections(sq, l)
self.assertEqual(len(ls), 2)
ls = list(set([(ls[0].x, ls[0].y), (ls[1].x, ls[1].y)]))
self.assertAlmostEqual(ls[0][0], 1, msg="testing x-coordinate of first point")
self.assertAlmostEqual(ls[0][1], 2.4, msg="testing y-coordinate of first point")
self.assertAlmostEqual(ls[1][0], 7, msg="testing x-coordinate of second point")
self.assertAlmostEqual(ls[1][1], 3.6, msg="testing y-coordinate of second point")
class Week09Tests(Report):
title = "Tests for week 09"
version = 1.0
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
pack_imports = [cp]
individual_imports = []
questions = [
(TestRectangleArea, 10),
(TestMakeARectangle, 10),
(TestSplitRectangle,10),
(TestRectangleInception, 10),
(TestMakeVector, 10),
(TestVectorOperations, 10),
(TestLineSegmentMethods, 10),
(TestIntersection, 10),
(GetAllIntersections, 10),
]
if __name__ == '__main__':
from unitgrade import evaluate_report_student
evaluate_report_student(Week09Tests())
from unitgrade import UTestCase, Report
import cp
class TestPerson(UTestCase):
def test_init(self):
from cp.ex10.hospital import Person
self.assertRaises(ValueError, Person, "John Doe", 35, "z")
def test_str(self):
from cp.ex10.hospital import Person
person = Person("John Doe", 35, "m")
self.assertEqual(str(person), "John Doe, 35m")
class TestPatient(UTestCase):
# Test Patient class
def test_init(self):
from cp.ex10.hospital import Person, Patient
patient = Patient("John Doe", 35, "m", "headache")
self.assertIsInstance(patient, Person, msg="Patient should inherit from Person")
self.assertRaises(ValueError, Patient, "John Doe", 35, "headache", "male")
def test_can_be_treated(self):
from cp.ex10.hospital import Patient
patient = Patient("John Doe", 35, "m", "headache")
self.assertTrue(patient.can_be_treated())
patient = Patient("John Doe", 35, "m", "unknown illness")
self.assertFalse(patient.can_be_treated())
def test_str(self):
from cp.ex10.hospital import Patient
patient = Patient("John Doe", 35, "m", "headache")
self.assertEqual(str(patient), "John Doe, 35m: headache")
class TestDoctor(UTestCase):
# Test Doctor class
def test_init(self):
from cp.ex10.hospital import Person, Doctor, Patient
doctor = Doctor("Dr. Smith", 45, "f", "head")
self.assertIsInstance(doctor, Person, msg="Doctor should inherit from Person")
self.assertNotIsInstance(doctor, Patient, msg="Doctor should not inherit from Patient")
self.assertRaises(ValueError, Doctor, "John Doe", 35, "feet", "female")
def test_str(self):
from cp.ex10.hospital import Doctor
doctor = Doctor("Dr. Smith", 45, "f", "head")
self.assertEqual(str(doctor), "Dr. Smith, 45f. Specialization: head")
def test_can_doctor_treat(self):
from cp.ex10.hospital import Doctor, Patient
doctor = Doctor("Dr. Smith", 45, "f", "head")
patient = Patient("John Doe", 35, "m", "headache")
self.assertTrue(doctor.can_doctor_treat(patient))
patient = Patient("John Doe", 35, "m", "stepped on lego")
self.assertFalse(doctor.can_doctor_treat(patient))
def test_treatment_cost(self):
from cp.ex10.hospital import Doctor, Patient
doctor = Doctor("Dr. Smith", 45, "f", "head")
patient = Patient("John Doe", 35, "m", "headache")
self.assertEqual(doctor.treatment_cost(patient), 100)
patient = Patient("John Doe", 35, "m", "toothache")
self.assertEqual(doctor.treatment_cost(patient), 200)
class Week10Tests(Report):
title = "Tests for week 10"
version = 1.0
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
pack_imports = [cp]
individual_imports = []
questions = [
(TestPerson, 10),
(TestDoctor, 10),
(TestPatient, 10),
]
if __name__ == '__main__':
from unitgrade import evaluate_report_student
evaluate_report_student(Week10Tests())
from unitgrade import Report
import cp
from unitgrade import UTestCase
class Week11DotProducta(UTestCase):
def test_dot_product_a(self):
with self.capture() as c:
from cp.ex11.dot_product import dot_product_a
result = dot_product_a([1,2,3,4,5],[3,4,5,6,7])
self.assertEqual(result, 85.)
result = dot_product_a([2,3,7,8,1,12,5,2,9],[8,4,3,4,5,6,7,17,2])
self.assertEqual(result, 245.)
import numpy as np
class Week11DotProductb(UTestCase):
def test_dot_product_b(self):
with self.capture() as c:
from cp.ex11.dot_product import dot_product_b
result = dot_product_b(np.array([1,2,3,4,5]),np.array([3,4,5,6,7]))
self.assertEqual(result, 85.)
result = dot_product_b(np.array([2,3,7,8,1,12,5,2,9]),np.array([8,4,3,4,5,6,7, 17,2]))
self.assertEqual(result, 245.)
import numpy as np
class Week11BMICalc(UTestCase):
def test_calc_bmi(self):
with self.capture() as c:
from cp.ex11.BMI_analysis import calc_bmi
data = np.array([[1, 1.65, 63.4],
[2, 1.70, 87.1],
[3, 1.82, 93.7],
[4, 1.62, 105],
[5, 1.97, 61.3],
[6, 1.62, 78],
[7, 1.73, 99.6],
[8, 1.77, 81],
[9, 1.77, 110.7],
[10, 1.60, 55.2]])
result = calc_bmi(data)
expected_result = np.array([[1.0, 1.65, 63.4, 23.29, 'Normal weight'],
[2.0, 1.70, 87.1, 30.14, 'Obese'],
[3.0, 1.82, 93.7, 28.29, 'Overweight'],
[4.0, 1.62, 105.0, 40.01, 'Obese'],
[5.0, 1.97, 61.3, 15.8, 'Underweight'],
[6.0, 1.62, 78.0, 29.72, 'Overweight'],
[7.0, 1.73, 99.6, 33.28, 'Obese'],
[8.0, 1.77, 81.0, 25.85, 'Overweight'],
[9.0, 1.77, 110.7, 35.33, 'Obese'],
[10.0, 1.60, 55.2, 21.56, 'Normal weight']])
# Check each element of the arrays individually
for i in range(len(result)):
for j in range(len(result[i])):
self.assertEqual(result[i][j], expected_result[i][j])
class Week11StableMeasurements(UTestCase):
def test_stable_measurements(self):
with self.capture() as c:
from cp.ex11.Stable_measurements import stable_measurements
result = stable_measurements(np.array([4.3, 5.7, 5.1, 6.4, 7.9, 12.8]))
self.assertEqual(result.tolist(), [6.4, 7.9, 12.8])
result = stable_measurements(np.array([4.3, 5.7, 5.1, 8, 7.9, 12.8]))
self.assertEqual(result.tolist(),[])
result = stable_measurements(np.array([8, 7.9, 12.8, 14, 17.5, 18.1]))
self.assertEqual(result.tolist(),[12.8, 14, 17.5, 18.1])
class Week11Tests(Report): #30 total.
title = "Tests for week 11"
version = 0.1
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
pack_imports = [cp]
individual_imports = []
questions = [
(Week11DotProducta, 10),
(Week11DotProductb, 10),
(Week11BMICalc, 10),
(Week11StableMeasurements, 10),
]
if __name__ == '__main__':
from unitgrade import evaluate_report_student
evaluate_report_student(Week11Tests())
from unitgrade import Report
import cp
from unitgrade import UTestCase
from cp.ex02.taylor import evaluate_taylor
class HelloWorld(UTestCase):
def test_say_hello(self):
from cp.ex00 import say_hello
evaluate_taylor(2)
self.assertEqual(0,0)
class Week12Tests(Report): #240 total.
title = "Tests for week 12"
version = 0.1
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
pack_imports = [cp]
individual_imports = []
questions = [
(HelloWorld, 10),
]
if __name__ == '__main__':
from unitgrade import evaluate_report_student
evaluate_report_student(Week12Tests())
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment