Skip to content

Commit a5e641f

Browse files
committed
Fixed .h files and added tests for non-square matrices
1 parent d45252c commit a5e641f

File tree

6 files changed

+225
-20
lines changed

6 files changed

+225
-20
lines changed

src/adapters/raw_2d_array.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424

2525

2626
// Set of functions for two-dimensional raw (C-style) array.
27-
template <typename T, const unsigned int dimention>
28-
Matrix <T> convert_raw_2d_array_to_munkres_matrix (const T array [dimention][dimention]);
27+
template <typename T, const unsigned int dimention1, const unsigned int dimention2>
28+
Matrix <T> convert_raw_2d_array_to_munkres_matrix (const T array [dimention1][dimention2]);
2929

30-
template <typename T, const unsigned int dimention>
31-
void fill_raw_2d_array_from_munkres_matrix (T array [dimention][dimention], const Matrix <T> & matrix);
30+
template <typename T, const unsigned int dimention1, const unsigned int dimention2>
31+
void fill_raw_2d_array_from_munkres_matrix (T array [dimention1][dimention2], const Matrix <T> & matrix);
3232

33-
template <const unsigned int dimension>
34-
void solve(double m [dimension][dimension]);
33+
template <const unsigned int dimention1, const unsigned int dimention2>
34+
void solve(double m [dimention1][dimention2]);
3535

3636
#ifndef USE_EXPORT_KEYWORD
3737
#include "raw_2d_array.cpp"

src/adapters/std_2d_array.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424

2525

2626
// Set of functions for two-dimensional std::array.
27-
template <typename T, const unsigned int dimention>
28-
Matrix <T> convert_std_2d_array_to_munkres_matrix (const std::array <std::array <T, dimention>, dimention> & array);
27+
template <typename T, const unsigned int dimention1, const unsigned int dimention2>
28+
Matrix <T> convert_std_2d_array_to_munkres_matrix (const std::array <std::array <T, dimention2>, dimention1> & array);
2929

30-
template <typename T, const unsigned int dimention>
31-
void fill_std_2d_array_from_munkres_matrix (std::array <std::array <T, dimention>, dimention> & array, const Matrix <T> & matrix);
30+
template <typename T, const unsigned int dimention1, const unsigned int dimention2>
31+
void fill_std_2d_array_from_munkres_matrix (std::array <std::array <T, dimention2>, dimention1> & array, const Matrix <T> & matrix);
3232

33-
template <const unsigned int dimension>
34-
void solve(std::array <std::array <double, dimension>, dimension> &m);
33+
template <const unsigned int dimention1, const unsigned int dimention2>
34+
void solve(std::array <std::array <double, dimention2>, dimention1> &m);
3535

3636
#ifndef USE_EXPORT_KEYWORD
3737
#include "std_2d_array.cpp"

tests/adapters/boost_matrixtest.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,31 @@ TEST_F (Adapters_boost_matrix_Test, convert_boost_matrix_to_munkres_matrix_Succe
3939
}
4040
}
4141

42+
TEST_F (Adapters_boost_matrix_Test, convert_non_square_boost_matrix_to_munkres_matrix_Success)
43+
{
44+
// Arrange.
45+
constexpr unsigned int dimension1 {2};
46+
constexpr unsigned int dimension2 {3};
47+
boost::numeric::ublas::matrix <double> test_boost_matrix (dimension1, dimension2);
48+
// {1, 2, 3},
49+
// {4, 5, 6}
50+
test_boost_matrix (0, 0) = 1; test_boost_matrix (0, 1) = 2; test_boost_matrix (0, 2) = 3;
51+
test_boost_matrix (1, 0) = 4; test_boost_matrix (1, 1) = 5; test_boost_matrix (1, 2) = 6;
52+
const Matrix <double> etalon_matrix {
53+
{1, 2, 3},
54+
{4, 5, 6}
55+
};
4256

57+
// Act.
58+
const auto test_matrix = convert_boost_matrix_to_munkres_matrix <double> (test_boost_matrix);
59+
60+
// Assert.
61+
for (unsigned int row = 0; row < dimension1; ++row) {
62+
for (unsigned int col = 0; col < dimension2; ++col) {
63+
EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) );
64+
}
65+
}
66+
}
4367

4468
TEST_F (Adapters_boost_matrix_Test, fill_boost_matrix_from_munkres_matrix_Success)
4569
{
@@ -78,7 +102,38 @@ TEST_F (Adapters_boost_matrix_Test, fill_boost_matrix_from_munkres_matrix_Succes
78102
}
79103
}
80104

