unittest.hh revision 7841:703fbf20c620
1/*
2 * Copyright (c) 2011 Advanced Micro Devices
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * 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 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Gabe Black
29 */
30
31/**
32 * @file This file defines functions and macros for use in unit tests.
33 */
34
35#ifndef __UNITTEST_UNITTEST_HH__
36#define __UNITTEST_UNITTEST_HH__
37
38namespace UnitTest {
39
40/**
41 * Function that actually handles checking whether an EXPECT_* passed. This
42 * should be used through the EXPECT macros below and not called directly.
43 * @param file The name of the file this check is in.
44 * @param line The line number this check is on.
45 * @param test Text specifying what check is being performed.
46 * @param result Whether the check passed.
47 */
48void checkVal(const char *file, const unsigned line,
49              const char *test, const bool result);
50
51/**
52 * Print on pass is a switch that specifies whether to print a message even
53 * when a check passes. It's default value is whether or not "PRINT_ON_PASS"
54 * is set in the calling environment. What it's actually set to is ignored.
55 */
56
57/**
58 * Function for retrieving the current setting for print on pass.
59 * @return The current setting.
60 */
61bool printOnPass();
62
63/**
64 * Function for setting print on pass.
65 * @param newVal The new setting.
66 */
67void printOnPass(bool newVal);
68
69/**
70 * Function that returns the current number of passed checks.
71 * @return Number of checks that have passed so far.
72 */
73unsigned passes();
74
75/**
76 * Function that returns the current number of failed checks.
77 * @return Number of checks that have failed so far.
78 */
79unsigned failures();
80
81/**
82 * Function to call at the end of a test that prints an overall result and a
83 * summary of how many checks passed and failed. main() should return the
84 * return value of this function which is the number of failed checks.
85 * @return Number of failed checks.
86 */
87unsigned printResults();
88
89/// Zero the number of passes and failures so far.
90void reset();
91
92/**
93 * Sets the current test case. Test cases are used to group checks together and
94 * describe what that group is doing. Setting a new case defines the start of
95 * a new group and the end of the previous one. The case string is used in
96 * place and not copied, so don't modify or invalidate it until a new case
97 * label is installed.
98 * @param newCase The name of the new test case.
99 */
100void setCase(const char *newCase);
101
102} // namespace UnitTest
103
104/// A macro which verifies that expr evaluates to true.
105#define EXPECT_TRUE(expr) \
106    UnitTest::checkVal(__FILE__, __LINE__, "EXPECT_TRUE(" #expr ")", (expr))
107/// A macro which verifies that expr evaluates to false.
108#define EXPECT_FALSE(expr) \
109    UnitTest::checkVal(__FILE__, __LINE__, \
110            "EXPECT_FALSE(" #expr ")", (expr) == false)
111/// A macro which verifies that lhs and rhs are equal to each other.
112#define EXPECT_EQ(lhs, rhs) \
113    UnitTest::checkVal(__FILE__, __LINE__, \
114            "EXPECT_EQ(" #lhs ", " #rhs ")", (lhs) == (rhs));
115
116#endif
117