1 | import unittest |
---|
2 | import numpy as np |
---|
3 | |
---|
4 | from common.keyword_vector import * |
---|
5 | |
---|
6 | class TestKeywordMethods(unittest.TestCase): |
---|
7 | |
---|
8 | def test_read_dictionary(self): |
---|
9 | """ |
---|
10 | Create test for read_dictionary function |
---|
11 | """ |
---|
12 | print("Testing read_dictionary function:") |
---|
13 | |
---|
14 | print("Non-existant file") |
---|
15 | self.assertRaises(FileNotFoundError, read_dictionary, "asdfasdf") |
---|
16 | |
---|
17 | print("Reading default file") |
---|
18 | correct = True |
---|
19 | read_dictionary() |
---|
20 | print() |
---|
21 | |
---|
22 | def test_count_occurances(self): |
---|
23 | """ |
---|
24 | Create test for count_occurances function |
---|
25 | """ |
---|
26 | print("Testing count_occurances function:") |
---|
27 | |
---|
28 | dic = read_dictionary() |
---|
29 | print("Test non-existant file") |
---|
30 | self.assertRaises(FileNotFoundError, count_occurances, "asdfasdf", |
---|
31 | dic) |
---|
32 | |
---|
33 | print("Count occurances with None dictionary:") |
---|
34 | self.assertRaises(AssertionError, count_occurances, |
---|
35 | "../Singular/table.h", |
---|
36 | None) |
---|
37 | |
---|
38 | print("Count occurances with empty dictionary:") |
---|
39 | self.assertRaises(AssertionError, count_occurances, |
---|
40 | "../Singular/table.h", |
---|
41 | np.array([])) |
---|
42 | |
---|
43 | print("vector of ../Singular/table.h") |
---|
44 | vec = count_occurances("../Singular/table.h", dic) |
---|
45 | print(vec) |
---|
46 | print("Don't normalise") |
---|
47 | vec = count_occurances("../Singular/table.h", dic, False) |
---|
48 | self.assertGreater(np.linalg.norm(vec), 1) |
---|
49 | print() |
---|
50 | |
---|
51 | |
---|
52 | def test_create_vector_dictionary(self): |
---|
53 | """ |
---|
54 | Create test for create_vector_dictionary function |
---|
55 | """ |
---|
56 | print("Testing create_vector_dictionary function:") |
---|
57 | |
---|
58 | read_dictionary() |
---|
59 | |
---|
60 | print("Create Vector Dictionary with None as dictionary:") |
---|
61 | self.assertRaises(AssertionError, create_vector_dictionary, None) |
---|
62 | |
---|
63 | print("Create Vector Dictionary with empty dictionary:") |
---|
64 | self.assertRaises(AssertionError, |
---|
65 | create_vector_dictionary, |
---|
66 | np.array([])) |
---|
67 | print() |
---|
68 | |
---|
69 | def test_vector_distance(self): |
---|
70 | """ |
---|
71 | Create test for vector_distance function |
---|
72 | """ |
---|
73 | print("Testing vector_distance function:") |
---|
74 | |
---|
75 | vector1 = np.array([3, 4]) |
---|
76 | vector1 = normalise_vector(vector1) |
---|
77 | vector2 = np.array([4, 3]) |
---|
78 | vector2 = normalise_vector(vector2) |
---|
79 | |
---|
80 | print("Distance of vectors of different dimensions:") |
---|
81 | self.assertRaises(AssertionError, |
---|
82 | vector_distance, |
---|
83 | np.array([1, 2, 3]), |
---|
84 | vector1) |
---|
85 | |
---|
86 | print("Distance same vector: " + str(vector_distance(vector1, vector1))) |
---|
87 | self.assertEqual(vector_distance(vector1, vector1), 0) |
---|
88 | |
---|
89 | print("Distance different vector: " + str(vector_distance(vector1, vector2))) |
---|
90 | self.assertGreater(vector_distance(vector1, vector2), 0) |
---|
91 | print() |
---|
92 | |
---|
93 | def test_normalise_vector(self): |
---|
94 | """ |
---|
95 | Create test for normalise_vector function |
---|
96 | """ |
---|
97 | print("Testing normalise_vector function:") |
---|
98 | testvector = np.array([3, 4]) |
---|
99 | testvector = normalise_vector(testvector) |
---|
100 | self.assertEqual(np.linalg.norm(testvector), 1) |
---|
101 | |
---|
102 | print("Attempt to normalise the zero vector") |
---|
103 | vec = normalise_vector(np.array([0, 0, 0, 0, 0])) |
---|
104 | self.assertEqual(np.linalg.norm(vec), 0) |
---|
105 | |
---|
106 | print("Attempt to normalise list") |
---|
107 | vec = normalise_vector([3, 4, 0, 0, 0]) |
---|
108 | self.assertEqual(np.linalg.norm(vec), 1) |
---|
109 | |
---|
110 | print("Attempt to normalise empty vector") |
---|
111 | vec = normalise_vector(np.array([])) |
---|
112 | self.assertEqual(np.linalg.norm(vec), 0) |
---|
113 | |
---|
114 | print("Attempt to normalise None") |
---|
115 | vec = normalise_vector(None) |
---|
116 | self.assertEqual(np.linalg.norm(vec), 0) |
---|
117 | print() |
---|
118 | |
---|
119 | |
---|
120 | def test_copy_dictionary(self): |
---|
121 | dic = np.array(["hello", "bye"]) |
---|
122 | dic_copy = copy_dictionary(dic) |
---|
123 | self.assertTrue((dic == dic_copy).all()) |
---|
124 | |
---|
125 | |
---|
126 | def test_copy_vector(self): |
---|
127 | vec = np.array([1, 2, 3]) |
---|
128 | vec_copy = copy_vector(vec) |
---|
129 | self.assertTrue((vec == vec_copy).all()) |
---|
130 | |
---|
131 | |
---|
132 | if __name__ == '__main__': |
---|
133 | unittest.main() |
---|