Skip to content

Commit

Permalink
Addding test for runCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
greenliquidlight committed Nov 19, 2023
1 parent b852216 commit 8e55599
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
4 changes: 2 additions & 2 deletions include/C_Callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/C_Callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
55 changes: 27 additions & 28 deletions tests/test_C_Callback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
// }
// 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");
}

0 comments on commit 8e55599

Please sign in to comment.