### Package cJSON for Linux Distribution Source: https://github.com/davegamble/cjson/blob/master/README.md Example configuration for packaging cJSON with specific build options and installation prefixes. ```bash mkdir build cd build cmake .. -DENABLE_CJSON_UTILS=On -DENABLE_CJSON_TEST=Off -DCMAKE_INSTALL_PREFIX=/usr make make DESTDIR=$pkgdir install ``` -------------------------------- ### Example Usage Source: https://github.com/davegamble/cjson/blob/master/README.md A practical example demonstrating how to build and parse a JSON structure using cJSON. ```APIDOC ## Example ### Description This example illustrates the process of creating a JSON structure in memory and then parsing it back into a string, showcasing both printing and parsing functionalities. ### JSON Structure Example ```json { "name": "Awesome 4K", "resolutions": [ { "width": 1280, "height": 720 }, { "width": 1920, "height": 1080 }, { "width": 3840, "height": 2160 } ] } ``` ### Code Snippet (Conceptual) ```c // Assume 'json_string' contains the JSON content shown above // Parsing const char *json_string = "{\"name\": \"Awesome 4K\", \"resolutions\": [{\"width\": 1280, \"height\": 720}, {\"width\": 1920, \"height\": 1080}, {\"width\": 3840, \"height\": 2160}]}"; cJSON *parsed_json = cJSON_Parse(json_string); // Check for parsing errors if (parsed_json == NULL) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) { fprintf(stderr, "Error Before: %s\n", json_string, error_ptr); } return 1; // Indicate error } // Printing (formatted) char *formatted_string = cJSON_Print(parsed_json); printf("Formatted JSON:\n%s\n", formatted_string); // Printing (unformatted) char *unformatted_string = cJSON_PrintUnformatted(parsed_json); printf("Unformatted JSON:\n%s\n", unformatted_string); // Clean up allocated memory cJSON_Delete(parsed_json); free(formatted_string); free(unformatted_string); ``` ``` -------------------------------- ### YAML Configuration for generate_test_runner.rb Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityHelperScriptsGuide.md Example YAML configuration file for the generate_test_runner.rb script, specifying includes, setup, and teardown options. ```yaml :unity: :includes: - stdio.h - microdefs.h :cexception: 1 :suit_setup: "blah = malloc(1024);" :suite_teardown: "free(blah);" ``` -------------------------------- ### Install cJSON via vcpkg Source: https://github.com/davegamble/cjson/blob/master/README.md Commands to clone vcpkg and install the cJSON package. ```bash git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install vcpkg install cjson ``` -------------------------------- ### Configure and Install cJSON Package Config File Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Configures the libcjson.pc file for pkg-config and installs it along with the cJSON header and library targets. ```cmake configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" @ONLY) install(FILES cJSON.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson") install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig") install(TARGETS "${CJSON_LIB}" EXPORT "${CJSON_LIB}" ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}" INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}" ) if (BUILD_SHARED_AND_STATIC_LIBS) install(TARGETS "${CJSON_LIB}-static" EXPORT "${CJSON_LIB}" ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}" ) endif() if(ENABLE_TARGET_EXPORT) # export library information for CMake projects install(EXPORT "${CJSON_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") endif() if(ENABLE_CJSON_VERSION_SO) set_target_properties("${CJSON_LIB}" PROPERTIES SOVERSION "${CJSON_VERSION_SO}" VERSION "${PROJECT_VERSION}") endif() ``` -------------------------------- ### Run a Unity Test Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Use the RUN_TEST macro to execute a test case. It handles setup, execution, and cleanup. ```c RUN_TEST(func, linenum) ``` -------------------------------- ### Create a Unity Test File Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityGettingStartedGuide.md A standard template for a C test file using Unity, including setup, teardown, and test execution in the main function. ```C #include "unity.h" #include "file_to_test.h" void setUp(void) { // set stuff up here } void tearDown(void) { // clean stuff up here } void test_function_should_doBlahAndBlah(void) { //test stuff } void test_function_should_doAlsoDoBlah(void) { //more test stuff } int main(void) { UNITY_BEGIN(); RUN_TEST(test_function_should_doBlahAndBlah); RUN_TEST(test_function_should_doAlsoDoBlah); return UNITY_END(); } ``` -------------------------------- ### Build cJSON with GNU Make Source: https://github.com/davegamble/cjson/blob/master/README.md Commands to compile and install cJSON using the legacy Makefile. ```bash make all ``` -------------------------------- ### Install cJSON_Utils Library and Package Config Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Installs the cJSON_Utils library targets, header, and pkg-config file. Handles both shared/static builds and CMake target exports. ```cmake configure_file("${CMAKE_CURRENT_SOURCE_DIR}/library_config/libcjson_utils.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" @ONLY) install(TARGETS "${CJSON_UTILS_LIB}" EXPORT "${CJSON_UTILS_LIB}" ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_FULL_BINDIR}" INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}" ) if (BUILD_SHARED_AND_STATIC_LIBS) install(TARGETS "${CJSON_UTILS_LIB}-static" EXPORT "${CJSON_UTILS_LIB}" ARCHIVE DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}" INCLUDES DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}" ) endif() install(FILES cJSON_Utils.h DESTINATION "${CMAKE_INSTALL_FULL_INCLUDEDIR}/cjson") install (FILES "${CMAKE_CURRENT_BINARY_DIR}/libcjson_utils.pc" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/pkgconfig") endif() ``` -------------------------------- ### Configure Weak Function Support Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Specify compiler-specific attributes or pragmas to enable optional setUp and tearDown functions. ```c #define UNITY_WEAK_ATTRIBUTE weak #define UNITY_WEAK_ATTRIBUTE __attribute__((weak)) #define UNITY_WEAK_PRAGMA #define UNITY_NO_WEAK ``` -------------------------------- ### Unity Test Summary Output Format Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityHelperScriptsGuide.md Example of the aggregated test summary output generated by the script. ```Text -------------------------- UNITY IGNORED TEST SUMMARY -------------------------- blah.c:22:test_sandwiches_should_HaveBreadOnTwoSides:IGNORE ------------------------- UNITY FAILED TEST SUMMARY ------------------------- blah.c:87:test_sandwiches_should_HaveCondiments:FAIL:Expected 1 was 0 meh.c:38:test_soda_should_BeCalledPop:FAIL:Expected "pop" was "coke" -------------------------- OVERALL UNITY TEST SUMMARY -------------------------- 45 TOTAL TESTS 2 TOTAL FAILURES 1 IGNORED ``` -------------------------------- ### Example JSON Structure Source: https://github.com/davegamble/cjson/blob/master/README.md This is an example of a JSON structure that can be built and parsed using cJSON. ```json { "name": "Awesome 4K", "resolutions": [ { "width": 1280, "height": 720 }, { "width": 1920, "height": 1080 }, { "width": 3840, "height": 2160 } ] } ``` -------------------------------- ### C Test Case Signatures Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityHelperScriptsGuide.md Example C function signatures that the generate_test_runner.rb script will detect as test cases. These functions should start with 'test' or 'spec'. ```c void testVerifyThatUnityIsAwesomeAndWillMakeYourLifeEasier(void) { ASSERT_TRUE(1); } void test_FunctionName_should_WorkProperlyAndReturn8(void) { ASSERT_EQUAL_INT(8, FunctionName()); } void spec_Function_should_DoWhatItIsSupposedToDo(void) { ASSERT_NOT_NULL(Function(5)); } ``` -------------------------------- ### Minimalist RUN_TEST Macro Definition Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md This macro defines the essential setup and teardown logic for executing a single Unity test case, including failure handling and setUp/tearDown calls. ```c #define RUN_TEST(testfunc) \ UNITY_NEW_TEST(#testfunc) \ if (TEST_PROTECT()) { \ setUp(); \ testfunc(); \ } \ if (TEST_PROTECT() && (!TEST_IS_IGNORED)) \ tearDown(); \ UnityConcludeTest(); ``` -------------------------------- ### Weak Function Support Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Control the use of weak functions for optional setUp() and tearDown() functions, especially useful for embedded targets. ```APIDOC ## UNITY_WEAK_ATTRIBUTE / UNITY_WEAK_PRAGMA / UNITY_NO_WEAK ### Description Enables or disables the use of weak functions for `setUp()` and `tearDown()`. Weak functions allow these setup and teardown functions to be optional. If your compiler supports weak functions (like gcc or clang), you can define `UNITY_WEAK_ATTRIBUTE` or `UNITY_WEAK_PRAGMA`. Define `UNITY_NO_WEAK` to explicitly disable this feature. ### Method Preprocessor Macro Definition ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```c #define UNITY_WEAK_ATTRIBUTE weak #define UNITY_WEAK_ATTRIBUTE __attribute__((weak)) #define UNITY_WEAK_PRAGMA #define UNITY_NO_WEAK ``` ### Response N/A #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Configure Library Export and Versioning Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Sets target properties for shared object versioning and installs library export files for CMake integration. ```cmake if(ENABLE_TARGET_EXPORT) # export library information for CMake projects install(EXPORT "${CJSON_UTILS_LIB}" DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") endif() if(ENABLE_CJSON_VERSION_SO) set_target_properties("${CJSON_UTILS_LIB}" PROPERTIES SOVERSION "${CJSON_UTILS_VERSION_SO}" VERSION "${PROJECT_VERSION}") endif() endif() ``` -------------------------------- ### Example of Non-Standard Integer Sizes Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Demonstrates a scenario where a target's natural register size (12-bit) conflicts with C standards, resulting in non-standard integer sizes. This can lead to unexpected behavior in C code. ```C char (8 bit) <= short (12 bit) <= int (12 bit) <= long (16 bit) ``` -------------------------------- ### Enable Fuzzing with AFL Source: https://github.com/davegamble/cjson/blob/master/fuzzing/CMakeLists.txt This snippet configures the build to enable fuzzing using AFL. It requires AFL to be installed and sanitizers to be enabled. The fuzzing executable is built and linked against the cjson library. ```cmake option(ENABLE_FUZZING "Create executables and targets for fuzzing cJSON with afl." Off) if (ENABLE_FUZZING) find_program(AFL_FUZZ afl-fuzz) if ("${AFL_FUZZ}" MATCHES "AFL_FUZZ-NOTFOUND") message(FATAL_ERROR "Couldn't find afl-fuzz.") endif() add_executable(afl-main afl.c) target_link_libraries(afl-main "${CJSON_LIB}") if (NOT ENABLE_SANITIZERS) message(FATAL_ERROR "Enable sanitizers with -DENABLE_SANITIZERS=On to do fuzzing.") endif() option(ENABLE_FUZZING_PRINT "Fuzz printing functions together with parser." On) set(fuzz_print_parameter "no") if (ENABLE_FUZZING_PRINT) set(fuzz_print_parameter "yes") endif() set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-error") add_custom_target(afl COMMAND "${AFL_FUZZ}" -i "${CMAKE_CURRENT_SOURCE_DIR}/inputs" -o "${CMAKE_CURRENT_BINARY_DIR}/findings" -x "${CMAKE_CURRENT_SOURCE_DIR}/json.dict" -- "${CMAKE_CURRENT_BINARY_DIR}/afl-main" "@@" "${fuzz_print_parameter}" DEPENDS afl-main) endif() ``` -------------------------------- ### Parse JSON with Options Source: https://github.com/davegamble/cjson/blob/master/README.md Use `cJSON_ParseWithOpts` for more control during parsing. It allows specifying whether the input must be null-terminated and provides a thread-safe way to get the end of the parsed JSON or error position. ```c cJSON_ParseWithOpts(const char *value, const char **return_parse_end, cJSON_bool require_null_terminated); ``` -------------------------------- ### Basic Unity Test Module Main Function Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Use this main function structure to initialize Unity, run tests, and conclude the test run. It's suitable for barebones test execution without arguments. ```c int main(void) { UNITY_BEGIN(); RUN_TEST(test_TheFirst); RUN_TEST(test_TheSecond); RUN_TEST(test_TheThird); return UNITY_END(); } ``` -------------------------------- ### Configure Plugins via YAML Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityHelperScriptsGuide.md Enable plugins like CException by defining them in a YAML configuration file. ```YAML :plugins: -:cexception ``` -------------------------------- ### Enable 64-bit support Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Define this macro to manually enable 64-bit support if it is not auto-detected. ```c #define UNITY_SUPPORT_64 ``` -------------------------------- ### Configure Plugins via Ruby Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityHelperScriptsGuide.md Enable plugins like CException by defining them in a Ruby hash. ```Ruby :plugins => [ :cexception ] ``` -------------------------------- ### Run generate_test_runner.rb from Command Line Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityHelperScriptsGuide.md Execute the generate_test_runner.rb script from the command line with test file and runner file names. ```shell ruby generate_test_runner.rb TestFile.c NameOfRunner.c ``` ```shell ruby generate_test_runner.rb TestFile.c ``` ```shell ruby generate_test_runner.rb TestFile.c my_config.yml ``` ```shell ruby generate_test_runner.rb TestFile.c my_config.yml extras.h ``` -------------------------------- ### Configure Custom Output Macros Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Define these macros to redirect test output to custom hardware interfaces when standard output is unavailable. ```c #define UNITY_OUTPUT_CHAR(a) RS232_putc(a) #define UNITY_OUTPUT_START() RS232_config(115200,1,8,0) #define UNITY_OUTPUT_FLUSH() RS232_flush() #define UNITY_OUTPUT_COMPLETE() RS232_close() ``` -------------------------------- ### Run Unity Test Summary Script Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityHelperScriptsGuide.md Execute the summary script to aggregate test results from a specified directory. ```Shell ruby unity_test_summary.rb build/test/ ``` ```Shell ruby unity_test_summary.rb build/test/ ~/projects/myproject/ ``` ```Shell ruby unity_test_summary.rb build\teat\ C:\projects\myproject\ ``` -------------------------------- ### Configure Test Suite Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Enables testing, defines the test executable, and sets up a custom 'check' target for running tests. ```cmake option(ENABLE_CJSON_TEST "Enable building cJSON test" ON) if(ENABLE_CJSON_TEST) enable_testing() set(TEST_CJSON cJSON_test) add_executable("${TEST_CJSON}" test.c) target_link_libraries("${TEST_CJSON}" "${CJSON_LIB}") add_test(NAME ${TEST_CJSON} COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TEST_CJSON}") # Disable -fsanitize=float-divide-by-zero for cJSON_test if (FLAG_SUPPORTED_fsanitizefloatdividebyzero) if ("${CMAKE_VERSION}" VERSION_LESS "2.8.12") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-sanitize=float-divide-by-zero") else() target_compile_options(${TEST_CJSON} PRIVATE "-fno-sanitize=float-divide-by-zero") endif() endif() #"check" target that automatically builds everything and runs the tests add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure DEPENDS ${TEST_CJSON}) endif() ``` -------------------------------- ### Basic Unity Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Use these macros for fundamental boolean assertions and explicit failure reporting. ```c TEST_ASSERT_TRUE(condition) ``` ```c TEST_ASSERT_FALSE(condition) ``` ```c TEST_ASSERT(condition) ``` ```c TEST_ASSERT_UNLESS(condition) ``` ```c TEST_FAIL() ``` ```c TEST_FAIL_MESSAGE(message) ``` -------------------------------- ### Build cJSON with CMake Source: https://github.com/davegamble/cjson/blob/master/README.md Standard commands to configure and build cJSON using CMake on Unix platforms. ```bash mkdir build cd build cmake .. ``` ```bash make ``` -------------------------------- ### C Integer Size Hierarchy Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Illustrates the guaranteed order of integer type sizes in C. Note that actual sizes can vary by target. ```C char <= short <= int <= long <= long long ``` -------------------------------- ### Include Subdirectories Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Adds tests and fuzzing directories to the build process. ```cmake add_subdirectory(tests) add_subdirectory(fuzzing) ``` -------------------------------- ### Generate Package Configuration Files Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Uses configure_file to generate CMake package configuration files from templates. ```cmake # create the other package config files configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfig.cmake.in" ${PROJECT_BINARY_DIR}/cJSONConfig.cmake @ONLY) configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/library_config/cJSONConfigVersion.cmake.in" ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake @ONLY) if(ENABLE_TARGET_EXPORT) # Install package config files install(FILES ${PROJECT_BINARY_DIR}/cJSONConfig.cmake ${PROJECT_BINARY_DIR}/cJSONConfigVersion.cmake DESTINATION "${CMAKE_INSTALL_FULL_LIBDIR}/cmake/cJSON") endif() ``` -------------------------------- ### Configure cJSON_Utils Library Build Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Configures the build for the cJSON_Utils library, enabling it if the option is set. It determines the library type (shared/static) and links against the cJSON library. ```cmake option(ENABLE_CJSON_UTILS "Enable building the cJSON_Utils library." OFF) if(ENABLE_CJSON_UTILS) set(CJSON_UTILS_LIB cjson_utils) file(GLOB HEADERS_UTILS cJSON_Utils.h) set(SOURCES_UTILS cJSON_Utils.c) if (NOT BUILD_SHARED_AND_STATIC_LIBS) add_library("${CJSON_UTILS_LIB}" "${CJSON_LIBRARY_TYPE}" "${HEADERS_UTILS}" "${SOURCES_UTILS}") target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}") else() add_library("${CJSON_UTILS_LIB}" SHARED "${HEADERS_UTILS}" "${SOURCES_UTILS}") target_link_libraries("${CJSON_UTILS_LIB}" "${CJSON_LIB}") add_library("${CJSON_UTILS_LIB}-static" STATIC "${HEADERS_UTILS}" "${SOURCES_UTILS}") target_link_libraries("${CJSON_UTILS_LIB}-static" "${CJSON_LIB}-static") set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES OUTPUT_NAME "${CJSON_UTILS_LIB}") set_target_properties("${CJSON_UTILS_LIB}-static" PROPERTIES PREFIX "lib") endif() ``` -------------------------------- ### Abort a Unity Test Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Use TEST_PROTECT to set up an abort mechanism and TEST_ABORT to trigger an early return from the test. ```c TEST_PROTECT() ``` ```c TEST_ABORT() ``` ```c main() { if (TEST_PROTECT()) { MyTest(); } } ``` -------------------------------- ### Enable Locale Support Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Adds a preprocessor definition to enable locale-aware functionality. ```cmake # Enable the use of locales option(ENABLE_LOCALES "Enable the use of locales" ON) if(ENABLE_LOCALES) add_definitions(-DENABLE_LOCALES) endif() ``` -------------------------------- ### Enable cJSON Testing Source: https://github.com/davegamble/cjson/blob/master/fuzzing/CMakeLists.txt This snippet enables the execution of cJSON tests. It adds an executable for fuzzing tests and links it against the cjson library. ```cmake if(ENABLE_CJSON_TEST) ADD_EXECUTABLE(fuzz_main fuzz_main.c cjson_read_fuzzer.c) TARGET_LINK_LIBRARIES(fuzz_main cjson) endif() ``` -------------------------------- ### Constructing JSON with Helper Functions Source: https://github.com/davegamble/cjson/blob/master/README.md Simplifies JSON construction using cJSON_Add...ToObject helper functions. The returned string is heap-allocated and requires manual freeing. ```c //NOTE: Returns a heap allocated string, you are required to free it after use. char *create_monitor_with_helpers(void) { const unsigned int resolution_numbers[3][2] = { {1280, 720}, {1920, 1080}, {3840, 2160} }; char *string = NULL; cJSON *resolutions = NULL; size_t index = 0; cJSON *monitor = cJSON_CreateObject(); if (cJSON_AddStringToObject(monitor, "name", "Awesome 4K") == NULL) { goto end; } resolutions = cJSON_AddArrayToObject(monitor, "resolutions"); if (resolutions == NULL) { goto end; } for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index) { cJSON *resolution = cJSON_CreateObject(); if (cJSON_AddNumberToObject(resolution, "width", resolution_numbers[index][0]) == NULL) { goto end; } if (cJSON_AddNumberToObject(resolution, "height", resolution_numbers[index][1]) == NULL) { goto end; } cJSON_AddItemToArray(resolutions, resolution); } string = cJSON_Print(monitor); if (string == NULL) { fprintf(stderr, "Failed to print monitor.\n"); } end: cJSON_Delete(monitor); return string; } ``` -------------------------------- ### Integrate cJSON with Meson Source: https://github.com/davegamble/cjson/blob/master/README.md Configuration for using cJSON as a dependency within a Meson project. ```meson project('c-json-example', 'c') cjson = dependency('libcjson') example = executable( 'example', 'example.c', dependencies: [cjson], ) ``` -------------------------------- ### Constructing and Printing JSON Manually Source: https://github.com/davegamble/cjson/blob/master/README.md Builds a JSON object by manually creating items and adding them to the structure. The returned string is heap-allocated and must be freed by the caller. ```c //create a monitor with a list of supported resolutions //NOTE: Returns a heap allocated string, you are required to free it after use. char *create_monitor(void) { const unsigned int resolution_numbers[3][2] = { {1280, 720}, {1920, 1080}, {3840, 2160} }; char *string = NULL; cJSON *name = NULL; cJSON *resolutions = NULL; cJSON *resolution = NULL; cJSON *width = NULL; cJSON *height = NULL; size_t index = 0; cJSON *monitor = cJSON_CreateObject(); if (monitor == NULL) { goto end; } name = cJSON_CreateString("Awesome 4K"); if (name == NULL) { goto end; } /* after creation was successful, immediately add it to the monitor, * thereby transferring ownership of the pointer to it */ cJSON_AddItemToObject(monitor, "name", name); resolutions = cJSON_CreateArray(); if (resolutions == NULL) { goto end; } cJSON_AddItemToObject(monitor, "resolutions", resolutions); for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index) { resolution = cJSON_CreateObject(); if (resolution == NULL) { goto end; } cJSON_AddItemToArray(resolutions, resolution); width = cJSON_CreateNumber(resolution_numbers[index][0]); if (width == NULL) { goto end; } cJSON_AddItemToObject(resolution, "width", width); height = cJSON_CreateNumber(resolution_numbers[index][1]); if (height == NULL) { goto end; } cJSON_AddItemToObject(resolution, "height", height); } string = cJSON_Print(monitor); if (string == NULL) { fprintf(stderr, "Failed to print monitor.\n"); } end: cJSON_Delete(monitor); return string; } ``` -------------------------------- ### Assert Equal String With Message Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Compare two null-terminate strings. Fail if any character is different or if the lengths are different. Output a custom message on failure. ```c TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) ``` -------------------------------- ### Use generate_test_runner.rb within Ruby Code Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityHelperScriptsGuide.md Instantiate and run the UnityTestRunnerGenerator from within a Ruby script, passing options as a hash. ```ruby require "generate_test_runner.rb" options = { :includes => ["stdio.h", "microdefs.h"], :cexception => 1, :suite_setup => "blah = malloc(1024);", :suite_teardown => "free(blah);" } UnityTestRunnerGenerator.new.run(testfile, runner_name, options) ``` ```ruby gen = UnityTestRunnerGenerator.new(options) test_files.each do |f| gen.run(f, File.basename(f,'.c')+"Runner.c") end ``` -------------------------------- ### Assert Equal String Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Compare two null-terminate strings. Fail if any character is different or if the lengths are different. ```c TEST_ASSERT_EQUAL_STRING(expected, actual) ``` -------------------------------- ### Configure floating point support Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Use these macros to explicitly include or exclude single or double precision floating point support. ```c //what manner of strange processor is this? #define UNITY_EXCLUDE_FLOAT #define UNITY_INCLUDE_DOUBLE ``` -------------------------------- ### String Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Macros for comparing null-terminated strings and fixed-length strings. ```APIDOC ## TEST_ASSERT_EQUAL_STRING(expected, actual) ### Description Compare two null-terminate strings. Fail if any character is different or if the lengths are different. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` ```APIDOC ## TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) ### Description Compare two strings. Fail if any character is different, stop comparing after len characters. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` ```APIDOC ## TEST_ASSERT_EQUAL_STRING_MESSAGE(expected, actual, message) ### Description Compare two null-terminate strings. Fail if any character is different or if the lengths are different. Output a custom message on failure. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` ```APIDOC ## TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) ### Description Compare two strings. Fail if any character is different, stop comparing after len characters. Output a custom message on failure. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Define Uninstall Target Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Creates a custom target to handle library uninstallation via a dedicated CMake script. ```cmake #Create the uninstall target option(ENABLE_CJSON_UNINSTALL "Enable creating uninstall target" ON) if(ENABLE_CJSON_UNINSTALL) add_custom_target(uninstall "${CMAKE_COMMAND}" -P "${PROJECT_SOURCE_DIR}/library_config/uninstall.cmake") endif() ``` -------------------------------- ### Configure cJSON Library Build Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Configures the build for the cJSON library, determining whether to build shared or static libraries based on various options. ```cmake set(CJSON_LIB cjson) file(GLOB HEADERS cJSON.h) set(SOURCES cJSON.c) option(BUILD_SHARED_AND_STATIC_LIBS "Build both shared and static libraries" Off) option(CJSON_OVERRIDE_BUILD_SHARED_LIBS "Override BUILD_SHARED_LIBS with CJSON_BUILD_SHARED_LIBS" OFF) option(CJSON_BUILD_SHARED_LIBS "Overrides BUILD_SHARED_LIBS if CJSON_OVERRIDE_BUILD_SHARED_LIBS is enabled" ON) option(ENABLE_CJSON_VERSION_SO "Enables cJSON so version" ON) if ((CJSON_OVERRIDE_BUILD_SHARED_LIBS AND CJSON_BUILD_SHARED_LIBS) OR ((NOT CJSON_OVERRIDE_BUILD_SHARED_LIBS) AND BUILD_SHARED_LIBS)) set(CJSON_LIBRARY_TYPE SHARED) else() set(CJSON_LIBRARY_TYPE STATIC) endif() ``` -------------------------------- ### Memory and String Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Macros for comparing pointers, strings, and raw memory blocks. ```APIDOC ## TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) ### Description Asserts that the contents of memory blocks are identical. ### Parameters - **expected** (pointer) - Pointer to expected memory. - **actual** (pointer) - Pointer to actual memory. - **len** (size_t) - Number of bytes to compare. ``` -------------------------------- ### Exclude limits.h Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Define this to prevent Unity from including limits.h, useful if your system does not support it. ```c #define UNITY_EXCLUDE_LIMITS_H ``` -------------------------------- ### Exclude Internal Details Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Disable internal scratchpads to reduce memory usage when features like CMock are not required. ```c #define UNITY_EXCLUDE_DETAILS ``` -------------------------------- ### Unity Assertion Macros Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Macros for validating conditions, numerical values, and array contents. ```APIDOC ## Basic Validity Tests - TEST_ASSERT_TRUE(condition): Fails if condition is false. - TEST_ASSERT_FALSE(condition): Fails if condition is true. - TEST_FAIL() / TEST_FAIL_MESSAGE(message): Forces a test failure. ## Numerical Assertions - TEST_ASSERT_EQUAL_INT/UINT/HEX[size](expected, actual): Compares integers for equality. - TEST_ASSERT_INT_WITHIN(delta, expected, actual): Asserts actual is within delta of expected. - TEST_ASSERT_GREATER_THAN(threshold, actual): Asserts actual > threshold. - TEST_ASSERT_LESS_THAN(threshold, actual): Asserts actual < threshold. ## Array Assertions - TEST_ASSERT_EQUAL_[TYPE]_ARRAY(expected, actual, elements): Compares arrays of specific types. - TEST_ASSERT_EACH_EQUAL_[TYPE](expected, actual, elements): Checks if every element in an array equals the expected value. ``` -------------------------------- ### Integer Equality Assertions (Signed) Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Compare two signed integers for equality. Variants exist for specific integer sizes. ```c TEST_ASSERT_EQUAL_INT(expected, actual) ``` ```c TEST_ASSERT_EQUAL_INT8(expected, actual) ``` ```c TEST_ASSERT_EQUAL_INT16(expected, actual) ``` ```c TEST_ASSERT_EQUAL_INT32(expected, actual) ``` ```c TEST_ASSERT_EQUAL_INT64(expected, actual) ``` -------------------------------- ### Integer Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Macros for comparing signed and unsigned integers of various bit widths. ```APIDOC ## Integer Comparison Macros ### Signed Integers - **TEST_ASSERT_EQUAL_INT(expected, actual)** - **TEST_ASSERT_EQUAL_INT8(expected, actual)** - **TEST_ASSERT_EQUAL_INT16(expected, actual)** - **TEST_ASSERT_EQUAL_INT32(expected, actual)** - **TEST_ASSERT_EQUAL_INT64(expected, actual)** ### Unsigned Integers - **TEST_ASSERT_EQUAL_UINT(expected, actual)** - **TEST_ASSERT_EQUAL_UINT8(expected, actual)** - **TEST_ASSERT_EQUAL_UINT16(expected, actual)** - **TEST_ASSERT_EQUAL_UINT32(expected, actual)** - **TEST_ASSERT_EQUAL_UINT64(expected, actual)** ### General - **TEST_ASSERT_EQUAL(expected, actual)** - **TEST_ASSERT_NOT_EQUAL(expected, actual)** ``` -------------------------------- ### Test Execution and Flow Control Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Macros for running, ignoring, and aborting tests within the Unity framework. ```APIDOC ## RUN_TEST(func, linenum) ### Description Executes a test function, performing setup before and cleanup/tabulation after. ## TEST_IGNORE() / TEST_IGNORE_MESSAGE(message) ### Description Immediately returns control to the caller, marking the test as ignored. Optionally outputs a message. ## TEST_PROTECT() / TEST_ABORT() ### Description Provides a mechanism to escape from a test early. TEST_PROTECT sets up the catch point, and TEST_ABORT returns control to that point. ``` -------------------------------- ### Include cJSON Header Source: https://github.com/davegamble/cjson/blob/master/README.md Standard C include directive for the cJSON library. ```c #include ``` -------------------------------- ### Each Equal Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Macros for verifying that every element in an array matches a single expected value. ```APIDOC ## Each Equal Assertions ### Description Verifies that every element in the 'actual' array is equal to the single 'expected' value. ### Parameters - **expected** (value) - Required - The single value to compare against each array element. - **actual** (array) - Required - The array to be tested. - **num_elements** (int) - Required - The number of elements to check. - **len** (int) - Optional - Used in TEST_ASSERT_EACH_EQUAL_MEMORY to specify bytes per element. ``` -------------------------------- ### Basic Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Fundamental test control and boolean assertion macros. ```APIDOC ## TEST_FAIL ### Description Forces a test failure, typically used within conditional logic. ## TEST_IGNORE ### Description Marks a test case as ignored, preventing it from executing. ## Boolean Assertions - **TEST_ASSERT(condition)**: Asserts that the condition is true. - **TEST_ASSERT_TRUE(condition)**: Asserts that the condition is true. - **TEST_ASSERT_FALSE(condition)**: Asserts that the condition is false. - **TEST_ASSERT_UNLESS(condition)**: Semantic variation of TEST_ASSERT_FALSE. - **TEST_ASSERT_NULL(pointer)**: Asserts that the pointer is NULL. - **TEST_ASSERT_NOT_NULL(pointer)**: Asserts that the pointer is not NULL. ``` -------------------------------- ### Hexadecimal Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Macros for comparing unsigned integers with hexadecimal output formatting. ```APIDOC ## TEST_ASSERT_EQUAL_HEX(expected, actual) ### Description Asserts that two unsigned integers are equal, providing failure messages in hexadecimal format. ### Parameters - **expected** (any) - The expected value. - **actual** (any) - The actual value to test. ``` -------------------------------- ### Output Redirection Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Configure how Unity test results are outputted, allowing redirection from stdout to custom functions or serial ports. ```APIDOC ## UNITY_OUTPUT_COMPLETE() ### Description Defines how Unity test results are finalized and outputted. By default, Unity prints to `stdout`. This macro allows redirection to custom functions, useful for embedded systems or simulators without `stdout` support. ### Method Preprocessor Macro Definition ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```c #define UNITY_OUTPUT_CHAR(a) RS232_putc(a) #define UNITY_OUTPUT_START() RS232_config(115200,1,8,0) #define UNITY_OUTPUT_FLUSH() RS232_flush() #define UNITY_OUTPUT_COMPLETE() RS232_close() ``` ### Response N/A #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### General Integer Equality Assertion Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md A general-purpose macro for integer equality comparison, equivalent to TEST_ASSERT_EQUAL_INT. ```c TEST_ASSERT_EQUAL(expected, actual) ``` -------------------------------- ### Exclude stdint.h Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Define this to prevent Unity from including stdint.h, useful if your system does not support it. ```c #define UNITY_EXCLUDE_STDINT_H ``` -------------------------------- ### Float Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Macros for asserting equality and range for floating-point numbers. ```APIDOC ## TEST_ASSERT_FLOAT_WITHIN(delta, expected, actual) ### Description Asserts that the actual value is within plus or minus delta of the expected value. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` ```APIDOC ## TEST_ASSERT_EQUAL_FLOAT(expected, actual) ### Description Asserts that two floating point values are "equal" within a small % delta of the expected value. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` ```APIDOC ## TEST_ASSERT_EQUAL_DOUBLE(expected, actual) ### Description Asserts that two floating point values are "equal" within a small % delta of the expected value. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Integer Equality Assertions (Unsigned) Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Compare two unsigned integers for equality. Variants exist for specific integer sizes. ```c TEST_ASSERT_EQUAL_UINT(expected, actual) ``` ```c TEST_ASSERT_EQUAL_UINT8(expected, actual) ``` ```c TEST_ASSERT_EQUAL_UINT16(expected, actual) ``` ```c TEST_ASSERT_EQUAL_UINT32(expected, actual) ``` ```c TEST_ASSERT_EQUAL_UINT64(expected, actual) ``` -------------------------------- ### Enable ANSI Color Output Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Enable the use of ANSI escape codes for colored test output. ```c #define UNITY_OUTPUT_COLOR ``` -------------------------------- ### Assert Equal String Length With Message Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Compare two strings. Fail if any character is different, stop comparing after len characters. Output a custom message on failure. ```c TEST_ASSERT_EQUAL_STRING_LEN_MESSAGE(expected, actual, len, message) ``` -------------------------------- ### Parse JSON with Length and Options Source: https://github.com/davegamble/cjson/blob/master/README.md Use `cJSON_ParseWithLengthOpts` to parse a JSON string with a given length and advanced options, including thread-safe error positioning and null-termination checking. ```c cJSON_ParseWithLengthOpts(const char *value, size_t buffer_length, const char **return_parse_end, cJSON_bool require_null_terminated); ``` -------------------------------- ### Assert Equal String Length Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Compare two strings. Fail if any character is different, stop comparing after len characters. ```c TEST_ASSERT_EQUAL_STRING_LEN(expected, actual, len) ``` -------------------------------- ### Assert Null Pointer Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Fails if the pointer is not equal to NULL. ```c TEST_ASSERT_NULL(pointer) ``` -------------------------------- ### Parse JSON and Validate Resolution in C Source: https://github.com/davegamble/cjson/blob/master/README.md Demonstrates parsing a JSON string and iterating through an array of objects to check for specific numeric values. Requires manual memory management via cJSON_Delete to prevent leaks. ```c /* return 1 if the monitor supports full hd, 0 otherwise */ int supports_full_hd(const char * const monitor) { const cJSON *resolution = NULL; const cJSON *resolutions = NULL; const cJSON *name = NULL; int status = 0; cJSON *monitor_json = cJSON_Parse(monitor); if (monitor_json == NULL) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) { fprintf(stderr, "Error before: %s\n", error_ptr); } status = 0; goto end; } name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name"); if (cJSON_IsString(name) && (name->valuestring != NULL)) { printf("Checking monitor \"%s\"\n", name->valuestring); } resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions"); cJSON_ArrayForEach(resolution, resolutions) { cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width"); cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height"); if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height)) { status = 0; goto end; } if ((width->valuedouble == 1920) && (height->valuedouble == 1080)) { status = 1; goto end; } } end: cJSON_Delete(monitor_json); return status; } ``` -------------------------------- ### C Brace Placement Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/ThrowTheSwitchCodingStandard.md Requires braces for all blocks, even single-line statements, with the left brace on the next line. ```C while (blah) { //Like so. Even if only one line, we use braces. } ``` -------------------------------- ### Integer Equality Assertions (Hexadecimal) Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Compare two integers for equality and display them in hexadecimal format. Size variants affect nibble display. ```c TEST_ASSERT_EQUAL_HEX(expected, actual) ``` ```c TEST_ASSERT_EQUAL_HEX8(expected, actual) ``` ```c TEST_ASSERT_EQUAL_HEX16(expected, actual) ``` ```c TEST_ASSERT_EQUAL_HEX32(expected, actual) ``` ```c TEST_ASSERT_EQUAL_HEX64(expected, actual) ``` -------------------------------- ### Pointer Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Macros for checking if a pointer is NULL or not NULL. ```APIDOC ## TEST_ASSERT_NULL(pointer) ### Description Fails if the pointer is not equal to NULL. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` ```APIDOC ## TEST_ASSERT_NOT_NULL(pointer) ### Description Fails if the pointer is equal to NULL. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Integer Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Assertions for comparing signed and unsigned integers of various bit widths. Availability depends on target configuration. ```c TEST_ASSERT_EQUAL_INT (expected, actual) TEST_ASSERT_EQUAL_INT8 (expected, actual) TEST_ASSERT_EQUAL_INT16 (expected, actual) TEST_ASSERT_EQUAL_INT32 (expected, actual) TEST_ASSERT_EQUAL_INT64 (expected, actual) TEST_ASSERT_EQUAL (expected, actual) TEST_ASSERT_NOT_EQUAL (expected, actual) TEST_ASSERT_EQUAL_UINT (expected, actual) TEST_ASSERT_EQUAL_UINT8 (expected, actual) TEST_ASSERT_EQUAL_UINT16 (expected, actual) TEST_ASSERT_EQUAL_UINT32 (expected, actual) TEST_ASSERT_EQUAL_UINT64 (expected, actual) ``` -------------------------------- ### Assert Bit Low Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Test a single bit and verify that it is low. The bit is specified 0-31 for a 32-bit integer. ```c TEST_ASSERT_BIT_LOW(bit, actual) ``` -------------------------------- ### Assert Not Null Pointer Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Fails if the pointer is equal to NULL. ```c TEST_ASSERT_NOT_NULL(pointer) ``` -------------------------------- ### Memory Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Macro for comparing blocks of memory. ```APIDOC ## TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) ### Description Compare two blocks of memory. This is a good generic assertion for types that can't be coerced into acting like standard types... but since it's a memory compare, you have to be careful that your data types are packed. ### Method TEST ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Output Color Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Enable the use of ANSI escape codes for colored output in Unity test results. ```APIDOC ## UNITY_OUTPUT_COLOR ### Description Enables the use of ANSI escape codes to add color to Unity's test output. This can improve readability in terminals that support ANSI color codes. ### Method Preprocessor Macro Definition ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example ```c #define UNITY_OUTPUT_COLOR ``` ### Response N/A #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Assert Equal Memory Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Compare two blocks of memory. This is a good generic assertion for types that can't be coerced into acting like standard types. Since it's a memory compare, you have to be careful that your data types are packed. ```c TEST_ASSERT_EQUAL_MEMORY(expected, actual, len) ``` -------------------------------- ### Basic Test Control Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Macros for manually failing a test or marking a test case as ignored. ```c TEST_FAIL() ``` ```c TEST_IGNORE() ``` -------------------------------- ### Assert Bits Low Source: https://github.com/davegamble/cjson/blob/master/tests/unity/README.md Use an integer mask to specify which bits should be inspected to determine if they are all set high. High bits in the mask are compared, low bits ignored. ```c TEST_ASSERT_BITS_LOW(mask, actual) ``` -------------------------------- ### Floating Point Assertions Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityAssertionsReference.md Macros for asserting properties of single-precision floating point numbers. ```APIDOC ## TEST_ASSERT_FLOAT_IS_NAN(actual) ### Description Asserts that the actual parameter is a Not A Number (NaN) floating point representation. ## TEST_ASSERT_FLOAT_IS_DETERMINATE(actual) ### Description Asserts that the actual parameter is usable for mathematical operations (not infinity or NaN). ## TEST_ASSERT_FLOAT_IS_NOT_INF(actual) ### Description Asserts that the actual parameter is not positive infinity. ## TEST_ASSERT_FLOAT_IS_NOT_NEG_INF(actual) ### Description Asserts that the actual parameter is not negative infinity. ## TEST_ASSERT_FLOAT_IS_NOT_NAN(actual) ### Description Asserts that the actual parameter is not NaN. ## TEST_ASSERT_FLOAT_IS_NOT_DETERMINATE(actual) ### Description Asserts that the actual parameter is not usable for mathematical operations (is infinity or NaN). ``` -------------------------------- ### Apply Custom Compiler Flags Source: https://github.com/davegamble/cjson/blob/master/CMakeLists.txt Iterates through custom compiler flags, cleans them, checks for support, and adds supported flags to CMAKE_C_FLAGS. ```cmake foreach(compiler_flag ${custom_compiler_flags}) #remove problematic characters string(REGEX REPLACE "[^a-zA-Z0-9]" "" current_variable ${compiler_flag}) CHECK_C_COMPILER_FLAG(${compiler_flag} "FLAG_SUPPORTED_${current_variable}") if (FLAG_SUPPORTED_${current_variable}) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${compiler_flag}") endif() endforeach() ``` -------------------------------- ### Set Integer Width Source: https://github.com/davegamble/cjson/blob/master/tests/unity/docs/UnityConfigurationGuide.md Define UNITY_INT_WIDTH to specify the number of bits an 'int' occupies on your system. Defaults to 32 if not autodetected. ```c #define UNITY_INT_WIDTH 16 ```