aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ode-0.9/tests/CppTestHarness/Checks.h
blob: 7b4c2ca19d2088c149b47c7a204338f102ba8129 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifndef CHECKS_H
#define CHECKS_H

#include <sstream>
#include <cmath>

namespace CppTestHarness
{
	template< typename Value >
	bool Check(Value const value)
	{
#ifdef _MSC_VER
#	pragma warning(push)
#	pragma warning(disable:4127) // conditional expression is constant
#	pragma warning(disable:4800) // forcing value to bool true/false, performance warning
#endif
		return value;
#ifdef _MSC_VER
#	pragma warning(pop)
#endif
	}

	template< typename Actual, typename Expected >
	bool CheckEqual(Actual const actual, Expected const expected)
	{
#ifdef _MSC_VER
#	pragma warning(push)
#	pragma warning(disable:4127) // conditional expression is constant
#endif
		return (actual == expected);
#ifdef _MSC_VER
#	pragma warning(pop)
#endif
	}

	template< typename Actual, typename Expected >
	bool CheckArrayEqual(Actual const actual, Expected const expected, int const count)
	{
		for (int i = 0; i < count; ++i)
		{
			if (!(actual[i] == expected[i]))
				return false;
		}

		return true;
	}

	template< typename Actual, typename Expected, typename Tolerance >
	bool CheckClose(Actual const actual, Expected const expected, Tolerance const tolerance)
	{
		return (std::abs(double(actual) - double(expected)) <= double(tolerance));
	}

	template< typename Actual, typename Expected, typename Tolerance >
	bool CheckArrayClose(Actual const actual, Expected const expected, int const count, Tolerance const tolerance)
	{
		for (int i = 0; i < count; ++i)
		{
			if (!CheckClose(actual[i], expected[i], tolerance))
				return false;
		}

		return true;
	}

	template< typename Actual, typename Expected >
	std::string BuildFailureString(Actual const actual, Expected const expected)
	{
		std::stringstream failureStr;
		failureStr << "Expected " << actual << " but got " << expected << std::endl;
		return failureStr.str();
	}

	template< typename Actual, typename Expected >
	std::string BuildFailureString(Actual const* actual, Expected const* expected, int const count)
	{
		std::stringstream failureStr;
		int i;
		
		failureStr << "Expected [ ";

		for (i = 0; i < count; ++i)
			failureStr << expected[i] << ' ';

		failureStr << "] but got [ ";

		for (i = 0; i < count; ++i)
			failureStr << expected[i] << ' ';

		failureStr << ']' << std::endl;

        return failureStr.str();		
	}
}

#endif