Skip to content
Snippets Groups Projects
Commit 74b73ddb authored by tuhe's avatar tuhe
Browse files

Added blok 5 for review

parent 5566611e
Branches
No related tags found
No related merge requests found
Showing
with 294 additions and 81 deletions
This diff is collapsed.
from unitgrade import UTestCase, Report, hide
import math
class TestSineCosineWeek9(UTestCase):
def test_make_symbol(self):
from cp.ex09.sinus import make_symbol, Variable
v = make_symbol('x')
self.assertIsInstance(v, Variable)
def test_sine(self):
from cp.ex09.sinus import make_symbol, sine, Sin
v = make_symbol('x')
s = sine(v)
self.assertIsInstance(s, Sin)
self.assertEqual(s.arg, v)
def test_cosine(self):
from cp.ex09.sinus import make_symbol, cosine, Cos
v = make_symbol('x')
c = cosine(v)
self.assertIsInstance(c, Cos)
self.assertEqual(c.arg, v)
def test_format_expression(self):
from cp.ex09.sinus import make_symbol, sine, cosine, format_expression
v = make_symbol('x')
s = sine(v)
c = cosine(v)
self.assertEqual(format_expression(v), 'x')
self.assertEqual(format_expression(s), 'sin(x)')
self.assertEqual(format_expression(c), 'cos(x)')
# Test nested expressions
s2 = sine(c)
c2 = cosine(s)
self.assertEqual(format_expression(s2), 'sin(cos(x))')
self.assertEqual(format_expression(c2), 'cos(sin(x))')
class TestConstant(UTestCase):
def test_inheritance(self):
from cp.ex10.symbolic import Constant, Expression
c = Constant(7)
self.assertIsInstance(c, Expression)
def test_print(self):
from cp.ex10.symbolic import Constant
self.assertEqual(str(Constant(7)), "7")
self.assertEqual(str(Constant(0)), "0")
self.assertEqual(str(Constant(3.14)), "3.14")
def test_negative_print(self):
from cp.ex10.symbolic import Constant
self.assertEqual(str(Constant(-101)), "(-101)")
def test_evaluate(self):
from cp.ex10.symbolic import Constant
c = Constant(7)
self.assertEqual(c.evaluate({}), 7)
self.assertEqual(c.evaluate({"z": -10}), 7)
def test_derivative(self):
from cp.ex10.symbolic import Constant
c = Constant(7)
d = c.differentiate("x")
self.assertIsInstance(d, Constant)
self.assertEqual(d.evaluate({}), 0)
class TestVariable(UTestCase):
def test_inheritance(self):
from cp.ex10.symbolic import Variable, Expression
self.assertIsInstance(Variable("x"), Expression)
def test_print(self):
from cp.ex10.symbolic import Variable
self.assertEqual(str(Variable("x")), "x")
def test_evaluate(self):
from cp.ex10.symbolic import Variable
c = Variable("x")
self.assertEqual(c.evaluate({"x": 7}), 7)
def test_derivative(self):
from cp.ex10.symbolic import Variable, Constant
c = Variable("x")
d = c.differentiate("x")
self.assertIsInstance(d, Constant)
self.assertEqual(str(d), "1")
class TestAddition(UTestCase):
def test_inheritance(self):
from cp.ex10.symbolic import Addition, Constant, BinaryOperator
self.assertIsInstance(Addition(Constant(1), Constant(2)), BinaryOperator)
def test_print(self):
from cp.ex10.symbolic import Addition, Variable
self.assertEqual(str(Addition(Variable("x"), Variable("y"))), "(x + y)")
def test_evaluate(self):
from cp.ex10.symbolic import Addition, Constant, Variable
c = Addition(Variable("x"), Constant(3))
self.assertEqual(c.evaluate({"x": 7}), 10)
def test_derivative(self):
from cp.ex10.symbolic import Addition, Constant, Variable
c = Addition(Variable("x"), Constant(3))
d = c.differentiate("x")
self.assertEqual(str(d), "(1 + 0)")
self.assertEqual(d.evaluate({}), 1)
def test_derivative_nested(self):
from cp.ex10.symbolic import Addition, Constant, Variable
c = Addition(Variable("x"), Addition(Constant(3), Variable("x")))
d = c.differentiate("x")
self.assertEqual(str(d), "(1 + (0 + 1))")
class TestMultiplication(UTestCase):
def test_print(self):
from cp.ex10.symbolic import Multiplication, Constant, Variable
self.assertEqual(str(Multiplication(Variable("x"), Constant(3))), "x * 3")
def test_evaluate(self):
from cp.ex10.symbolic import Multiplication, Constant, Variable
self.assertEqual(Multiplication(Variable("x"), Constant(3)).evaluate({"x": 2}), 6)
def test_derivative(self):
from cp.ex10.symbolic import Multiplication, Constant, Variable
c = Multiplication(Variable("x"), Constant(3))
d = c.differentiate("x")
self.assertEqual(str(d), "(1 * 3 + x * 0)")
self.assertEqual(d.evaluate({"x": 10}), 3)
def test_derivative_nested(self):
from cp.ex10.symbolic import Multiplication, Constant, Variable, Addition
c = Multiplication(Variable("x"), Addition(Constant(3), Variable("x")))
d = c.differentiate("x")
self.assertEqual(str(d), "(1 * (3 + x) + x * (0 + 1))")
class TestTrigonometricBasic(UTestCase):
def test_sin_print(self):
from cp.ex10.symbolic import Variable, Sin
sin = Sin(Variable("x"))
self.assertEqual(str(sin), "sin(x)")
def test_cos_print(self):
from cp.ex10.symbolic import Constant, Cos
cos = Cos(Constant(3))
self.assertEqual(str(cos), "cos(3)")
def test_sin_evaluate(self):
from cp.ex10.symbolic import Variable, Sin
sin = Sin(Variable("x"))
self.assertAlmostEqual(sin.evaluate({"x": 2}), math.sin(2))
def test_cos_evaluate(self):
from cp.ex10.symbolic import Variable, Cos
self.assertAlmostEqual(Cos(Variable("x")).evaluate({"x": 2}), math.cos(2))
class TestEverything(UTestCase):
def test_everything(self):
from cp.ex10.symbolic import Variable, Cos, Multiplication, Sin, Addition, Constant
x = Variable("x")
y = Addition( Multiplication(Constant(3), Sin(x)), Cos(x))
str(y)
self.assertEqual(str(y), '(3 * sin(x) + cos(x))')
self.assertAlmostEqual(y.evaluate({"x": math.pi/2}), 3)
self.assertEqual(str(y.differentiate("x")), '((0 * sin(x) + 3 * cos(x) * 1) + (-1) * sin(x) * 1)')
class TestTrigonometricDerivative(UTestCase):
def test_sin(self):
from cp.ex10.symbolic import Variable, Sin
x = Variable("x")
dx = Sin(x).differentiate("x")
self.assertEqual(str(dx), "cos(x) * 1")
def test_cos(self):
from cp.ex10.symbolic import Variable, Cos
x = Variable("x")
dx = Cos(x).differentiate("x")
self.assertEqual(str(dx), "(-1) * sin(x) * 1")
def test_sin_advanced(self):
from cp.ex10.symbolic import Variable, Cos, Constant, Multiplication, Addition
x = Variable("x")
dx = Multiplication(x, Cos(Addition(x, Constant(3)))).differentiate("x")
self.assertEqual(str(dx), "(1 * cos((x + 3)) + x * (-1) * sin((x + 3)) * (1 + 0))")
class Project5(Report):
title = "Project 5"
remote_url = "https://cp.pages.compute.dtu.dk/02002public/_static/evaluation/"
abbreviate_questions = True
questions = [(TestSineCosineWeek9, 20),
(TestConstant, 10),
(TestVariable, 10),
(TestAddition, 10),
(TestMultiplication, 10),
(TestTrigonometricBasic, 10),
(TestTrigonometricDerivative, 10),
(TestEverything, 10),
]
import cp
pack_imports = [cp]
if __name__ == "__main__":
from unitgrade import evaluate_report_student
evaluate_report_student(Project5())
File added
File added
File added
File added
File added
File added
File added
File added
...@@ -3,36 +3,27 @@ import cp ...@@ -3,36 +3,27 @@ import cp
import io import io
import unittest import unittest
from unittest.mock import patch from unittest.mock import patch
import importlib
from cp.ex00 import say_hello
class HelloWorld2(UTestCase):
class SayHelloWorld(UTestCase):
@unittest.mock.patch('sys.stdout', new_callable=io.StringIO) @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
def test_say_hello2(self, mock_stdout): def test_say_hello(self, mock_stdout):
# from cp.ex00 import say_hello import cp.ex00.say_hello
lib = importlib.reload(say_hello)
lines = mock_stdout.getvalue().strip().splitlines() lines = mock_stdout.getvalue().strip().splitlines()
self.assertEqual(len(lines), 2, msg=f"Wrong number of lines. Your program should print(..) out two lines, you got: {lines}")
self.assertEqual(len(lines), 2, msg=f"Wrong number of lines. Your program should print(..) out two lines, you got: {lines}, {say_hello.__file__}")
print("Testing the first line") print("Testing the first line")
self.assertEqual(lines[0], "Hello", msg="You must have somehow changed the first print(...) statement in the program. Did you delete it?") # Rem self.assertEqual(lines[0], "Hello", msg="You must have somehow changed the first print(...) statement in the program. Did you delete it?") # Rem
print("Testing the second line") print("Testing the second line")
self.assertEqual(lines[1], "World", msg="Your second print statement did not produce the correct output.") # Remember the output must be capitalized. self.assertEqual(lines[1], "World", msg="Your second print statement did not produce the correct output.")
class Week00Tests(Report): class Week00Tests(Report):
title = "Tests for week 00 (Try to complete these before the course start)" title = "Tests for week 00 (Try to complete this before the course start)"
version = 1.0 version = 1.0
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests" url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
pack_imports = [cp] pack_imports = [cp]
individual_imports = [] individual_imports = []
questions = [ questions = [(SayHelloWorld, 10)]
(HelloWorld2, 10),
]
if __name__ == '__main__': if __name__ == '__main__':
from unitgrade import evaluate_report_student from unitgrade import evaluate_report_student
......
from unitgrade import Report from unitgrade import UTestCase, Report
import unittest.mock
import io
import cp import cp
from unitgrade import UTestCase
class Week03CompareNumbers(UTestCase): class Week03CompareNumbers(UTestCase):
def test_compare_numbers(self): def test_compare_numbers(self):
...@@ -70,9 +70,7 @@ class Week03BACCalculator(UTestCase): ...@@ -70,9 +70,7 @@ class Week03BACCalculator(UTestCase):
result = bac_calculator(0.021, 70., "female", 2.) result = bac_calculator(0.021, 70., "female", 2.)
self.assertEqual(result,0.020545454545454547) self.assertEqual(result,0.020545454545454547)
class AckermannTestCase(UTestCase): class AckermannTestCase(UTestCase):
def test_ackermann(self): def test_ackermann(self):
from cp.ex03.ackermann import ackermann from cp.ex03.ackermann import ackermann
self.assertEqual(ackermann(0, 0), 1) self.assertEqual(ackermann(0, 0), 1)
...@@ -86,11 +84,8 @@ class AckermannTestCase(UTestCase): ...@@ -86,11 +84,8 @@ class AckermannTestCase(UTestCase):
self.assertEqual(ackermann(3, 0), 5) self.assertEqual(ackermann(3, 0), 5)
self.assertEqual(ackermann(3, 1), 13) self.assertEqual(ackermann(3, 1), 13)
self.assertEqual(ackermann(3, 2), 29) self.assertEqual(ackermann(3, 2), 29)
# Add more test cases as needed
class ExponentialTestCase(UTestCase): class ExponentialTestCase(UTestCase):
def test_exponential_with_positive_power(self): def test_exponential_with_positive_power(self):
from cp.ex03.exponential import exponential from cp.ex03.exponential import exponential
self.assertEqual(exponential(2, 0), 1.0) self.assertEqual(exponential(2, 0), 1.0)
...@@ -98,7 +93,6 @@ class ExponentialTestCase(UTestCase): ...@@ -98,7 +93,6 @@ class ExponentialTestCase(UTestCase):
self.assertEqual(exponential(2, 2), 4.0) self.assertEqual(exponential(2, 2), 4.0)
self.assertEqual(exponential(3, 3), 27.0) self.assertEqual(exponential(3, 3), 27.0)
self.assertEqual(exponential(5, 4), 625.0) self.assertEqual(exponential(5, 4), 625.0)
# Add more test cases as needed
def test_exponential_with_negative_power(self): def test_exponential_with_negative_power(self):
from cp.ex03.exponential import exponential from cp.ex03.exponential import exponential
...@@ -106,14 +100,12 @@ class ExponentialTestCase(UTestCase): ...@@ -106,14 +100,12 @@ class ExponentialTestCase(UTestCase):
self.assertEqual(exponential(2, -2), 0.25) self.assertEqual(exponential(2, -2), 0.25)
self.assertAlmostEqual(exponential(3, -3), 0.037037037037) self.assertAlmostEqual(exponential(3, -3), 0.037037037037)
self.assertAlmostEqual(exponential(5, -4), 5**(-4) ) self.assertAlmostEqual(exponential(5, -4), 5**(-4) )
# Add more test cases as needed
def test_exponential_with_zero_power(self): def test_exponential_with_zero_power(self):
from cp.ex03.exponential import exponential from cp.ex03.exponential import exponential
self.assertEqual(exponential(2, 0), 1.0) self.assertEqual(exponential(2, 0), 1.0)
self.assertEqual(exponential(3, 0), 1.0) self.assertEqual(exponential(3, 0), 1.0)
self.assertEqual(exponential(5, 0), 1.0) self.assertEqual(exponential(5, 0), 1.0)
# Add more test cases as needed
class HeartAttackTests(UTestCase): class HeartAttackTests(UTestCase):
...@@ -129,9 +121,6 @@ class HeartAttackTests(UTestCase): ...@@ -129,9 +121,6 @@ class HeartAttackTests(UTestCase):
self.assertEqual(heart_attack(45, 70, True), "high") self.assertEqual(heart_attack(45, 70, True), "high")
self.assertEqual(heart_attack(11, 70, True), "high") self.assertEqual(heart_attack(11, 70, True), "high")
import unittest.mock
import io
class SolarPanelTests(UTestCase): class SolarPanelTests(UTestCase):
@unittest.mock.patch('sys.stdout', new_callable=io.StringIO) @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
def test_maybe(self, mock_stdout): def test_maybe(self, mock_stdout):
...@@ -171,7 +160,7 @@ class SolarPanelTests(UTestCase): ...@@ -171,7 +160,7 @@ class SolarPanelTests(UTestCase):
self.assertEqual(out.strip().lower(), "sure") self.assertEqual(out.strip().lower(), "sure")
class Week03Tests(Report): #40 total. class Week03Tests(Report):
title = "Tests for week 03" title = "Tests for week 03"
version = 1.0 version = 1.0
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests" url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
......
from unitgrade import Report from unitgrade import UTestCase, Report
import cp
from unitgrade import UTestCase
import unittest import unittest
from unittest.mock import patch
import cp
import io import io
import unittest
from unittest.mock import patch, call
class Palindrome(UTestCase): class Palindrome(UTestCase):
def test_is_palindrome(self): def test_is_palindrome(self):
...@@ -18,7 +15,6 @@ class Palindrome(UTestCase): ...@@ -18,7 +15,6 @@ class Palindrome(UTestCase):
class Bug(UTestCase): class Bug(UTestCase):
@unittest.mock.patch('sys.stdout', new_callable=io.StringIO) @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
def test_nice_sign(self, mock_stdout): def test_nice_sign(self, mock_stdout):
from cp.ex04.bug import nice_sign from cp.ex04.bug import nice_sign
...@@ -49,8 +45,8 @@ class Bug(UTestCase): ...@@ -49,8 +45,8 @@ class Bug(UTestCase):
self.assertEqual(l2, "| I have written my last bug |") self.assertEqual(l2, "| I have written my last bug |")
self.assertEqual(l3, "------------------------------") self.assertEqual(l3, "------------------------------")
class Math(UTestCase):
class Math(UTestCase):
def test_square_root(self): def test_square_root(self):
from cp.ex04.mathematics import square_root from cp.ex04.mathematics import square_root
self.assertAlmostEqual(square_root(9), 3, places=3) self.assertAlmostEqual(square_root(9), 3, places=3)
...@@ -62,7 +58,6 @@ class Math(UTestCase): ...@@ -62,7 +58,6 @@ class Math(UTestCase):
class Parenthesis(UTestCase): class Parenthesis(UTestCase):
def test_matching(self): def test_matching(self):
from cp.ex04.parenthesis import matching from cp.ex04.parenthesis import matching
self.assertTrue(matching('3x(y+x)')) self.assertTrue(matching('3x(y+x)'))
...@@ -105,6 +100,7 @@ class Dialogue(UTestCase): ...@@ -105,6 +100,7 @@ class Dialogue(UTestCase):
out = mock_stdout.getvalue() out = mock_stdout.getvalue()
self.assertEqual(out.strip(), "How are you doing?\nStill stuck on my 02002 programming problems") self.assertEqual(out.strip(), "How are you doing?\nStill stuck on my 02002 programming problems")
class Prefix(UTestCase): class Prefix(UTestCase):
def test_common_prefix(self): def test_common_prefix(self):
from cp.ex04.prefix import common_prefix from cp.ex04.prefix import common_prefix
...@@ -125,7 +121,6 @@ class Week04Tests(Report): ...@@ -125,7 +121,6 @@ class Week04Tests(Report):
version = 1.0 version = 1.0
url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests" url = "https://gitlab.compute.dtu.dk/cp/02002students/-/blob/master/cp/tests"
pack_imports = [cp] pack_imports = [cp]
questions = [ questions = [
(Math, 10), (Math, 10),
(Palindrome, 10), (Palindrome, 10),
......
from unitgrade import Report from unitgrade import Report, UTestCase
import cp
from unitgrade import UTestCase
import unittest import unittest
from cp.ex09.vector import Vector, make_vector, print_vector, dot, scale, add, sub, hat, make_line_segment, point_on_line, LineSegment from unittest.mock import patch
# from cp.ex09.vector import make_vector, make_square_with_position, square_to_lines, intersect, do_they_intersect, get_intersections import cp
from cp.ex09.vector import make_vector, make_square_with_position, square_to_lines, intersect, do_they_intersect, get_intersections
from cp.ex09.vector import make_line_segment
import io import io
from unittest.mock import patch, call
class TestRectangleArea(UTestCase): class TestRectangleArea(UTestCase):
def test_area(self): def test_area(self):
...@@ -44,7 +39,7 @@ class TestMakeARectangle(UTestCase): ...@@ -44,7 +39,7 @@ class TestMakeARectangle(UTestCase):
class TestSplitRectangle(UTestCase): class TestSplitRectangle(UTestCase):
def setUp(self): def setUp(self):
from cp.ex09.rectangle import make_a_rectangle, Rectangle, Point from cp.ex09.rectangle import make_a_rectangle
self.rectangle = make_a_rectangle(0, 0, 4, 6) self.rectangle = make_a_rectangle(0, 0, 4, 6)
def test_split_horizontally(self): def test_split_horizontally(self):
...@@ -78,15 +73,16 @@ class TestSplitRectangle(UTestCase): ...@@ -78,15 +73,16 @@ class TestSplitRectangle(UTestCase):
class TestRectangleInception(UTestCase): class TestRectangleInception(UTestCase):
def test_inception_areas(self): def test_inception_areas(self):
from cp.ex09.rectangle import make_a_rectangle, Rectangle, Point, rectangle_inception, area from cp.ex09.rectangle import make_a_rectangle, rectangle_inception, area
r = make_a_rectangle(2, 4, 12, 16) r = make_a_rectangle(2, 4, 12, 16)
rs = rectangle_inception(r, 4) rs = rectangle_inception(r, 4)
areas = [area(r_) for r_ in rs] areas = [area(r_) for r_ in rs]
self.assertEqual(len(areas), 5) self.assertEqual(len(areas), 5)
self.assertEqual(areas, [96.0, 48.0, 24.0, 12.0, 12.0]) self.assertEqual(areas, [96.0, 48.0, 24.0, 12.0, 12.0])
def test_inception_location(self): def test_inception_location(self):
from cp.ex09.rectangle import make_a_rectangle, Rectangle, Point, rectangle_inception, area from cp.ex09.rectangle import make_a_rectangle, Rectangle, rectangle_inception
rs = rectangle_inception(make_a_rectangle(2, 4, 12, 16), 4) rs = rectangle_inception(make_a_rectangle(2, 4, 12, 16), 4)
r = rs[-1] r = rs[-1]
self.assertIsInstance(r, Rectangle) self.assertIsInstance(r, Rectangle)
...@@ -96,8 +92,9 @@ class TestRectangleInception(UTestCase): ...@@ -96,8 +92,9 @@ class TestRectangleInception(UTestCase):
self.assertEqual(r.height, 4) self.assertEqual(r.height, 4)
class TestMakeVector(UTestCase): class TestMakeVector(UTestCase):
def test_make_vector(self): def test_make_vector(self):
from cp.ex09.vector import Vector, make_vector
v1 = make_vector(2,3) v1 = make_vector(2,3)
self.assertIsInstance(v1, Vector, msg="Must return a Vector instance.") self.assertIsInstance(v1, Vector, msg="Must return a Vector instance.")
self.assertEqual(v1.x, 2) self.assertEqual(v1.x, 2)
...@@ -105,6 +102,8 @@ class TestMakeVector(UTestCase): ...@@ -105,6 +102,8 @@ class TestMakeVector(UTestCase):
@unittest.mock.patch('sys.stdout', new_callable=io.StringIO) @unittest.mock.patch('sys.stdout', new_callable=io.StringIO)
def test_print_vector(self, mock_stdout): def test_print_vector(self, mock_stdout):
from cp.ex09.vector import make_vector, print_vector
v1 = make_vector(2, 3) v1 = make_vector(2, 3)
print_vector(v1) print_vector(v1)
out = mock_stdout.getvalue().strip() out = mock_stdout.getvalue().strip()
...@@ -112,11 +111,13 @@ class TestMakeVector(UTestCase): ...@@ -112,11 +111,13 @@ class TestMakeVector(UTestCase):
class TestVectorOperations(UTestCase): class TestVectorOperations(UTestCase):
def test_dot_product(self): def test_dot_product(self):
from cp.ex09.vector import make_vector, dot
v1 = make_vector(2,3) v1 = make_vector(2,3)
v2 = make_vector(4,5) v2 = make_vector(4,5)
self.assertEqual(dot(v1, v2), 23) self.assertEqual(dot(v1, v2), 23)
def test_scale(self): def test_scale(self):
from cp.ex09.vector import make_vector, scale
v = make_vector(2,3) v = make_vector(2,3)
s = 2 s = 2
v_scaled = scale(s, v) v_scaled = scale(s, v)
...@@ -124,6 +125,7 @@ class TestVectorOperations(UTestCase): ...@@ -124,6 +125,7 @@ class TestVectorOperations(UTestCase):
self.assertEqual(v_scaled.y, 6) self.assertEqual(v_scaled.y, 6)
def test_add(self): def test_add(self):
from cp.ex09.vector import make_vector, add
v1 = make_vector(2,3) v1 = make_vector(2,3)
v2 = make_vector(4,5) v2 = make_vector(4,5)
v_sum = add(v1, v2) v_sum = add(v1, v2)
...@@ -131,6 +133,7 @@ class TestVectorOperations(UTestCase): ...@@ -131,6 +133,7 @@ class TestVectorOperations(UTestCase):
self.assertEqual(v_sum.y, 8) self.assertEqual(v_sum.y, 8)
def test_subtract(self): def test_subtract(self):
from cp.ex09.vector import make_vector, sub
v1 = make_vector(2,3) v1 = make_vector(2,3)
v2 = make_vector(4,5) v2 = make_vector(4,5)
v_sub = sub(v1, v2) v_sub = sub(v1, v2)
...@@ -138,6 +141,7 @@ class TestVectorOperations(UTestCase): ...@@ -138,6 +141,7 @@ class TestVectorOperations(UTestCase):
self.assertEqual(v_sub.y, -2) self.assertEqual(v_sub.y, -2)
def test_hat(self): def test_hat(self):
from cp.ex09.vector import make_vector, hat
v = make_vector(2,3) v = make_vector(2,3)
v_hat = hat(v) v_hat = hat(v)
self.assertEqual(v_hat.x, -3) self.assertEqual(v_hat.x, -3)
...@@ -146,6 +150,7 @@ class TestVectorOperations(UTestCase): ...@@ -146,6 +150,7 @@ class TestVectorOperations(UTestCase):
class TestLineSegmentMethods(UTestCase): class TestLineSegmentMethods(UTestCase):
def test_make_line_segment(self): def test_make_line_segment(self):
from cp.ex09.vector import make_vector, make_line_segment, LineSegment
p = make_vector(1, 2) p = make_vector(1, 2)
q = make_vector(3, 4) q = make_vector(3, 4)
l = make_line_segment(p, q) l = make_line_segment(p, q)
...@@ -153,6 +158,7 @@ class TestLineSegmentMethods(UTestCase): ...@@ -153,6 +158,7 @@ class TestLineSegmentMethods(UTestCase):
def test_point_on_line(self): 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) p = make_vector(1, 2)
q = make_vector(3, 4) q = make_vector(3, 4)
l = make_line_segment(p, q) l = make_line_segment(p, q)
...@@ -164,33 +170,30 @@ class TestLineSegmentMethods(UTestCase): ...@@ -164,33 +170,30 @@ class TestLineSegmentMethods(UTestCase):
self.assertEqual(point_on_line(l, 1).y, q.y) self.assertEqual(point_on_line(l, 1).y, q.y)
def test_point_on_line_midpoint(self): 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) p = make_vector(1, 2)
q = make_vector(3, 4) q = make_vector(3, 4)
l = make_line_segment(p, q) 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).x, (p.x+q.x)/2)
self.assertEqual(point_on_line(l, .5).y, (p.y+q.y)/2) self.assertEqual(point_on_line(l, .5).y, (p.y+q.y)/2)
from cp.ex09.vector import SquareWithPosition
class SquareWithLocationTests(UTestCase): class SquareWithLocationTests(UTestCase):
def test_make_square(self): def test_make_square(self):
from cp.ex09.vector import SquareWithPosition, make_square_with_position
sq = make_square_with_position(1, 2, 3) sq = make_square_with_position(1, 2, 3)
self.assertIsInstance(sq, SquareWithPosition) self.assertIsInstance(sq, SquareWithPosition)
def test_square_to_lines(self): 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) sq = make_square_with_position(1, 2, 3)
lines = square_to_lines(sq) lines = square_to_lines(sq)
self.assertIsInstance(lines, list, msg="Must return a list") self.assertIsInstance(lines, list, msg="Must return a list")
self.assertEqual(len(lines), 4, msg="Must return 4 lines") self.assertEqual(len(lines), 4, msg="Must return 4 lines")
for l in lines: for l in lines:
self.assertIsInstance(l, LineSegment, msg="Must return 4 line segments") self.assertIsInstance(l, LineSegment, msg="Must return 4 line segments")
# get all end-points:
tpl = lambda v: (v.x, v.y) tpl = lambda v: (v.x, v.y)
lmd = lambda l: tuple( set( (tpl(point_on_line(l, 0) ), tpl(point_on_line(l, 1) )) ) ) 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] ) ) 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[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[1], ((1, 2), (4, 2)))
self.assertEqual(l2[2], ((1, 2), (1, 5))) self.assertEqual(l2[2], ((1, 2), (1, 5)))
...@@ -198,7 +201,7 @@ class SquareWithLocationTests(UTestCase): ...@@ -198,7 +201,7 @@ class SquareWithLocationTests(UTestCase):
class TestIntersection(UTestCase): class TestIntersection(UTestCase):
def test_do_they_intersect(self): def test_do_they_intersect(self):
from cp.ex09.vector import make_vector, make_line_segment, do_they_intersect, intersect, print_vector 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)) l1 = make_line_segment(make_vector(1, 1), make_vector(4, 1))
l2 = make_line_segment(make_vector(2, -2), make_vector(2, 3)) l2 = make_line_segment(make_vector(2, -2), make_vector(2, 3))
...@@ -211,7 +214,7 @@ class TestIntersection(UTestCase): ...@@ -211,7 +214,7 @@ class TestIntersection(UTestCase):
self.assertFalse(do_they_intersect(l3, l1), msg="Reverse order; lines should still not intersect") self.assertFalse(do_they_intersect(l3, l1), msg="Reverse order; lines should still not intersect")
def test_intersect(self): def test_intersect(self):
from cp.ex09.vector import make_vector, make_line_segment, do_they_intersect, intersect, print_vector from cp.ex09.vector import make_vector, make_line_segment, intersect
l1 = make_line_segment(make_vector(1, 1), make_vector(4, 1)) l1 = make_line_segment(make_vector(1, 1), make_vector(4, 1))
l2 = make_line_segment(make_vector(2, -2), make_vector(2, 3)) l2 = make_line_segment(make_vector(2, -2), make_vector(2, 3))
isect = intersect(l1, l2) isect = intersect(l1, l2)
...@@ -220,6 +223,8 @@ class TestIntersection(UTestCase): ...@@ -220,6 +223,8 @@ class TestIntersection(UTestCase):
class GetAllIntersections(UTestCase): class GetAllIntersections(UTestCase):
def test_get_intersections_none(self): 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) sq = make_square_with_position(1, 1, 6)
l = make_line_segment(make_vector(-1, 2), make_vector(0, 0.5)) l = make_line_segment(make_vector(-1, 2), make_vector(0, 0.5))
intersections = get_intersections(sq, l) intersections = get_intersections(sq, l)
...@@ -227,8 +232,7 @@ class GetAllIntersections(UTestCase): ...@@ -227,8 +232,7 @@ class GetAllIntersections(UTestCase):
self.assertEqual(len(intersections), 0) self.assertEqual(len(intersections), 0)
def test_get_intersections_one(self): def test_get_intersections_one(self):
# from cp.ex09.vector import make_vector, make_line_segment, make_square_with_position, get_intersections from cp.ex09.vector import make_vector, make_square_with_position, get_intersections, make_line_segment
# from cp.ex09.vector import plot_point, plot_line, plot_square
sq = make_square_with_position(1, 1, 6) sq = make_square_with_position(1, 1, 6)
l = make_line_segment(make_vector(-1, 2), make_vector(4, 3)) l = make_line_segment(make_vector(-1, 2), make_vector(4, 3))
intersections = get_intersections(sq, l) intersections = get_intersections(sq, l)
...@@ -237,13 +241,11 @@ class GetAllIntersections(UTestCase): ...@@ -237,13 +241,11 @@ class GetAllIntersections(UTestCase):
self.assertAlmostEqual(intersections[0].y, 2.4) self.assertAlmostEqual(intersections[0].y, 2.4)
def test_get_intersections_two(self): def test_get_intersections_two(self):
# from cp.ex09.vector import make_vector, make_line_segment, make_square_with_position, get_intersections from cp.ex09.vector import make_line_segment, make_vector, make_square_with_position, get_intersections
# from cp.ex09.vector import plot_point, plot_line, plot_square
sq = make_square_with_position(1, 1, 6) sq = make_square_with_position(1, 1, 6)
l = make_line_segment(make_vector(-1, 2), make_vector(9, 4)) l = make_line_segment(make_vector(-1, 2), make_vector(9, 4))
ls = get_intersections(sq, l) ls = get_intersections(sq, l)
self.assertEqual(len(ls), 2) self.assertEqual(len(ls), 2)
ls = list(set([(ls[0].x, ls[0].y), (ls[1].x, ls[1].y)])) 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][0], 1, msg="testing x-coordinate of first point")
...@@ -252,10 +254,6 @@ class GetAllIntersections(UTestCase): ...@@ -252,10 +254,6 @@ class GetAllIntersections(UTestCase):
self.assertAlmostEqual(ls[1][0], 7, msg="testing x-coordinate of second 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") self.assertAlmostEqual(ls[1][1], 3.6, msg="testing y-coordinate of second point")
class Week09Tests(Report): class Week09Tests(Report):
title = "Tests for week 09" title = "Tests for week 09"
version = 1.0 version = 1.0
......
from unitgrade import Report from unitgrade import UTestCase, Report
import cp import cp
from unitgrade import UTestCase
class TestPerson(UTestCase): class TestPerson(UTestCase):
# Test Person class
def test_init(self): def test_init(self):
from cp.ex10.hospital import Person from cp.ex10.hospital import Person
# person = Person("John Doe", 35, "m")
# self.assertEqual(person.name, "John Doe")
# self.assertEqual(person.age, 35)
# self.assertEqual(person.gender, "m")
self.assertRaises(ValueError, Person, "John Doe", 35, "z") self.assertRaises(ValueError, Person, "John Doe", 35, "z")
def test_str(self): def test_str(self):
...@@ -69,8 +62,6 @@ class TestDoctor(UTestCase): ...@@ -69,8 +62,6 @@ class TestDoctor(UTestCase):
patient = Patient("John Doe", 35, "m", "toothache") patient = Patient("John Doe", 35, "m", "toothache")
self.assertEqual(doctor.treatment_cost(patient), 200) self.assertEqual(doctor.treatment_cost(patient), 200)
class Week10Tests(Report): class Week10Tests(Report):
title = "Tests for week 10" title = "Tests for week 10"
version = 1.0 version = 1.0
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment