From 8e555992261dca202a30e2ece6eded79f3648a24 Mon Sep 17 00:00:00 2001 From: Amber Date: Sun, 19 Nov 2023 14:27:09 -0500 Subject: [PATCH] Addding test for runCallback --- include/C_Callback.h | 4 +-- src/C_Callback.c | 4 +-- tests/test_C_Callback.cpp | 55 +++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/include/C_Callback.h b/include/C_Callback.h index 059a1b9..2ef6bf5 100644 --- a/include/C_Callback.h +++ b/include/C_Callback.h @@ -16,10 +16,10 @@ extern "C" { typedef const void (*CallbackFunction)(int, char*, size_t); // Simple function that prints a number to the console -DLL_EXPORT void printNumberInDec(int number, char* buffer, size_t bufferSize); +DLL_EXPORT CallbackFunction printNumberInDec(int number, char* buffer, size_t bufferSize); // Simple function that prints a number to the console in hex -DLL_EXPORT void printNumberInHex(int number, char* buffer, size_t bufferSize); +DLL_EXPORT CallbackFunction printNumberInHex(int number, char* buffer, size_t bufferSize); // Function that takes a function pointer as an argument and // set the callback function to the one passed in diff --git a/src/C_Callback.c b/src/C_Callback.c index 055adfd..436bc5a 100644 --- a/src/C_Callback.c +++ b/src/C_Callback.c @@ -7,13 +7,13 @@ static CallbackFunction callbackFunction = NULL; // Print a number to a string -void printNumberInDec(int number, char* buffer, size_t bufferSize) +CallbackFunction printNumberInDec(int number, char* buffer, size_t bufferSize) { snprintf(buffer, bufferSize, "Number: %d\n", number); } // Prints a number to a string in hex -void printNumberInHex(int number, char* buffer, size_t bufferSize) +CallbackFunction printNumberInHex(int number, char* buffer, size_t bufferSize) { snprintf(buffer, bufferSize, "Number: 0x%X\n", number); } diff --git a/tests/test_C_Callback.cpp b/tests/test_C_Callback.cpp index 655d2f8..7fe6e66 100644 --- a/tests/test_C_Callback.cpp +++ b/tests/test_C_Callback.cpp @@ -26,31 +26,30 @@ TEST(C_Callback, testPrintNumberInHex) } -// // Test setCallback and runCallback functions -// void testCallbackFunctions() -// { -// char buffer[100]; -// int number = 42; - -// // Test when callback function is not set -// setCallback(NULL); -// runCallback(number, buffer, sizeof(buffer)); -// printf("Callback Function (NULL): %s\n", buffer); -// // Add assertions to check the output - -// // Test when callback function is set -// // setCallback(printNumberInDec); -// runCallback(number, buffer, sizeof(buffer)); -// printf("Callback Function (printNumberInDec): %s\n", buffer); -// // Add assertions to check the output -// } - -// TEST(C_Callback, testPrintNumberInHex) -// { -// testPrintNumberInHex(); -// } - -// TEST(C_Callback, testCallbackFunctions) -// { -// testCallbackFunctions(); -// } \ No newline at end of file + // Test setCallback and runCallback functions + TEST(C_Callback, testCallbackFunctions) + { + char buffer[100]; + int number = 42; + + // Test when callback function is not set + setCallback(NULL); + runCallback(number, buffer, sizeof(buffer)); + printf("Callback Function (NULL): %s\n", buffer); + // Add assertions to check the output + ASSERT_STREQ(buffer, "No callback function set\n"); + + // Test when callback function is set + setCallback((CallbackFunction) printNumberInDec); + runCallback(number, buffer, sizeof(buffer)); + printf("Callback Function (printNumberInDec): %s\n", buffer); + // Add assertions to check the output + ASSERT_STREQ(buffer, "Number: 42\n"); + + // Test when callback function is set + setCallback((CallbackFunction)printNumberInHex); + runCallback(number, buffer, sizeof(buffer)); + printf("Callback Function (printNumberInDec): %s\n", buffer); + // Add assertions to check the output + ASSERT_STREQ(buffer, "Number: 0x2A\n"); + }