Home | History | Annotate | Download | only in unittest
      1 /*
      2   This file is part of Valgrind, a dynamic binary instrumentation
      3   framework.
      4 
      5   Copyright (C) 2008-2008 Google Inc
      6      opensource (at) google.com
      7 
      8   This program is free software; you can redistribute it and/or
      9   modify it under the terms of the GNU General Public License as
     10   published by the Free Software Foundation; either version 2 of the
     11   License, or (at your option) any later version.
     12 
     13   This program is distributed in the hope that it will be useful, but
     14   WITHOUT ANY WARRANTY; without even the implied warranty of
     15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16   General Public License for more details.
     17 
     18   You should have received a copy of the GNU General Public License
     19   along with this program; if not, write to the Free Software
     20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
     21   02111-1307, USA.
     22 
     23   The GNU General Public License is contained in the file COPYING.
     24 */
     25 
     26 /* Author: Konstantin Serebryany <opensource (at) google.com>
     27 
     28  This file contains a simple test suite for some of our old unit-tests.
     29  These tests are likely to be moved to googletest framework over time.
     30 */
     31 #ifndef OLD_TEST_SUITE_H__
     32 #define OLD_TEST_SUITE_H__
     33 
     34 #include <map>
     35 #include <set>
     36 #include <cstring>
     37 
     38 #include "test_utils.h"
     39 
     40 typedef void (*void_func_void_t)(void);
     41 
     42 enum TEST_FLAG {
     43   FEATURE           = 1 << 0,
     44   STABILITY         = 1 << 1,
     45   PERFORMANCE       = 1 << 2,
     46   EXCLUDE_FROM_ALL  = 1 << 3,
     47   NEEDS_ANNOTATIONS = 1 << 4,
     48   RACE_DEMO         = 1 << 5,
     49   MEMORY_USAGE      = 1 << 6,
     50   PRINT_STATS       = 1 << 7
     51 };
     52 
     53 // Put everything into stderr.
     54 extern Mutex printf_mu;
     55 #ifndef WIN32
     56 #define printf(args...) \
     57     do{ \
     58       printf_mu.Lock();\
     59       fprintf(stderr, args);\
     60       printf_mu.Unlock(); \
     61     }while(0)
     62 #endif
     63 
     64 struct Test{
     65   void_func_void_t f_;
     66   int id_;
     67   int flags_;
     68   Test(void_func_void_t f, int id, int flags)
     69     : f_(f)
     70     , id_(id)
     71     , flags_(flags)
     72   {}
     73   Test() : f_(0), flags_(0) {}
     74   void Run() {
     75      if (flags_ & PERFORMANCE) {
     76         long start = GetTimeInMs();
     77         f_();
     78         long end = GetTimeInMs();
     79         printf("*RESULT test%d: time= %4ld ms\n", id_, end - start);
     80 //         printf ("Time: %4ldms\n", end-start);
     81      } else
     82         f_();
     83   }
     84 };
     85 
     86 extern std::map<int, Test> *TheMapOfTests;
     87 
     88 struct TestAdder {
     89   TestAdder(void_func_void_t f, int id, int flags = FEATURE) {
     90     if (TheMapOfTests == NULL)
     91       TheMapOfTests = new std::map<int, Test>;
     92     CHECK(TheMapOfTests->count(id) == 0);
     93     (*TheMapOfTests)[id] = Test(f, id, flags);
     94   }
     95 };
     96 
     97 #define REGISTER_TEST(f, id)         TestAdder add_test_##id (f, id);
     98 #define REGISTER_TEST2(f, id, flags) TestAdder add_test_##id (f, id, flags);
     99 
    100 #endif  // OLD_TEST_SUITE_H__
    101 // End {{{1
    102  // vim:shiftwidth=2:softtabstop=2:expandtab:foldmethod=marker
    103