Skip to content

Commit ce892ef

Browse files
committed
Added unit test for random search.
1 parent c75b95a commit ce892ef

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#! usr/bin/env python3
2+
3+
import random_search as rs
4+
5+
import unittest
6+
7+
class RandomSearchTests(unittest.TestCase):
8+
"""
9+
Unit tests for functions in random_search.py .
10+
"""
11+
12+
def test_objective_function(self):
13+
#integer
14+
self.assertEqual(99**2, rs.objective_function([99]))
15+
#float
16+
self.assertEqual(0.1**2.0, rs.objective_function([0.1]))
17+
#vector
18+
self.assertEqual((1**2) + (2**2) + (3**2), rs.objective_function([1,2,3]))
19+
20+
def test_random_vector(self):
21+
bounds, trials, size = [-3, 3], 300, 20
22+
minmax = [bounds for i in range(size)]
23+
24+
for i in range(trials):
25+
vector, total = rs.random_vector(minmax), 0.0
26+
self.assertEqual(size, len(vector))
27+
28+
for v in vector:
29+
self.assertTrue(v >= bounds[0])
30+
self.assertTrue(v < bounds[1])
31+
total += v
32+
33+
#TODO: test with total
34+
35+
if __name__ == "__main__":
36+
tests = unittest.TestLoader().loadTestsFromTestCase(RandomSearchTests)
37+
unittest.TextTestRunner(verbosity=2).run(tests)

0 commit comments

Comments
 (0)