cleanup, code style

This commit is contained in:
Gwendolyn 2022-01-11 20:01:15 +01:00
parent d163648a4d
commit 083892db7a
17 changed files with 173 additions and 143 deletions

View file

@ -4,6 +4,7 @@ target_link_libraries(tested-code xtest-assert)
function(xtest_define_example NAME)
set(TARGET_NAME "example-${NAME}")
add_executable(${TARGET_NAME} ${NAME}.c ${ARGN})
target_compile_options(${TARGET_NAME} PRIVATE -Wall -Wextra -pedantic)
if (NAME STREQUAL "all")
target_compile_definitions(${TARGET_NAME} PRIVATE XTEST_ALL_EXAMPLES)
endif ()

View file

@ -1,15 +1,5 @@
#include "xtest.h"
void example_assertions();
void example_expect_assertions();
void example_fail();
void example_float();
void example_groups();
void example_parameterized();
void example_skip();
#include "examples.h"
XTEST_MAIN {
xtest_run_group(example_assertions);
@ -18,5 +8,6 @@ XTEST_MAIN {
xtest_run_group(example_float);
xtest_run_group(example_groups);
xtest_run_group(example_parameterized);
xtest_run_group(example_prng);
xtest_run_group(example_skip);
}

View file

@ -1,8 +1,8 @@
#include <stddef.h>
#include "xtest.h"
#include "source.h"
#include "examples.h"
void test_assert() {
static void test_assert() {
int i1 = 1;
int i2 = 1;
int i3 = 2;
@ -32,7 +32,7 @@ void test_assert() {
xtest_assert(ret_1());
}
void test_assert_is() {
static void test_assert_is() {
int i1 = 1;
int i2 = 1;
int i3 = 2;
@ -66,7 +66,7 @@ void test_assert_is() {
xtest_assert_is_not(b1, b3);
}
void test_assert_str_is() {
static void test_assert_str_is() {
const char *s1 = "ret_1";
const char *s2 = "ret_1";
const char *s3 = "bar";
@ -75,7 +75,7 @@ void test_assert_str_is() {
xtest_assert_str_is_not(s1, s3);
}
void test_assert_mem_is() {
static void test_assert_mem_is() {
char m1[100];
char m2[100];
for (int i = 0; i < 100; ++i) {
@ -91,7 +91,7 @@ void test_assert_mem_is() {
xtest_assert_mem_is_not(m1, m2, 100);
}
void example_assertions() {
void example_assertions(void) {
xtest_run(test_assert);
xtest_run(test_assert_is);
xtest_run(test_assert_str_is);

11
examples/examples.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef XTEST_EXAMPLES_H
#define XTEST_EXAMPLES_H
void example_assertions(void);
void example_expect_assertions(void);
void example_fail(void);
void example_float(void);
void example_groups(void);
void example_parameterized(void);
void example_prng(void);
void example_skip(void);
#endif //XTEST_EXAMPLES_H

View file

@ -1,29 +1,30 @@
#include "xtest.h"
#include "source.h"
#include "examples.h"
void test_simple() {
static void test_simple() {
xtest_expect_assertion_failure();
div(10, 0);
}
void test_no_failure() {
div(10,1);
static void test_no_failure() {
div(10, 1);
}
void test_no_failure_2() {
div(10,2);
static void test_no_failure_2() {
div(10, 2);
}
void test_will_fail() {
static void test_will_fail() {
xtest_expect_assertion_failure();
div(10,1);
div(10, 1);
}
void test_will_fail_2() {
div(10,0);
static void test_will_fail_2() {
div(10, 0);
}
void test_other_failure() {
static void test_other_failure() {
xtest_expect_assertion_failure();
xtest_assert(0);
div(10, 0);

View file

@ -1,136 +1,136 @@
#include <stddef.h>
#include "xtest.h"
#include "examples.h"
void fail_assert() {
static void fail_assert() {
xtest_assert(0);
}
void fail_assert_is_char() {
xtest_assert_is((char)'a', (char)'b');
static void fail_assert_is_char() {
xtest_assert_is((char) 'a', (char) 'b');
}
void fail_assert_is_not_char() {
xtest_assert_is_not((char)'a', (char)'a');
static void fail_assert_is_not_char() {
xtest_assert_is_not((char) 'a', (char) 'a');
}
void fail_assert_is_signed_char() {
xtest_assert_is((signed char)'a', (signed char)'b');
static void fail_assert_is_signed_char() {
xtest_assert_is((signed char) 'a', (signed char) 'b');
}
void fail_assert_is_not_signed_char() {
xtest_assert_is_not((signed char)'a', (signed char)'a');
static void fail_assert_is_not_signed_char() {
xtest_assert_is_not((signed char) 'a', (signed char) 'a');
}
void fail_assert_is_unsigned_char() {
xtest_assert_is((unsigned char)'a', (unsigned char)'b');
static void fail_assert_is_unsigned_char() {
xtest_assert_is((unsigned char) 'a', (unsigned char) 'b');
}
void fail_assert_is_not_unsigned_char() {
xtest_assert_is_not((unsigned char)'a', (unsigned char)'a');
static void fail_assert_is_not_unsigned_char() {
xtest_assert_is_not((unsigned char) 'a', (unsigned char) 'a');
}
void fail_assert_is_short() {
static void fail_assert_is_short() {
xtest_assert_is((short) 1, (short) 2);
}
void fail_assert_is_not_short() {
static void fail_assert_is_not_short() {
xtest_assert_is_not((short) 1, (short) 1);
}
void fail_assert_is_ushort() {
static void fail_assert_is_ushort() {
xtest_assert_is((unsigned short) 1, (unsigned short) 2);
}
void fail_assert_is_not_ushort() {
static void fail_assert_is_not_ushort() {
xtest_assert_is_not((unsigned short) 1, (unsigned short) 1);
}
void fail_assert_is_int() {
static void fail_assert_is_int() {
xtest_assert_is((int) 1, (int) 2);
}
void fail_assert_is_not_int() {
static void fail_assert_is_not_int() {
xtest_assert_is_not((int) 1, (int) 1);
}
void fail_assert_is_uint() {
static void fail_assert_is_uint() {
xtest_assert_is((unsigned int) 1, (unsigned int) 2);
}
void fail_assert_is_not_uint() {
static void fail_assert_is_not_uint() {
xtest_assert_is_not((unsigned int) 1, (unsigned int) 1);
}
void fail_assert_is_long() {
static void fail_assert_is_long() {
xtest_assert_is((short) 1, (short) 2);
}
void fail_assert_is_not_long() {
static void fail_assert_is_not_long() {
xtest_assert_is_not((long) 1, (long) 1);
}
void fail_assert_is_ulong() {
static void fail_assert_is_ulong() {
xtest_assert_is((unsigned long) 1, (unsigned long) 2);
}
void fail_assert_is_not_ulong() {
static void fail_assert_is_not_ulong() {
xtest_assert_is_not((unsigned long) 1, (unsigned long) 1);
}
void fail_assert_is_longlong() {
static void fail_assert_is_longlong() {
xtest_assert_is((long long) 1, (long long) 2);
}
void fail_assert_is_not_longlong() {
static void fail_assert_is_not_longlong() {
xtest_assert_is_not((long long) 1, (long long) 1);
}
void fail_assert_is_ulonglong() {
static void fail_assert_is_ulonglong() {
xtest_assert_is((unsigned long long) 1, (unsigned long long) 2);
}
void fail_assert_is_not_ulonglong() {
static void fail_assert_is_not_ulonglong() {
xtest_assert_is_not((unsigned long long) 1, (unsigned long long) 1);
}
void fail_assert_is_ptr() {
static void fail_assert_is_ptr() {
int i = 1;
int j = 1;
xtest_assert_is((void*) &i, (void*) &j);
xtest_assert_is((void *) &i, (void *) &j);
}
void fail_assert_is_not_ptr() {
static void fail_assert_is_not_ptr() {
int i = 1;
xtest_assert_is_not((void*) &i, (void*) &i);
xtest_assert_is_not((void *) &i, (void *) &i);
}
void fail_assert_str_is() {
static void fail_assert_str_is() {
xtest_assert_str_is("foo", "bar");
}
void fail_assert_str_is_not() {
static void fail_assert_str_is_not() {
xtest_assert_str_is_not("foo", "foo");
}
void fail_assert_mem_is() {
static void fail_assert_mem_is() {
xtest_assert_mem_is("aaaaaaaaaaaaa", "aaaaaaaaaaaab", 13);
}
void fail_assert_mem_is_not() {
static void fail_assert_mem_is_not() {
xtest_assert_mem_is_not("aaaaaaaaaaaaa", "aaaaaaaaaaaab", 12);
}
void fail_assert_float_is() {
static void fail_assert_float_is() {
xtest_assert_float_is(1.2356, 1.2346, 3);
}
void fail_assert_float_is_not() {
static void fail_assert_float_is_not() {
xtest_assert_float_is_not(1.2356, 1.2346, 2);
}
void fail_assert_is() {
static void fail_assert_is(void) {
xtest_run(fail_assert_is_char);
xtest_run(fail_assert_is_not_char);
xtest_run(fail_assert_is_signed_char);
@ -157,22 +157,22 @@ void fail_assert_is() {
xtest_run(fail_assert_is_not_ptr);
}
void fail_assert_str() {
static void fail_assert_str(void) {
xtest_run(fail_assert_str_is);
xtest_run(fail_assert_str_is_not);
}
void fail_assert_mem() {
static void fail_assert_mem(void) {
xtest_run(fail_assert_mem_is);
xtest_run(fail_assert_mem_is_not);
}
void fail_assert_float() {
static void fail_assert_float(void) {
xtest_run(fail_assert_float_is);
xtest_run(fail_assert_float_is_not);
}
void example_fail() {
void example_fail(void) {
xtest_run(fail_assert);
xtest_run_group(fail_assert_is);
xtest_run_group(fail_assert_str);

View file

@ -1,5 +1,5 @@
#include <stddef.h>
#include "xtest.h"
#include "examples.h"
void test_float() {
xtest_assert_float_is(1.0, 1.0, 0);
@ -34,7 +34,7 @@ void test_float() {
xtest_assert_float_is_not(0.001, 0.00100000000001, 16);
}
void example_float() {
void example_float(void) {
xtest_run(test_float);
}

View file

@ -1,18 +1,26 @@
#include <stddef.h>
#include "xtest.h"
#include "examples.h"
void test_1() {}
void test_2() {}
void test_3() {}
void test_4() {}
void test_5() {}
void test_6() {}
void test_7() {}
void test_8() {}
void test_9() {}
static void test_1() {}
static void test_2() {}
static void test_3() {}
static void test_4() {}
static void test_5() {}
static void test_6() {}
static void test_7() {}
static void test_8() {}
static void test_9() {}
void groups_manual() {
static void groups_manual(void) {
xtest_group(A);
xtest_group(foo);
xtest_run(test_1);
@ -42,55 +50,55 @@ void groups_manual() {
xtest_end_group();
}
void bar() {
static void bar(void) {
xtest_run(test_2);
xtest_run(test_3);
}
void foo() {
static void foo(void) {
xtest_run(test_1);
xtest_run_group(bar);
}
void A() {
static void A(void) {
xtest_run_group(foo);
xtest_run(test_4);
}
void B() {
static void B(void) {
xtest_run(test_5);
xtest_run(test_6);
}
void Z() {
static void Z(void) {
xtest_run(test_7);
}
void Y() {
static void Y(void) {
xtest_run_group(Z);
}
void X() {
static void X(void) {
xtest_run_group(Y);
}
void C() {
static void C(void) {
xtest_run_group(X);
}
void D() {
static void D(void) {
xtest_run(test_8);
xtest_run(test_9);
}
void groups_convenient() {
static void groups_convenient(void) {
xtest_run_group(A);
xtest_run_group(B);
xtest_run_group(C);
xtest_run_group(D);
}
void example_groups() {
void example_groups(void) {
groups_manual();
groups_convenient();
}

View file

@ -1,6 +1,6 @@
#include <stddef.h>
#include "xtest.h"
#include "source.h"
#include "examples.h"
void test_add(XTEST_UNUSED void *fixture, void **params) {
int a = xtest_get_param(int, 0, params);
@ -20,7 +20,6 @@ void test_str_equals(XTEST_UNUSED void *fixture, void **params) {
}
xtest_param add_params[] = {
{
.name = "A",
@ -73,7 +72,7 @@ xtest_param str_params[] = {
}
};
void example_parameterized() {
void example_parameterized(void) {
xtest_run_parameterized(test_add, add_params);
xtest_run_parameterized(test_str_equals, str_params);
}

View file

@ -1,5 +1,6 @@
#include "xtest.h"
#include "source.h"
#include "examples.h"
void rand_int() {
@ -9,9 +10,19 @@ void rand_int() {
xtest_assert_is(add(a, b), res);
}
void rand_double() {
double a = xtest_rand_double_range(0, 10);
double b = xtest_rand_double_range(0, 10);
double diff = a - b;
xtest_assert(diff < 10.0);
}
void example_prng(void) {
xtest_run(rand_int);
xtest_run(rand_double);
}
#ifndef XTEST_ALL_EXAMPLES
XTEST_MAIN {
xtest_run(rand_int);
}
XTEST_RUN_MAIN(example_prng)
#endif

View file

@ -1,5 +1,6 @@
#include "xtest.h"
#include "source.h"
#include "examples.h"
void no_skip_1() {}
@ -37,7 +38,7 @@ void skip_6() {
xtest_skip(NULL);
}
void example_skip() {
void example_skip(void) {
xtest_run(no_skip_1);
xtest_run(skip_1);
xtest_run(no_skip_2);

View file

@ -2,13 +2,13 @@
#include "source.h"
#include "assert.h"
int ret_1() {
int ret_1(void) {
return 1;
}
double div(double a, double b) {
assert(b != 0.0);
return a/b;
return a / b;
}

View file

@ -1,13 +1,13 @@
#ifndef XTEST_SOURCE_H
#define XTEST_SOURCE_H
int ret_1();
int ret_1(void);
double div(double a, double b);
int add(int a, int b);
int str_equals(const char * s1, const char * s2);
int str_equals(const char *s1, const char *s2);
#endif //XTEST_SOURCE_H

View file

@ -1,5 +1,7 @@
#if defined(__GNUC__) || defined(__clang__) || defined(XTEST_USE_INCLUDE_NEXT)
#include_next <assert.h>
#else
#include <assert.h>
#endif
@ -9,11 +11,11 @@
#if defined(XTEST) && !defined(XTEST_XTEST_H)
void xtest_internal_assert(const char* file, int line, const char* func, const char* expr);
void xtest_internal_assert(const char *file, int line, const char *func, const char *expr);
#undef assert
#define assert(__e) ((__e) ? (void)0 : xtest_internal_assert(__FILE__, __LINE__, \
__func__, #__e))
__func__, #__e))
#endif //defined(XTEST) && !defined(XTEST_XTEST_H)

View file

@ -15,9 +15,9 @@
#include <string.h>
#include <stdint.h>
typedef void (*xtest_test_fn)(void * fixture, void ** params);
typedef void (*xtest_setup_fn)(void ** fixture);
typedef void (*xtest_teardown_fn)(void * fixture);
typedef void (*xtest_test_fn)(void *fixture, void **params);
typedef void (*xtest_setup_fn)(void **fixture);
typedef void (*xtest_teardown_fn)(void *fixture);
typedef struct xtest_param_s xtest_param;
@ -48,37 +48,39 @@ enum xtest_type {
xtest_type_other,
};
void xtest_init(int argc, char**argv);
void xtest_init(int argc, char **argv);
void xtest_fail_assert(const char *expression, const char *file, int line, void *expected,
void *actual, _Bool invert, enum xtest_type type);
void
xtest_assert_float(double expected, double actual, int precision, _Bool invert, const char *expression, const char *file,
xtest_assert_float(double expected, double actual, int precision, _Bool invert, const char *expression,
const char *file,
int line);
void xtest_assert_mem(const char *expected, const char *actual, size_t length, _Bool invert, const char *expression, const char *file,
void xtest_assert_mem(const char *expected, const char *actual, size_t length, _Bool invert, const char *expression,
const char *file,
int line);
void xtest_internal_run(xtest_test_fn fn, const char *name, xtest_param *params, xtest_setup_fn setup,
xtest_teardown_fn teardown);
void xtest_internal_start_group(const char *name);
void xtest_internal_end_group();
void xtest_internal_end_group(void);
void xtest_skip(const char *reason);
void xtest_expect_assertion_failure();
int xtest_complete();
void xtest_expect_assertion_failure(void);
int xtest_complete(void);
#ifdef XTEST_PRNG
void xtest_rand_fill(char* buf, size_t len);
double xtest_rand_double();
void xtest_rand_fill(char *buf, size_t len);
double xtest_rand_double(void);
double xtest_rand_double_range(double min, double max);
int xtest_rand_int();
int xtest_rand_int(void);
int xtest_rand_int_range(int min, int max);
uint8_t xtest_rand_8();
uint16_t xtest_rand_16();
uint32_t xtest_rand_32();
uint64_t xtest_rand_64();
uint8_t xtest_rand_8(void);
uint16_t xtest_rand_16(void);
uint32_t xtest_rand_32(void);
uint64_t xtest_rand_64(void);
#endif

View file

@ -1,18 +1,18 @@
set(XTEST_LINK_LIBRARIES "")
if(XTEST_ENABLE_PRNG)
if (XTEST_ENABLE_PRNG)
add_library(pcg ${XTEST_SOURCE_DIR}/extern/pcg/pcg_basic.c)
set(XTEST_LINK_LIBRARIES ${XTEST_LINK_LIBRARIES} pcg)
set(XTEST_ADDITIONAL_DEFINES XTEST_PRNG)
find_library(MATH_LIBRARY m)
if(MATH_LIBRARY)
if (MATH_LIBRARY)
set(XTEST_LINK_LIBRARIES ${XTEST_LINK_LIBRARIES} ${MATH_LIBRARY})
endif()
endif()
endif ()
endif ()
add_library(xtest xtest.c)
target_compile_options(xtest PRIVATE -Wall -Wextra -pedantic)
target_compile_options(xtest PRIVATE -Wall -Wextra -pedantic -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -Wold-style-declaration)
target_include_directories(xtest PRIVATE ${XTEST_SOURCE_DIR}/extern/pcg)
target_link_libraries(xtest PRIVATE ${XTEST_LINK_LIBRARIES})
target_compile_definitions(xtest PUBLIC XTEST ${XTEST_ADDITIONAL_DEFINES})

View file

@ -76,6 +76,9 @@ int skipped_tests = 0;
_Bool list_tests = 0;
void xtest_internal_assert(const char *file, int line, const char *func, const char *expr);
#define USAGE_BASE "[-?|--help] [-l|--list] [-m|--match filter] [-f|--output-format format]"
#define USAGE_PRNG "[-s|--seed prng-seed]"
@ -206,7 +209,7 @@ void xtest_init(int argc, char **argv) {
}
int xtest_complete() {
int xtest_complete(void) {
if (!list_tests) {
if (output_format == format_default) {
printf("============\nTotal: %d, Failed: %d, Skipped: %d\n", num_tests, failed_tests, skipped_tests);
@ -218,7 +221,7 @@ int xtest_complete() {
}
}
void subunit_message(const char *name, const char *message, const char *details) {
static void subunit_message(const char *name, const char *message, const char *details) {
printf("%s: %s", message, name);
if (details == NULL) {
printf("\n");
@ -467,7 +470,7 @@ xtest_assert_float(double expected, double actual, int precision, _Bool invert,
longjmp(xtest_jmp, 1);
}
size_t find_diff_offset(const char *a, const char *b, size_t length) {
static size_t find_diff_offset(const char *a, const char *b, size_t length) {
size_t offset = 0;
while (offset < length && *(a + offset) == *(b + offset)) offset++;
return offset;
@ -510,13 +513,13 @@ void xtest_internal_start_group(const char *name) {
xtest_indent += XTEST_INDENT;
}
void xtest_internal_end_group() {
void xtest_internal_end_group(void) {
assert(group_nesting_pos > 0);
group_nesting_pos -= 1;
xtest_indent -= XTEST_INDENT;
}
void xtest_expect_assertion_failure() {
void xtest_expect_assertion_failure(void) {
expecting_assertion = 1;
}
@ -537,7 +540,7 @@ union seed_encode_box {
uint8_t bytes[PRNG_SEED_LENGTH * 6 / 8 + 1];
};
void encode_seed(prng_seed seed, uint64_t state, uint64_t seq) {
static void encode_seed(prng_seed seed, uint64_t state, uint64_t seq) {
union seed_encode_box box;
memset(&box, 0, sizeof(box));
box.state = state;
@ -571,7 +574,7 @@ void encode_seed(prng_seed seed, uint64_t state, uint64_t seq) {
seed[PRNG_SEED_LENGTH] = '\0';
}
void decode_seed(const prng_seed seed, uint64_t *state, uint64_t *seq) {
static void decode_seed(const prng_seed seed, uint64_t *state, uint64_t *seq) {
union seed_encode_box box;
memset(&box, 0, sizeof(box));
uint8_t cur;
@ -638,7 +641,7 @@ void xtest_rand_fill(char *buf, size_t len) {
}
}
double xtest_rand_double() {
double xtest_rand_double(void) {
return ldexp((double) (xtest_rand_64() & 0xFFFFFFFFFFFFF), -52);
}
@ -647,7 +650,7 @@ double xtest_rand_double_range(double min, double max) {
return (max - min) * xtest_rand_double() + min;
}
int xtest_rand_int() {
int xtest_rand_int(void) {
uint32_t uval = pcg32_random_r(&rng);
int32_t *ival;
ival = (int32_t *) &uval;
@ -662,20 +665,20 @@ int xtest_rand_int_range(int min, int max) {
return (int) *ival + min;
}
uint8_t xtest_rand_8() {
uint8_t xtest_rand_8(void) {
return (uint8_t) pcg32_random_r(&rng);
}
uint16_t xtest_rand_16() {
uint16_t xtest_rand_16(void) {
return (uint16_t) pcg32_random_r(&rng);
}
uint32_t xtest_rand_32() {
uint32_t xtest_rand_32(void) {
return pcg32_random_r(&rng);
}
uint64_t xtest_rand_64() {
uint64_t xtest_rand_64(void) {
uint32_t l = pcg32_random_r(&rng);
uint32_t h = pcg32_random_r(&rng);
return ((uint64_t) h) << 32 | l;