Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Folder full of pertinent coursework

1666 views
1
from juliaset import JuliaSet
2
from random import uniform, randint
3
from math import sqrt
4
from nose import with_setup
5
6
###
7
# Test Suite for specified JuliaSet interface
8
#
9
# Run with the command: "nosetests juliatests.py"
10
###
11
12
13
# Custom random numbers
14
15
def rand_range():
16
"""Return a random complex number bounded by real and imaginary axes [-2, 2]"""
17
return (uniform(-2,2) + uniform(-2,2)*1j)
18
19
def rand_circle():
20
"""Return a random complex number within the unit circle"""
21
r = uniform(-1,1)
22
dr = sqrt(1 - r**2)
23
i = uniform(-dr, dr)
24
return (r + i*1j)
25
26
# Test classes for several cases
27
28
class TestRandomC:
29
"""Define a julia set with a random c seed value, test interface"""
30
31
def setup(self):
32
"""Setup fixture is run before every test method separately"""
33
self.c = rand_range()
34
self.n = randint(2,100)
35
self.j = JuliaSet(self.c, self.n)
36
37
def test_c_value(self):
38
"""Test that c is an attribute"""
39
assert self.j.c == self.c
40
41
def test_n_value(self):
42
"""Test that n is an attribute"""
43
assert self.j.n == self.n
44
45
def test_juliamap(self):
46
"""Test that juliamap is implemented properly"""
47
z = rand_range()
48
print "z = ", z
49
print "z**2 = ", z**2
50
zcorrect = z**2 + self.c
51
print "z**2 + c = ", zcorrect
52
znew = self.j.juliamap(z)
53
print "juliamap(z) = ", znew
54
assert znew == zcorrect
55
56
def test_set_spacing(self):
57
"""Test that changing spacing works"""
58
print "Test original spacing _d = 0.001"
59
assert self.j._d == 0.001
60
print "Test new spacing of _d = 0.1"
61
self.j.set_spacing(0.1)
62
print "_d = ", self.j._d
63
assert self.j._d == 0.1
64
print "Test that complex plane is regenerated"
65
print "len(_complexplane) = ", len(self.j._complexplane)
66
print "int(4.0 / 0.1)**2 = ", int(4.0 / 0.1)**2
67
assert len(self.j._complexplane) == int(4.0 / 0.1)**2
68
69
def test_generate(self):
70
"""Test that generating the julia set works"""
71
self.j.set_spacing(0.1)
72
self.j.generate()
73
print "Test that j.set exists, and is of the same length as j._complexplane"
74
assert (len(self.j.set) == len(self.j._complexplane))
75
76
class TestTrivial:
77
"""Test that a seed value of c=0 leaves the unit circle invariant"""
78
79
@classmethod
80
def setup_class(cls):
81
cls.j = JuliaSet(0)
82
83
def test_trivial_seed(self):
84
def check_z(z):
85
"""Test all z inside unit circle return 0"""
86
m = TestTrivial.j.iterate(z)
87
print "m = ", m
88
assert m == 0
89
# A generator like this runs a test for every yield
90
for _ in xrange(100):
91
z = rand_circle()
92
yield check_z, z
93
94
class TestHuge:
95
"""Test that a huge seed always causes a divergence after 1 iteration"""
96
97
@classmethod
98
def setup_class(cls):
99
cls.j = JuliaSet(16)
100
101
def test_huge_seed(self):
102
def check_z(z):
103
"""Test all z escape after 1 iteration"""
104
print "z = ", z
105
print "z**2 = ", z**2
106
print "z**2 + c = ", z**2 + 16
107
print "juliamap(z) = ", TestHuge.j.juliamap(z)
108
assert TestHuge.j.iterate(z) == 1
109
# Again, a generator runs a test for every yield
110
for _ in xrange(100):
111
z = rand_range()
112
yield check_z, z
113
114
115