title: A good way to write unit tests
body: <p>So, I previously wasn't really in the practice of writing unit tests - now I kind of am and I need to check if I'm on the right track.</p>
  
  <p>Say you have a class that deals with math computations.</p>
  
  <pre><code>class Vector3
  {
  public:  // Yes, public.
    float x,y,z ;
    // ... ctors ...
  } ;
  
  Vector3 operator+( const Vector3&amp; a, const Vector3 &amp;b )
  {
    return Vector3( a.x + b.y /* oops!! hence the need for unit testing.. */,
                    a.y + b.y,
                    a.z + b.z ) ;
  }
  </code></pre>
  
  <p>There are 2 ways I can really think of to do a unit test on a Vector class:</p>
  
  <h2>1)  Hand-solve some problems, then hard code the numbers into the unit test and pass only if equal to your hand and hard-coded result</h2>
  
  <pre><code>bool UnitTest_ClassVector3_operatorPlus()
  {
    Vector3 a( 2, 3, 4 ) ;
    Vector3 b( 5, 6, 7 ) ;
  
    Vector3 result = a + b ;
  
    // "expected" is computed outside of computer, and
    // hard coded here.  For more complicated operations like
    // arbitrary axis rotation this takes a bit of paperwork,
    // but only the final result will ever be entered here.
    Vector3 expected( 7, 9, 11 ) ;
  
    if( result.isNear( expected ) )
      return PASS ;
    else
      return FAIL ;
  }
  </code></pre>
  
  <h2>2)  Rewrite the computation code very carefully inside the unit test.</h2>
  
  <pre><code>bool UnitTest_ClassVector3_operatorPlus()
  {
    Vector3 a( 2, 3, 4 ) ;
    Vector3 b( 5, 6, 7 ) ;
  
    Vector3 result = a + b ;
  
    // "expected" is computed HERE.  This
    // means all you've done is coded the
    // same thing twice, hopefully not having
    // repeated the same mistake again
    Vector3 expected( 2 + 5, 6 + 3, 4 + 7 ) ;
  
    if( result.isNear( expected ) )
      return PASS ;
    else
      return FAIL ;
  }
  </code></pre>
  
  <p>Or is there another way to do something like this?</p>