105+
TEST_F (Adapters_boost_matrix_Test, fill_non_square_boost_matrix_from_munkres_matrix_Success)
106+
{
107+
// Arrange.
108+
constexpr unsigned int dimension1 {2};
109+
constexpr unsigned int dimension2 {3};
110+
boost::numeric::ublas::matrix <double> test_boost_matrix (dimension1, dimension2);
111+
// {0, 0, 0},
112+
// {0, 0, 0}
113+
test_boost_matrix (0, 0) = 0; test_boost_matrix (0, 1) = 0; test_boost_matrix (0, 2) = 0;
114+
test_boost_matrix (1, 0) = 0; test_boost_matrix (1, 1) = 0; test_boost_matrix (1, 2) = 0;
115+
116+
boost::numeric::ublas::matrix <double> etalon_boost_matrix (dimension1, dimension2);
117+
// {1, 2, 3},
118+
// {4, 5, 6}
119+
etalon_boost_matrix (0, 0) = 1; etalon_boost_matrix (0, 1) = 2; etalon_boost_matrix (0, 2) = 3;
120+
etalon_boost_matrix (1, 0) = 4; etalon_boost_matrix (1, 1) = 5; etalon_boost_matrix (1, 2) = 6;
81121

122+
const Matrix <double> etalon_matrix {
123+
{1, 2, 3},
124+
{4, 5, 6}
125+
};
126+
127+
// Act.
128+
fill_boost_matrix_from_munkres_matrix <double> (test_boost_matrix, etalon_matrix);
129+
130+
// Assert.
131+
for (unsigned int row = 0; row < dimension1; ++row) {
132+
for (unsigned int col = 0; col < dimension2; ++col) {
133+
EXPECT_EQ (etalon_boost_matrix (row, col), test_boost_matrix (row, col) );
134+
}
135+
}
136+
}
82137

83138
TEST_F (Adapters_boost_matrix_Test, solve_boost_matrix_Success)
84139
{

tests/adapters/raw_2d_arraytest.cpp

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ TEST_F (Adapters_raw_2d_array_Test, convert_raw_2d_array_to_munkres_matrix_Succe
2727
};
2828

2929
// Act.
30-
const auto test_matrix = convert_raw_2d_array_to_munkres_matrix <double, dimension> (test_array);
30+
const auto test_matrix = convert_raw_2d_array_to_munkres_matrix <double, dimension, dimension> (test_array);
3131

3232
// Assert.
3333
for (unsigned int row = 0; row < dimension; ++row) {
@@ -37,7 +37,30 @@ TEST_F (Adapters_raw_2d_array_Test, convert_raw_2d_array_to_munkres_matrix_Succe
3737
}
3838
}
3939

40-
40+
TEST_F (Adapters_raw_2d_array_Test, convert_raw_2d_non_square_array_to_munkres_matrix_Success)
41+
{
42+
// Arrange.
43+
constexpr unsigned int dimension1 {2};
44+
constexpr unsigned int dimension2 {3};
45+
const double test_array [dimension1][dimension2] {
46+
{1, 2, 3},
47+
{4, 5, 6}
48+
};
49+
const Matrix <double> etalon_matrix {
50+
{1, 2, 3},
51+
{4, 5, 6}
52+
};
53+
54+
// Act.
55+
const auto test_matrix = convert_raw_2d_array_to_munkres_matrix <double, dimension1, dimension2> (test_array);
56+
57+
// Assert.
58+
for (unsigned int row = 0; row < dimension1; ++row) {
59+
for (unsigned int col = 0; col < dimension2; ++col) {
60+
EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) );
61+
}
62+
}
63+
}
4164

