17.1.1 Square Tester

You’d expect this is when we add the content of these methods. Instead, we will work on the application ‘square tester’. You know you should test your code very frequently during the development of a program. But in a major project that is difficult. We have to write lots of classes before you can actually run the application.

That’s why we use test programs. These will be written to test a specific class. In this case, we make sure that all methods of the class square can be tested. The code for ‘square tester’ could look like this:

 
 
Memc<square> squares; 
 
void InitPre() 
5{ 
   EE_INIT(); 
} 
 
bool Init() 
10{ 
   // Here you create the test function, with different 
   // block types. 
   squares.New().create(VecI2( 2,  2), BT_S          ); 
   squares.New().create(VecI2( 4,  2), BT_T          ); 
15   squares.New().create(VecI2( 6,  7), BT_L          ); 
   squares.New().create(VecI2(10, 12), BT_BACKWARDS_S); 
   squares.New().create(VecI2( 8,  8), BT_BACKWARDS_L); 
   squares.New().create(VecI2( 5,  1), BT_SQUARE     ); 
   return true
20} 
 
void Shut() {} 
 
bool Update() 
25{ 
   if(Kb.bp(KB_ESC)) return false
 
   // We declare a direction and control the arrow keys 
   DIRECTION d = D_NONE; 
30   if(Kb.bp(KB_DOWN )) d = D_DOWN ; 
   if(Kb.bp(KB_LEFT )) d = D_LEFT ; 
   if(Kb.bp(KB_RIGHT)) d = D_RIGHT; 
 
   // Next we move all the blocks in this direction. When 
35   // no key is pressed, the squares should not move. 
   REPA(squares) 
   { 
      squares[i].move(d); 
   } 
40 
   // The methods getPos and setPos should also be tested. We are doing that 
   // when the spacebar is pressed. We retrieve the current position 
   // of each square and change the vertical value. The altered position 
   // will be put back into the square. 
45   if(Kb.bp(KB_SPACE)) 
   { 
      REPA(squares) 
      { 
         VecI2 pos = squares[i].getPos(); 
50         pos.y += 4; 
         squares[i].setPos(pos); 
      } 
   } 
 
55   return true
} 
 
void Draw() 
{ 
60   D.clear(BLACK); 
 
   // Here we test the draw function of each square. 
   REPA(squares) 
   { 
65      squares[i].draw(); 
   } 
}  
70

Note
You have often many ways to write a test program. The important thing is that you test as many class methods in the easiest way possible. This minimizes the chance of errors later on.