Skip to content

Commit

Permalink
tests: Normalize pass/fail strings for easier post-processing.
Browse files Browse the repository at this point in the history
Allow building libxorp/tests/* files with enable_tests=yes
for faster debugging & development.

Signed-off-by: Ben Greear <greearb@candelatech.com>
  • Loading branch information
greearb committed Jun 18, 2010
1 parent 9edc3ea commit aedaca0
Show file tree
Hide file tree
Showing 28 changed files with 328 additions and 928 deletions.
9 changes: 9 additions & 0 deletions xorp/libxorp/tests/SConscript
Expand Up @@ -58,8 +58,17 @@ tests = [
if not (env.has_key('disable_profile') and env['disable_profile']):
tests.append('profile')

libxorptestpath = '$exec_prefix/libxorp/tests'

test_targets = []

for t in tests:
test_targets.append(env.AutoTest(target = 'test_%s' % t,
source = 'test_%s.cc' % t))

if env['enable_tests']:
env.Alias('install', env.InstallProgram(libxorptestpath, 'test_%s' %t))

if env['enable_tests']:
Default(test_targets)

25 changes: 20 additions & 5 deletions xorp/libxorp/tests/test_asyncio.cc
Expand Up @@ -35,6 +35,16 @@

#include "asyncio.hh"

static bool s_verbose = false;
bool verbose() { return s_verbose; }
void set_verbose(bool v) { s_verbose = v; }

static int s_failures = 0;
bool failures() { return s_failures; }
void incr_failures() { s_failures++; }

#include "xorp_tests.hh"


static const int TIMEOUT_MS = 2000;
static const int MAX_ITERS = 50;
Expand Down Expand Up @@ -240,17 +250,20 @@ run_test()

xsock_t s[2]; // pair of sockets - one for read, one for write
if (local_comm_sock_pair(AF_UNIX, SOCK_STREAM, 0, s) != XORP_OK) {
puts("Failed to open socket pair");
print_failed("Failed to open socket pair");
exit(1);
}
print_passed("open socket");
if (local_comm_sock_set_blocking(s[0], 0) != XORP_OK) {
puts("Failed to set socket non-blocking");
print_failed("Failed to set socket non-blocking");
exit(1);
}
print_passed("set nonblock");
if (local_comm_sock_set_blocking(s[1], 0) != XORP_OK) {
puts("Failed to set socket non-blocking");
print_failed("Failed to set socket non-blocking");
exit(1);
}
print_passed("set nonblock[1]");

AsyncFileWriter afw(e, s[0]);
AsyncFileReader afr(e, s[1]);
Expand Down Expand Up @@ -316,8 +329,10 @@ run_test()
while (eof_test_done == false)
e.run();

printf("\nTransfered %u bytes between AsyncFileWriter and "
"AsyncFileReader.\n", XORP_UINT_CAST(bytes_transferred));
char buf[500];
snprintf(buf, 500, "\nTransfered %u bytes between AsyncFileWriter and "
"AsyncFileReader.\n", XORP_UINT_CAST(bytes_transferred));
print_passed(buf);
}

int
Expand Down
18 changes: 14 additions & 4 deletions xorp/libxorp/tests/test_callback.cc
Expand Up @@ -31,6 +31,16 @@
#include "xorp.h"
#include "callback.hh"

static bool s_verbose = false;
bool verbose() { return s_verbose; }
void set_verbose(bool v) { s_verbose = v; }

static int s_failures = 0;
bool failures() { return s_failures; }
void incr_failures() { s_failures++; }

#include "libxorp/xorp_tests.hh"

typedef XorpCallback1<void, int>::RefPtr TestCallback;

class Widget {
Expand Down Expand Up @@ -66,7 +76,7 @@ main()

// The callback should be empty
if (!cbm.is_empty()) {
printf("ERROR: callback is not empty\n");
print_failed("callback is not empty");
return -1;
}

Expand All @@ -75,7 +85,7 @@ main()

// Test that the callback is not empty
if (cbm.is_empty()) {
printf("ERROR: callback is empty\n");
print_failed("ERROR: callback is empty");
return -1;
}

Expand Down Expand Up @@ -109,7 +119,7 @@ main()
}

if (counter != stop_point) {
printf("ERROR: safe callback executed after object deletion\n");
print_failed("safe callback executed after object deletion");
return -1;
}

Expand All @@ -127,6 +137,6 @@ main()
}
delete sw;
}

print_passed("Callback tests");
return (0);
}
58 changes: 1 addition & 57 deletions xorp/libxorp/tests/test_config_param.cc
Expand Up @@ -33,7 +33,6 @@

#include "config_param.hh"


//
// XXX: MODIFY FOR YOUR TEST PROGRAM
//
Expand All @@ -52,62 +51,7 @@ static int s_failures = 0;
bool failures() { return s_failures; }
void incr_failures() { s_failures++; }



//
// printf(3)-like facility to conditionally print a message if verbosity
// is enabled.
//
#define verbose_log(x...) _verbose_log(__FILE__,__LINE__, x)

#define _verbose_log(file, line, x...) \
do { \
if (verbose()) { \
printf("From %s:%d: ", file, line); \
printf(x); \
} \
} while(0)


//
// Test and print a message whether two strings are lexicographically same.
// The strings can be either C or C++ style.
//
#define verbose_match(s1, s2) \
_verbose_match(__FILE__, __LINE__, s1, s2)

bool
_verbose_match(const char* file, int line, const string& s1, const string& s2)
{
bool match = s1 == s2;

_verbose_log(file, line, "Comparing %s == %s : %s\n",
s1.c_str(), s2.c_str(), match ? "OK" : "FAIL");
if (match == false)
incr_failures();
return match;
}


//
// Test and print a message whether a condition is true.
//
// The first argument is the condition to test.
// The second argument is a string with a brief description of the tested
// condition.
//
#define verbose_assert(cond, desc) \
_verbose_assert(__FILE__, __LINE__, cond, desc)

bool
_verbose_assert(const char* file, int line, bool cond, const string& desc)
{
_verbose_log(file, line,
"Testing %s : %s\n", desc.c_str(), cond ? "OK" : "FAIL");
if (cond == false)
incr_failures();
return cond;
}
#include "libxorp/xorp_tests.hh"


/**
Expand Down
58 changes: 1 addition & 57 deletions xorp/libxorp/tests/test_heap.cc
Expand Up @@ -52,63 +52,7 @@ static int s_failures = 0;
bool failures() { return s_failures; }
void incr_failures() { s_failures++; }



//
// printf(3)-like facility to conditionally print a message if verbosity
// is enabled.
//
#define verbose_log(x...) _verbose_log(__FILE__,__LINE__, x)

#define _verbose_log(file, line, x...) \
do { \
if (verbose()) { \
printf("From %s:%d: ", file, line); \
printf(x); \
} \
} while(0)


//
// Test and print a message whether two strings are lexicographically same.
// The strings can be either C or C++ style.
//
#define verbose_match(s1, s2) \
_verbose_match(__FILE__, __LINE__, s1, s2)

bool
_verbose_match(const char* file, int line, const string& s1, const string& s2)
{
bool match = s1 == s2;

_verbose_log(file, line, "Comparing %s == %s : %s\n",
s1.c_str(), s2.c_str(), match ? "OK" : "FAIL");
if (match == false)
incr_failures();
return match;
}


//
// Test and print a message whether a condition is true.
//
// The first argument is the condition to test.
// The second argument is a string with a brief description of the tested
// condition.
//
#define verbose_assert(cond, desc) \
_verbose_assert(__FILE__, __LINE__, cond, desc)

bool
_verbose_assert(const char* file, int line, bool cond, const string& desc)
{
_verbose_log(file, line,
"Testing %s : %s\n", desc.c_str(), cond ? "OK" : "FAIL");
if (cond == false)
incr_failures();
return cond;
}

#include "libxorp/xorp_tests.hh"

/**
* Print program info to output stream.
Expand Down
48 changes: 23 additions & 25 deletions xorp/libxorp/tests/test_ipnet.cc
Expand Up @@ -37,18 +37,16 @@
// ----------------------------------------------------------------------------
// Verbose output

static bool s_verbose = false;

static bool s_verbose = false;
bool verbose() { return s_verbose; }
void set_verbose(bool v) { s_verbose = v; }

#define verbose_log(x...) \
do { \
if (verbose()) { \
printf("From %s:%d: ", __FILE__, __LINE__); \
printf(x); \
} \
} while(0)
static int s_failures = 0;
bool failures() { return s_failures; }
void incr_failures() { s_failures++; }

#include "libxorp/xorp_tests.hh"

// ----------------------------------------------------------------------------
// The tests
Expand All @@ -70,29 +68,29 @@ v4_serialization_test()
for (size_t i = 0; i < sizeof(srep) / sizeof(srep[0]); i++) {
IPv4Net n(srep[i].net);
if (n.prefix_len() != srep[i].prefix_len) {
verbose_log("item %u bad prefix_len %u\n", XORP_UINT_CAST(i),
verbose_log("Test Failed: item %u bad prefix_len %u\n", XORP_UINT_CAST(i),
XORP_UINT_CAST(n.prefix_len()));
return false;
} else if (n.masked_addr() != srep[i].v4) {
verbose_log("item %u bad addr %s != %s\n",
verbose_log("Test Failed: item %u bad addr %s != %s\n",
XORP_UINT_CAST(i), n.masked_addr().str().c_str(),
srep[i].v4.str().c_str());
return false;
} else if (n.netmask() != IPv4::make_prefix(n.prefix_len())) {
verbose_log("item %u bad netmask %s != %s\n",
verbose_log("Test Failed: item %u bad netmask %s != %s\n",
XORP_UINT_CAST(i), n.netmask().str().c_str(),
IPv4::make_prefix(n.prefix_len()).str().c_str());
return false;
}

IPv4Net u (n.str().c_str());
if (u != n) {
verbose_log("item %u to string and back failed.",
verbose_log("Test Failed: item %u to string and back failed.",
XORP_UINT_CAST(i));
return false;
}
}
verbose_log("Passed serialization test.\n");
verbose_log("Test Passed: serialization test.\n");
return true;
}

Expand All @@ -105,13 +103,13 @@ v4_less_than_test()
IPv4Net lower(d[i - 1]);
IPv4Net upper(d[i]);
if (!(lower < upper)) {
verbose_log("%s is not less than %s\n",
verbose_log("Test Failed: %s is not less than %s\n",
lower.str().c_str(),
upper.str().c_str());
return false;
}
}
verbose_log("Passed operator< test.\n");
verbose_log("Test Passed: operator< test.\n");
return true;
}

Expand All @@ -126,34 +124,34 @@ v4_is_unicast_test()
IPv4Net odd4("128.0.0.0/2"); // only unicast, should be valid
IPv4Net deflt("0.0.0.0/0"); // default route, is valid
if (!uni.is_unicast()) {
verbose_log("%s failed is_unicast test.\n", uni.str().c_str());
verbose_log("Test Failed: %s failed is_unicast test.\n", uni.str().c_str());
return false;
}
if (multi.is_unicast()) {
verbose_log("%s failed is_unicast test.\n", multi.str().c_str());
verbose_log("Test Failed: %s failed is_unicast test.\n", multi.str().c_str());
return false;
}
if (odd1.is_unicast()) {
verbose_log("%s failed is_unicast test.\n", odd1.str().c_str());
verbose_log("Test Failed: %s failed is_unicast test.\n", odd1.str().c_str());
return false;
}
if (odd2.is_unicast()) {
verbose_log("%s failed is_unicast test.\n", odd2.str().c_str());
verbose_log("Test Failed: %s failed is_unicast test.\n", odd2.str().c_str());
return false;
}
if (!odd3.is_unicast()) {
verbose_log("%s failed is_unicast test.\n", odd3.str().c_str());
verbose_log("Test Failed: %s failed is_unicast test.\n", odd3.str().c_str());
return false;
}
if (!odd4.is_unicast()) {
verbose_log("%s failed is_unicast test.\n", odd4.str().c_str());
verbose_log("Test Failed: %s failed is_unicast test.\n", odd4.str().c_str());
return false;
}
if (!deflt.is_unicast()) {
verbose_log("%s failed is_unicast test.\n", deflt.str().c_str());
verbose_log("Test Failed: %s failed is_unicast test.\n", deflt.str().c_str());
return false;
}
verbose_log("Passed is_unicast test.\n");
verbose_log("Test Passed: is_unicast test.\n");
return true;
}

Expand All @@ -178,12 +176,12 @@ v4_overlap_test()
IPv4Net u(a, p);
IPv4Net v(b, p);
if (u.is_overlap(v) != (p <= data[i].overlap)) {
verbose_log("bad overlap %u\n", XORP_UINT_CAST(p));
verbose_log("Test Failed: bad overlap %u\n", XORP_UINT_CAST(p));
return -1;
}
}
}
verbose_log("Passed overlap test.\n");
verbose_log("Test Passed: overlap test.\n");
return true;
}

Expand Down

0 comments on commit aedaca0

Please sign in to comment.