Unttests for nested functions

Derek Parnell derek at psych.ward
Sun May 28 18:02:53 PDT 2006


On Sun, 28 May 2006 02:46:24 +0000 (UTC), BCS wrote:

> how do I unittest bar?

Currently one can't directly test it using unit tests, however on the
assumption that the outer function uses the inner function I guess you can
test it indirectly, plus you can also use inner asserts and DbC.

 int foo(int i)
 {
   int j=0;

   int bar(int k)
   out(x)
   {
    assert( x == j - k);
   }
   body
   {
     int ov = j;
     j += k;
     return ov; // The old value.
   }

  while(i)
    bar(i--);

  return j;
 }

 unittest
 {
  assert (foo(0) == 0);
  assert (foo(1) == 1);
  assert (foo(10) == 55);
 }

 void main()
 {
 }

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocracy!"
29/05/2006 10:38:21 AM



More information about the Digitalmars-d-learn mailing list