xorp

xorp_tests.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8:
00003 
00004 // Copyright (c) 2010-2011 XORP, Inc and Others
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU Lesser General Public License, Version
00008 // 2.1, June 1999 as published by the Free Software Foundation.
00009 // Redistribution and/or modification of this program under the terms of
00010 // any other version of the GNU Lesser General Public License is not
00011 // permitted.
00012 // 
00013 // This program is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00016 // see the GNU Lesser General Public License, Version 2.1, a copy of
00017 // which can be found in the XORP LICENSE.lgpl file.
00018 // 
00019 // XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00020 // http://xorp.net
00021 
00022 #ifndef __LIBXORP_TESTS_HH__
00023 #define __LIBXORP_TESTS_HH__
00024 
00025 #define print_passed(a) cout << "Test Passed (" << a << ")  " << __FUNCTION__ \
00026                              << " " << __FILE__ << ":" << __LINE__ << endl;
00027 
00028 #define print_failed(a) cerr << "Test Failed (" << a << ")  " << __FUNCTION__ \
00029                              << " " << __FILE__ << ":" << __LINE__ << endl;
00030 
00031 
00032 //
00033 // printf(3)-like facility to conditionally print a message if verbosity
00034 // is enabled.
00035 //
00036 #define verbose_log(x...) _verbose_log(__FILE__,__LINE__, x)
00037 
00038 #define _verbose_log(file, line, x...)                  \
00039 do {                                    \
00040     if (verbose()) {                            \
00041     printf("From %s:%d: ", file, line);             \
00042     printf(x);                          \
00043     }                                   \
00044 } while(0)
00045 
00046 
00047 
00048 //
00049 // Test and print a message whether two strings are lexicographically same.
00050 // The strings can be either C or C++ style.
00051 //
00052 #define verbose_match(s1, s2)                       \
00053     _verbose_match(__FILE__, __LINE__, s1, s2)
00054 
00055 bool
00056 _verbose_match(const char* file, int line, const string& s1, const string& s2)
00057 {
00058     bool match = s1 == s2;
00059     
00060     _verbose_log(file, line, "%s:  Comparing %s == %s\n",
00061          match ? "Test Passed" : "Test Failed",
00062          s1.c_str(), s2.c_str());
00063     if (match == false)
00064     incr_failures();
00065     return match;
00066 }
00067 
00068 
00069 //
00070 // Test and print a message whether a condition is true.
00071 //
00072 // The first argument is the condition to test.
00073 // The second argument is a string with a brief description of the tested
00074 // condition.
00075 //
00076 #define verbose_assert(cond, desc)                  \
00077     _verbose_assert(__FILE__, __LINE__, cond, desc)
00078 
00079 bool
00080 _verbose_assert(const char* file, int line, bool cond, const string& desc)
00081 {
00082     _verbose_log(file, line,
00083          "%s:  Testing %s\n", cond ? "Test Passed" : "Test Failed", desc.c_str());
00084     if (cond == false)
00085     incr_failures();
00086     return cond;
00087 }
00088 
00089 #endif // __LIBXORP_TESTS_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations