Skip to content

Commit 53ea25f

Browse files
committed
Convert tests to Catch2
1 parent 912e19d commit 53ea25f

File tree

2 files changed

+132
-24
lines changed

2 files changed

+132
-24
lines changed

source-code/convolution/cpp/src/CMakeLists.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
add_subdirectory(convolution)
22

3-
add_executable(test_convolution.exe
3+
find_package(Catch2)
4+
5+
if(Catch2_FOUND)
6+
add_executable(test_convolution.exe
47
test_convolution.cpp)
5-
target_include_directories(test_convolution.exe
6-
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/convolution)
7-
target_link_libraries(test_convolution.exe
8-
PRIVATE convolution)
8+
target_link_libraries(test_convolution.exe
9+
PRIVATE Catch2::Catch2WithMain)
10+
target_include_directories(test_convolution.exe
11+
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/convolution)
12+
target_link_libraries(test_convolution.exe
13+
PRIVATE convolution)
14+
else()
15+
message(STATUS "Catch2 not found, tests will not be built")
16+
endif()
917

1018
add_executable(benchmark_convolution.exe
1119
benchmark_convolution.cpp)
Lines changed: 119 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,134 @@
1+
#define CATCH_CONFIG_MAIN
2+
#include <catch2/catch.hpp>
13
#include <convolution.h>
2-
#include <iostream>
3-
4-
int main() {
5-
// Create a 10x10 image
6-
Matrix image(10, 10);
7-
for (int i = 0; i < image.rows(); ++i) {
8-
for (int j = 0; j < image.cols(); ++j) {
9-
image(i, j) = 1.0;
4+
5+
TEST_CASE("3x3 kernel", "[convolution]") {
6+
const int input_size {5};
7+
const int kernel_size {3};
8+
9+
Matrix input(input_size, input_size);
10+
for (int i = 0; i < input.rows(); ++i) {
11+
for (int j = 0; j < input.cols(); ++j) {
12+
input(i, j) = 1.0;
1013
}
1114
}
1215

13-
// Print the image
14-
std::cout << image << std::endl;
16+
Matrix kernel(kernel_size, kernel_size);
17+
for (int i = 0; i < kernel.rows(); ++i) {
18+
for (int j = 0; j < kernel.cols(); ++j) {
19+
kernel(i, j) = 1.0;
20+
}
21+
}
1522

16-
// Create a 3x3 kernel
17-
Matrix kernel(3, 3);
23+
Matrix target(input_size, input_size);
24+
target(0, 0) = 4.0;
25+
target(0, 1) = 6.0;
26+
target(0, 2) = 6.0;
27+
target(0, 3) = 6.0;
28+
target(0, 4) = 4.0;
29+
target(1, 0) = 6.0;
30+
target(1, 1) = 9.0;
31+
target(1, 2) = 9.0;
32+
target(1, 3) = 9.0;
33+
target(1, 4) = 6.0;
34+
target(2, 0) = 6.0;
35+
target(2, 1) = 9.0;
36+
target(2, 2) = 9.0;
37+
target(2, 3) = 9.0;
38+
target(2, 4) = 6.0;
39+
target(3, 0) = 6.0;
40+
target(3, 1) = 9.0;
41+
target(3, 2) = 9.0;
42+
target(3, 3) = 9.0;
43+
target(3, 4) = 6.0;
44+
target(4, 0) = 4.0;
45+
target(4, 1) = 6.0;
46+
target(4, 2) = 6.0;
47+
target(4, 3) = 6.0;
48+
target(4, 4) = 4.0;
49+
50+
auto result = convolve(input, kernel);
51+
52+
REQUIRE(result == target);
53+
}
54+
55+
TEST_CASE("5x5 kernel", "[convolution]") {
56+
const int input_size {6};
57+
const int kernel_size {5};
58+
59+
Matrix input(input_size, input_size);
60+
for (int i = 0; i < input.rows(); ++i) {
61+
for (int j = 0; j < input.cols(); ++j) {
62+
input(i, j) = 1.0;
63+
}
64+
}
65+
66+
Matrix kernel(kernel_size, kernel_size);
1867
for (int i = 0; i < kernel.rows(); ++i) {
1968
for (int j = 0; j < kernel.cols(); ++j) {
2069
kernel(i, j) = 1.0;
2170
}
2271
}
2372

24-
// Print the kernel
25-
std::cout << kernel << std::endl;
73+
Matrix target(input_size, input_size);
74+
target(0, 0) = 9.0;
75+
target(0, 1) = 12.0;
76+
target(0, 2) = 15.0;
77+
target(0, 3) = 15.0;
78+
target(0, 4) = 12.0;
79+
target(0, 5) = 9.0;
80+
target(1, 0) = 12.0;
81+
target(1, 1) = 16.0;
82+
target(1, 2) = 20.0;
83+
target(1, 3) = 20.0;
84+
target(1, 4) = 16.0;
85+
target(1, 5) = 12.0;
86+
target(2, 0) = 15.0;
87+
target(2, 1) = 20.0;
88+
target(2, 2) = 25.0;
89+
target(2, 3) = 25.0;
90+
target(2, 4) = 20.0;
91+
target(2, 5) = 15.0;
92+
target(3, 0) = 15.0;
93+
target(3, 1) = 20.0;
94+
target(3, 2) = 25.0;
95+
target(3, 3) = 25.0;
96+
target(3, 4) = 20.0;
97+
target(3, 5) = 15.0;
98+
target(4, 0) = 12.0;
99+
target(4, 1) = 16.0;
100+
target(4, 2) = 20.0;
101+
target(4, 3) = 20.0;
102+
target(4, 4) = 16.0;
103+
target(4, 5) = 12.0;
104+
target(5, 0) = 9.0;
105+
target(5, 1) = 12.0;
106+
target(5, 2) = 15.0;
107+
target(5, 3) = 15.0;
108+
target(5, 4) = 12.0;
109+
target(5, 5) = 9.0;
110+
auto result = convolve(input, kernel);
111+
112+
REQUIRE(result == target);
113+
}
26114

27-
// Create a convolution object
28-
auto new_image = convolve(image, kernel);
115+
TEST_CASE("even-sized kernel error", "[convolution]") {
116+
const int input_size {5};
117+
const int kernel_size {4};
29118

30-
// Print the result
31-
std::cout << new_image << std::endl;
119+
Matrix input(input_size, input_size);
120+
for (int i = 0; i < input.rows(); ++i) {
121+
for (int j = 0; j < input.cols(); ++j) {
122+
input(i, j) = 1.0;
123+
}
124+
}
32125

33-
return 0;
126+
Matrix kernel(kernel_size, kernel_size);
127+
for (int i = 0; i < kernel.rows(); ++i) {
128+
for (int j = 0; j < kernel.cols(); ++j) {
129+
kernel(i, j) = 1.0;
130+
}
131+
}
132+
REQUIRE_THROWS_AS(convolve(input, kernel), std::invalid_argument);
34133
}
134+

0 commit comments

Comments
 (0)