Pella86

Unit test vector

Oct 12th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. class UnitTestVector{
  2.  
  3.  
  4.     V2<int> v2i{1, 2};
  5.     V2<float> v2f{4.0, 5.0};
  6.     V2d magtest{6.0, 8.0};
  7.     V3d normtest{3., 2., 1.};
  8.  
  9.     // V2<double> v2d; --- not defined yet
  10.  
  11.     #define utv_test(title, cond) do{ \
  12.         cout << "--- " << title << " ---" << endl; \
  13.         cout << "Test: " << #cond << endl; \
  14.         bool test_passed = true; \
  15.         try{ \
  16.             if(!(cond)) throw "Test: failed"; \
  17.         } catch(const char* e){ \
  18.             cout << e << endl; \
  19.             test_passed = false; \
  20.         } \
  21.         if (test_passed) cout << "Test: passed" << endl; \
  22.         }while(0);
  23.  
  24. public:
  25.     UnitTestVector(){
  26.  
  27.         utv_test("Test operatror[] integer vector", v2i[0] == 1);
  28.         utv_test("Test y() integer vetor", v2i.y() == 2);
  29.         utv_test("Test operator[] float vector", v2f[0] == 4.0);
  30.         utv_test("Test y() float vector", v2f.y() == 5.0);
  31.         // https://www.mathsisfun.com/algebra/vectors.html
  32.         utv_test("Test magnitude", magtest.length() == 10.0);
  33.  
  34.         //http://www.fundza.com/vectors/normalize/
  35.         normtest.normalize();
  36.         V3d expnorm{0.802, 0.267, 0.534};
  37.         utv_test("Test normalization", normtest.cmp_close(expnorm));
  38.  
  39.     }
  40. };
  41.  
  42.  
  43. /*** OUTPUT ***
  44.  
  45. --- Test operatror[] integer vector ---
  46. Test: v2i[0] == 1
  47. Test: passed
  48. --- Test y() integer vetor ---
  49. Test: v2i.y() == 2
  50. Test: passed
  51. --- Test operator[] float vector ---
  52. Test: v2f[0] == 4.0
  53. Test: passed
  54. --- Test y() float vector ---
  55. Test: v2f.y() == 5.0
  56. Test: passed
  57. --- Test magnitude ---
  58. Test: magtest.length() == 10.0
  59. Test: passed
  60. --- Test normalization ---
  61. Test: normtest.cmp_close(expnorm)
  62. Test: passed
  63. ***/
Advertisement
Add Comment
Please, Sign In to add comment