From a17d6eb3e3efd83d522103292f944655a8364d6b Mon Sep 17 00:00:00 2001 From: Joshua Potter Date: Fri, 24 Nov 2023 19:35:22 -0700 Subject: [PATCH] Run formatting with additional rules. --- .clang-format | 4 +- .githooks/pre-commit | 2 +- include/cJSON.h | 22 +- include/parser.h | 5 +- include/validator.h | 5 +- src/cJSON.c | 101 ++++++---- src/config.c | 5 +- src/dyn_array.c | 4 +- src/evaluator.c | 3 +- src/parser.c | 8 +- src/path.c | 6 +- src/validator.c | 14 +- test/sput.h | 453 ++++++++++++++++++++---------------------- test/test_path.h | 6 +- test/test_validator.h | 55 ++--- 15 files changed, 354 insertions(+), 339 deletions(-) diff --git a/.clang-format b/.clang-format index b7d6df4..65cc883 100644 --- a/.clang-format +++ b/.clang-format @@ -1,6 +1,8 @@ -BasedOnStyle: LLVM +BasedOnStyle: Google AlignAfterOpenBracket: BlockIndent BinPackArguments: false BinPackParameters: false +ColumnLimit: 80 ContinuationIndentWidth: 2 +IndentCaseLabels: false IndentWidth: 2 diff --git a/.githooks/pre-commit b/.githooks/pre-commit index cb2d72e..47ee87b 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -3,7 +3,7 @@ set -e filesToFormat=$( git --no-pager diff --name-status --no-color --cached | \ - awk '$1 != "D" && $2 ~ /\.c|\.h/ {print $NF}' + awk '$1 != "D" && $2 ~ /\.c$|\.h$/ {print $NF}' ) for path in $filesToFormat diff --git a/include/cJSON.h b/include/cJSON.h index bcdc1fb..318dee1 100644 --- a/include/cJSON.h +++ b/include/cJSON.h @@ -27,7 +27,7 @@ extern "C" { #endif -#if !defined(__WINDOWS__) && \ +#if !defined(__WINDOWS__) && \ (defined(WIN32) || defined(WIN64) || defined(_MSC_VER) || defined(_WIN32)) #define __WINDOWS__ #endif @@ -62,7 +62,7 @@ CJSON_EXPORT_SYMBOLS does /* export symbols by default, this is necessary for copy pasting the C and * header file */ -#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && \ +#if !defined(CJSON_HIDE_SYMBOLS) && !defined(CJSON_IMPORT_SYMBOLS) && \ !defined(CJSON_EXPORT_SYMBOLS) #define CJSON_EXPORT_SYMBOLS #endif @@ -78,7 +78,7 @@ CJSON_EXPORT_SYMBOLS does #define CJSON_CDECL #define CJSON_STDCALL -#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) && \ +#if (defined(__GNUC__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)) && \ defined(CJSON_API_VISIBILITY) #define CJSON_PUBLIC(type) __attribute__((visibility("default"))) type #else @@ -368,11 +368,11 @@ cJSON_AddArrayToObject(cJSON *const object, const char *const name); /* When assigning an integer value, it needs to be propagated to valuedouble * too. */ -#define cJSON_SetIntValue(object, number) \ +#define cJSON_SetIntValue(object, number) \ ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) /* helper for the cJSON_SetNumberValue macro */ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); -#define cJSON_SetNumberValue(object, number) \ +#define cJSON_SetNumberValue(object, number) \ ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number)) /* Change the valuestring of a cJSON_String object, only takes effect when type * of object is cJSON_String */ @@ -381,15 +381,15 @@ cJSON_SetValuestring(cJSON *object, const char *valuestring); /* If the object is not a boolean type this does nothing and returns * cJSON_Invalid else it returns the new type*/ -#define cJSON_SetBoolValue(object, boolValue) \ - ((object != NULL && ((object)->type & (cJSON_False | cJSON_True))) \ - ? (object)->type = ((object)->type & (~(cJSON_False | cJSON_True))) | \ - ((boolValue) ? cJSON_True : cJSON_False) \ +#define cJSON_SetBoolValue(object, boolValue) \ + ((object != NULL && ((object)->type & (cJSON_False | cJSON_True))) \ + ? (object)->type = ((object)->type & (~(cJSON_False | cJSON_True))) | \ + ((boolValue) ? cJSON_True : cJSON_False) \ : cJSON_Invalid) /* Macro for iterating over an array or object */ -#define cJSON_ArrayForEach(element, array) \ - for (element = (array != NULL) ? (array)->child : NULL; element != NULL; \ +#define cJSON_ArrayForEach(element, array) \ + for (element = (array != NULL) ? (array)->child : NULL; element != NULL; \ element = element->next) /* malloc/free objects using the malloc/free functions that have been set with diff --git a/include/parser.h b/include/parser.h index 38f0dcf..e1fb4f0 100644 --- a/include/parser.h +++ b/include/parser.h @@ -19,7 +19,8 @@ cannot be found, the @parsed pointer is set to NULL with a success return code. @return: 0 on success and a @SpecParseError otherwise. */ -enum SpecParseError -parse_spec_json(const struct Config *const config, cJSON **parsed); +enum SpecParseError parse_spec_json( + const struct Config *const config, cJSON **parsed +); #endif /* _BOOTSTRAP_PARSER_H */ diff --git a/include/validator.h b/include/validator.h index 50736e6..61c99d0 100644 --- a/include/validator.h +++ b/include/validator.h @@ -24,7 +24,8 @@ enum SpecValidationError { SVE_FIELD_PROMPT_INVALID, }; -enum SpecValidationError -validate_spec_json(const cJSON *const parsed, struct DynArray **fields); +enum SpecValidationError validate_spec_json( + const cJSON *const parsed, struct DynArray **fields +); #endif /* _BOOTSTRAP_VALIDATOR_H */ diff --git a/src/cJSON.c b/src/cJSON.c index a3a7676..6373193 100644 --- a/src/cJSON.c +++ b/src/cJSON.c @@ -114,7 +114,7 @@ CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON *const item) { /* This is a safeguard to prevent copy-pasters from using incompatible C and * header files */ -#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || \ +#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 7) || \ (CJSON_VERSION_PATCH != 16) #error cJSON.h and cJSON.c have different versions. Make sure that both have the same. #endif @@ -180,8 +180,9 @@ static void *CJSON_CDECL internal_realloc(void *pointer, size_t size) { static internal_hooks global_hooks = { internal_malloc, internal_free, internal_realloc}; -static unsigned char * -cJSON_strdup(const unsigned char *string, const internal_hooks *const hooks) { +static unsigned char *cJSON_strdup( + const unsigned char *string, const internal_hooks *const hooks +) { size_t length = 0; unsigned char *copy = NULL; @@ -275,20 +276,21 @@ typedef struct { /* check if the given size is left to read in a given parse buffer (starting * with 1) */ -#define can_read(buffer, size) \ +#define can_read(buffer, size) \ ((buffer != NULL) && (((buffer)->offset + size) <= (buffer)->length)) /* check if the buffer can be accessed at the given index (starting with 0) */ -#define can_access_at_index(buffer, index) \ +#define can_access_at_index(buffer, index) \ ((buffer != NULL) && (((buffer)->offset + index) < (buffer)->length)) -#define cannot_access_at_index(buffer, index) \ +#define cannot_access_at_index(buffer, index) \ (!can_access_at_index(buffer, index)) /* get a pointer to the buffer at the position */ #define buffer_at_offset(buffer) ((buffer)->content + (buffer)->offset) /* Parse the input text to generate a number, and populate the result into item. */ -static cJSON_bool -parse_number(cJSON *const item, parse_buffer *const input_buffer) { +static cJSON_bool parse_number( + cJSON *const item, parse_buffer *const input_buffer +) { double number = 0; unsigned char *after_end = NULL; unsigned char number_c_string[64]; @@ -494,8 +496,9 @@ static cJSON_bool compare_double(double a, double b) { } /* Render the number nicely from the given item into a string. */ -static cJSON_bool -print_number(const cJSON *const item, printbuffer *const output_buffer) { +static cJSON_bool print_number( + const cJSON *const item, printbuffer *const output_buffer +) { unsigned char *output_pointer = NULL; double d = item->valuedouble; int length = 0; @@ -689,8 +692,9 @@ fail: } /* Parse the input text into an unescaped cinput, and populate item. */ -static cJSON_bool -parse_string(cJSON *const item, parse_buffer *const input_buffer) { +static cJSON_bool parse_string( + cJSON *const item, parse_buffer *const input_buffer +) { const unsigned char *input_pointer = buffer_at_offset(input_buffer) + 1; const unsigned char *input_end = buffer_at_offset(input_buffer) + 1; unsigned char *output_pointer = NULL; @@ -926,18 +930,24 @@ static cJSON_bool print_string(const cJSON *const item, printbuffer *const p) { } /* Predeclare these prototypes. */ -static cJSON_bool -parse_value(cJSON *const item, parse_buffer *const input_buffer); -static cJSON_bool -print_value(const cJSON *const item, printbuffer *const output_buffer); -static cJSON_bool -parse_array(cJSON *const item, parse_buffer *const input_buffer); -static cJSON_bool -print_array(const cJSON *const item, printbuffer *const output_buffer); -static cJSON_bool -parse_object(cJSON *const item, parse_buffer *const input_buffer); -static cJSON_bool -print_object(const cJSON *const item, printbuffer *const output_buffer); +static cJSON_bool parse_value( + cJSON *const item, parse_buffer *const input_buffer +); +static cJSON_bool print_value( + const cJSON *const item, printbuffer *const output_buffer +); +static cJSON_bool parse_array( + cJSON *const item, parse_buffer *const input_buffer +); +static cJSON_bool print_array( + const cJSON *const item, printbuffer *const output_buffer +); +static cJSON_bool parse_object( + cJSON *const item, parse_buffer *const input_buffer +); +static cJSON_bool print_object( + const cJSON *const item, printbuffer *const output_buffer +); /* Utility to jump whitespace and cr/lf */ static parse_buffer *buffer_skip_whitespace(parse_buffer *const buffer) { @@ -1199,8 +1209,9 @@ cJSON_PrintPreallocated( } /* Parser core - when encountering text, process appropriately. */ -static cJSON_bool -parse_value(cJSON *const item, parse_buffer *const input_buffer) { +static cJSON_bool parse_value( + cJSON *const item, parse_buffer *const input_buffer +) { if ((input_buffer == NULL) || (input_buffer->content == NULL)) { return false; /* no input */ } @@ -1246,8 +1257,9 @@ parse_value(cJSON *const item, parse_buffer *const input_buffer) { } /* Render a value to text. */ -static cJSON_bool -print_value(const cJSON *const item, printbuffer *const output_buffer) { +static cJSON_bool print_value( + const cJSON *const item, printbuffer *const output_buffer +) { unsigned char *output = NULL; if ((item == NULL) || (output_buffer == NULL)) { @@ -1312,8 +1324,9 @@ print_value(const cJSON *const item, printbuffer *const output_buffer) { } /* Build an array from input text. */ -static cJSON_bool -parse_array(cJSON *const item, parse_buffer *const input_buffer) { +static cJSON_bool parse_array( + cJSON *const item, parse_buffer *const input_buffer +) { cJSON *head = NULL; /* head of the linked list */ cJSON *current_item = NULL; @@ -1398,8 +1411,9 @@ fail: } /* Render an array to text */ -static cJSON_bool -print_array(const cJSON *const item, printbuffer *const output_buffer) { +static cJSON_bool print_array( + const cJSON *const item, printbuffer *const output_buffer +) { unsigned char *output_pointer = NULL; size_t length = 0; cJSON *current_element = item->child; @@ -1452,8 +1466,9 @@ print_array(const cJSON *const item, printbuffer *const output_buffer) { } /* Build an object from the text. */ -static cJSON_bool -parse_object(cJSON *const item, parse_buffer *const input_buffer) { +static cJSON_bool parse_object( + cJSON *const item, parse_buffer *const input_buffer +) { cJSON *head = NULL; /* linked list head */ cJSON *current_item = NULL; @@ -1551,8 +1566,9 @@ fail: } /* Render an object to text. */ -static cJSON_bool -print_object(const cJSON *const item, printbuffer *const output_buffer) { +static cJSON_bool print_object( + const cJSON *const item, printbuffer *const output_buffer +) { unsigned char *output_pointer = NULL; size_t length = 0; cJSON *current_item = item->child; @@ -1756,8 +1772,9 @@ static void suffix_object(cJSON *prev, cJSON *item) { } /* Utility for handling references. */ -static cJSON * -create_reference(const cJSON *item, const internal_hooks *const hooks) { +static cJSON *create_reference( + const cJSON *item, const internal_hooks *const hooks +) { cJSON *reference = NULL; if (item == NULL) { return NULL; @@ -1807,8 +1824,8 @@ CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) { return add_item_to_array(array, item); } -#if defined(__clang__) || \ - (defined(__GNUC__) && \ +#if defined(__clang__) || \ + (defined(__GNUC__) && \ ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) #pragma GCC diagnostic push #endif @@ -1817,8 +1834,8 @@ CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item) { #endif /* helper function to cast away const */ static void *cast_away_const(const void *string) { return (void *)string; } -#if defined(__clang__) || \ - (defined(__GNUC__) && \ +#if defined(__clang__) || \ + (defined(__GNUC__) && \ ((__GNUC__ > 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ > 5)))) #pragma GCC diagnostic pop #endif diff --git a/src/config.c b/src/config.c index c871689..c5fcb15 100644 --- a/src/config.c +++ b/src/config.c @@ -1,9 +1,10 @@ +#include "config.h" + #include #include #include #include -#include "config.h" #include "path.h" enum ConfigError config_load( @@ -22,7 +23,7 @@ enum ConfigError config_load( return CE_TARGET_INVALID; } - { // Check if the specified directory exists. + { // Check if the specified directory exists. struct stat sb; const char *segments[] = {root_dir, target}; diff --git a/src/dyn_array.c b/src/dyn_array.c index 72560e9..0bdfb1d 100644 --- a/src/dyn_array.c +++ b/src/dyn_array.c @@ -1,7 +1,7 @@ -#include - #include "dyn_array.h" +#include + struct DynArray *dyn_array_new(size_t capacity) { struct DynArray *a = malloc(sizeof(struct DynArray)); size_t new_capacity = capacity ? capacity : 1; diff --git a/src/evaluator.c b/src/evaluator.c index 5cbe954..341d2d2 100644 --- a/src/evaluator.c +++ b/src/evaluator.c @@ -1,7 +1,8 @@ +#include "evaluator.h" + #include #include -#include "evaluator.h" #include "path.h" static int find_run_sh(const struct Config *const config, FILE **handle) { diff --git a/src/parser.c b/src/parser.c index 6a6364c..7254bb0 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,9 +1,10 @@ +#include "parser.h" + #include #include #include #include -#include "parser.h" #include "path.h" static int find_spec_json(const struct Config *const config, FILE **handle) { @@ -23,8 +24,9 @@ static int find_spec_json(const struct Config *const config, FILE **handle) { return retval; } -enum SpecParseError -parse_spec_json(const struct Config *const config, cJSON **parsed) { +enum SpecParseError parse_spec_json( + const struct Config *const config, cJSON **parsed +) { FILE *handle = 0; int retval = find_spec_json(config, &handle); diff --git a/src/path.c b/src/path.c index d5056dd..e4c6cf7 100644 --- a/src/path.c +++ b/src/path.c @@ -1,8 +1,8 @@ +#include "path.h" + #include #include -#include "path.h" - char *join_path_segments(size_t n, const char *segments[static n]) { assert(n > 0); @@ -10,7 +10,7 @@ char *join_path_segments(size_t n, const char *segments[static n]) { for (int i = 0; i < n; ++i) { length += strlen(segments[i]); } - length += n - 1; // Path separators. + length += n - 1; // Path separators. size_t offset = 0; char *joined = calloc(1, length + 1); diff --git a/src/validator.c b/src/validator.c index 57eda71..5c48f36 100644 --- a/src/validator.c +++ b/src/validator.c @@ -1,9 +1,10 @@ -#include - #include "validator.h" -static enum SpecValidationError -read_field(const cJSON *const field, struct Field **out) { +#include + +static enum SpecValidationError read_field( + const cJSON *const field, struct Field **out +) { if (!cJSON_IsObject(field)) { return SVE_FIELD_NOT_OBJECT; } @@ -40,8 +41,9 @@ cleanup: return retval; } -enum SpecValidationError -validate_spec_json(const cJSON *const parsed, struct DynArray **fields) { +enum SpecValidationError validate_spec_json( + const cJSON *const parsed, struct DynArray **fields +) { *fields = 0; if (!parsed) { diff --git a/test/sput.h b/test/sput.h index 31d9e27..3ce5129 100644 --- a/test/sput.h +++ b/test/sput.h @@ -33,287 +33,268 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - #ifndef HAVE_SPUT_H #define HAVE_SPUT_H - #ifdef __cplusplus extern "C" { #endif - #include #include #include #include +/* =================================================================== + * definitions + * =================================================================== */ - /* =================================================================== - * definitions - * =================================================================== */ - -#define SPUT_VERSION_MAJOR 1 -#define SPUT_VERSION_MINOR 4 -#define SPUT_VERSION_PATCH 0 -#define SPUT_VERSION_STRING "1.4.0" +#define SPUT_VERSION_MAJOR 1 +#define SPUT_VERSION_MINOR 4 +#define SPUT_VERSION_PATCH 0 +#define SPUT_VERSION_STRING "1.4.0" #define SPUT_DEFAULT_SUITE_NAME "Unlabeled Suite" #define SPUT_DEFAULT_CHECK_NAME "Unlabeled Check" -#define SPUT_INITIALIZED 0x06 /* ACK */ +#define SPUT_INITIALIZED 0x06 /* ACK */ +/* =================================================================== + * sput global variable + * =================================================================== */ - /* =================================================================== - * sput global variable - * =================================================================== */ +static struct sput { + FILE *out; + char initialized; - static struct sput - { - FILE *out; - char initialized; + struct sput_overall { + unsigned long checks; + unsigned long suites; + unsigned long ok; + unsigned long nok; + } overall; - struct sput_overall - { - unsigned long checks; - unsigned long suites; - unsigned long ok; - unsigned long nok; - } overall; + struct sput_suite { + const char *name; + unsigned long nr; + unsigned long checks; + unsigned long ok; + unsigned long nok; + } suite; - struct sput_suite - { - const char *name; - unsigned long nr; - unsigned long checks; - unsigned long ok; - unsigned long nok; - } suite; + struct sput_test { + const char *name; + unsigned long nr; + } test; - struct sput_test - { - const char *name; - unsigned long nr; - } test; + struct sput_check { + const char *name; + const char *cond; + const char *type; + unsigned long line; + } check; - struct sput_check - { - const char *name; - const char *cond; - const char *type; - unsigned long line; - } check; + struct sput_time { + time_t start; + time_t end; + } time; +} __sput; - struct sput_time - { - time_t start; - time_t end; - } time; - } __sput; +/* ================================================================== + * sput internal macros + * ================================================================== */ +#define _sput_die_unless_initialized() \ + if (__sput.initialized != SPUT_INITIALIZED) { \ + fputs("sput_start_testing() omitted\n", stderr); \ + exit(EXIT_FAILURE); \ + } - /* ================================================================== - * sput internal macros - * ================================================================== */ +#define _sput_die_unless_suite_set() \ + if (!__sput.suite.name) { \ + fputs("sput_enter_suite() omitted\n", __sput.out); \ + exit(EXIT_FAILURE); \ + } -#define _sput_die_unless_initialized() \ - if (__sput.initialized != SPUT_INITIALIZED) \ - { \ - fputs("sput_start_testing() omitted\n", stderr); \ - exit(EXIT_FAILURE); \ - } +#define _sput_die_unless_test_set() \ + if (!__sput.test.name) { \ + fputs("sput_run_test() omitted\n", __sput.out); \ + exit(EXIT_FAILURE); \ + } +#define _sput_check_failed() \ + { \ + _sput_die_unless_initialized(); \ + _sput_die_unless_suite_set(); \ + __sput.suite.nok++; \ + fprintf( \ + __sput.out, \ + "[%lu:%lu] %s:#%lu \"%s\" FAIL\n" \ + "! Type: %s\n" \ + "! Condition: %s\n" \ + "! Line: %lu\n", \ + __sput.suite.nr, \ + __sput.suite.checks, \ + __sput.test.name, \ + __sput.test.nr, \ + __sput.check.name, \ + __sput.check.type, \ + __sput.check.cond, \ + __sput.check.line \ + ); \ + } -#define _sput_die_unless_suite_set() \ - if (! __sput.suite.name) \ - { \ - fputs("sput_enter_suite() omitted\n", __sput.out); \ - exit(EXIT_FAILURE); \ - } +#define _sput_check_succeeded() \ + { \ + _sput_die_unless_initialized(); \ + _sput_die_unless_suite_set(); \ + __sput.suite.ok++; \ + fprintf( \ + __sput.out, \ + "[%lu:%lu] %s:#%lu \"%s\" pass\n", \ + __sput.suite.nr, \ + __sput.suite.checks, \ + __sput.test.name, \ + __sput.test.nr, \ + __sput.check.name \ + ); \ + } +/* ================================================================== + * user macros + * ================================================================== */ -#define _sput_die_unless_test_set() \ - if (! __sput.test.name) \ - { \ - fputs("sput_run_test() omitted\n", __sput.out); \ - exit(EXIT_FAILURE); \ - } +#define sput_start_testing() \ + do { \ + memset(&__sput, 0, sizeof(__sput)); \ + __sput.out = stdout; \ + __sput.time.start = time(NULL); \ + __sput.initialized = SPUT_INITIALIZED; \ + } while (0) +#define sput_leave_suite() \ + do { \ + float failpls = 0.0f; \ + _sput_die_unless_initialized(); \ + _sput_die_unless_suite_set(); \ + failpls = __sput.suite.checks \ + ? (float)((__sput.suite.nok * 100.0) / __sput.suite.checks) \ + : 0.0f; \ + fprintf( \ + __sput.out, \ + "\n--> %lu check(s), %lu ok, %lu failed (%.2f%%)\n", \ + __sput.suite.checks, \ + __sput.suite.ok, \ + __sput.suite.nok, \ + failpls \ + ); \ + __sput.overall.checks += __sput.suite.checks; \ + __sput.overall.ok += __sput.suite.ok; \ + __sput.overall.nok += __sput.suite.nok; \ + memset(&__sput.suite, 0, sizeof(__sput.suite)); \ + } while (0) -#define _sput_check_failed() \ - { \ - _sput_die_unless_initialized(); \ - _sput_die_unless_suite_set(); \ - __sput.suite.nok++; \ - fprintf(__sput.out, \ - "[%lu:%lu] %s:#%lu \"%s\" FAIL\n" \ - "! Type: %s\n" \ - "! Condition: %s\n" \ - "! Line: %lu\n", \ - __sput.suite.nr, __sput.suite.checks, __sput.test.name, \ - __sput.test.nr, __sput.check.name, __sput.check.type, \ - __sput.check.cond, __sput.check.line); \ - } +#define sput_get_return_value() \ + (__sput.overall.nok > 0 ? EXIT_FAILURE : EXIT_SUCCESS) +#define sput_enter_suite(_name) \ + do { \ + _sput_die_unless_initialized(); \ + if (__sput.suite.name) { \ + sput_leave_suite(); \ + } \ + __sput.suite.name = _name != NULL ? _name : SPUT_DEFAULT_SUITE_NAME; \ + __sput.suite.nr = ++__sput.overall.suites; \ + fprintf( \ + __sput.out, \ + "\n== Entering suite #%lu, \"%s\" ==\n\n", \ + __sput.suite.nr, \ + __sput.suite.name \ + ); \ + } while (0) -#define _sput_check_succeeded() \ - { \ - _sput_die_unless_initialized(); \ - _sput_die_unless_suite_set(); \ - __sput.suite.ok++; \ - fprintf(__sput.out, \ - "[%lu:%lu] %s:#%lu \"%s\" pass\n", \ - __sput.suite.nr, __sput.suite.checks, \ - __sput.test.name, \ - __sput.test.nr, \ - __sput.check.name); \ - } +#define sput_finish_testing() \ + do { \ + float failpft = 0.0f; \ + _sput_die_unless_initialized(); \ + if (__sput.suite.name) { \ + sput_leave_suite(); \ + } \ + failpft = \ + __sput.overall.checks \ + ? (float)((__sput.overall.nok * 100.0) / __sput.overall.checks) \ + : 0.0f; \ + __sput.time.end = time(NULL); \ + fprintf( \ + __sput.out, \ + "\n==> %lu check(s) in %lu suite(s) finished after %.2f " \ + "second(s),\n" \ + " %lu succeeded, %lu failed (%.2f%%)\n" \ + "\n[%s]\n", \ + __sput.overall.checks, \ + __sput.overall.suites, \ + difftime(__sput.time.end, __sput.time.start), \ + __sput.overall.ok, \ + __sput.overall.nok, \ + failpft, \ + (sput_get_return_value() == EXIT_SUCCESS) ? "SUCCESS" : "FAILURE" \ + ); \ + } while (0) +#define sput_set_output_stream(_fp) \ + do { \ + __sput.out = _fp != NULL ? _fp : stdout; \ + } while (0) - /* ================================================================== - * user macros - * ================================================================== */ +#define sput_fail_if(_cond, _name) \ + do { \ + _sput_die_unless_initialized(); \ + _sput_die_unless_suite_set(); \ + _sput_die_unless_test_set(); \ + __sput.check.name = _name != NULL ? _name : SPUT_DEFAULT_CHECK_NAME; \ + __sput.check.line = __LINE__; \ + __sput.check.cond = #_cond; \ + __sput.check.type = "fail-if"; \ + __sput.test.nr++; \ + __sput.suite.checks++; \ + if ((_cond)) { \ + _sput_check_failed(); \ + } else { \ + _sput_check_succeeded(); \ + } \ + } while (0) -#define sput_start_testing() \ - do { \ - memset(&__sput, 0, sizeof(__sput)); \ - __sput.out = stdout; \ - __sput.time.start = time(NULL); \ - __sput.initialized = SPUT_INITIALIZED; \ - } while (0) - - -#define sput_leave_suite() \ - do { \ - float failpls = 0.0f; \ - _sput_die_unless_initialized(); \ - _sput_die_unless_suite_set(); \ - failpls = __sput.suite.checks ? (float) \ - ((__sput.suite.nok * 100.0) / __sput.suite.checks) : \ - 0.0f; \ - fprintf(__sput.out, \ - "\n--> %lu check(s), %lu ok, %lu failed (%.2f%%)\n", \ - __sput.suite.checks, __sput.suite.ok, __sput.suite.nok, \ - failpls); \ - __sput.overall.checks += __sput.suite.checks; \ - __sput.overall.ok += __sput.suite.ok; \ - __sput.overall.nok += __sput.suite.nok; \ - memset(&__sput.suite, 0, sizeof(__sput.suite)); \ - } while (0) - - -#define sput_get_return_value() \ - (__sput.overall.nok > 0 ? EXIT_FAILURE : EXIT_SUCCESS) - - -#define sput_enter_suite(_name) \ - do { \ - _sput_die_unless_initialized(); \ - if (__sput.suite.name) \ - { \ - sput_leave_suite(); \ - } \ - __sput.suite.name = _name != NULL ? \ - _name : SPUT_DEFAULT_SUITE_NAME; \ - __sput.suite.nr = ++__sput.overall.suites; \ - fprintf(__sput.out, "\n== Entering suite #%lu, \"%s\" ==\n\n", \ - __sput.suite.nr, __sput.suite.name); \ - } while (0) - - -#define sput_finish_testing() \ - do { \ - float failpft = 0.0f; \ - _sput_die_unless_initialized(); \ - if (__sput.suite.name) \ - { \ - sput_leave_suite(); \ - } \ - failpft = __sput.overall.checks ? (float) \ - ((__sput.overall.nok * 100.0) / __sput.overall.checks) : \ - 0.0f; \ - __sput.time.end = time(NULL); \ - fprintf(__sput.out, \ - "\n==> %lu check(s) in %lu suite(s) finished after %.2f " \ - "second(s),\n" \ - " %lu succeeded, %lu failed (%.2f%%)\n" \ - "\n[%s]\n", \ - __sput.overall.checks, __sput.overall.suites, \ - difftime(__sput.time.end, __sput.time.start), \ - __sput.overall.ok, __sput.overall.nok, failpft, \ - (sput_get_return_value() == EXIT_SUCCESS) ? \ - "SUCCESS" : "FAILURE"); \ - } while (0) - - -#define sput_set_output_stream(_fp) \ - do { \ - __sput.out = _fp != NULL ? _fp : stdout; \ - } while (0) - - -#define sput_fail_if(_cond, _name) \ - do { \ - _sput_die_unless_initialized(); \ - _sput_die_unless_suite_set(); \ - _sput_die_unless_test_set(); \ - __sput.check.name = _name != NULL ? \ - _name : SPUT_DEFAULT_CHECK_NAME; \ - __sput.check.line = __LINE__; \ - __sput.check.cond = #_cond; \ - __sput.check.type = "fail-if"; \ - __sput.test.nr++; \ - __sput.suite.checks++; \ - if ((_cond)) \ - { \ - _sput_check_failed(); \ - } \ - else \ - { \ - _sput_check_succeeded(); \ - } \ - } while (0) - - -#define sput_fail_unless(_cond, _name) \ - do { \ - _sput_die_unless_initialized(); \ - _sput_die_unless_suite_set(); \ - _sput_die_unless_test_set(); \ - __sput.check.name = _name != NULL ? \ - _name : SPUT_DEFAULT_CHECK_NAME; \ - __sput.check.line = __LINE__; \ - __sput.check.cond = #_cond; \ - __sput.check.type = "fail-unless"; \ - __sput.test.nr++; \ - __sput.suite.checks++; \ - if (! (_cond)) \ - { \ - _sput_check_failed(); \ - } \ - else \ - { \ - _sput_check_succeeded(); \ - } \ - } while (0) - - -#define sput_run_test(_func) \ - do { \ - _sput_die_unless_initialized(); \ - _sput_die_unless_suite_set(); \ - memset(&__sput.test, 0, sizeof(__sput.test)); \ - __sput.test.name = #_func; \ - _func(); \ - } while (0) +#define sput_fail_unless(_cond, _name) \ + do { \ + _sput_die_unless_initialized(); \ + _sput_die_unless_suite_set(); \ + _sput_die_unless_test_set(); \ + __sput.check.name = _name != NULL ? _name : SPUT_DEFAULT_CHECK_NAME; \ + __sput.check.line = __LINE__; \ + __sput.check.cond = #_cond; \ + __sput.check.type = "fail-unless"; \ + __sput.test.nr++; \ + __sput.suite.checks++; \ + if (!(_cond)) { \ + _sput_check_failed(); \ + } else { \ + _sput_check_succeeded(); \ + } \ + } while (0) +#define sput_run_test(_func) \ + do { \ + _sput_die_unless_initialized(); \ + _sput_die_unless_suite_set(); \ + memset(&__sput.test, 0, sizeof(__sput.test)); \ + __sput.test.name = #_func; \ + _func(); \ + } while (0) #ifdef __cplusplus } #endif - #endif /* HAVE_SPUT_H */ - /* vim: set ft=c sts=4 sw=4 ts=4 ai et: */ diff --git a/test/test_path.h b/test/test_path.h index 6f6d362..eb9911a 100644 --- a/test/test_path.h +++ b/test/test_path.h @@ -6,14 +6,16 @@ static void test_join_path_single_segments() { const char *segments[] = {"abc"}; - char *joined = join_path_segments(sizeof(segments) / sizeof(char *), segments); + char *joined = + join_path_segments(sizeof(segments) / sizeof(char *), segments); sput_fail_unless(strcmp(joined, "abc") == 0, "abc"); free(joined); } static void test_join_path_multiple_segments() { const char *segments[] = {"abc", "def", "ghi"}; - char *joined = join_path_segments(sizeof(segments) / sizeof(char *), segments); + char *joined = + join_path_segments(sizeof(segments) / sizeof(char *), segments); sput_fail_unless(strcmp(joined, "abc/def/ghi") == 0, "abc/def/ghi"); free(joined); } diff --git a/test/test_validator.h b/test/test_validator.h index aae5a7d..4fb5bc4 100644 --- a/test/test_validator.h +++ b/test/test_validator.h @@ -49,11 +49,13 @@ static void test_validator_field_not_object() { } static void test_validator_field_type_invalid() { - struct TestValidatorFixture *fixture = test_validator_setup("{" - " \"key\": {" - " \"type\": 2" - " }" - "}"); + struct TestValidatorFixture *fixture = test_validator_setup( + "{" + " \"key\": {" + " \"type\": 2" + " }" + "}" + ); enum SpecValidationError retval = validate_spec_json(fixture->parsed, &fixture->prompts); @@ -63,12 +65,13 @@ static void test_validator_field_type_invalid() { } static void test_validator_field_type_unknown() { - struct TestValidatorFixture *fixture = - test_validator_setup("{" - " \"key\": {" - " \"type\": \"UNKNOWN\"" - " }" - "}"); + struct TestValidatorFixture *fixture = test_validator_setup( + "{" + " \"key\": {" + " \"type\": \"UNKNOWN\"" + " }" + "}" + ); enum SpecValidationError retval = validate_spec_json(fixture->parsed, &fixture->prompts); @@ -78,13 +81,14 @@ static void test_validator_field_type_unknown() { } static void test_validator_field_prompt_invalid() { - struct TestValidatorFixture *fixture = - test_validator_setup("{" - " \"key\": {" - " \"type\": \"STRING\"," - " \"prompt\": 2" - " }" - "}"); + struct TestValidatorFixture *fixture = test_validator_setup( + "{" + " \"key\": {" + " \"type\": \"STRING\"," + " \"prompt\": 2" + " }" + "}" + ); enum SpecValidationError retval = validate_spec_json(fixture->parsed, &fixture->prompts); @@ -94,13 +98,14 @@ static void test_validator_field_prompt_invalid() { } static void test_validator_valid() { - struct TestValidatorFixture *fixture = - test_validator_setup("{" - " \"key\": {" - " \"type\": \"STRING\"," - " \"prompt\": \"What value for key?\"" - " }" - "}"); + struct TestValidatorFixture *fixture = test_validator_setup( + "{" + " \"key\": {" + " \"type\": \"STRING\"," + " \"prompt\": \"What value for key?\"" + " }" + "}" + ); enum SpecValidationError retval = validate_spec_json(fixture->parsed, &fixture->prompts);