### Introduction `unitt` is a simple unit testing module for the C language. It defines multiple function pointer types, covering types corresponding to functions related to clock, testing, pre-test setup, post-test cleanup, and random input generation. Meanwhile, it defines structures representing test cases and test suites. In addition, it provides a series of functions for asserting whether various conditions are met and a function for executing test suites. Moreover, through macro definitions, it simplifies the definition and operation of test cases and the test execution process, helping developers efficiently conduct unit testing work in C language projects and check whether the code functions meet expectations. ### Interfaces #### Functions ##### `unitt_det_true` Function ```c int unitt_det_true(int condition, const char* message); ``` **Function Description**: It is used to assert whether the given condition is true. During the testing process, by passing in the condition expression to be evaluated and the prompt message to be displayed when the condition is not met (i.e., the assertion fails), if the condition is true, it means the assertion passes and the function returns `UNITT_E_OK` (with a value of 0). If the condition is false, the assertion fails and the function returns `UNITT_E_FAIL` (with a value of -1). Meanwhile, corresponding error prompt messages may be output according to the specific implementation of the testing framework. **Parameter Introduction**: - `condition`: The condition expression to be evaluated. Usually, it is an expression that returns a Boolean type (represented by an integer in the C language, where non-zero means true and zero means false), such as comparisons between variables, results of logical operations, etc., which is used to judge whether specific testing expectations are met. - `message`: The prompt message to be displayed when the assertion fails. It is a string constant, which is used to inform developers which testing condition has not passed and related error information, facilitating the location of problems. ##### `unitt_det_false` Function ```c int unitt_det_false(int condition, const char* message); ``` **Function Description**: Opposite to `unitt_det_true`, it is used to assert whether the given condition is false. If the passed-in condition expression evaluates to false, the assertion passes and the function returns `UNITT_E_OK`. If the condition is true, the assertion fails and the function returns `UNITT_E_FAIL`. At the same time, corresponding error prompt messages will also be displayed according to the setting (if the testing framework supports message output). **Parameter Introduction**: - `condition`: The condition expression to be evaluated, similar to that in `unitt_det_true`, it is an expression that can yield a Boolean type result (represented by an integer), but here it is expected to be false for the assertion to pass. - `message`: The prompt message to be displayed when the assertion fails. It is also a string constant, which is used to clearly indicate the error situation where the condition expected to be false in the test is actually true. ##### `unitt_det_equal` Function ```c int unitt_det_equal(int expected, int actual, const char* message); ``` **Function Description**: It is used to assert whether two integers are equal. In unit testing, the expected integer value is compared with the actual integer value obtained. If the two values are equal, the assertion passes and the function returns `UNITT_E_OK`. If they are not equal, the assertion fails and the function returns `UNITT_E_FAIL`, and the specified error prompt message will be displayed to help developers know which integer comparison has a difference. **Parameter Introduction**: - `expected`: The expected integer value, that is, the integer result that developers expect to obtain in the test, which serves as the standard value for comparison in the assertion judgment. - `actual`: The actual integer value, which is the integer value actually generated after the test execution and will be compared with the expected value to determine whether it meets the testing expectations. - `message`: The prompt message to be displayed when the two integers are not equal and the assertion fails. Through detailed text descriptions, developers can quickly understand which two integers have a comparison problem and the corresponding test scenarios and other information. ##### `unitt_det_nequal` Function ```c int unitt_det_nequal(int expected, int actual, const char* message); ``` **Function Description**: It is used to assert whether two integers are not equal. If the passed-in expected integer value and the actual integer value are indeed not equal, the assertion passes and the function returns `UNITT_E_OK`. If the two values are equal, the assertion fails and the function returns `UNITT_E_FAIL`, and the corresponding error prompt message will be displayed to inform developers that the two integers that should be unequal are actually equal, which does not meet expectations. **Parameter Introduction**: - `expected`: The expected integer value, which serves as a reference for comparison and is expected to be different from the actual value for the assertion to pass. - `actual`: The actual integer value obtained, which is compared with the expected value to determine whether it meets the assertion requirement of being unequal. - `message`: The prompt message to be displayed when the assertion fails (i.e., when the two integers are equal), helping developers understand which group of integer comparisons has violated expectations and related test background information. ##### `unitt_det_null` Function ```c int unitt_det_null(void* pointer, const char* message); ``` **Function Description**: It is used to assert whether the given pointer is `NULL`. When testing code involving pointer operations, if the passed-in pointer value is `NULL`, it means the assertion passes and the function returns `UNITT_E_OK`. If the pointer is not `NULL`, the assertion fails and the function returns `UNITT_E_FAIL`, and the corresponding error prompt message will be output to help developers know which pointer's `NULL` check has a problem. **Parameter Introduction**: - `pointer`: The pointer variable to be evaluated. Its value will be checked to see if it is `NULL` to verify whether the handling of the pointer being null in the relevant code meets expectations, such as whether a function correctly handles the boundary case of an incoming null pointer. - `message`: The prompt message to be displayed when the pointer is not `NULL` and the assertion fails. It details which pointer does not meet the expected `NULL` state and related test situations, facilitating the location of the problem source. ##### `unitt_det_nnull` Function ```c int unitt_det_nnull(void* pointer, const char* message); ``` **Function Description**: Opposite to `unitt_det_null`, it is used to assert whether the given pointer is not `NULL`. If the passed-in pointer is not `NULL`, the assertion passes and the function returns `UNITT_E_OK`. If the pointer is `NULL`, the assertion fails and the function returns `UNITT_E_FAIL`, and the corresponding error prompt message will be displayed to prompt developers that the pointer being null does not meet expectations. **Parameter Introduction**: - `pointer`: The pointer variable to be evaluated. It is expected that its value is not `NULL` for the assertion to hold. It is often used in test scenarios such as checking whether the pointer returned by a function is valid and whether the pointer member in a structure is correctly assigned. - `message`: The prompt message to be displayed when the pointer is `NULL` and the assertion fails. It informs developers which pointer has not reached the expected non-`NULL` state and the corresponding test background information, which helps in troubleshooting. ##### `unitt_det_float` Function ```c int unitt_det_float(float expected, float actual, float epsilon, const char* message); ``` **Function Description**: It is used to assert whether two floating-point numbers are approximately equal. Due to the precision issues in the storage and calculation of floating-point numbers in computers, simple equality judgment cannot be used. Therefore, by passing in the acceptable difference (`epsilon`), the range of approximate equality is determined. If the difference between the two floating-point numbers is within the `epsilon` range, they are considered approximately equal and the assertion passes, and the function returns `UNITT_E_OK`. If the difference exceeds the range, the assertion fails and the function returns `UNITT_E_FAIL`, and the specified error prompt message will be output to inform developers that the comparison of floating-point numbers does not meet expectations. **Parameter Introduction**: - `expected`: The expected floating-point number, which is the floating-point number result that developers expect to obtain in the test and serves as a reference value for the approximate equality judgment. - `actual`: The actual floating-point number obtained, which is compared with the expected floating-point number. Based on the comparison between the difference between the two and `epsilon`, it is determined whether the assertion requirement of approximate equality is met. - `epsilon`: The acceptable difference, which is used to define the range of approximate equality of two floating-point numbers. That is, as long as `|expected - actual| <= epsilon`, the two floating-point numbers are considered approximately equal. This parameter needs to be reasonably set according to specific testing precision requirements. - `message`: The prompt message to be displayed when the difference between the two floating-point numbers exceeds the `epsilon` range and the assertion fails. It helps developers understand which two floating-point numbers have a comparison that does not meet the expectation of approximate equality and the corresponding test scenarios and other information. ##### `unitt_det_string` Function ```c int unitt_det_string(const char* expected, const char* actual, const char* message); ``` **Function Description**: It is used to assert whether two strings are equal. It will compare the passed-in expected string and the actual string character by character. If the two strings are exactly the same, the assertion passes and the function returns `UNITT_E_OK`. If there is any difference in characters, the assertion fails and the function returns `UNITT_E_FAIL`, and the corresponding error prompt message will be output to inform developers which two strings have a comparison inconsistency and related test background information. **Parameter Introduction**: - `expected`: The expected string, that is, the string content that developers expect to obtain in the test, which serves as the standard string for comparison in the assertion judgment. - `actual`: The actual string, which is the string content actually generated after the test execution and will be compared with the expected string character by character to determine whether it meets the testing expectations. - `message`: The prompt message to be displayed when the two strings are not equal and the assertion fails. Through detailed text descriptions, developers can quickly know which group of strings has a comparison problem and the corresponding test scenarios and other information. ##### `unitt_fail_handle` Function ```c int unitt_fail_handle(const char* name); ``` **Function Description**: It is used to handle the situation of test failure. When a test case fails during execution, by passing in the name of the failed test case, the function can execute the corresponding failure handling logic, such as recording failure information, outputting specific error prompts, etc. And regardless of the specific handling process, this function will finally return `UNITT_E_OK` to indicate that the process of handling the failure situation has been completed. **Parameter Introduction**: - `name`: The name of the failed test case, which is a string constant. Based on this name, it can be located which test case has a problem, and then targeted failure handling operations can be executed, such as accurately recording which test link has failed in the test report. ##### `unitt_execute` Function ```c void unitt_execute(UNITT* suites, uint32_t count, unitt_failcb_t failcb, const char* specific, const char* filename); ``` **Function Description**: It is responsible for executing a series of test suites. It will traverse the passed-in array of test suites and execute the testing operations one by one according to the test cases included in each test suite. During the testing process, it will call the corresponding setup function (if any), execute the testing function, make assertion judgments, call the cleanup function (if any), etc., and update the relevant statistical information (such as the number of test executions, the number of passed tests, and the total time consumption) according to the test results. If a test case fails, it will also call the passed-in failure callback function to handle the failure situation. Meanwhile, according to the passed-in specific test name parameter, it can choose to execute only a specific test case with a matching name, or it can also specify the file name of the output result (if it is `NULL`, the result will be output to the standard output). In this way, it can flexibly control the test execution and result output methods. **Parameter Introduction**: - `suites`: A pointer to an array of test suites of the `UNITT` structure type. The `UNITT` structure contains key information such as the name of the test suite, an array of test cases, the number of test cases, and a pointer to the clock function for timing. Through this pointer, each test suite to be executed and the situation of the test cases inside it can be located, which is the core data source for executing the test. - `count`: The number of elements in the array of test suites, that is, the number of test suites to be executed, which is used to determine the number of times to loop through the test suites to ensure that each test suite can be correctly processed. - `failcb`: A pointer to the failure callback function, whose type is `unitt_failcb_t`. When a test case fails, the function pointed to by this pointer will be called to handle the operations related to the failure, such as recording the failure log, outputting error prompts, etc. Developers can customize functions that meet the requirements of this type to implement personalized failure handling logic. - `specific`: The specific test name, which is a string constant. If it is not `NULL`, it means only the specific test case with a matching name will be executed, achieving targeted test execution. If it is `NULL`, all the test cases in all the test suites will be executed in turn. - `filename`: The file name of the output result. If it is `NULL`, the test result will be output to the standard output (usually the console). If a specific file name is specified, the test result will be written into the corresponding file in a certain format, facilitating the subsequent viewing and analysis of the test situation.