Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include "Matrix.h"
  4.  
  5. using namespace std;
  6.  
  7. // Definition: Matrix foo
  8. // Definition: Matrix foo(row, col)
  9. // Definition: Matrix foo(row, col, double[])
  10. void test_matrix()
  11. {
  12. Matrix a(1, 2, new double[2] {0, 1});
  13. assert(a(0, 0) == 0);
  14. assert(a(0, 1) == 1);
  15.  
  16. Matrix b(2, 1, new double[2]{ 2, -1 });
  17. assert(b(0, 0) == 2);
  18. assert(b(1, 0) == -1);
  19. cout << "[PASSED] test_matrix" << endl;
  20. }
  21.  
  22. // Editable: matrix[row][col]
  23. // ReadOnly: matrix(row, col)
  24. void test_refer()
  25. {
  26. Matrix a(2, 2);
  27. a[0][0] = 1;
  28. a[0][1] = 2;
  29. a[1][0] = 3;
  30. a[1][1] = 4;
  31. assert(a(0, 0) == 1);
  32. assert(a(0, 1) == 2);
  33. assert(a(1, 0) == 3);
  34. assert(a(1, 1) == 4);
  35. cout << "[PASSED] test_refer" << endl;
  36. }
  37.  
  38. // Matrix = Matrix
  39. void test_assignment()
  40. {
  41. Matrix a(1, 2, new double[2]{ 0, 1 });
  42. Matrix b = a;
  43. assert(b(0, 0) == 0);
  44. assert(b(0, 1) == 1);
  45.  
  46. Matrix c;
  47. c = a;
  48. assert(c(0, 0) == 0);
  49. assert(c(0, 1) == 1);
  50. cout << "[PASSED] test_assignment" << endl;
  51. }
  52.  
  53. // Matrix + Matrix
  54. // Matrix -= Matrix
  55. void test_add()
  56. {
  57. Matrix a(2, 1, new double[2] {0, 5});
  58. Matrix b(2, 1, new double[2] {1, -2});
  59. Matrix c = a + b;
  60. assert(c(0, 0) == 1);
  61. assert(c(1, 0) == 3);
  62.  
  63. Matrix d = a;
  64. d += b;
  65. assert(d(0, 0) == 1);
  66. assert(d(1, 0) == 3);
  67. cout << "[PASSED] test_add" << endl;
  68. }
  69.  
  70. // Matrix - Matrix
  71. // Matrix -= Matrix
  72. void test_sub()
  73. {
  74. Matrix a(2, 1, new double[2] {0, 5});
  75. Matrix b(2, 1, new double[2] {1, -2});
  76. Matrix c = a - b;
  77. assert(c(0, 0) == -1);
  78. assert(c(1, 0) == 7);
  79.  
  80. Matrix d = a;
  81. d -= b;
  82. assert(d(0, 0) == -1);
  83. assert(d(1, 0) == 7);
  84. cout << "[PASSED] test_sub" << endl;
  85. }
  86.  
  87. // implemented: Matrix * double
  88. // unimplemented: double * Matrix
  89. void test_multi()
  90. {
  91. Matrix a(2, 1, new double[2] {0, 5});
  92. Matrix b(1, 2, new double[2] {1, -2});
  93. Matrix c = a * b;
  94. assert(c(0, 0) == 0);
  95. assert(c(0, 1) == 0);
  96. assert(c(1, 0) == 5);
  97. assert(c(1, 1) == -10);
  98.  
  99. Matrix d = a * 2;
  100. assert(d(0, 0) == 0);
  101. assert(d(1, 0) == 10);
  102.  
  103. cout << "[PASSED] test_multi" << endl;
  104. }
  105.  
  106. // implemented: Matrix / double
  107. // unimplemented: double / Matrix
  108. void test_div()
  109. {
  110. Matrix a(2, 1, new double[2]{ 0, 5 });
  111. Matrix c = a / 2;
  112. assert(c(0, 0) == 0);
  113. assert(c(1, 0) == 2.5);
  114. cout << "[PASSED] test_div" << endl;
  115. }
  116.  
  117. // Matrix.transpose()
  118. void test_transpose()
  119. {
  120. Matrix a(2, 1, new double[2] {0, 5});
  121. Matrix b = a.transpose();
  122. assert(b(0, 0) == 0);
  123. assert(b(0, 1) == 5);
  124. cout << "[PASSED] test_transpose" << endl;
  125. }
  126.  
  127. // Matrix == Matrix
  128. // Matrix != Matrix
  129. void test_equal()
  130. {
  131. Matrix a(2, 1, new double[2]{ 0, 5 });
  132. Matrix b(2, 1, new double[2]{ 0, 5 });
  133. Matrix c(1, 2, new double[2]{ 0, 5 });
  134. Matrix d(1, 2, new double[2]{ 1, 5 });
  135. assert(a == b);
  136. assert(a != c);
  137. assert(a != d);
  138. cout << "[PASSED] test_equal" << endl;
  139. }
  140.  
  141. // Matrix.random(min, max)
  142. // Matrix.random(min, max, seed)
  143. void sample_random()
  144. {
  145. Matrix a(2, 1);
  146. a.random(0, 1);/*
  147. cout << a(0, 0) << endl;
  148. cout << a(1, 0) << endl;*/
  149. }
  150.  
  151. int main()
  152. {
  153. test_matrix();
  154. test_refer();
  155. test_assignment();
  156. test_add();
  157. test_sub();
  158. test_multi();
  159. test_div();
  160. test_transpose();
  161. test_equal();
  162. sample_random();
  163. return 0;
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement