Line data Source code
1 : /**
2 : * Unit tests for the Linear Algebra module's Vector operations
3 : */
4 : #include <chrono>
5 : #include <cstdint>
6 : #include <cstdlib>
7 : #include <gtest/gtest.h>
8 : #include <iostream>
9 : #include <openGPMP/linalg/vector.hpp>
10 : #include <random>
11 : #include <stdexcept>
12 : #include <vector>
13 :
14 : const double TOLERANCE = 1e-3;
15 :
16 : #define TEST_COUT std::cerr << "\033[32m[ ] [ INFO ] \033[0m"
17 : #define INFO_COUT \
18 : std::cerr << "\033[32m[ ] [ INFO ] \033[0m\033[1;34m\033[1m"
19 4 : TEST(VectorVectorTestDouble, Addition) {
20 1 : INFO_COUT << "Vector (as Vectors) NAIVE" << std::endl;
21 :
22 1 : std::vector<double> vec1 = {1.0, 2.0, 3.0};
23 1 : std::vector<double> vec2 = {4.0, 5.0, 6.0};
24 1 : std::vector<double> result;
25 :
26 1 : gpmp::linalg::std_vector_add(vec1, vec2, result);
27 :
28 1 : ASSERT_EQ(result.size(), 3);
29 1 : EXPECT_DOUBLE_EQ(result[0], 5.0);
30 1 : EXPECT_DOUBLE_EQ(result[1], 7.0);
31 1 : EXPECT_DOUBLE_EQ(result[2], 9.0);
32 1 : }
33 :
34 : // Test cases for gpmp::linalg::std_vector_sub function
35 4 : TEST(VectorVectorTestDouble, Subtraction) {
36 1 : std::vector<double> vec1 = {4.0, 5.0, 6.0};
37 1 : std::vector<double> vec2 = {1.0, 2.0, 3.0};
38 1 : std::vector<double> result;
39 :
40 1 : gpmp::linalg::std_vector_sub(vec1, vec2, result);
41 :
42 1 : ASSERT_EQ(result.size(), 3);
43 1 : EXPECT_DOUBLE_EQ(result[0], 3.0);
44 1 : EXPECT_DOUBLE_EQ(result[1], 3.0);
45 1 : EXPECT_DOUBLE_EQ(result[2], 3.0);
46 1 : }
47 :
48 : // Test cases for gpmp::linalg::std_scalar_mult function
49 4 : TEST(VectorVectorTestDouble, Multiplication) {
50 1 : std::vector<double> vec = {1.0, 2.0, 3.0};
51 1 : std::vector<double> result;
52 :
53 1 : gpmp::linalg::std_scalar_mult(vec, 2.0, result);
54 :
55 1 : ASSERT_EQ(result.size(), 3);
56 1 : EXPECT_DOUBLE_EQ(result[0], 2.0);
57 1 : EXPECT_DOUBLE_EQ(result[1], 4.0);
58 1 : EXPECT_DOUBLE_EQ(result[2], 6.0);
59 1 : }
60 :
61 : // Test cases for std_dot_product function
62 4 : TEST(VectorVectorTestDouble, DotProduct) {
63 1 : std::vector<double> vec1 = {1.0, 2.0, 3.0};
64 1 : std::vector<double> vec2 = {4.0, 5.0, 6.0};
65 :
66 1 : double result = gpmp::linalg::std_dot_product(vec1, vec2);
67 :
68 1 : EXPECT_DOUBLE_EQ(result, 32.0);
69 1 : }
70 :
71 : // Test cases for std_cross_product function
72 4 : TEST(VectorVectorTestDouble, CrossProduct) {
73 1 : std::vector<double> vec1 = {1.0, 2.0, 3.0};
74 1 : std::vector<double> vec2 = {3.0, 4.0, 5.0};
75 1 : std::vector<double> result(3);
76 :
77 1 : gpmp::linalg::std_cross_product(vec1, vec2, result);
78 :
79 1 : ASSERT_EQ(result.size(), 3);
80 1 : EXPECT_DOUBLE_EQ(result[0], -2);
81 1 : EXPECT_DOUBLE_EQ(result[1], 4);
82 1 : EXPECT_DOUBLE_EQ(result[2], -2);
83 1 : }
84 :
85 4 : TEST(VectorVectorTestDouble, Magnitude) {
86 1 : std::vector<double> vec = {3.0, 4.0};
87 1 : double norm = gpmp::linalg::std_vector_norm(vec);
88 1 : EXPECT_DOUBLE_EQ(norm, 5.0);
89 1 : }
90 :
91 : // Unit tests for gpmp::linalg::std_vector_normalize function
92 4 : TEST(VectorVectorTestDouble, Normalization) {
93 1 : std::vector<double> vec = {3.0, 4.0};
94 1 : std::vector<double> result;
95 1 : gpmp::linalg::std_vector_normalize(vec, result);
96 1 : EXPECT_DOUBLE_EQ(result[0], 0.6);
97 1 : EXPECT_DOUBLE_EQ(result[1], 0.8);
98 1 : }
99 :
100 : // Unit tests for gpmp::linalg::std_vector_projection function
101 4 : TEST(VectorVectorTestDouble, Projection) {
102 1 : std::vector<double> vec = {3.0, 2.0};
103 1 : std::vector<double> onto_vec = {4.0, 0.0};
104 1 : std::vector<double> result;
105 1 : gpmp::linalg::std_vector_projection(vec, onto_vec, result);
106 1 : EXPECT_DOUBLE_EQ(result[0], 3.0);
107 1 : EXPECT_DOUBLE_EQ(result[1], 0.0);
108 1 : }
109 :
110 4 : TEST(VectorVectorTestInt, Addition) {
111 1 : std::vector<int> vec1 = {1, 2, 3};
112 1 : std::vector<int> vec2 = {4, 5, 6};
113 1 : std::vector<int> result;
114 :
115 1 : gpmp::linalg::std_vector_add(vec1, vec2, result);
116 :
117 1 : ASSERT_EQ(result.size(), 3);
118 1 : EXPECT_EQ(result[0], 5);
119 1 : EXPECT_EQ(result[1], 7);
120 1 : EXPECT_EQ(result[2], 9);
121 1 : }
122 :
123 : // Unit tests for std_vector_sub function
124 4 : TEST(VectorVectorTestInt, Subtraction) {
125 1 : std::vector<int> vec1 = {4, 5, 6};
126 1 : std::vector<int> vec2 = {1, 2, 3};
127 1 : std::vector<int> result;
128 :
129 1 : gpmp::linalg::std_vector_sub(vec1, vec2, result);
130 :
131 1 : ASSERT_EQ(result.size(), 3);
132 1 : EXPECT_EQ(result[0], 3);
133 1 : EXPECT_EQ(result[1], 3);
134 1 : EXPECT_EQ(result[2], 3);
135 1 : }
136 :
137 : // Unit tests for std_scalar_mult function
138 4 : TEST(VectorVectorTestInt, Multiplication) {
139 1 : std::vector<int> vec = {1, 2, 3};
140 1 : std::vector<int> result;
141 :
142 1 : gpmp::linalg::std_scalar_mult(vec, 2, result);
143 :
144 1 : ASSERT_EQ(result.size(), 3);
145 1 : EXPECT_EQ(result[0], 2);
146 1 : EXPECT_EQ(result[1], 4);
147 1 : EXPECT_EQ(result[2], 6);
148 1 : }
149 :
150 : // Unit tests for std_dot_product function
151 4 : TEST(VectorVectorTestInt, DotProduct) {
152 1 : std::vector<int> vec1 = {1, 2, 3};
153 1 : std::vector<int> vec2 = {4, 5, 6};
154 :
155 1 : int result = gpmp::linalg::std_dot_product(vec1, vec2);
156 :
157 1 : EXPECT_EQ(result, 32);
158 1 : }
159 :
160 : // Unit tests for std_cross_product function
161 4 : TEST(VectorVectorTestInt, CrossProduct) {
162 1 : std::vector<int> vec1 = {1, 0, 0};
163 1 : std::vector<int> vec2 = {0, 1, 0};
164 1 : std::vector<int> result(3);
165 :
166 1 : gpmp::linalg::std_cross_product(vec1, vec2, result);
167 :
168 1 : ASSERT_EQ(result.size(), 3);
169 1 : EXPECT_EQ(result[0], 0);
170 1 : EXPECT_EQ(result[1], 0);
171 1 : EXPECT_EQ(result[2], 1);
172 1 : }
173 :
174 4 : TEST(VectorVectorTestInt, Magnitude) {
175 1 : std::vector<int> vec = {3, 4};
176 1 : double norm = gpmp::linalg::std_vector_norm(vec);
177 1 : EXPECT_DOUBLE_EQ(norm, 5.0);
178 1 : }
179 :
180 : // Unit tests for std_vector_normalize function
181 4 : TEST(VectorVectorTestInt, Normalization) {
182 1 : std::vector<int> vec = {3, 4};
183 1 : std::vector<double> result;
184 1 : gpmp::linalg::std_vector_normalize(vec, result);
185 1 : EXPECT_DOUBLE_EQ(result[0], 0.6);
186 1 : EXPECT_DOUBLE_EQ(result[1], 0.8);
187 1 : }
188 :
189 : // Unit tests for std_vector_projection function
190 4 : TEST(VectorVectorTestInt, Projection) {
191 1 : std::vector<int> vec = {3, 2};
192 1 : std::vector<int> onto_vec = {4, 0};
193 1 : std::vector<double> result;
194 1 : gpmp::linalg::std_vector_projection(vec, onto_vec, result);
195 1 : EXPECT_DOUBLE_EQ(result[0], 3.0);
196 1 : EXPECT_DOUBLE_EQ(result[1], 0.0);
197 1 : }
|