unittest.hh revision 8332
17841Sgblack@eecs.umich.edu/* 28332Snate@binkert.org * Copyright (c) 2011 Advanced Micro Devices, Inc. 37841Sgblack@eecs.umich.edu * All rights reserved. 47841Sgblack@eecs.umich.edu * 57841Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without 67841Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are 77841Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright 87841Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer; 97841Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright 107841Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the 117841Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution; 127841Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its 137841Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from 147841Sgblack@eecs.umich.edu * this software without specific prior written permission. 157841Sgblack@eecs.umich.edu * 167841Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 177841Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 187841Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 197841Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 207841Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 217841Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 227841Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 237841Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 247841Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 257841Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 267841Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 277841Sgblack@eecs.umich.edu * 287841Sgblack@eecs.umich.edu * Authors: Gabe Black 297841Sgblack@eecs.umich.edu */ 307841Sgblack@eecs.umich.edu 317841Sgblack@eecs.umich.edu/** 327841Sgblack@eecs.umich.edu * @file This file defines functions and macros for use in unit tests. 337841Sgblack@eecs.umich.edu */ 347841Sgblack@eecs.umich.edu 357841Sgblack@eecs.umich.edu#ifndef __UNITTEST_UNITTEST_HH__ 367841Sgblack@eecs.umich.edu#define __UNITTEST_UNITTEST_HH__ 377841Sgblack@eecs.umich.edu 387841Sgblack@eecs.umich.edunamespace UnitTest { 397841Sgblack@eecs.umich.edu 407841Sgblack@eecs.umich.edu/** 417841Sgblack@eecs.umich.edu * Function that actually handles checking whether an EXPECT_* passed. This 427841Sgblack@eecs.umich.edu * should be used through the EXPECT macros below and not called directly. 437841Sgblack@eecs.umich.edu * @param file The name of the file this check is in. 447841Sgblack@eecs.umich.edu * @param line The line number this check is on. 457841Sgblack@eecs.umich.edu * @param test Text specifying what check is being performed. 467841Sgblack@eecs.umich.edu * @param result Whether the check passed. 477841Sgblack@eecs.umich.edu */ 487841Sgblack@eecs.umich.eduvoid checkVal(const char *file, const unsigned line, 497841Sgblack@eecs.umich.edu const char *test, const bool result); 507841Sgblack@eecs.umich.edu 517841Sgblack@eecs.umich.edu/** 527841Sgblack@eecs.umich.edu * Print on pass is a switch that specifies whether to print a message even 537841Sgblack@eecs.umich.edu * when a check passes. It's default value is whether or not "PRINT_ON_PASS" 547841Sgblack@eecs.umich.edu * is set in the calling environment. What it's actually set to is ignored. 557841Sgblack@eecs.umich.edu */ 567841Sgblack@eecs.umich.edu 577841Sgblack@eecs.umich.edu/** 587841Sgblack@eecs.umich.edu * Function for retrieving the current setting for print on pass. 597841Sgblack@eecs.umich.edu * @return The current setting. 607841Sgblack@eecs.umich.edu */ 617841Sgblack@eecs.umich.edubool printOnPass(); 627841Sgblack@eecs.umich.edu 637841Sgblack@eecs.umich.edu/** 647841Sgblack@eecs.umich.edu * Function for setting print on pass. 657841Sgblack@eecs.umich.edu * @param newVal The new setting. 667841Sgblack@eecs.umich.edu */ 677841Sgblack@eecs.umich.eduvoid printOnPass(bool newVal); 687841Sgblack@eecs.umich.edu 697841Sgblack@eecs.umich.edu/** 707841Sgblack@eecs.umich.edu * Function that returns the current number of passed checks. 717841Sgblack@eecs.umich.edu * @return Number of checks that have passed so far. 727841Sgblack@eecs.umich.edu */ 737841Sgblack@eecs.umich.eduunsigned passes(); 747841Sgblack@eecs.umich.edu 757841Sgblack@eecs.umich.edu/** 767841Sgblack@eecs.umich.edu * Function that returns the current number of failed checks. 777841Sgblack@eecs.umich.edu * @return Number of checks that have failed so far. 787841Sgblack@eecs.umich.edu */ 797841Sgblack@eecs.umich.eduunsigned failures(); 807841Sgblack@eecs.umich.edu 817841Sgblack@eecs.umich.edu/** 827841Sgblack@eecs.umich.edu * Function to call at the end of a test that prints an overall result and a 837841Sgblack@eecs.umich.edu * summary of how many checks passed and failed. main() should return the 847841Sgblack@eecs.umich.edu * return value of this function which is the number of failed checks. 857841Sgblack@eecs.umich.edu * @return Number of failed checks. 867841Sgblack@eecs.umich.edu */ 877841Sgblack@eecs.umich.eduunsigned printResults(); 887841Sgblack@eecs.umich.edu 897841Sgblack@eecs.umich.edu/// Zero the number of passes and failures so far. 907841Sgblack@eecs.umich.eduvoid reset(); 917841Sgblack@eecs.umich.edu 927841Sgblack@eecs.umich.edu/** 937841Sgblack@eecs.umich.edu * Sets the current test case. Test cases are used to group checks together and 947841Sgblack@eecs.umich.edu * describe what that group is doing. Setting a new case defines the start of 957841Sgblack@eecs.umich.edu * a new group and the end of the previous one. The case string is used in 967841Sgblack@eecs.umich.edu * place and not copied, so don't modify or invalidate it until a new case 977841Sgblack@eecs.umich.edu * label is installed. 987841Sgblack@eecs.umich.edu * @param newCase The name of the new test case. 997841Sgblack@eecs.umich.edu */ 1007841Sgblack@eecs.umich.eduvoid setCase(const char *newCase); 1017841Sgblack@eecs.umich.edu 1027841Sgblack@eecs.umich.edu} // namespace UnitTest 1037841Sgblack@eecs.umich.edu 1047841Sgblack@eecs.umich.edu/// A macro which verifies that expr evaluates to true. 1057841Sgblack@eecs.umich.edu#define EXPECT_TRUE(expr) \ 1067841Sgblack@eecs.umich.edu UnitTest::checkVal(__FILE__, __LINE__, "EXPECT_TRUE(" #expr ")", (expr)) 1077841Sgblack@eecs.umich.edu/// A macro which verifies that expr evaluates to false. 1087841Sgblack@eecs.umich.edu#define EXPECT_FALSE(expr) \ 1097841Sgblack@eecs.umich.edu UnitTest::checkVal(__FILE__, __LINE__, \ 1107841Sgblack@eecs.umich.edu "EXPECT_FALSE(" #expr ")", (expr) == false) 1117841Sgblack@eecs.umich.edu/// A macro which verifies that lhs and rhs are equal to each other. 1127841Sgblack@eecs.umich.edu#define EXPECT_EQ(lhs, rhs) \ 1137841Sgblack@eecs.umich.edu UnitTest::checkVal(__FILE__, __LINE__, \ 1147841Sgblack@eecs.umich.edu "EXPECT_EQ(" #lhs ", " #rhs ")", (lhs) == (rhs)); 1157841Sgblack@eecs.umich.edu 1167841Sgblack@eecs.umich.edu#endif 117