matrix.t.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //
  2. // matrix_tc.h
  3. // math
  4. //
  5. // Created by Sam Jaffe on 8/18/16.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "matrix.hpp"
  10. #include "matrix_helpers.hpp"
  11. class matrix_TestSuite : public CxxTest::TestSuite {
  12. public:
  13. using matr2i = math::matrix::matrix<int, 2, 2>;
  14. using matr2 = math::matrix::matrix<double, 2, 2>;
  15. public:
  16. void test_matrix_equals() const {
  17. using math::matrix::identity;
  18. TS_ASSERT_EQUALS((identity<int, 2>()), (matr2i{{1,0},{0,1}}));
  19. }
  20. void test_matrix_sum() const {
  21. auto iden = math::matrix::identity<int, 2>();
  22. auto result = matr2i{{2,0},{0,2}};
  23. TS_ASSERT_EQUALS(iden + iden, result);
  24. }
  25. void test_matrix_default_zero() const {
  26. auto zero = matr2i{{0,0},{0,0}};
  27. TS_ASSERT_EQUALS(matr2i{}, zero);
  28. }
  29. void test_matrix_subtract() const {
  30. auto zero = matr2i{{0,0},{0,0}};
  31. auto iden = math::matrix::identity<int, 2>();
  32. TS_ASSERT_EQUALS(iden-iden, zero);
  33. }
  34. // void test_matrix_negate() const {
  35. // auto iden = math::matrix::identity<int, 2>();
  36. // TS_ASSERT_EQUALS(-iden, (matr2i{{-1,0},{0,-1}}));
  37. // }
  38. void test_matrix_scaling() const {
  39. auto iden = math::matrix::identity<double, 2>();
  40. TS_ASSERT_EQUALS(2*iden, (matr2{{2.0,0.0},{0.0,2.0}}));
  41. TS_ASSERT_EQUALS(iden*2, (matr2{{2.0,0.0},{0.0,2.0}}));
  42. TS_ASSERT_EQUALS(iden/2.0, (matr2{{0.5,0.0},{0.0,0.5}}));
  43. }
  44. void test_matrix_multiplication_same_dim() const {
  45. auto A = matr2i{{1,2},{2,3}};
  46. auto B = matr2i{{1,0},{1,1}};
  47. TS_ASSERT_EQUALS(A*B, (matr2i{{3,2},{5,3}}));
  48. TS_ASSERT_EQUALS(B*A, (matr2i{{1,2},{3,5}}));
  49. }
  50. void test_matrix_multiplication_diff_dim() const {
  51. auto A = math::matrix::matrix<int, 3, 2>{{1,0},{0,1},{1,1}};
  52. auto B = math::matrix::matrix<int, 2, 3>{{0,1,0},{1,0,1}};
  53. TS_ASSERT_EQUALS(A*B, (math::matrix::matrix<int, 3, 3>{{0,1,0},{1,0,1},{1,1,1}}));
  54. TS_ASSERT_EQUALS(B*A, (math::matrix::matrix<int, 2, 2>{{0,1},{2,1}}));
  55. }
  56. void test_matrix_vector_multiplication() const {
  57. auto A = matr2i{{1,0},{1,2}};
  58. auto x = math::vector::vector<int, 2>{1,2};
  59. TS_ASSERT_EQUALS(A*x, (math::vector::vector<int, 2>{1,5}));
  60. }
  61. // void test_matrix_composition() const {
  62. // using namespace math::matrix;
  63. // using vec4 = math::vector::vector<double, 4>;
  64. // using vec3 = math::vector::vector<double, 3>;
  65. // auto rot = rotation<4>(math::degree{90}, rotate::X_AXIS);
  66. // auto mov = translation(vec3{2.0, 2.5, 1.5});
  67. // auto scl = scalar(vec3{2.0, math::vector::fill});
  68. // vec4 epsilon{0.00001, math::vector::fill};
  69. // TS_ASSERT_DELTA((mov * scl * rot * vec4{1,2,3,1}),
  70. // (vec4{4.0,-1.5,-4.5,1.0}),
  71. // epsilon);
  72. // }
  73. void test_matrix_from_vector() const {
  74. using vec3 = math::vector::vector<double, 3>;
  75. vec3 v = vec3{1,2,3};
  76. math::matrix::matrix<double, 3, 1> m{v};
  77. TS_ASSERT_EQUALS(m(0,0), v[0]);
  78. TS_ASSERT_EQUALS(m(1,0), v[1]);
  79. TS_ASSERT_EQUALS(m(2,0), v[2]);
  80. }
  81. void test_matrix_init_list_except() const {
  82. TS_ASSERT_THROWS((math::matrix::matrix<double, 2, 1>{{1.0}}), std::logic_error);
  83. }
  84. void test_assign_row() const {
  85. matr2i A = math::matrix::identity<int, 2>();
  86. matr2i const B = 2 * A;
  87. A[0] = B[0];
  88. TS_ASSERT_EQUALS(A, (matr2i{{2,0},{0,1}}));
  89. }
  90. private:
  91. };