Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TDD correctly to implement a numerical method?

Tags:

tdd

numerical

I am trying to use Test Driven Development to implement my signal processing library. But I have a little doubt: Assume I am trying to implement a sine method (I'm not):

  1. Write the test (pseudo-code)

    assertEqual(0, sine(0))
    
  2. Write the first implementation

    function sine(radians)
        return 0
    
  3. Second test

    assertEqual(1, sine(pi))
    

At this point, should I:

  1. implement a smart code that will work for pi and other values, or
  2. implement the dumbest code that will work only for 0 and pi?

If you choose the second option, when can I jump to the first option? I will have to do it eventually...

like image 231
Jader Dias Avatar asked Sep 23 '09 02:09

Jader Dias


People also ask

What are the 3 stages of TDD?

Test-driven development (TDD) is an approach to software development where you write tests first, then use those tests to drive the design and development of your software application.


1 Answers

Note that (in NUnit) you can also do

Assert.That(2.1 + 1.2, Is.EqualTo(3.3).Within(0.0005);

when you're dealing with floating-point equality.

One piece of advice I remember reading was to try to refactor out the magic numbers from your implementations.

like image 194
TrueWill Avatar answered Nov 12 '22 12:11

TrueWill