4265
TEST_F (Adapters_raw_2d_array_Test, fill_raw_2d_array_from_munkres_matrix_Success)
4366
{
@@ -60,7 +83,7 @@ TEST_F (Adapters_raw_2d_array_Test, fill_raw_2d_array_from_munkres_matrix_Succes
6083
};
6184

6285
// Act.
63-
fill_raw_2d_array_from_munkres_matrix <double, dimension> (test_array, etalon_matrix);
86+
fill_raw_2d_array_from_munkres_matrix <double, dimension, dimension> (test_array, etalon_matrix);
6487

6588
// Assert.
6689
for (unsigned int row = 0; row < dimension; ++row) {
@@ -70,7 +93,34 @@ TEST_F (Adapters_raw_2d_array_Test, fill_raw_2d_array_from_munkres_matrix_Succes
7093
}
7194
}
7295

73-
96+
TEST_F (Adapters_raw_2d_array_Test, fill_raw_2d_non_square_array_from_munkres_matrix_Success)
97+
{
98+
// Arrange.
99+
constexpr unsigned int dimension1 {2};
100+
constexpr unsigned int dimension2 {3};
101+
double test_array [dimension1][dimension2] {
102+
{0, 0, 0},
103+
{0, 0, 0}
104+
};
105+
const double etalon_array [dimension1][dimension2] {
106+
{1, 2, 3},
107+
{4, 5, 6}
108+
};
109+
const Matrix <double> etalon_matrix {
110+
{1, 2, 3},
111+
{4, 5, 6}
112+
};
113+
114+
// Act.
115+
fill_raw_2d_array_from_munkres_matrix <double, dimension1, dimension2> (test_array, etalon_matrix);
116+
117+
// Assert.
118+
for (unsigned int row = 0; row < dimension1; ++row) {
119+
for (unsigned int col = 0; col < dimension2; ++col) {
120+
EXPECT_EQ (etalon_array [row][col], test_array [row][col]);
121+
}
122+
}
123+
}
74124

75125
TEST_F (Adapters_raw_2d_array_Test, solve_raw_2d_array_Success)
76126
{
@@ -88,7 +138,7 @@ TEST_F (Adapters_raw_2d_array_Test, solve_raw_2d_array_Success)
88138
};
89139

90140
// Act.
91-
solve <dimension> (test_array);
141+
solve <dimension, dimension> (test_array);
92142

93143
// Assert.
94144
for (unsigned int row = 0; row < dimension; ++row) {

tests/adapters/std_2d_arraytest.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ TEST_F (Adapters_std_2d_array_Test, convert_std_2d_array_to_munkres_matrix_Succe
2727
};
2828

2929
// Act.
30-
const auto test_matrix = convert_std_2d_array_to_munkres_matrix <double, test_array.size ()> (test_array);
30+
const auto test_matrix = convert_std_2d_array_to_munkres_matrix <double, test_array.size(), test_array.size()> (test_array);
3131

3232
// Assert.
3333
for (unsigned int row = 0; row < dimension; ++row) {
@@ -37,7 +37,30 @@ TEST_F (Adapters_std_2d_array_Test, convert_std_2d_array_to_munkres_matrix_Succe
3737
}
3838
}
3939

40+
TEST_F (Adapters_std_2d_array_Test, convert_non_square_std_2d_array_to_munkres_matrix_Success)
41+
{
42+
// Arrange.
43+
constexpr unsigned int dimension1 {2};
44+
constexpr unsigned int dimension2 {3};
45+
const std::array <std::array <double, dimension2>, dimension1> test_array {{
46+
{1, 2, 3},
47+
{4, 5, 6}
48+
}};
49+
const Matrix <double> etalon_matrix {
50+
{1, 2, 3},
51+
{4, 5, 6}
52+
};
53+
54+
// Act.
55+
const auto test_matrix = convert_std_2d_array_to_munkres_matrix <double, test_array.size(), test_array[0].size()> (test_array);
4056

57+
// Assert.
58+
for (unsigned int row = 0; row < dimension1; ++row) {
59+
for (unsigned int col = 0; col < dimension2; ++col) {
60+
EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) );
61+
}
62+
}
63+
}
4164

