/* * Copyright (c) 2012 David SiƱuela Pastor, siu.4coders@gmail.com * * 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, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef UNIT_TEST_FRAMEWORK_H #define UNIT_TEST_FRAMEWORK_H /////////////////////////////////////////////////////////////////////////////// // Set macro and log tools // #define XILINX_PLATFORM // #include "lw_printf.h" #include "stdio.h" #define test_printf printf ////////////////////////////////////////////////////////////////////////////// #ifdef __cplusplus extern "C" { #endif #include #define ASSERT_EQUAL(first, second) \ assert_true((first == second), __LINE__, \ "ASSERT_EQUAL(" #first ", " #second ")"); #define ASSERT_NOT_EQUAL(first, second) \ assert_false((first == second), __LINE__, \ "ASSERT_NOT_EQUAL(" #first ", " #second ")"); #define ASSERT_TRUE(value) \ assert_true((value), __LINE__, "ASSERT_TRUE(" #value ")"); #define ASSERT_FALSE(value) \ assert_false((value), __LINE__, "ASSERT_FALSE(" #value ")"); #define ASSERT_ALMOST_EQUAL(first, second, precision) \ assert_almost_equal((first), (second), (precision), __LINE__, \ "ASSERT_ALMOST_EQUAL(" #first ", " #second ", " #precision ")"); #if defined(_WIN32) #include #if defined(_MSC_VER) && _MSC_VER < 1900 #define snprintf _snprintf #define __func__ __FUNCTION__ #endif #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) /* Change POSIX C SOURCE version for pure c99 compilers */ #if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L #undef _POSIX_C_SOURCE #define _POSIX_C_SOURCE 200112L #endif #include /* POSIX flags */ #include /* clock_gettime(), time() */ #include /* gethrtime(), gettimeofday() */ #include #include #include #if defined(__MACH__) && defined(__APPLE__) #include #include #endif #if __GNUC__ >= 5 && !defined(__STDC_VERSION__) #define __func__ __extension__ __FUNCTION__ #endif #else // baremetal application #include // Xilinx #ifdef XILINX_PLATFORM #include "xparameters.h" #if !defined(PLATFORM_MP) && !defined(PLATFORM_RISCV) #include "xtime_l.h" #if defined(PLATFORM_ZYNQ) #define TIMER_CLK_FREQ_HZ (XPAR_CPU_CORTEXA9_0_CPU_CLK_FREQ_HZ / 2) #elif defined(PLATFORM_ZYNQMP) || defined(versal) #if defined(__aarch64__) #define TIMER_CLK_FREQ_HZ XPAR_PSU_CORTEXA53_0_TIMESTAMP_CLK_FREQ #elif defined(__arm__) #define TIMER_CLK_FREQ_HZ (XPAR_PSU_CORTEXR5_0_CPU_CLK_FREQ_HZ / 2) #else #Warning "Unable to define timers for microblaze platform, timer is disabled" #endif #endif #else #Warning "Unable to define timers for microblaze platform, timer is disabled" #endif #endif #endif #include /* Maximum length of last message */ #define TEST_MESSAGE_LEN 1024 /* Accuracy with which floats are compared */ #define TEST_EPSILON 1E-12 /* Misc. counters */ static int unit_run = 0; static int unit_assert = 0; static int unit_fail = 0; static int unit_status = 0; /* Timers */ static double unit_real_timer = 0; static double unit_proc_timer = 0; /* Last message */ static char unit_last_message[TEST_MESSAGE_LEN]; /* Test setup and teardown function pointers */ static void (*unit_setup)(void) = NULL; static void (*unit_teardown)(void) = NULL; /* Definitions */ #define TEST(method_name) static void method_name(void) #define TEST_SUITE(suite_name) static void suite_name(void) #define __SAFE_BLOCK(block) \ do \ { \ block \ } while (0) /* Run test suite and unset setup and teardown functions */ #define RUN_TEST_SUITE(suite_name) __SAFE_BLOCK( \ suite_name(); \ unit_setup = NULL; \ unit_teardown = NULL;) /* Configure setup and teardown functions */ #define TEST_SUITE_CONFIGURE(setup_fun, teardown_fun) __SAFE_BLOCK( \ unit_setup = setup_fun; \ unit_teardown = teardown_fun;) /* Test runner */ #define RUN_TEST(test) __SAFE_BLOCK( \ if (unit_real_timer == 0 && unit_proc_timer == 0) { \ unit_real_timer = timer_real(); \ unit_proc_timer = timer_cpu(); \ } if (unit_setup) (*unit_setup)(); \ unit_status = 0; \ test(); \ unit_run++; \ if (unit_status) { \ unit_fail++; \ test_printf("%s\n\t--FAIL\n", #test); \ test_printf("%s\n", unit_last_message); \ } else { \ test_printf("%s\n\t--PASS\n", #test); \ }(void)fflush(stdout); \ if (unit_teardown)(*unit_teardown)();) /* Report */ #define TEST_REPORT() __SAFE_BLOCK( \ double unit_end_real_timer; \ double unit_end_proc_timer; \ test_printf("\n\n%d tests, %d assertions, %d failures\n", unit_run, unit_assert, unit_fail); \ unit_end_real_timer = timer_real(); \ unit_end_proc_timer = timer_cpu(); \ test_printf("\nFinished in %.8f seconds (real) %.8f seconds (proc)\n\n", \ unit_end_real_timer - unit_real_timer, \ unit_end_proc_timer - unit_proc_timer);) #define TEST_EXIT_CODE unit_fail /* Assertions */ void assert_true(bool value, int line_number, const char *assertion_text) { unit_assert++; if (value) { return; } unit_status = 1; test_printf("In %s, line %d:\n", assertion_text, line_number); test_printf("Expected true, but was false.\n"); } void assert_false(bool value, int line_number, const char *assertion_text) { unit_assert++; if (!value) { return; } unit_status = 1; test_printf("In %s, line %d:\n", assertion_text, line_number); test_printf("Expected false, but was true.\n"); } void assert_almost_equal(double first, double second, double precision, int line_number, const char *assertion_text) { unit_assert++; if (fabs(first - second) <= precision) { return; } unit_status = 1; test_printf("In %s, line %d:\n", assertion_text, line_number); test_printf("Values too far apart: %f and %f.\n", first, second); } /** * Returns the real time, in seconds, or -1.0 if an error occurred. * * Time is measured since an arbitrary and OS-dependent start time. * The returned real time is only useful for computing an elapsed time * between two calls to this function. */ static double timer_real(void) { #if defined(_WIN32) /* Windows 2000 and later. ---------------------------------- */ LARGE_INTEGER Time; LARGE_INTEGER Frequency; QueryPerformanceFrequency(&Frequency); QueryPerformanceCounter(&Time); Time.QuadPart *= 1e6; Time.QuadPart /= Frequency.QuadPart; return (double)Time.QuadPart / 1e6; #elif (defined(__hpux) || defined(hpux)) || ((defined(__sun__) || defined(__sun) || defined(sun)) && (defined(__SVR4) || defined(__svr4__))) /* HP-UX, Solaris. ------------------------------------------ */ return (double)gethrtime() / 1e6; #elif defined(__MACH__) && defined(__APPLE__) /* OSX. ----------------------------------------------------- */ static double timeConvert = 0.0; if (timeConvert == 0.0) { mach_timebase_info_data_t timeBase; (void)mach_timebase_info(&timeBase); timeConvert = (double)timeBase.numer / (double)timeBase.denom / 1e6; } return (double)mach_absolute_time() * timeConvert; #elif defined(_POSIX_VERSION) /* POSIX. --------------------------------------------------- */ struct timeval tm; /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris. ----- */ gettimeofday(&tm, NULL); return (double)tm.tv_sec + (double)tm.tv_usec / 1e6; #else // bare metal platform #if defined(XILINX_PLATFORM) && defined(TIMER_CLK_FREQ_HZ) // Xilinx platform int64_t clocks; XTime_GetTime(&clocks); return ((double)clocks / (double)TIMER_CLK_FREQ_HZ); #endif #endif } /** * Returns the amount of CPU time used by the current process, * in seconds, or -1.0 if an error occurred. */ static double timer_cpu(void) { #if defined(_WIN32) /* Windows -------------------------------------------------- */ FILETIME createTime; FILETIME exitTime; FILETIME kernelTime; FILETIME userTime; /* This approach has a resolution of 1/64 second. Unfortunately, Windows' API does not offer better */ if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime) != 0) { ULARGE_INTEGER userSystemTime; memcpy(&userSystemTime, &userTime, sizeof(ULARGE_INTEGER)); return (double)userSystemTime.QuadPart / 1e6; } #elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__)) /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */ #if defined(CLOCKS_PER_SEC) clock_t cl = clock(); if (cl != (clock_t)-1) return (double)cl / (double)CLOCKS_PER_SEC; #endif #else // bare metal platform return timer_real(); #endif return -1.0; /* Failed. */ } #ifdef __cplusplus } #endif #endif /* UNIT_TEST_FRAMEWORK_H */