UnitTest.cc Source File

Back to the index.

UnitTest.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2010 Anders Gavare. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <iostream>
29 
30 #include "misc.h"
31 #include "UnitTest.h"
32 
33 // The list of classes to test is detected by the configure script, and
34 // placed in unittest.h, but the classes corresponding .h files also need
35 // to be included. This is done by unittest_h.h (also generated by the
36 // configure script).
37 #include "../../unittest_h.h"
38 
39 
40 void UnitTest::Assert(const string& strFailMessage, bool condition)
41 {
42  if (!condition)
43  Fail(strFailMessage);
44 }
45 
46 
47 void UnitTest::Assert(const string& strFailMessage,
48  uint64_t actualValue, uint64_t expectedValue)
49 {
50  if (actualValue != expectedValue) {
51  stringstream ss;
52  ss.flags(std::ios::hex | std::ios::showbase);
53  ss << strFailMessage <<
54  "\n\tExpected: " << expectedValue <<
55  "\n\tbut was: " << actualValue;
56  Fail(ss.str());
57  }
58 }
59 
60 
61 void UnitTest::Assert(const string& strFailMessage,
62  const string& actualValue, const string& expectedValue)
63 {
64  if (actualValue != expectedValue) {
65  size_t pos;
66  for (pos = 0; pos < actualValue.length() && pos < expectedValue.length(); pos++)
67  if (actualValue[pos] != expectedValue[pos])
68  break;
69 
70  stringstream mismatchPosition;
71  mismatchPosition << "\n\tMismatch at position " << pos;
72 
73  Fail(strFailMessage + mismatchPosition.str() +
74  "\n\tExpected: \"" + expectedValue + "\"" +
75  "\n\tbut was: \"" + actualValue + "\"");
76  }
77 }
78 
79 
80 void UnitTest::Fail(const string& strMessage)
81 {
82  throw UnitTestFailedException(strMessage);
83 }
84 
85 
86 #ifndef WITHUNITTESTS
87 
88 
90 {
91  std::cerr << "Skipping unit tests, because WITHUNITTESTS "
92  "was not defined.\n";
93 
94  return 0;
95 }
96 
97 
98 #else // WITHUNITTESTS
99 
100 
101 int UnitTest::RunTests()
102 {
103  int nSucceeded = 0, nFailed = 0;
104 
105 #include "../../unittest.h"
106 
107  if (nFailed == 0)
108  std::cerr << nSucceeded << " (all) tests passed.\n";
109  else
110  std::cerr << "\n" << nFailed << " TESTS FAILED!\n";
111 
112  // Returns 0 if there were no errors.
113  return nFailed > 0;
114 }
115 
116 
117 #endif // WITHUNITTESTS
UnitTest::Assert
static void Assert(const string &strFailMessage, bool condition)
Asserts that a boolean condition is correct.
Definition: UnitTest.cc:40
misc.h
UnitTest.h
UnitTestFailedException
An exception thrown by unit test cases that fail.
Definition: UnitTest.h:39
UnitTest::Fail
static void Fail(const string &strMessage)
Fails a unit test unconditionally, by throwing a UnitTestFailedException.
Definition: UnitTest.cc:80
UnitTest::RunTests
static int RunTests()
Runs all unit tests in GXemul.
Definition: UnitTest.cc:89

Generated on Tue Mar 24 2020 14:04:48 for GXemul by doxygen 1.8.17