ExampleΒΆ

This is an example using multiple files. Two test suites are defined and a main program - test application - that contains the test runner.

// File complex.hpp
// Class to be tested. A simple complex class acts as an example.
#include<iostream>

class complex {
public:
  double Real;
  double Imag;
  
  complex() : Real(0), Imag(0) {}
  complex(double real, double imag) : Real(real), Imag(imag) {}
  
  complex operator+=(complex rhs) {
    Real += rhs.Real;
    Imag += rhs.Imag;
  }
  
  complex operator+(complex rhs) {
    complex result(*this);
    result += rhs;
    return result;
  }
  
  complex operator-=(complex rhs) {
    Real -= rhs.Real;
    Imag -= rhs.Imag;
  }
  
  complex operator-(complex rhs) {
    complex result(*this);
    result -= rhs;
    return result;
  }
};


std::ostream& operator<<(std::ostream &output, complex &c) {
  output << "(" << c.Real << ", " << c.Imag << ")";
  return output;
}
// File test_complex.hpp
#include<sstream>

#include<wide/unittest>
#include "complex.hpp"  

using wide::unittest::test_suite;

class test_arithmetic : public test_suite<test_arithmetic> {
public:
  test_arithmetic() :
    test_suite("Test arithmetic operators of complex class.") {
    stage(&test_arithmetic::test_add_assign, "Test operator+=()");
    stage(&test_arithmetic::test_add, "Test operator+()");
    stage(&test_arithmetic::test_sub_assign, "Test operator-=()");
    stage(&test_arithmetic::test_sub, "Test operator-()");
  }

  complex Complex1;
  complex Complex2;

  void test_setup() {
    Complex1 = complex(1.0, 2.0);
    Complex2 = complex(3.0, 4.0);
  }

  void test_add_assign() {
    complex result(Complex1);
    result += Complex2;
    Verify(result.Real, Is.CloseTo(4.0)); 
    Verify(result.Imag, Is.CloseTo(6.0)); 
  }
  
  void test_add() {
    complex result = Complex1 + Complex2;
    Verify(result.Real, Is.CloseTo(4.0)); 
    Verify(result.Imag, Is.CloseTo(6.0)); 
  }
  
  void test_sub_assign() {
    complex result(Complex1);
    result -= Complex2;
    Verify(result.Real, Is.CloseTo(-2.0)); 
    Verify(result.Imag, Is.CloseTo(-2.0));
  }
  
  void test_sub() {
    complex result = Complex1 - Complex2;
    Verify(result.Real, Is.CloseTo(-2.0)); 
    Verify(result.Imag, Is.CloseTo(-2.0)); 
  }
};


class test_stream_integration : public test_suite<test_stream_integration> {
public:
  test_stream_integration() :
    test_suite("Test complex class stream integration.") {
    stage(&test_stream_integration::test_output, "Test stream output operator<<");
  }
  
  complex Complex;
  
  void test_setup() {
    Complex = complex(1.0, 2.0);
  }
  
  void test_output() {
    // Make this test fail on purpose to show fail log.
    std::stringstream complexString;
    complexString << Complex;
    Verify(complexString.str(), Is.EqualTo("1.0 + 2.0j"));
  }
};
// File test_complex.cpp
#include<iostream>
#include<wide/unittest>

#include "test_complex.hpp"

using namespace wide::unittest;

int main() {
  test_runner tester;
  
  tester.stage<test_stream_integration>();
  tester.stage<test_arithmetic>();
  
  int failCount = tester.run_all();
  
  return failCount;
}

The output will be:

Test complex class stream integration.
  Test stream output operator<<... FAIL
Tests: 1,  Failed: 1,  Errors: 0

Test arithmetic operators of complex class.
  Test operator+=()... pass
  Test operator+()... pass
  Test operator-=()... pass
  Test operator-()... pass
Tests: 4,  Failed: 0,  Errors: 0

========================================================================
FAIL
------------------------------------------------------------------------
Suite: Test complex class stream integration.
Test: Test stream output operator<<
File: example/unittest/example-02/test_complex.hpp line 72
------------------------------------------------------------------------
Invalid value.
Value: (1, 2)
Constraint: 1.0 + 2.0j
========================================================================

Suites: 2,  Tests: 5,  Failed 1,  Errors: 0