This is a single header file type-safe C assertion and testing library.
Basic assertion that checks if expression is true.
ASSERT(x > 0); // Fails if x <= 0Prints fatal error message and terminates program.
FATAL("Critical system failure");Boolean value assertions.
ASSERT_TRUE(is_valid);
ASSERT_FALSE(has_error);Pointer null checks.
ASSERT_NOT_NULL(buffer);
ASSERT_NULL(freed_ptr);Basic integer comparisons using int64_t casting:
ASSERT_EQ(a, b)- Equal (==)ASSERT_NE(a, b)- Not equal (!=)ASSERT_LT(a, b)- Less than (<)ASSERT_LE(a, b)- Less or equal (<=)ASSERT_GT(a, b)- Greater than (>)ASSERT_GE(a, b)- Greater or equal (>=)
ASSERT_EQ(result, 42);
ASSERT_LT(count, MAX_SIZE);Type-checked versions that prevent implicit conversions:
ASSERT_EQ_INT8(a, b),ASSERT_NE_INT8(a, b)ASSERT_LT_INT8(a, b),ASSERT_LE_INT8(a, b)ASSERT_GT_INT8(a, b),ASSERT_GE_INT8(a, b)
ASSERT_EQ_UINT8(a, b),ASSERT_NE_UINT8(a, b)ASSERT_LT_UINT8(a, b),ASSERT_LE_UINT8(a, b)ASSERT_GT_UINT8(a, b),ASSERT_GE_UINT8(a, b)
Same pattern as above for all integer types.
ASSERT_EQ_SIZE(a, b),ASSERT_NE_SIZE(a, b)ASSERT_LT_SIZE(a, b),ASSERT_LE_SIZE(a, b)ASSERT_GT_SIZE(a, b),ASSERT_GE_SIZE(a, b)
ASSERT_EQ_CHAR(a, b),ASSERT_NE_CHAR(a, b)ASSERT_EQ_UCHAR(a, b),ASSERT_NE_UCHAR(a, b)
int8_t a = 10, b = 20;
ASSERT_LT_INT8(a, b); // OK
uint32_t x = 100;
int32_t y = 100;
ASSERT_EQ_UINT32(x, y); // COMPILE ERROR: Type mismatch!Epsilon-based floating point comparisons for precise equality checking:
ASSERT_FLOAT_EQ(a, b, epsilon)- Float equality with toleranceASSERT_FLOAT_NE(a, b, epsilon)- Float inequality with tolerance
ASSERT_DOUBLE_EQ(a, b, epsilon)- Double equality with toleranceASSERT_DOUBLE_NE(a, b, epsilon)- Double inequality with tolerance
float a = 0.1f + 0.2f;
float b = 0.3f;
ASSERT_FLOAT_EQ(a, b, 1e-6f);
double precise = calculate_pi();
ASSERT_DOUBLE_EQ(precise, 3.14159265, 1e-9);For simple magnitude comparisons without precision concerns, use regular integer assertions:
ASSERT_GT(temperature, 0.0);ASSERT_STR_EQ(a, b)- Strings equalASSERT_STR_NE(a, b)- Strings not equalASSERT_STR_EQ_LEN(a, b, len)- First len chars equalASSERT_STR_NE_LEN(a, b, len)- First len chars not equal
ASSERT_STR_EQ(name, "John");
ASSERT_STR_EQ_LEN(prefix, "test_", 5);ASSERT_MEM_EQ(a, b, len)- Memory blocks equalASSERT_MEM_NE(a, b, len)- Memory blocks not equal
ASSERT_MEM_EQ(buffer1, buffer2, sizeof(buffer1));Checks if return code is 0 (success).
ASSERT_OK(function_call()); // Fails if non-zero returnedenum TestStatus {
TEST_OK = 0,
TEST_SKIP = 1
};RETURN_OK()- Mark test as passedRETURN_SKIP(explanation)- Skip test with reasonRUN_TEST(test_func)- Execute and report test result
int test_example() {
ASSERT_EQ(2 + 2, 4);
RETURN_OK();
}
int main() {
RUN_TEST(test_example);
return 0;
}#include "myassert.h"
int test_function() {
uint32_t size = 100;
ASSERT_GT_UINT32(size, 0);
char* buffer = malloc(size);
ASSERT_NOT_NULL(buffer);
ASSERT_STR_EQ(get_name(), "expected");
free(buffer);
RETURN_OK();
}Lisences under MIT.