aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/libbb.h146
-rw-r--r--include/platform.h3
2 files changed, 149 insertions, 0 deletions
diff --git a/include/libbb.h b/include/libbb.h
index c80cd80d7..f5ecc025f 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -347,6 +347,8 @@ enum { /* DO NOT CHANGE THESE VALUES! cp.c, mv.c, install.c depend on them. */
347 FILEUTILS_SET_SECURITY_CONTEXT = 1 << 10, 347 FILEUTILS_SET_SECURITY_CONTEXT = 1 << 10,
348#endif 348#endif
349 FILEUTILS_IGNORE_CHMOD_ERR = 1 << 11, 349 FILEUTILS_IGNORE_CHMOD_ERR = 1 << 11,
350 /* -v */
351 FILEUTILS_VERBOSE = (1 << 12) * ENABLE_FEATURE_VERBOSE,
350}; 352};
351#define FILEUTILS_CP_OPTSTR "pdRfilsLH" IF_SELINUX("c") 353#define FILEUTILS_CP_OPTSTR "pdRfilsLH" IF_SELINUX("c")
352extern int remove_file(const char *path, int flags) FAST_FUNC; 354extern int remove_file(const char *path, int flags) FAST_FUNC;
@@ -742,6 +744,15 @@ extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p) FAST
742/* Never returns NULL */ 744/* Never returns NULL */
743extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC; 745extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p) FAST_FUNC RETURNS_MALLOC;
744 746
747#if defined ARG_MAX
748# define bb_arg_max() ((unsigned)ARG_MAX)
749#elif defined _SC_ARG_MAX
750unsigned bb_arg_max(void) FAST_FUNC;
751#else
752# define bb_arg_max() ((unsigned)(32 * 1024))
753#endif
754unsigned bb_clk_tck(void) FAST_FUNC;
755
745#define SEAMLESS_COMPRESSION (0 \ 756#define SEAMLESS_COMPRESSION (0 \
746 || ENABLE_FEATURE_SEAMLESS_XZ \ 757 || ENABLE_FEATURE_SEAMLESS_XZ \
747 || ENABLE_FEATURE_SEAMLESS_LZMA \ 758 || ENABLE_FEATURE_SEAMLESS_LZMA \
@@ -1957,6 +1968,141 @@ static ALWAYS_INLINE unsigned char bb_ascii_tolower(unsigned char a)
1957#define isprint_asciionly(a) ((unsigned)((a) - 0x20) <= 0x7e - 0x20) 1968#define isprint_asciionly(a) ((unsigned)((a) - 0x20) <= 0x7e - 0x20)
1958 1969
1959 1970
1971/* Simple unit-testing framework */
1972
1973typedef void (*bbunit_testfunc)(void);
1974
1975struct bbunit_listelem {
1976 struct bbunit_listelem* next;
1977 const char* name;
1978 bbunit_testfunc testfunc;
1979};
1980
1981void bbunit_registertest(struct bbunit_listelem* test);
1982void bbunit_settestfailed(void);
1983
1984#define BBUNIT_DEFINE_TEST(NAME) \
1985 static void bbunit_##NAME##_test(void); \
1986 static struct bbunit_listelem bbunit_##NAME##_elem = { \
1987 .name = #NAME, \
1988 .testfunc = bbunit_##NAME##_test, \
1989 }; \
1990 static void INIT_FUNC bbunit_##NAME##_register(void) \
1991 { \
1992 bbunit_registertest(&bbunit_##NAME##_elem); \
1993 } \
1994 static void bbunit_##NAME##_test(void)
1995
1996/*
1997 * Both 'goto bbunit_end' and 'break' are here only to get rid
1998 * of compiler warnings.
1999 */
2000#define BBUNIT_ENDTEST \
2001 do { \
2002 goto bbunit_end; \
2003 bbunit_end: \
2004 break; \
2005 } while (0)
2006
2007#define BBUNIT_PRINTASSERTFAIL \
2008 do { \
2009 bb_error_msg( \
2010 "[ERROR] Assertion failed in file %s, line %d", \
2011 __FILE__, __LINE__); \
2012 } while (0)
2013
2014#define BBUNIT_ASSERTION_FAILED \
2015 do { \
2016 bbunit_settestfailed(); \
2017 goto bbunit_end; \
2018 } while (0)
2019
2020/*
2021 * Assertions.
2022 * For now we only offer assertions which cause tests to fail
2023 * immediately. In the future 'expects' might be added too -
2024 * similar to those offered by the gtest framework.
2025 */
2026#define BBUNIT_ASSERT_EQ(EXPECTED, ACTUAL) \
2027 do { \
2028 if ((EXPECTED) != (ACTUAL)) { \
2029 BBUNIT_PRINTASSERTFAIL; \
2030 bb_error_msg("[ERROR] '%s' isn't equal to '%s'", \
2031 #EXPECTED, #ACTUAL); \
2032 BBUNIT_ASSERTION_FAILED; \
2033 } \
2034 } while (0)
2035
2036#define BBUNIT_ASSERT_NOTEQ(EXPECTED, ACTUAL) \
2037 do { \
2038 if ((EXPECTED) == (ACTUAL)) { \
2039 BBUNIT_PRINTASSERTFAIL; \
2040 bb_error_msg("[ERROR] '%s' is equal to '%s'", \
2041 #EXPECTED, #ACTUAL); \
2042 BBUNIT_ASSERTION_FAILED; \
2043 } \
2044 } while (0)
2045
2046#define BBUNIT_ASSERT_NOTNULL(PTR) \
2047 do { \
2048 if ((PTR) == NULL) { \
2049 BBUNIT_PRINTASSERTFAIL; \
2050 bb_error_msg("[ERROR] '%s' is NULL!", #PTR); \
2051 BBUNIT_ASSERTION_FAILED; \
2052 } \
2053 } while (0)
2054
2055#define BBUNIT_ASSERT_NULL(PTR) \
2056 do { \
2057 if ((PTR) != NULL) { \
2058 BBUNIT_PRINTASSERTFAIL; \
2059 bb_error_msg("[ERROR] '%s' is not NULL!", #PTR); \
2060 BBUNIT_ASSERTION_FAILED; \
2061 } \
2062 } while (0)
2063
2064#define BBUNIT_ASSERT_FALSE(STATEMENT) \
2065 do { \
2066 if ((STATEMENT)) { \
2067 BBUNIT_PRINTASSERTFAIL; \
2068 bb_error_msg("[ERROR] Statement '%s' evaluated to true!", \
2069 #STATEMENT); \
2070 BBUNIT_ASSERTION_FAILED; \
2071 } \
2072 } while (0)
2073
2074#define BBUNIT_ASSERT_TRUE(STATEMENT) \
2075 do { \
2076 if (!(STATEMENT)) { \
2077 BBUNIT_PRINTASSERTFAIL; \
2078 bb_error_msg("[ERROR] Statement '%s' evaluated to false!", \
2079 #STATEMENT); \
2080 BBUNIT_ASSERTION_FAILED; \
2081 } \
2082 } while (0)
2083
2084#define BBUNIT_ASSERT_STREQ(STR1, STR2) \
2085 do { \
2086 if (strcmp(STR1, STR2) != 0) { \
2087 BBUNIT_PRINTASSERTFAIL; \
2088 bb_error_msg("[ERROR] Strings '%s' and '%s' " \
2089 "are not the same", STR1, STR2); \
2090 BBUNIT_ASSERTION_FAILED; \
2091 } \
2092 } while (0)
2093
2094#define BBUNIT_ASSERT_STRNOTEQ(STR1, STR2) \
2095 do { \
2096 if (strcmp(STR1, STR2) == 0) { \
2097 BBUNIT_PRINTASSERTFAIL; \
2098 bb_error_msg("[ERROR] Strings '%s' and '%s' " \
2099 "are the same, but were " \
2100 "expected to differ", STR1, STR2); \
2101 BBUNIT_ASSERTION_FAILED; \
2102 } \
2103 } while (0)
2104
2105
1960POP_SAVED_FUNCTION_VISIBILITY 2106POP_SAVED_FUNCTION_VISIBILITY
1961 2107
1962#endif 2108#endif
diff --git a/include/platform.h b/include/platform.h
index 884c6e95f..d9b82b2ed 100644
--- a/include/platform.h
+++ b/include/platform.h
@@ -85,6 +85,9 @@
85# define UNUSED_PARAM_RESULT 85# define UNUSED_PARAM_RESULT
86#endif 86#endif
87 87
88/* used by unit test machinery to run registration functions before calling main() */
89#define INIT_FUNC __attribute__ ((constructor))
90
88/* -fwhole-program makes all symbols local. The attribute externally_visible 91/* -fwhole-program makes all symbols local. The attribute externally_visible
89 * forces a symbol global. */ 92 * forces a symbol global. */
90#if __GNUC_PREREQ(4,1) 93#if __GNUC_PREREQ(4,1)