### Example TAP Log Output Source: https://libcheck.github.io/check/doc/check_html/check_4.html This is an example of the output generated when TAP logging is enabled. It follows the Test Anything Protocol format. ```text ok 1 - mytests.c:test_suite_name:my_test_1: Passed ok 2 - mytests.c:test_suite_name:my_test_2: Passed not ok 3 - mytests.c:test_suite_name:my_test_3: Foo happened ok 4 - mytests.c:test_suite_name:my_test_1: Passed 1..4 ``` -------------------------------- ### Install Check on Ubuntu/Debian Source: https://libcheck.github.io/check/web/install.html Use apt-get to install the Check package on Ubuntu and Debian systems. ```bash $ sudo apt-get install check ``` -------------------------------- ### Install Check on OpenBSD Source: https://libcheck.github.io/check/web/install.html Use pkg_add to install the Check package on OpenBSD systems. ```bash $ sudo pkg_add check ``` -------------------------------- ### Import Check in CMakeLists.txt Source: https://libcheck.github.io/check/doc/check_html/check_5.html This example shows how to find the Check package and link it to your executable in a CMake project. Ensure Check is installed or configured via check_ROOT. ```cmake find_package(check REQUIRED CONFIG) add_executable(myproj.test myproj.test.c) target_link_libraries(myproj.test Check::check) add_test(NAME MyProj COMMAND myproj.test) ``` -------------------------------- ### Install Check on OSX via Homebrew Source: https://libcheck.github.io/check/web/install.html Use the brew command to install the Check package on OSX systems with Homebrew installed. ```bash $ brew install check ``` -------------------------------- ### Valgrind Memory Leak Summary Example Source: https://libcheck.github.io/check/doc/check_html/check_4.html Example output from Valgrind showing memory leak summary. Focuses on 'definitely lost' and 'still reachable' categories. ```text ==3979== LEAK SUMMARY: ==3979== definitely lost: 0 bytes in 0 blocks ==3979== indirectly lost: 0 bytes in 0 blocks ==3979== possibly lost: 0 bytes in 0 blocks ==3979== still reachable: 548 bytes in 24 blocks ==3979== suppressed: 0 bytes in 0 blocks ``` -------------------------------- ### Manual Check Installation Directory Configuration Source: https://libcheck.github.io/check/doc/check_html/check_3.html Sets the installation directory for the Check library when pkg-config is not available or cannot locate Check. ```cmake set(CHECK_INSTALL_DIR "C:/Program Files/check") ``` -------------------------------- ### Directory Structure Example Source: https://libcheck.github.io/check/doc/check_html/check_3.html Illustrates the recommended directory layout for the project, including source files, tests, and CMake configuration files. ```text . |-- Makefile.am |-- README |-- CMakeLists.txt |-- cmake | |-- config.h.in | |-- FindCheck.cmake |-- src | |-- CMakeLists.txt | |-- main.c | |-- money.c | `-- money.h `-- tests |-- CMakeLists.txt `-- check_money.c ``` -------------------------------- ### Install Check on Arch Linux Source: https://libcheck.github.io/check/web/install.html Use pacman to install the Check package on Arch Linux systems. ```bash $ sudo pacman -S check ``` -------------------------------- ### Install Check on Fedora Source: https://libcheck.github.io/check/web/install.html Use yum to install the Check package on Fedora systems. ```bash $ sudo yum install check ``` -------------------------------- ### Configure, Build, and Install Check on Solaris Source: https://libcheck.github.io/check/web/install.html Execute the standard autotools build process for Check from source on Solaris. ```bash $ ./configure CFLAGS=-m64 LDFLAGS=-m64 $ gmake $ gmake check $ gmake install ``` -------------------------------- ### Install Check on OSX via MacPorts Source: https://libcheck.github.io/check/web/install.html Use the port command to install the Check package on OSX systems with MacPorts installed. ```bash $ sudo port install check ``` -------------------------------- ### Configure CMake to find Check installation Source: https://libcheck.github.io/check/doc/check_html/check_5.html When Check is not installed in a system directory, use this command to tell your CMake build where to find it. Replace ${INSTALL_PREFIX} with the actual installation path. ```bash cmake -Dcheck_ROOT=${INSTALL_PREFIX} ``` -------------------------------- ### Example Test Code with mark_point (C) Source: https://libcheck.github.io/check/doc/check_html/check_3.html Illustrates how `mark_point()` helps pinpoint the location of errors in test code. ```c stuff_that_works (); mark_point (); stuff_that_dies (); ``` -------------------------------- ### START_TEST: Start a unit test Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Initiates a unit test with a given name. It sets up the necessary structures for the test function and must be paired with END_TEST. Variables can be declared within the braces of a START_TEST/END_TEST block. ```c #define START_TEST(unit_name) static void unit_name ## _fn (int _i CK_ATTRIBUTE_UNUSED); static const TTest unit_name ## _ttest = {""# unit_name, unit_name ## _fn, __FILE__, __LINE__}; static const TTest * unit_name = & unit_name ## _ttest; static void unit_name ## _fn (int _i CK_ATTRIBUTE_UNUSED) ``` -------------------------------- ### Typedef for Setup/Teardown Function Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Defines the type for setup and teardown functions used in test suites. ```c typedef void(* SFun) (void) ``` -------------------------------- ### Using ck_assert for Integer Equality Check Source: https://libcheck.github.io/check/doc/check_html/check_3.html This example demonstrates using the ck_assert macro to check for integer equality, providing a concise way to assert simple conditions. ```c ck_assert (money_amount (m) == 5); ``` -------------------------------- ### Example gcov Coverage Report Source: https://libcheck.github.io/check/doc/check_html/check_4.html An example of the output from the gcov utility, showing annotated source code with execution counts. Lines marked with '#####' were not executed. ```text -: 41: * object */ 18: 42: if (ht->table[p] != NULL) { -: 43: /* replaces the current entry */ #####: 44: ht->count--; #####: 45: ht->size -= ht->table[p]->size + #####: 46: sizeof(struct hashtable_entry); ``` -------------------------------- ### Using ck_assert for String Comparison Source: https://libcheck.github.io/check/doc/check_html/check_3.html This example shows how to use the ck_assert macro with a strcmp function to verify string equality, which is an alternative to ck_assert_str_eq. ```c ck_assert(strcmp (money_currency (m), "USD") == 0); ``` -------------------------------- ### Example Test Log Output Source: https://libcheck.github.io/check/doc/check_html/check_4.html Sample output from a Check test log file, showing test results organized by suite. ```text Running suite S1 ex_log_output.c:8:P:Core:test_pass: Test passed ex_log_output.c:14:F:Core:test_fail: Failure ex_log_output.c:18:E:Core:test_exit: (after this point) Early exit with return value 1 Running suite S2 ex_log_output.c:26:P:Core:test_pass2: Test passed Results for all suites run: 50%: Checks: 4, Failures: 1, Errors: 1 ``` -------------------------------- ### Check Unit Test Example Source: https://libcheck.github.io/check/doc/check_html/check_3.html This snippet demonstrates a basic Check unit test for a money creation function. It includes assertions for the amount and currency, and frees the allocated memory. ```c #include #include #include "../src/money.h" START_TEST(test_money_create) { Money *m; m = money_create(5, "USD"); ck_assert_int_eq(money_amount(m), 5); ck_assert_str_eq(money_currency(m), "USD"); money_free(m); } END_TEST ``` -------------------------------- ### Test Start and End Macros Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Macros to delineate the beginning and end of a test. ```c #define | START_TEST(__testname) #define | END_TEST ``` -------------------------------- ### Example XML Test Log Output Source: https://libcheck.github.io/check/doc/check_html/check_4.html This is an example of the XML output generated by the Check framework. It includes test suite and individual test case details, such as results, paths, function names, and messages. Note the structure for handling different test outcomes (success, failure, error) and escaped characters. ```xml 2012-10-19 09:56:06 S1 . ex_xml_output.c:10 test_pass 0 0.000013 Core Passed . ex_xml_output.c:16 test_fail 0 -1.000000 Core Failure . ex_xml_output.c:20 test_exit 0 -1.000000 Core Early exit with return value 1 S2 . ex_xml_output.c:28 test_pass2 0 0.000011 Core Passed . ex_xml_output.c:34 test_loop 0 -1.000000 Core Iteration 0 failed . ex_xml_output.c:34 test_loop 1 0.000010 Core Passed . ex_xml_output.c:34 test_loop 2 -1.000000 Core Iteration 2 failed XML escape " ' < > & tests . ex_xml_output.c:40 test_xml_esc_fail_msg 0 -1.000000 description " ' < > & fail " ' < > & message 0.001610 ``` -------------------------------- ### Example Test Failure Output Source: https://libcheck.github.io/check/doc/check_html/check_3.html Illustrates the typical output format when a unit test fails, including the test location, assertion details, and summary. ```text Running suite(s): Money 0%: Checks: 1, Failures: 1, Errors: 0 check_money.c:9:F:Core:test_money_create:0: Assertion 'money_amount (m)==5' failed: money_amount (m)==0, 5==5 FAIL: check_money ===================================================== 1 of 1 test failed Please report to check-devel AT lists.sourceforge.net ===================================================== ``` -------------------------------- ### C Test Fixture Setup and Teardown Source: https://libcheck.github.io/check/doc/check_html/check_4.html This C code patch introduces global variables and setup/teardown functions for a test fixture in the Check unit testing framework. Use this to manage shared resources across multiple tests within a test case. ```c /* * Check: a unit test framework for C * Copyright (C) 2001, 2002 Arien Malec * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * MA 02110-1301, USA. */ #include #include #include "../src/money.h" Money *five_dollars; void setup(void) { five_dollars = money_create(5, "USD"); } void teardown(void) { money_free(five_dollars); } START_TEST(test_money_create) { ck_assert_int_eq(money_amount(five_dollars), 5); ck_assert_str_eq(money_currency(five_dollars), "USD"); } END_TEST START_TEST(test_money_create_neg) { Money *m = money_create(-1, "USD"); ck_assert_msg(m == NULL, "NULL should be returned on attempt to create with " "a negative amount"); } END_TEST START_TEST(test_money_create_zero) { Money *m = money_create(0, "USD"); if (money_amount(m) != 0) { ck_abort_msg("Zero is a valid amount of money"); } } END_TEST Suite * money_suite(void) { Suite *s; TCase *tc_core; TCase *tc_limits; s = suite_create("Money"); /* Core test case */ tc_core = tcase_create("Core"); tcase_add_checked_fixture(tc_core, setup, teardown); tcase_add_test(tc_core, test_money_create); suite_add_tcase(s, tc_core); /* Limits test case */ tc_limits = tcase_create("Limits"); tcase_add_test(tc_limits, test_money_create_neg); tcase_add_test(tc_limits, test_money_create_zero); suite_add_tcase(s, tc_limits); return s; } int main(void) { int number_failed; Suite *s; SRunner *sr; s = money_suite(); sr = srunner_create(s); srunner_run_all(sr, CK_NORMAL); number_failed = srunner_ntests_failed(sr); srunner_free(sr); return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } ``` -------------------------------- ### START_TEST Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Starts a unit test with a given name. It should be paired with END_TEST. Variables can be declared within the braces of a START_TEST/END_TEST pair. ```APIDOC ## START_TEST ### Description Start a unit test with START_TEST(unit_name), end with END_TEST. One must use braces within a START_/END_ pair to declare new variables. ### Value static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED); static const TTest __testname ## _ttest = {"#"__testname, __testname ## _fn, __FILE__, __LINE__}; static const TTest * __testname = & __testname ## _ttest; static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED) ### TTest **Definition:** check.h:167 ### Since 0.6.0 ``` -------------------------------- ### Python Test Runner Integration with Subunit Source: https://libcheck.github.io/check/doc/check_html/check_4.html This Python example shows how to set up a test runner to execute Check test executables that output in Subunit format. The test runner will invoke the specified check drivers. ```python import subunit class ShellTests(subunit.ExecTestCase): """Run some tests from the C codebase.""" def test_group_one(self): """./foo/check_driver""" def test_group_two(self): """./foo/other_driver""" ``` -------------------------------- ### Compile Check with Autotools on Cygwin Source: https://libcheck.github.io/check/web/install.html Standard autotools build process for Cygwin. Ensure gcc and make are installed. ```bash $ ./configure $ make $ make check $ sudo make install ``` -------------------------------- ### tcase_add_checked_fixture Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h_source.html Adds a setup and teardown function to a test case. These functions are executed before and after each test within the case. ```APIDOC ## tcase_add_checked_fixture ### Description Adds a setup and teardown function to a test case. These functions are executed before and after each test within the case. ### Function Signature ```c CK_DLL_EXP void CK_EXPORT tcase_add_checked_fixture(TCase *tc, SFun setup, SFun teardown) ``` ### Parameters * **tc** (TCase *) - A pointer to the TCase structure. * **setup** (SFun) - A pointer to the setup function. * **teardown** (SFun) - A pointer to the teardown function. ``` -------------------------------- ### Valgrind Heap Summary After Disabling Fork Source: https://libcheck.github.io/check/doc/check_html/check_4.html Example output from Valgrind when fork() is disabled, indicating no heap memory leaks. ```text ==4924== HEAP SUMMARY: ==4924== in use at exit: 0 bytes in 0 blocks ==4924== total heap usage: 482 allocs, 482 frees, 122,351 bytes allocated ==4924== ==4924== All heap blocks were freed -- no leaks are possible ``` -------------------------------- ### tcase_fn_start Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h_source.html Marks the start of a test function within a test case. This is typically called internally by the framework. ```APIDOC ## tcase_fn_start ### Description Marks the start of a test function within a test case. This is typically called internally by the framework. ### Function Signature ```c CK_DLL_EXP void CK_EXPORT tcase_fn_start(const char *fname, const char *file, int line) ``` ### Parameters * **fname** (const char *) - The name of the test function. * **file** (const char *) - The source file where the test function is defined. * **line** (int) - The line number where the test function starts. ``` -------------------------------- ### Enum for Test Context Types Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Enumeration representing the different contexts a test can be in, such as setup, test body, or teardown. ```c enum ck_result_ctx ``` -------------------------------- ### Using ck_abort_msg for Unconditional Failure Source: https://libcheck.github.io/check/doc/check_html/check_3.html This example shows how to use ck_abort_msg to unconditionally fail a test and print a custom error message. This is useful when a specific condition must be met. ```c if (strcmp (money_currency (m), "USD") != 0) { ck_abort_msg ("Currency not set correctly on creation"); } ``` -------------------------------- ### tcase_add_unchecked_fixture Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Adds setup and teardown functions to a test case that are executed once at the beginning and end of the test case, respectively. These functions run in the same process space as the test case. ```APIDOC ## tcase_add_unchecked_fixture() ### Description Add unchecked fixture setup/teardown functions to a test case. Unchecked fixture functions are run at the start and end of the test case, and not before and after unit tests. Further, unchecked fixture functions are not run in a separate address space, like test functions, and so must not exit or signal (e.g., segfault). Also, when run in CK_NOFORK mode, unchecked fixture functions may lead to different unit test behavior if unit tests change data setup by the fixture functions. Note that if a setup function fails, the remaining setup functions will be omitted, as will the test case and the teardown functions. If a teardown function fails the remaining teardown functins will be omitted. ### Parameters - **tc** (TCase *) - test case to add unchecked fixture setup/teardown to - **setup** (SFun) - function to add to be executed before the test case; if NULL no setup function is added - **teardown** (SFun) - function to add to be executed after the test case; if NULL no teardown function is added ### Since 0.8.0 ``` -------------------------------- ### Build and Test Execution Commands Source: https://libcheck.github.io/check/doc/check_html/check_3.html Commands to set up the build system, configure the project, build the main program and libraries, and run the Check tests. ```bash $ autoreconf --install $ ./configure $ make ``` ```bash make check ``` -------------------------------- ### Run the 'check_money' test suite Source: https://libcheck.github.io/check/doc/check_html/check_3.html Execute the test suite for the Money project. Use 'make test' on Unix-compatible systems or 'nmake test' on Windows with MSVC. This command verifies the project's functionality. ```bash make test ``` ```bash nmake test ``` -------------------------------- ### Control Flow with Checked and Unchecked Fixtures Source: https://libcheck.github.io/check/doc/check_html/check_4.html Illustrates the execution order of unchecked setup/teardown and checked setup/teardown fixtures around unit tests within a test case. ```c unchecked_setup(); fork(); checked_setup(); check_one(); checked_teardown(); wait(); fork(); checked_setup(); check_two(); checked_teardown(); wait(); unchecked_teardown(); ``` -------------------------------- ### Build 'main' and 'libmoney.la' on Unix-compatible systems Source: https://libcheck.github.io/check/doc/check_html/check_3.html Use these commands to create the CMake build system and build the project on Unix-compatible systems. This will build the 'main' executable and the 'libmoney.la' library. ```bash cmake . make ``` -------------------------------- ### Build 'main' and 'libmoney.la' on Windows using MSVC Source: https://libcheck.github.io/check/doc/check_html/check_3.html Use these commands to create the CMake build system and build the project on Windows using MSVC. This will build the 'main' executable and the 'libmoney.la' library. ```bash cmake -G "NMake Makefiles" . nmake ``` -------------------------------- ### tr_ctx Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Gets the context of a test result. ```APIDOC ## tr_ctx ### Description Retrieves the context (e.g., setup, test, teardown) in which a test result occurred. ### Method tr_ctx ### Parameters #### Path Parameters - **tr** (TestResult *) - Description: Pointer to the TestResult structure. ``` -------------------------------- ### srunner_tap_fname Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Gets the file name for TAP output. ```APIDOC ## srunner_tap_fname ### Description Returns the file name configured for TAP output. ### Method srunner_tap_fname ### Parameters #### Path Parameters - **sr** (SRunner *) - Description: The test runner. ``` -------------------------------- ### tr_rtype Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Gets the result type of a test result. ```APIDOC ## tr_rtype ### Description Retrieves the result type (e.g., pass, failure, error) from a TestResult structure. ### Method tr_rtype ### Parameters #### Path Parameters - **tr** (TestResult *) - Description: Pointer to the TestResult structure. ``` -------------------------------- ### tr_lno Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Gets the line number where a test result occurred. ```APIDOC ## tr_lno ### Description Retrieves the line number in the source file where the test result (e.g., failure, error) occurred. ### Method tr_lno ### Parameters #### Path Parameters - **tr** (TestResult *) - Description: Pointer to the TestResult structure. ``` -------------------------------- ### srunner_fork_status Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Gets the current fork status setting for the test runner. ```APIDOC ## srunner_fork_status ### Description Retrieves the current setting for how child processes are handled during test execution (e.g., inherit environment, fork, no fork). ### Method srunner_fork_status ### Parameters #### Path Parameters - **sr** (SRunner *) - Description: The test runner. ``` -------------------------------- ### Adding Multiple Suites to an SRunner Source: https://libcheck.github.io/check/doc/check_html/check_4.html Demonstrates how to initialize an SRunner with a master suite and add additional suites using srunner_add_suite(). This is useful for organizing tests for different modules in a large program. ```c SRunner *sr; sr = srunner_create (make_master_suite ()); srunner_add_suite (sr, make_list_suite ()); srunner_add_suite (sr, make_msg_suite ()); srunner_add_suite (sr, make_log_suite ()); srunner_add_suite (sr, make_limit_suite ()); srunner_add_suite (sr, make_fork_suite ()); srunner_add_suite (sr, make_fixture_suite ()); srunner_add_suite (sr, make_pack_suite ()); ``` -------------------------------- ### srunner_ntests_run Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Gets the total number of tests run by the test runner. ```APIDOC ## srunner_ntests_run ### Description Returns the total count of test cases that were executed. ### Method srunner_ntests_run ### Parameters #### Path Parameters - **sr** (SRunner *) - Description: The test runner. ``` -------------------------------- ### tr_tcname Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Gets the name of the test case associated with a test result. ```APIDOC ## tr_tcname ### Description Retrieves the name of the test case to which this test result belongs. ### Method tr_tcname ### Parameters #### Path Parameters - **tr** (TestResult *) - Description: Pointer to the TestResult structure. ``` -------------------------------- ### tcase_add_unchecked_fixture Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Adds an unchecked fixture (setup and teardown functions) to a test case. ```APIDOC ## tcase_add_unchecked_fixture ### Description Adds setup and teardown functions to a test case that will be executed without explicit checks on their return values. ### Method tcase_add_unchecked_fixture ### Parameters #### Path Parameters - **tc** (TCase *) - Description: The test case to add the fixture to. - **setup** (SFun) - Description: The setup function. - **teardown** (SFun) - Description: The teardown function. ``` -------------------------------- ### Set up MSVC Environment for CMake Build Source: https://libcheck.github.io/check/web/install.html Execute the vcvarsall.bat script to set up the MSVC environment variables for the current terminal session. This is necessary before running CMake commands. ```batch > C:\Program Files\Microsoft Visual Studios 10.0\VC\vcvarsall.bat ``` -------------------------------- ### Check Test Runner Execution Source: https://libcheck.github.io/check/doc/check_html/check_3.html The main function sets up and runs the test suite using Check's suite runner. It reports the number of failed tests and returns an appropriate exit status. ```c int main(void) { int number_failed; Suite *s; SRunner *sr; s = money_suite(); sr = srunner_create(s); srunner_run_all(sr, CK_NORMAL); number_failed = srunner_ntests_failed(sr); srunner_free(sr); return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } ``` -------------------------------- ### srunner_log_fname Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h_source.html Gets the filename used for log output from the SRunner. This is useful for inspecting detailed test execution information. ```APIDOC ## srunner_log_fname ### Description Gets the filename used for log output from the SRunner. This is useful for inspecting detailed test execution information. ### Function Signature ```c CK_DLL_EXP const char *CK_EXPORT srunner_log_fname(SRunner *sr) ``` ### Parameters * **sr** (SRunner *) - A pointer to the SRunner structure. ``` -------------------------------- ### srunner_xml_fname Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h_source.html Gets the filename used for XML output from the SRunner. This is useful for inspecting test results in XML format. ```APIDOC ## srunner_xml_fname ### Description Gets the filename used for XML output from the SRunner. This is useful for inspecting test results in XML format. ### Function Signature ```c CK_DLL_EXP const char *CK_EXPORT srunner_xml_fname(SRunner *sr) ``` ### Parameters * **sr** (SRunner *) - A pointer to the SRunner structure. ``` -------------------------------- ### C Unit Test for Money Class Creation Source: https://libcheck.github.io/check/doc/check_html/check_3.html This snippet demonstrates a basic unit test for creating a Money object and verifying its amount and currency. It includes necessary includes and the test structure for the Check framework. ```c #include #include "../src/money.h" START_TEST(test_money_create) { Money *m; m = money_create(5, "USD"); ck_assert_int_eq(money_amount(m), 5); ck_assert_str_eq(money_currency(m), "USD"); money_free(m); } END_TEST int main(void) { return 0; } ``` -------------------------------- ### suite_create Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Creates a new test suite with the given name. ```APIDOC ## suite_create ### Description Creates a new test suite with the specified name. ### Method suite_create ### Parameters #### Path Parameters - **name** (const char *) - Description: The name of the suite. ``` -------------------------------- ### Basic Unit Test Structure Source: https://libcheck.github.io/check/doc/check_html/check_3.html Define a basic unit test using the START_TEST and END_TEST macros. The code for your unit test goes between these macros. ```c START_TEST (test_name) { /* unit test code */ } END_TEST ``` -------------------------------- ### SRunner Segmentation Fault Output Source: https://libcheck.github.io/check/doc/check_html/check_3.html Example output from SRunner indicating a segmentation fault (Signal 11) during a unit test. ```text Running suite(s): Money 0%: Checks: 1, Failures: 0, Errors: 1 check_money.c:5:E:Core:test_money_create:0: (after this point) Received signal 11 (Segmentation fault) ``` -------------------------------- ### Check Test Suite Creation Source: https://libcheck.github.io/check/doc/check_html/check_3.html Defines a function to create a test suite for the 'Money' functionality. It includes creating a test case and adding the 'test_money_create' test to it. ```c Suite * money_suite(void) { Suite *s; TCase *tc_core; s = suite_create("Money"); /* Core test case */ tc_core = tcase_create("Core"); tcase_add_test(tc_core, test_money_create); suite_add_tcase(s, tc_core); return s; } ``` -------------------------------- ### Initial Money Functions (C) Source: https://libcheck.github.io/check/doc/check_html/check_3.html These are the initial, unimplemented functions for the Money type, which lead to errors when tested. ```c char *money_currency(Money * m) { return NULL; } void money_free(Money * m) { return; } ``` -------------------------------- ### Adding a Looping Test Source: https://libcheck.github.io/check/doc/check_html/check_4.html Use `tcase_add_loop_test` to add a test function that will be executed multiple times with a loop variable. Specify the start and end values for the loop. ```c static const int primes[5] = {2,3,5,7,11}; START_TEST (check_is_prime) { ck_assert (is_prime (primes[_i])); } END_TEST ... tcase_add_loop_test (tcase, check_is_prime, 0, 5); ``` -------------------------------- ### Configure Test Logging with srunner_set_log Source: https://libcheck.github.io/check/doc/check_html/check_4.html Set up test logging by providing a log file name to the srunner_set_log function. Results will be written to the specified file. ```c SRunner *sr; sr = srunner_create (make_s1_suite ()); srunner_add_suite (sr, make_s2_suite ()); srunner_set_log (sr, "test.log"); srunner_run_all (sr, CK_NORMAL); ``` -------------------------------- ### Implement Money Structure and Functions Source: https://libcheck.github.io/check/doc/check_html/check_3.html Provides the C code for a Money structure and its associated creation and amount retrieval functions, including necessary headers and library license information. ```c /* * Check: a unit test framework for C * Copyright (C) 2001, 2002 Arien Malec * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * MA 02110-1301, USA. */ #include #include "money.h" struct Money { int amount; }; Money *money_create(int amount, char *currency) { return NULL; } int money_amount(Money * m) { return m->amount; } ``` -------------------------------- ### Compile Check from Source on GNU/Linux Source: https://libcheck.github.io/check/web/install.html Standard autotools build process for compiling Check from source on GNU/Linux. Ensure to run 'make check' and investigate failures. ```bash $ ./configure $ make $ make check $ sudo make install ``` -------------------------------- ### Set Compiler to Solaris Studio Source: https://libcheck.github.io/check/web/install.html Set the CC environment variable to 'cc' to explicitly use the Solaris Studio compiler. ```bash CC=cc ``` -------------------------------- ### Empty Main Function for Unit Tests Source: https://libcheck.github.io/check/doc/check_html/check_3.html An empty main function intended for the unit test file, serving as a placeholder before test cases are added. ```c /* * Check: a unit test framework for C * Copyright (C) 2001, 2002 Arien Malec * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, ``` -------------------------------- ### srunner_set_tap Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Configures a suite runner to output results in TAP (Test Anything Protocol) format to a specified file. This is an initialize-only operation, best done right after SRunner creation, and the TAP file cannot be changed later. It can operate alongside other logging configurations. ```APIDOC ## srunner_set_tap() ### Description Set the suite runner to output the result in TAP format to the given file. Note: TAP file setting is an initialize only operation – it should be done immediately after SRunner creation, and the TAP file can't be changed after being set. This setting does not conflict with the other log output types; all logging types can occur concurrently if configured. ### Parameters - **sr** (SRunner *) - The suite runner to log results of in TAP format. - **fname** (const char *) - The file name to output TAP results to. ### Since 0.9.12 ``` -------------------------------- ### srunner_create Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Creates a new test runner for a given suite. ```APIDOC ## srunner_create ### Description Initializes a test runner associated with a specific test suite. ### Method srunner_create ### Parameters #### Path Parameters - **s** (Suite *) - Description: The test suite for which to create a runner. ``` -------------------------------- ### srunner_results Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Returns an array of TestResult objects for all tests executed by a suite runner. The size of the array corresponds to the number of tests run, excluding setup function failures. Individual result details can be accessed using specific query functions. The returned memory must be freed. ```APIDOC ## srunner_results() ### Description Return an array of results for all tests run by a suite runner. Number of results is equal to srunner_ntests_run(), and excludes failures due to setup function failure. Information about individual results can be queried using: tr_rtype(), tr_ctx(), tr_msg(), tr_lno(), tr_lfile(), and tr_tcname(). Memory is malloc'ed and must be freed; however free the entire structure instead of individual test cases. ### Parameters - **sr** (SRunner *) - suite runner to retrieve results from ### Returns array of TestResult objects ### Since 0.6.1 ``` -------------------------------- ### Empty Main Function for Check Test Source: https://libcheck.github.io/check/doc/check_html/check_3.html An empty C main function for a test program. This serves as a minimal entry point for a test executable when no specific test logic is yet implemented. ```c /* * Check: a unit test framework for C * Copyright (C) 2001, 2002 Arien Malec * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, * MA 02110-1301, USA. */ int main(void) { return 0; } ``` -------------------------------- ### Integrate Check with Autotools using pkg-config Source: https://libcheck.github.io/check/doc/check_html/check_5.html Use this macro in your 'configure.ac' file to locate and use Check via pkg-config. Specify the minimum required version of Check. ```autoconf PKG_CHECK_MODULES([CHECK], [check >= MINIMUM-VERSION]) ``` ```autoconf PKG_CHECK_MODULES([CHECK], [check >= 0.9.6]) ``` -------------------------------- ### srunner_run_all Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Runs all suites contained within a suite runner, printing the results to standard output according to the specified print_mode. Log output is also generated if configured. Environment variables can influence which suites or test cases are executed. ```APIDOC ## srunner_run_all() ### Description Runs a suite runner and all contained suite, printing results to stdout as specified by the print_mode. In addition to running all suites, if the suite runner has been configured to output to a log, that is also performed. Note that if the CK_RUN_CASE, CK_RUN_SUITE, CK_INCLUDE_TAGS and/or CK_EXCLUDE_TAGS environment variables are defined, then only the named suites or test cases will run. ### Parameters - **sr** (SRunner *) - suite runner to run all suites from - **print_mode** (enum print_output) - the verbosity in which to report results to stdout ### Since 0.6.0 ``` -------------------------------- ### Build with GCC for Test Coverage Source: https://libcheck.github.io/check/doc/check_html/check_4.html Use these GCC flags to enable profiling and test coverage when building your source code. This is a prerequisite for generating coverage data. ```bash $ gcc -g -Wall -fprofile-arcs -ftest-coverage -o foo foo.c foo_check.c ``` -------------------------------- ### Directory Structure for Money Library Source: https://libcheck.github.io/check/doc/check_html/check_3.html This is the directory structure for the money library project, including source files and test files, managed with Autotools. ```shell . |-- Makefile.am |-- README |-- configure.ac |-- src | |-- Makefile.am | |-- main.c | |-- money.c | `-- money.h `-- tests |-- Makefile.am `-- check_money.c ``` -------------------------------- ### Compile Check with Autotools on MinGW/MinGW-w64 Source: https://libcheck.github.io/check/web/install.html Standard autotools build process for MinGW/MinGW-w64. Fork mode will be disabled. ```bash $ ./configure $ make $ make check $ make install ``` -------------------------------- ### Set PATH for Solaris Studio on Solaris Source: https://libcheck.github.io/check/web/install.html Configure the PATH environment variable to prioritize POSIX-compliant tools when using Solaris Studio for building. ```bash PATH=/opt/solarisstudio12.3/bin:/usr/xpg6/bin:/usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/sbin:/usr/sbin:/opt/csw/bin ``` -------------------------------- ### Test Runner Creation and Management Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h_source.html Functions for creating, adding suites to, and freeing a test runner. ```APIDOC ## srunner_create ### Description Creates a new test runner associated with a given suite. ### Signature `SRunner *srunner_create(Suite * s);` ## srunner_add_suite ### Description Adds a suite to an existing test runner. ### Signature `void srunner_add_suite(SRunner * sr, Suite * s);` ## srunner_free ### Description Frees the memory associated with a test runner. ### Signature `void srunner_free(SRunner * sr);` ``` -------------------------------- ### Adding New Test Cases to Money Suite Source: https://libcheck.github.io/check/doc/check_html/check_4.html This snippet shows how to add new test cases, specifically for negative and zero amounts, to an existing test suite. It involves creating new test case structures and adding them to the suite. ```c #include #include #include "../src/money.h" START_TEST(test_money_create) { Money *m; m = money_create(5, "USD"); ck_assert_int_eq(money_amount(m), 5); ck_assert_str_eq(money_currency(m), "USD"); money_free(m); } END_TEST START_TEST(test_money_create_neg) { Money *m = money_create(-1, "USD"); ck_assert_msg(m == NULL, "NULL should be returned on attempt to create with " "a negative amount"); } END_TEST START_TEST(test_money_create_zero) { Money *m = money_create(0, "USD"); if (money_amount(m) != 0) { ck_abort_msg("Zero is a valid amount of money"); } } END_TEST Suite * money_suite(void) { Suite *s; TCase *tc_core; TCase *tc_limits; s = suite_create("Money"); /* Core test case */ tc_core = tcase_create("Core"); tcase_add_test(tc_core, test_money_create); suite_add_tcase(s, tc_core); /* Limits test case */ tc_limits = tcase_create("Limits"); tcase_add_test(tc_limits, test_money_create_neg); tcase_add_test(tc_limits, test_money_create_zero); suite_add_tcase(s, tc_limits); return s; } int main(void) { int number_failed; Suite *s; SRunner *sr; s = money_suite(); sr = srunner_create(s); srunner_run_all(sr, CK_NORMAL); number_failed = srunner_ntests_failed(sr); srunner_free(sr); return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; } ``` -------------------------------- ### ck_abort Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h.html Unconditionally fails the current test. ```APIDOC ## ck_abort ### Description Unconditionally fails the current test. Once called, the remaining of the test is aborted. ### Method ck_abort ### Parameters None ### Note Once called, the remaining of the test is aborted. ### Since 0.9.6 ``` -------------------------------- ### GNU Free Documentation License - Copyright Notice Source: https://libcheck.github.io/check/doc/check_html/check_8.html This snippet shows the copyright notice as defined in the GNU Free Documentation License, Version 1.2. It permits copying and distribution of the license document itself. ```plaintext Copyright © 2000,2001,2002 Free Software Foundation, Inc. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. ``` -------------------------------- ### Output and Logging Source: https://libcheck.github.io/check/doc/doxygen/html/check_8h_source.html Functions for printing test results, setting and checking log files, and setting XML output files. ```APIDOC ## srunner_print ### Description Prints the test results to the standard output. ### Parameters - `sr` (SRunner *) - The test runner. - `print_mode` (enum print_output) - The mode for printing output. ### Signature `void srunner_print(SRunner *sr, enum print_output print_mode);` ## srunner_set_log ### Description Sets the filename for logging test results. ### Parameters - `sr` (SRunner *) - The test runner. - `fname` (const char *) - The filename for the log. ### Signature `void srunner_set_log(SRunner *sr, const char *fname);` ## srunner_has_log ### Description Checks if a log file has been set for the test runner. ### Signature `int srunner_has_log(SRunner *sr);` ## srunner_log_fname ### Description Retrieves the filename of the set log file. ### Signature `const char *srunner_log_fname(SRunner *sr);` ## srunner_set_xml ### Description Sets the filename for XML output. ### Parameters - `sr` (SRunner *) - The test runner. - `fname` (const char *) - The filename for the XML output. ### Signature `void srunner_set_xml(SRunner *sr, const char *fname);` ## srunner_has_xml ### Description Checks if an XML output file has been set for the test runner. ### Signature `int srunner_has_xml(SRunner *sr);` ## srunner_xml_fname ### Description Retrieves the filename of the set XML output file. ### Signature `const char *srunner_xml_fname(SRunner *sr);` ## srunner_set_tap ### Description Sets the filename for TAP (Test Anything Protocol) output. ### Parameters - `sr` (SRunner *) - The test runner. - `fname` (const char *) - The filename for the TAP output. ### Signature `void srunner_set_tap(SRunner *sr, const char *fname);` ## srunner_has_tap ### Description Checks if a TAP output file has been set for the test runner. ### Signature `int srunner_has_tap(SRunner *sr);` ## srunner_tap_fname ### Description Retrieves the filename of the set TAP output file. ### Signature `const char *srunner_tap_fname(SRunner *sr);` ```