Skip to content

Commit

Permalink
fix(templates): add missing vector include and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
STommydx committed Apr 5, 2024
1 parent c692850 commit b3ace84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
3 changes: 2 additions & 1 deletion matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define MATRIX_HPP

#include <valarray>
#include <vector>

template <class T> class matrix;

Expand Down Expand Up @@ -68,7 +69,7 @@ template <class T> class matrix {
matrix(const std::vector<std::vector<T>> &v)
: n(v.size()), m(v.empty() ? 0 : v[0].size()), dat(n * m) {
for (size_t i = 0; i < n; ++i) {
std::ranges::copy(v[i], dat.begin() + i * m);
std::ranges::copy(v[i], begin(dat) + i * m);
}
}

Expand Down
17 changes: 17 additions & 0 deletions matrix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ TEST_CASE("matrix construction and member functions", "[matrix]") {
a >>= 3;
REQUIRE(a(0, 0) == 3);
}
SECTION("std::vector conversion") {
std::vector<std::vector<int>> v = a;
REQUIRE(v[0][0] == 1);
REQUIRE(v[1][0] == 1);
REQUIRE(v[1][1] == 1);
REQUIRE(v[0][1] == 1);
matrix<int> m = v;
REQUIRE(m(0, 0) == 1);
REQUIRE(m(1, 0) == 1);
REQUIRE(m(1, 1) == 1);
REQUIRE(m(0, 1) == 1);
std::vector<std::vector<int>> result = matrix<int>(v) + m;
REQUIRE(result[0][0] == 2);
REQUIRE(result[1][0] == 2);
REQUIRE(result[1][1] == 2);
REQUIRE(result[0][1] == 2);
}
}

TEST_CASE("matrix arithmetic operator overload", "[matrix]") {
Expand Down

0 comments on commit b3ace84

Please sign in to comment.