#include "unittest.h"
static int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
UnittestTestResult_t* test_fibonacci_base_cases(void) {
UNITTEST_BEGIN_TEST("TestFibonacci", "test_base_cases");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 0, fibonacci(0), "fib(0) should be 0");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 1, fibonacci(1), "fib(1) should be 1");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_fibonacci_sequence(void) {
UNITTEST_BEGIN_TEST("TestFibonacci", "test_sequence");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 1, fibonacci(2), "fib(2) should be 1");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 2, fibonacci(3), "fib(3) should be 2");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 5, fibonacci(5), "fib(5) should be 5");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 55, fibonacci(10), "fib(10) should be 55");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_gcd_basic(void) {
UNITTEST_BEGIN_TEST("TestGCD", "test_basic");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 6, gcd(12, 18), "gcd(12,18) should be 6");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 1, gcd(7, 13), "gcd(7,13) should be 1");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 5, gcd(5, 0), "gcd(5,0) should be 5");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_string_operations(void) {
UNITTEST_BEGIN_TEST("TestStrings", "test_operations");
UNITTEST_ASSERT_EQUAL_STR(_unittest_result, "hello", "hello", "strings should match");
UNITTEST_ASSERT_NOT_EQUAL_STR(_unittest_result, "hello", "world", "strings should differ");
UNITTEST_ASSERT_STRING_CONTAINS(_unittest_result, "ell", "hello world", "should contain substring");
UNITTEST_ASSERT_STRING_STARTS_WITH(_unittest_result, "hello", "hello world", "should start with prefix");
UNITTEST_ASSERT_STRING_ENDS_WITH(_unittest_result, "world", "hello world", "should end with suffix");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_boolean_operations(void) {
UNITTEST_BEGIN_TEST("TestBooleans", "test_operations");
UNITTEST_ASSERT_TRUE(_unittest_result, 1 == 1, "1 should equal 1");
UNITTEST_ASSERT_FALSE(_unittest_result, 1 == 2, "1 should not equal 2");
UNITTEST_ASSERT_TRUE(_unittest_result, 5 > 3, "5 should be greater than 3");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_pointer_operations(void) {
UNITTEST_BEGIN_TEST("TestPointers", "test_operations");
int x = 42;
int *ptr = &x;
int *null_ptr = NULL;
UNITTEST_ASSERT_NOT_NULL(_unittest_result, ptr, "ptr should not be null");
UNITTEST_ASSERT_NULL(_unittest_result, null_ptr, "null_ptr should be null");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_comparison_operations(void) {
UNITTEST_BEGIN_TEST("TestComparisons", "test_operations");
UNITTEST_ASSERT_GREATER(_unittest_result, 10, 5, "10 should be greater than 5");
UNITTEST_ASSERT_LESS(_unittest_result, 3, 7, "3 should be less than 7");
UNITTEST_ASSERT_GREATER_EQUAL(_unittest_result, 5, 5, "5 should be >= 5");
UNITTEST_ASSERT_LESS_EQUAL(_unittest_result, 4, 4, "4 should be <= 4");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_double_operations(void) {
UNITTEST_BEGIN_TEST("TestDoubles", "test_operations");
UNITTEST_ASSERT_EQUAL_DOUBLE(_unittest_result, 3.14159, 3.14159, 0.00001, "pi should match");
UNITTEST_ASSERT_EQUAL_DOUBLE(_unittest_result, 1.0/3.0, 0.333333, 0.0001, "1/3 should be ~0.333");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_array_operations(void) {
UNITTEST_BEGIN_TEST("TestArrays", "test_operations");
int arr1[] = {1, 2, 3, 4, 5};
int arr2[] = {1, 2, 3, 4, 5};
UNITTEST_ASSERT_ARRAY_INT_EQUAL(_unittest_result, arr1, arr2, 5, "arrays should be equal");
UNITTEST_ASSERT_MEMORY_EQUAL(_unittest_result, arr1, arr2, sizeof(arr1), "memory should match");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_intentional_failure(void) {
UNITTEST_BEGIN_TEST("TestFailures", "test_intentional_failure");
UNITTEST_ASSERT_EQUAL_INT(_unittest_result, 42, 41, "this should fail: 42 != 41");
UNITTEST_END_TEST();
}
UnittestTestResult_t* test_skip_example(void) {
UNITTEST_BEGIN_TEST("TestSkipped", "test_skip_example");
UNITTEST_SKIP("Feature not yet implemented");
}
int main(int argc, char **argv) {
UnittestConfig_t *config = unittest_config_create();
config->verbosity = 2;
config->track_execution_time = true;
if (argc > 1) {
if (strcmp(argv[1], "--json") == 0) {
config->output_format = UNITTEST_FORMAT_JSON;
config->use_colors = false;
} else if (strcmp(argv[1], "--xml") == 0) {
config->output_format = UNITTEST_FORMAT_XML;
config->use_colors = false;
} else if (strcmp(argv[1], "--html") == 0) {
config->output_format = UNITTEST_FORMAT_HTML;
config->use_colors = false;
} else if (strcmp(argv[1], "--tap") == 0) {
config->output_format = UNITTEST_FORMAT_TAP;
config->use_colors = false;
} else if (strcmp(argv[1], "--quiet") == 0) {
config->output_format = UNITTEST_FORMAT_QUIET;
}
}
UnittestTestSuite_t *suite = unittest_test_suite_create("Unit Test Framework Demo");
UnittestTestCase_t *tc_fibonacci = unittest_test_case_create("TestFibonacci");
unittest_test_case_add_result(tc_fibonacci, test_fibonacci_base_cases());
unittest_test_case_add_result(tc_fibonacci, test_fibonacci_sequence());
unittest_test_suite_add_test_case(suite, tc_fibonacci);
UnittestTestCase_t *tc_gcd = unittest_test_case_create("TestGCD");
unittest_test_case_add_result(tc_gcd, test_gcd_basic());
unittest_test_suite_add_test_case(suite, tc_gcd);
UnittestTestCase_t *tc_strings = unittest_test_case_create("TestStrings");
unittest_test_case_add_result(tc_strings, test_string_operations());
unittest_test_suite_add_test_case(suite, tc_strings);
UnittestTestCase_t *tc_booleans = unittest_test_case_create("TestBooleans");
unittest_test_case_add_result(tc_booleans, test_boolean_operations());
unittest_test_suite_add_test_case(suite, tc_booleans);
UnittestTestCase_t *tc_pointers = unittest_test_case_create("TestPointers");
unittest_test_case_add_result(tc_pointers, test_pointer_operations());
unittest_test_suite_add_test_case(suite, tc_pointers);
UnittestTestCase_t *tc_comparisons = unittest_test_case_create("TestComparisons");
unittest_test_case_add_result(tc_comparisons, test_comparison_operations());
unittest_test_suite_add_test_case(suite, tc_comparisons);
UnittestTestCase_t *tc_doubles = unittest_test_case_create("TestDoubles");
unittest_test_case_add_result(tc_doubles, test_double_operations());
unittest_test_suite_add_test_case(suite, tc_doubles);
UnittestTestCase_t *tc_arrays = unittest_test_case_create("TestArrays");
unittest_test_case_add_result(tc_arrays, test_array_operations());
unittest_test_suite_add_test_case(suite, tc_arrays);
UnittestTestCase_t *tc_failures = unittest_test_case_create("TestFailures");
unittest_test_case_add_result(tc_failures, test_intentional_failure());
unittest_test_suite_add_test_case(suite, tc_failures);
UnittestTestCase_t *tc_skipped = unittest_test_case_create("TestSkipped");
unittest_test_case_add_result(tc_skipped, test_skip_example());
unittest_test_suite_add_test_case(suite, tc_skipped);
unittest_generate_report(suite, config);
int failures = suite->total_failed + suite->total_errors;
unittest_test_suite_destroy(suite);
unittest_config_destroy(config);
return failures > 0 ? 1 : 0;
}