unittest.cc 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#include <cstdlib>
327841Sgblack@eecs.umich.edu
337841Sgblack@eecs.umich.edu#include "base/cprintf.hh"
347841Sgblack@eecs.umich.edu#include "unittest/unittest.hh"
357841Sgblack@eecs.umich.edu
367841Sgblack@eecs.umich.edunamespace {
377841Sgblack@eecs.umich.edu
387841Sgblack@eecs.umich.edubool _printOnPass = (getenv("PRINT_ON_PASS") != NULL);
397841Sgblack@eecs.umich.eduunsigned _passes = 0;
407841Sgblack@eecs.umich.eduunsigned _failures = 0;
417841Sgblack@eecs.umich.edu
427841Sgblack@eecs.umich.edubool _casePrinted = false;
437841Sgblack@eecs.umich.educonst char *_case = NULL;
447841Sgblack@eecs.umich.edu
457841Sgblack@eecs.umich.edu} // anonymous namespace
467841Sgblack@eecs.umich.edu
477841Sgblack@eecs.umich.edunamespace UnitTest {
487841Sgblack@eecs.umich.edu
497841Sgblack@eecs.umich.eduvoid
507841Sgblack@eecs.umich.educheckVal(const char *file, const unsigned line,
517841Sgblack@eecs.umich.edu         const char *test, const bool result)
527841Sgblack@eecs.umich.edu{
537841Sgblack@eecs.umich.edu    if (!result || _printOnPass) {
547841Sgblack@eecs.umich.edu        if (!_casePrinted && _case) {
557841Sgblack@eecs.umich.edu            cprintf("CASE %s:\n", _case);
567841Sgblack@eecs.umich.edu            _casePrinted = true;
577841Sgblack@eecs.umich.edu        }
587841Sgblack@eecs.umich.edu        cprintf("   CHECK %s:   %s:%d   %s\n",
597841Sgblack@eecs.umich.edu                result ? "PASSED" : "FAILED", file, line, test);
607841Sgblack@eecs.umich.edu    }
617841Sgblack@eecs.umich.edu    if (result) _passes++;
627841Sgblack@eecs.umich.edu    else _failures++;
637841Sgblack@eecs.umich.edu}
647841Sgblack@eecs.umich.edu
657841Sgblack@eecs.umich.edubool printOnPass() { return _printOnPass; }
667841Sgblack@eecs.umich.eduvoid printOnPass(bool newPrintOnPass) { _printOnPass = newPrintOnPass; }
677841Sgblack@eecs.umich.edu
687841Sgblack@eecs.umich.eduunsigned passes() { return _passes; }
697841Sgblack@eecs.umich.eduunsigned failures() { return _failures; }
707841Sgblack@eecs.umich.edu
717841Sgblack@eecs.umich.eduunsigned
727841Sgblack@eecs.umich.eduprintResults()
737841Sgblack@eecs.umich.edu{
747841Sgblack@eecs.umich.edu    cprintf("TEST %s:   %d checks passed, %d checks failed.\n",
757841Sgblack@eecs.umich.edu            _failures ? "FAILED" : "PASSED", _passes, _failures);
767841Sgblack@eecs.umich.edu    return _failures;
777841Sgblack@eecs.umich.edu}
787841Sgblack@eecs.umich.edu
797841Sgblack@eecs.umich.eduvoid
807841Sgblack@eecs.umich.edureset()
817841Sgblack@eecs.umich.edu{
827841Sgblack@eecs.umich.edu    _passes = 0;
837841Sgblack@eecs.umich.edu    _failures = 0;
847841Sgblack@eecs.umich.edu}
857841Sgblack@eecs.umich.edu
867841Sgblack@eecs.umich.eduvoid
877841Sgblack@eecs.umich.edusetCase(const char *newCase)
887841Sgblack@eecs.umich.edu{
897841Sgblack@eecs.umich.edu    _casePrinted = false;
907841Sgblack@eecs.umich.edu    _case = newCase;
917841Sgblack@eecs.umich.edu}
927841Sgblack@eecs.umich.edu
937841Sgblack@eecs.umich.edu} //namespace UnitTest
94