| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- //
- // matrix_tc.h
- // math
- //
- // Created by Sam Jaffe on 8/18/16.
- //
- #pragma once
- #include <cxxtest/TestSuite.h>
- #include "matrix.hpp"
- #include "matrix_helpers.hpp"
- class matrix_TestSuite : public CxxTest::TestSuite {
- public:
- using matr2i = math::matrix::matrix<int, 2, 2>;
- using matr2 = math::matrix::matrix<double, 2, 2>;
- public:
- void test_matrix_equals() const {
- using math::matrix::identity;
- TS_ASSERT_EQUALS((identity<int, 2>()), (matr2i{{1,0},{0,1}}));
- }
-
- void test_matrix_sum() const {
- auto iden = math::matrix::identity<int, 2>();
- auto result = matr2i{{2,0},{0,2}};
- TS_ASSERT_EQUALS(iden + iden, result);
- }
-
- void test_matrix_default_zero() const {
- auto zero = matr2i{{0,0},{0,0}};
- TS_ASSERT_EQUALS(matr2i{}, zero);
- }
- void test_matrix_subtract() const {
- auto zero = matr2i{{0,0},{0,0}};
- auto iden = math::matrix::identity<int, 2>();
- TS_ASSERT_EQUALS(iden-iden, zero);
- }
-
- // void test_matrix_negate() const {
- // auto iden = math::matrix::identity<int, 2>();
- // TS_ASSERT_EQUALS(-iden, (matr2i{{-1,0},{0,-1}}));
- // }
-
- void test_matrix_scaling() const {
- auto iden = math::matrix::identity<double, 2>();
- TS_ASSERT_EQUALS(2*iden, (matr2{{2.0,0.0},{0.0,2.0}}));
- TS_ASSERT_EQUALS(iden*2, (matr2{{2.0,0.0},{0.0,2.0}}));
- TS_ASSERT_EQUALS(iden/2.0, (matr2{{0.5,0.0},{0.0,0.5}}));
- }
-
- void test_matrix_multiplication_same_dim() const {
- auto A = matr2i{{1,2},{2,3}};
- auto B = matr2i{{1,0},{1,1}};
- TS_ASSERT_EQUALS(A*B, (matr2i{{3,2},{5,3}}));
- TS_ASSERT_EQUALS(B*A, (matr2i{{1,2},{3,5}}));
- }
-
- void test_matrix_multiplication_diff_dim() const {
- auto A = math::matrix::matrix<int, 3, 2>{{1,0},{0,1},{1,1}};
- auto B = math::matrix::matrix<int, 2, 3>{{0,1,0},{1,0,1}};
- TS_ASSERT_EQUALS(A*B, (math::matrix::matrix<int, 3, 3>{{0,1,0},{1,0,1},{1,1,1}}));
- TS_ASSERT_EQUALS(B*A, (math::matrix::matrix<int, 2, 2>{{0,1},{2,1}}));
- }
-
- void test_matrix_vector_multiplication() const {
- auto A = matr2i{{1,0},{1,2}};
- auto x = math::vector::vector<int, 2>{1,2};
- TS_ASSERT_EQUALS(A*x, (math::vector::vector<int, 2>{1,5}));
- }
-
- // void test_matrix_composition() const {
- // using namespace math::matrix;
- // using vec4 = math::vector::vector<double, 4>;
- // using vec3 = math::vector::vector<double, 3>;
- // auto rot = rotation<4>(math::degree{90}, rotate::X_AXIS);
- // auto mov = translation(vec3{2.0, 2.5, 1.5});
- // auto scl = scalar(vec3{2.0, math::vector::fill});
- // vec4 epsilon{0.00001, math::vector::fill};
- // TS_ASSERT_DELTA((mov * scl * rot * vec4{1,2,3,1}),
- // (vec4{4.0,-1.5,-4.5,1.0}),
- // epsilon);
- // }
-
- void test_matrix_from_vector() const {
- using vec3 = math::vector::vector<double, 3>;
- vec3 v = vec3{1,2,3};
- math::matrix::matrix<double, 3, 1> m{v};
- TS_ASSERT_EQUALS(m(0,0), v[0]);
- TS_ASSERT_EQUALS(m(1,0), v[1]);
- TS_ASSERT_EQUALS(m(2,0), v[2]);
- }
-
- void test_matrix_init_list_except() const {
- TS_ASSERT_THROWS((math::matrix::matrix<double, 2, 1>{{1.0}}), std::logic_error);
- }
-
- void test_assign_row() const {
- matr2i A = math::matrix::identity<int, 2>();
- matr2i const B = 2 * A;
- A[0] = B[0];
- TS_ASSERT_EQUALS(A, (matr2i{{2,0},{0,1}}));
- }
- private:
- };
|