matrix_test.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * Matric unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <matrix.h>
  10. #include <tuples.h>
  11. #include <math.h>
  12. #include <gtest/gtest.h>
  13. TEST(MatrixTest, Constructing_and_inspecting_a_4x4_Matrix)
  14. {
  15. double values[] = {1, 2, 3, 4,
  16. 5.5, 6.5, 7.5, 8.5,
  17. 9, 10, 11, 12,
  18. 13.5, 14.5, 15.5, 16.5};
  19. Matrix4 m = Matrix4(values);
  20. ASSERT_EQ(m.get(0, 0), 1);
  21. ASSERT_EQ(m.get(0, 3), 4);
  22. ASSERT_EQ(m.get(1, 0), 5.5);
  23. ASSERT_EQ(m.get(1, 2), 7.5);
  24. ASSERT_EQ(m.get(2, 2), 11);
  25. ASSERT_EQ(m.get(3, 0), 13.5);
  26. ASSERT_EQ(m.get(3, 2), 15.5);
  27. }
  28. TEST(MatrixTest, A_2x2_matric_ought_to_be_representable)
  29. {
  30. double values[] = {-3, 5,
  31. 1, -2};
  32. Matrix2 m = Matrix2(values);
  33. ASSERT_EQ(m.get(0, 0), -3);
  34. ASSERT_EQ(m.get(0, 1), 5);
  35. ASSERT_EQ(m.get(1, 0), 1);
  36. ASSERT_EQ(m.get(1, 1), -2);
  37. }
  38. TEST(MatrixTest, A_3x3_matric_ought_to_be_representable)
  39. {
  40. double values[] = {-3, 5, 0,
  41. 1, -2, -7,
  42. 0, 1, 1};
  43. Matrix3 m = Matrix3(values);
  44. ASSERT_EQ(m.get(0, 0), -3);
  45. ASSERT_EQ(m.get(1, 1), -2);
  46. ASSERT_EQ(m.get(2, 2), 1);
  47. }
  48. TEST(MatrixTest, Matrix_equality_with_identical_matrix)
  49. {
  50. double values1[] = {1, 2, 3, 4,
  51. 5, 6, 7, 8,
  52. 9, 8, 7, 6,
  53. 5, 4, 3, 2};
  54. double values2[] = {1, 2, 3, 4,
  55. 5, 6, 7, 8,
  56. 9, 8, 7, 6,
  57. 5, 4, 3, 2};
  58. Matrix4 A = Matrix4(values1);
  59. Matrix4 B = Matrix4(values2);
  60. ASSERT_EQ(A, B);
  61. }
  62. TEST(MatrixTest, Matrix_equality_with_different_matrix)
  63. {
  64. double values1[] = {1, 2, 3, 4,
  65. 5, 6, 7, 8,
  66. 9, 8, 7, 6,
  67. 5, 4, 3, 2};
  68. double values2[] = {2, 3, 4, 5,
  69. 6, 7, 8, 9,
  70. 8, 7, 6, 5,
  71. 4, 3, 2, 1};
  72. Matrix4 A = Matrix4(values1);
  73. Matrix4 B = Matrix4(values2);
  74. ASSERT_NE(A, B);
  75. }
  76. TEST(MatrixTest, Multiplying_two_matrices)
  77. {
  78. double values1[] = {1, 2, 3, 4,
  79. 5, 6, 7, 8,
  80. 9, 8, 7, 6,
  81. 5, 4, 3, 2};
  82. double values2[] = {-2, 1, 2, 3,
  83. 3, 2, 1, -1,
  84. 4, 3, 6, 5,
  85. 1, 2, 7, 8};
  86. double results[] = {20, 22, 50, 48,
  87. 44, 54, 114, 108,
  88. 40, 58, 110, 102,
  89. 16, 26, 46, 42};
  90. Matrix4 A = Matrix4(values1);
  91. Matrix4 B = Matrix4(values2);
  92. ASSERT_EQ(A * B, Matrix4(results));
  93. }
  94. TEST(MatrixTest, A_matrix_multiplyed_by_a_tuple)
  95. {
  96. double valuesA[] = {1, 2, 3, 4,
  97. 2, 4, 4, 2,
  98. 8, 6, 4, 1,
  99. 0, 0, 0, 1};
  100. Matrix4 A = Matrix4(valuesA);
  101. Tuple b = Tuple(1, 2, 3, 1);
  102. ASSERT_EQ(A * b, Tuple(18, 24, 33, 1));
  103. }
  104. TEST(MatrixTest, Multiplying_a_matrix_by_the_identity_matrix)
  105. {
  106. double valuesA[] = {0, 1, 2, 4,
  107. 1, 2, 4, 8,
  108. 2, 4, 8, 16,
  109. 4, 8, 16, 32};
  110. Matrix4 A = Matrix4(valuesA);
  111. Matrix ident = Matrix4().identity();
  112. ASSERT_EQ(A * ident, A);
  113. }
  114. TEST(MatrixTest, Multiplying_the_identity_matrix_by_a_tuple)
  115. {
  116. Tuple a = Tuple(1, 2, 3, 4);
  117. Matrix ident = Matrix4().identity();
  118. ASSERT_EQ(ident * a, a);
  119. }
  120. TEST(MatrixTest, Transposing_a_matrix)
  121. {
  122. double valuesA[] = {0, 9, 3, 0,
  123. 9, 8, 0, 8,
  124. 1, 8, 5, 3,
  125. 0, 0, 5, 8};
  126. double results[] = {0, 9, 1, 0,
  127. 9, 8, 8, 0,
  128. 3, 0, 5, 5,
  129. 0, 8, 3, 8};
  130. Matrix A = Matrix4(valuesA);
  131. ASSERT_EQ(A.transpose(), Matrix4(results));
  132. }
  133. TEST(MatrixTest, Transposing_this_identity_matrix)
  134. {
  135. Matrix ident = Matrix4().identity();
  136. ASSERT_EQ(ident.transpose(), ident);
  137. }