From a0ebfd19e2ae9e9e5cb6968ce0b5d2c976fb245c Mon Sep 17 00:00:00 2001 From: ENGO150 Date: Sat, 23 Jul 2022 15:37:54 +0200 Subject: [PATCH] made why2-test failure much better for debugging & made it more variable I hope I am deallocating all new things :D --- src/lib/test/main.c | 51 +++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/lib/test/main.c b/src/lib/test/main.c index f3465ad..bb35562 100644 --- a/src/lib/test/main.c +++ b/src/lib/test/main.c @@ -9,8 +9,11 @@ int main(void) //VARIABLES char *textBuffer = malloc(128); char *keyBuffer; + char *statusBuffer; + char *outputBuffer = malloc(1); //THIS IS TEMP ALLOCATION int exitCode = 0; unsigned long timeBuffer; + FILE *outputStreamBuffer = stdout; //FLAGS inputFlags flags = @@ -30,6 +33,9 @@ int main(void) //SET ENCRYPTION_SEPARATOR TO '|' setEncryptionSeparator('|'); + //SET outputBuffer to NULL + outputBuffer[0] = '\0'; + //ENCRYPT outputFlags encrypted = encryptText(TEST_TEXT, NULL); @@ -46,31 +52,44 @@ int main(void) timeBuffer += encrypted.elapsedTime; //GET TIME 1 //COMPARE DIFFERENCE - if (strcmp(encrypted.outputText, TEST_TEXT) == 0 && encrypted.exitCode == 0) + if (strcmp(encrypted.outputText, TEST_TEXT) == 0 && encrypted.exitCode == 0) //SUCCESS { - printf - ( - "Test successful!\n\n" - - "TEXT: \t\t\"%s\"\n" - "OUTPUT: \t\"%s\"\n" - "KEY: \t\t\"%s\"\n" - "TIME: \t\t\"%lums\"\n" - "UNUSED KEY: \t\"%lu\"\n" - "EXIT CODE: \t\"%d\"\n" - - , TEST_TEXT, textBuffer, encrypted.usedKey, timeBuffer / 1000, encrypted.unusedKeySize, encrypted.exitCode - ); + statusBuffer = malloc(11); + strcpy(statusBuffer, "successful"); } - else + else //FAILURE { - fprintf(stderr, "Test failed! (%u)\n\n%s // %s\n", encrypted.exitCode, encrypted.outputText, TEST_TEXT); + statusBuffer = malloc(8); + strcpy(statusBuffer, "failed"); + + outputBuffer = realloc(outputBuffer, strlen(encrypted.outputText) + 6); + sprintf(outputBuffer, "\t\t\"%s\"\n", encrypted.outputText); + + outputStreamBuffer = stderr; exitCode = 1; } + //PRINT OUTPUT + fprintf + ( + outputStreamBuffer, + + "Test %s!\n\n" + + "TEXT: \t\t\"%s\"\n%s" + "OUTPUT: \t\"%s\"\n" + "KEY: \t\t\"%s\"\n" + "TIME: \t\t\"%lums\"\n" + "UNUSED KEY: \t\"%lu\"\n" + "EXIT CODE: \t\"%d\"\n" + + , statusBuffer, TEST_TEXT, outputBuffer, textBuffer, encrypted.usedKey, timeBuffer / 1000, encrypted.unusedKeySize, encrypted.exitCode + ); + //DEALLOCATION free(textBuffer); free(keyBuffer); + free(statusBuffer); deallocateOutput(encrypted); return exitCode;