From 61bb2c3a16c0c9723bbb6492283e349a8ccdf11f Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Wed, 6 Jul 2011 14:00:31 -0700 Subject: [PATCH] Merge in basic unittest for test framework. --- .bzrignore | 1 + libtest/include.am | 10 ++++-- libtest/test.cc | 7 +++- libtest/test.h | 10 +++--- libtest/unittest.cc | 87 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 libtest/unittest.cc diff --git a/.bzrignore b/.bzrignore index a64e1be4..8f970c35 100644 --- a/.bzrignore +++ b/.bzrignore @@ -103,3 +103,4 @@ libtest/wait docs/text docs/changes tests/cycle +libtest/unittest diff --git a/libtest/include.am b/libtest/include.am index 059ee741..4ce5a568 100644 --- a/libtest/include.am +++ b/libtest/include.am @@ -48,7 +48,7 @@ libtest_libtest_la_SOURCES=\ libtest/framework.cc \ libtest/test.cc libtest_libtest_la_CFLAGS= ${AM_CFLAGS} ${NO_CONVERSION} -DBUILDING_LIBTEST -libtest_libtest_la_CPPFLAGS= ${AM_CPPFLAGS} +libtest_libtest_la_CXXFLAGS= ${AM_CXXFLAGS} ${NO_CONVERSION} -DBUILDING_LIBTEST clearn-var: @rm -f tests/var/log/* @@ -68,6 +68,12 @@ tests/var/tmp: tests/var/run: $(mkdir_p) tests/var/run -noinst_PROGRAMS+= libtest/wait +libtest_unittest_LDADD= \ + libtest/libtest.la +libtest_unittest_SOURCES= \ + libtest/unittest.cc +noinst_PROGRAMS+= libtest/unittest +check_PROGRAMS+= libtest/unittest libtest_wait_SOURCES= libtest/wait.cc +noinst_PROGRAMS+= libtest/wait diff --git a/libtest/test.cc b/libtest/test.cc index b07340e4..d87f1b17 100644 --- a/libtest/test.cc +++ b/libtest/test.cc @@ -50,7 +50,12 @@ const char *default_socket() assert(global_socket[0]); return global_socket; } - + +bool test_is_local() +{ + return (getenv("LIBTEST_LOCAL")); +} + void set_default_socket(const char *socket) { strncpy(global_socket, socket, strlen(socket)); diff --git a/libtest/test.h b/libtest/test.h index 9939886f..4665043e 100644 --- a/libtest/test.h +++ b/libtest/test.h @@ -7,6 +7,10 @@ #pragma once +#ifndef __INTEL_COMPILER +#pragma GCC diagnostic ignored "-Wold-style-cast" +#endif + /** A structure describing the test case. */ @@ -16,10 +20,8 @@ struct test_st { test_callback_fn *test_fn; }; -#define TEST_STRINGIFY(x) #x -#define TEST_TOSTRING(x) TEST_STRINGIFY(x) -#define TEST_AT __FILE__ ":" TEST_TOSTRING(__LINE__) - +LIBTEST_API +bool test_is_local(void); #define test_assert_errno(A) \ do \ diff --git a/libtest/unittest.cc b/libtest/unittest.cc new file mode 100644 index 00000000..e0df62f7 --- /dev/null +++ b/libtest/unittest.cc @@ -0,0 +1,87 @@ +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * uTest self unit test. + * + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include +#include + +static test_return_t test_success_test(void *) +{ + return TEST_SUCCESS; +} + +static test_return_t local_test(void *) +{ + char buffer[sizeof("LIBTEST_LOCAL=1")]; + + snprintf(buffer, sizeof(buffer), "%s", "LIBTEST_LOCAL=1"); + test_compare(0, putenv(buffer)); + + test_true(test_is_local()); + + return TEST_SUCCESS; +} + +static test_return_t local_not_test(void *) +{ + test_compare(0, unsetenv("LIBTEST_LOCAL")); + + test_false(test_is_local()); + + return TEST_SUCCESS; +} + +test_st tests_log[] ={ + {"TEST_SUCCESS", 0, test_success_test }, + {0, 0, 0} +}; + +test_st local_log[] ={ + {"test_is_local()", 0, local_test }, + {"test_is_local(NOT)", 0, local_not_test }, + {0, 0, 0} +}; + +collection_st collection[] ={ + {"return values", 0, 0, tests_log}, + {"local", 0, 0, local_log}, + {0, 0, 0, 0} +}; + +void get_world(Framework *arg) +{ + arg->collections= collection; +} -- 2.30.2