// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*- // Copyright (c) 2001-2007 International Computer Science Institute // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software") // to deal in the Software without restriction, subject to the conditions // listed in the XORP LICENSE file. These conditions include: you must // preserve this copyright notice, and you cannot mention the copyright // holders in advertising related to the Software without their permission. // The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This // notice is a summary of the XORP LICENSE file; the license in that file is // legally binding. #ident "$XORP: xorp/libxorp/test_profile.cc,v 1.8 2007/02/16 22:46:25 pavlin Exp $" #include "libxorp_module.h" #include "xorp.h" #include "debug.h" #include "xlog.h" #include "test_main.hh" #include "exceptions.hh" #include "profile.hh" #include "clock.hh" #include "timeval.hh" #include "timer.hh" #ifndef DEBUG_LOGGING #define DEBUG_LOGGING #endif /* DEBUG_LOGGING */ #ifndef DEBUG_PRINT_FUNCTION_NAME #define DEBUG_PRINT_FUNCTION_NAME #endif /* DEBUG_PRINT_FUNCTION_NAME */ bool test1(TestInfo& info) { Profile p; string ar = "add_route"; p.create(ar); // It shouldn't be possible to create this variable again. try { p.create(ar); DOUT(info) << "Create variable twice!!! " << ar << endl; return false; } catch(PVariableExists& p) { DOUT(info) << "Exception " << p.str() << endl; } catch(...) { DOUT(info) << "Unknown Exception\n"; return false; } // Enable this variable. // XXX - This should enable global profiling. p.enable(ar); // Make sure that this variable is not enabled. string bogus = "bogus"; // Test for an unknown variable. try { if (p.enabled(bogus)) { DOUT(info) << "Testing for a bogus variable succeeded " << bogus << endl; return false; } return false; } catch(PVariableUnknown& p) { DOUT(info) << "Exception " << p.str() << endl; } catch(...) { DOUT(info) << "Unknown Exception\n"; return false; } // Disable this variable. // XXX - This should disable global profiling. p.disable(ar); // Testing for a bogus variable will return false, but no // exception will be thrown as global profiling is now disabled. if (p.enabled(bogus)) { DOUT(info) << "Testing for a bogus variable succeeded " << bogus << endl; return false; } // Try and log to a bogus variable. try { p.log(bogus, c_format("wow")); DOUT(info) << "Testing for a bogus variable succeeded " << bogus << endl; return false; } catch(PVariableUnknown& p) { DOUT(info) << "Exception " << p.str() << endl; } catch(...) { DOUT(info) << "Unknown Exception\n"; return false; } // Try and log to a valid variable that is not enabled. try { p.log(ar, c_format("wow")); DOUT(info) << "Logging to a valid disabled variable worked!!! " << ar << endl; return false; } catch(PVariableNotEnabled& p) { DOUT(info) << "Exception " << p.str() << endl; } catch(...) { DOUT(info) << "Unknown Exception\n"; return false; } // Enable the variable for logging. p.enable(ar); string message = "wow"; // Logging should succeed now. try { p.log(ar, message); DOUT(info) << "Logging succeeded " << ar << endl; } catch(PVariableNotEnabled& p) { DOUT(info) << "Exception " << p.str() << endl; } catch(...) { DOUT(info) << "Unknown Exception\n"; return false; } // Disable the logging. p.disable(ar); // Lock the log. p.lock_log(ar); ProfileLogEntry ple; if (!p.read_log(ar, ple)) { DOUT(info) << "There should be one entry\n"; return false; } if (ple.loginfo() != message) { DOUT(info) << "Expected " << message << " got " << ple.loginfo() <::RefPtr cb; } tests[] = { {"test1", callback(test1)}, {"test2", callback(test2)}, {"test3", callback(test3)}, {"test4", callback(test4)}, }; try { if (test.empty()) { for (size_t i = 0; i < sizeof(tests) / sizeof(struct test); i++) t.run(tests[i].test_name, tests[i].cb); } else { for (size_t i = 0; i < sizeof(tests) / sizeof(struct test); i++) if (test == tests[i].test_name) { t.run(tests[i].test_name, tests[i].cb); return t.exit(); } t.failed("No test with name " + test + " found\n"); } } catch(...) { xorp_catch_standard_exceptions(); } xlog_stop(); xlog_exit(); return t.exit(); }