4265
TEST_F (Adapters_std_2d_array_Test, fill_std_2d_array_from_munkres_matrix_Success)
4366
{
@@ -60,7 +83,7 @@ TEST_F (Adapters_std_2d_array_Test, fill_std_2d_array_from_munkres_matrix_Succes
6083
};
6184

6285
// Act.
63-
fill_std_2d_array_from_munkres_matrix <double, dimension> (test_array, etalon_matrix);
86+
fill_std_2d_array_from_munkres_matrix <double, dimension, dimension> (test_array, etalon_matrix);
6487

6588
// Assert.
6689
for (unsigned int row = 0; row < dimension; ++row) {
@@ -70,7 +93,34 @@ TEST_F (Adapters_std_2d_array_Test, fill_std_2d_array_from_munkres_matrix_Succes
7093
}
7194
}
7295

96+
TEST_F (Adapters_std_2d_array_Test, fill_non_square_std_2d_array_from_munkres_matrix_Success)
97+
{
98+
// Arrange.
99+
constexpr unsigned int dimension1 {2};
100+
constexpr unsigned int dimension2 {3};
101+
std::array <std::array <double, dimension2>, dimension1> test_array {{
102+
{0, 0, 0},
103+
{0, 0, 0}
104+
}};
105+
const std::array <std::array <double, dimension2>, dimension1> etalon_array {{
106+
{1, 2, 3},
107+
{4, 5, 6}
108+
}};
109+
const Matrix <double> etalon_matrix {
110+
{1, 2, 3},
111+
{4, 5, 6}
112+
};
73113

114+
// Act.
115+
fill_std_2d_array_from_munkres_matrix <double, dimension1, dimension2> (test_array, etalon_matrix);
116+
117+
// Assert.
118+
for (unsigned int row = 0; row < dimension1; ++row) {
119+
for (unsigned int col = 0; col < dimension2; ++col) {
120+
EXPECT_EQ (etalon_array [row][col], test_array [row][col]);
121+
}
122+
}
123+
}
74124

75125
TEST_F (Adapters_std_2d_array_Test, solve_std_2d_array_Success)
76126
{
@@ -88,7 +138,7 @@ TEST_F (Adapters_std_2d_array_Test, solve_std_2d_array_Success)
88138
}};
89139

90140
// Act.
91-
solve <dimension> (test_array);
141+
solve <dimension, dimension> (test_array);
92142

93143
// Assert.
94144
for (unsigned int row = 0; row < dimension; ++row) {

tests/adapters/std_2d_vectortest.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,30 @@ TEST_F (Adapters_std_2d_vector_Test, convert_std_2d_vector_to_munkres_matrix_Suc
3535
}
3636
}
3737

38+
TEST_F (Adapters_std_2d_vector_Test, convert_non_square_std_2d_vector_to_munkres_matrix_Success)
39+
{
40+
// Arrange.
41+
constexpr unsigned int dimension1 {2};
42+
constexpr unsigned int dimension2 {3};
43+
const std::vector <std::vector <double> > test_vector {{
44+
{1, 2, 3},
45+
{4, 5, 6}
46+
}};
47+
const Matrix <double> etalon_matrix {
48+
{1, 2, 3},
49+
{4, 5, 6}
50+
};
51+
52+
// Act.
53+
const auto test_matrix = convert_std_2d_vector_to_munkres_matrix <double> (test_vector);
3854

55+
// Assert.
56+
for (unsigned int row = 0; row < dimension1; ++row) {
57+
for (unsigned int col = 0; col < dimension2; ++col) {
58+
EXPECT_EQ (etalon_matrix (row, col), test_matrix (row, col) );
59+
}
60+
}
61+
}
3962

4063
TEST_F (Adapters_std_2d_vector_Test, fill_std_2d_vector_from_munkres_matrix_Success)
4164
{
@@ -68,7 +91,34 @@ TEST_F (Adapters_std_2d_vector_Test, fill_std_2d_vector_from_munkres_matrix_Succ
6891
}
6992
}
7093

94+
TEST_F (Adapters_std_2d_vector_Test, fill_non_square_std_2d_vector_from_munkres_matrix_Success)
95+
{
96+
// Arrange.
97+
constexpr unsigned int dimension1 {2};
98+
constexpr unsigned int dimension2 {3};
99+
std::vector <std::vector <double> > test_vector {{
100+
{0, 0, 0},
101+
{0, 0, 0}
102+
}};
103+
const std::vector <std::vector <double> > etalon_vector {{
104+
{1, 2, 3},
105+
{4, 5, 6}
106+
}};
107+
const Matrix <double> etalon_matrix {
108+
{1, 2, 3},
109+
{4, 5, 6}
110+
};
111+
112+
// Act.
113+
fill_std_2d_vector_from_munkres_matrix <double> (test_vector, etalon_matrix);
71114

115+
// Assert.
116+
for (unsigned int row = 0; row < dimension1; ++row) {
117+
for (unsigned int col = 0; col < dimension2; ++col) {
118+
EXPECT_EQ (etalon_vector [row][col], test_vector [row][col]);
119+
}
120+
}
121+
}
72122

73123
TEST_F (Adapters_std_2d_vector_Test, solve_std_2d_vector_Success)
74124
{

0 commit comments

Comments
 (0)