diff --git a/build/test/cache/test_mainMenu.c b/build/test/cache/test_mainMenu.c new file mode 100644 index 0000000..c8bdf9f --- /dev/null +++ b/build/test/cache/test_mainMenu.c @@ -0,0 +1,299 @@ +#include "src/mainMenu.h" +#include "/var/lib/gems/3.0.0/gems/ceedling-0.31.1/vendor/unity/src/unity.h" + + + + + + + + +void setUp(void) + +{ + +} + + + +void tearDown(void) + +{ + +} + + + +void test_agePermissionValidAge(void) + +{ + + + + + + + + + + int validAge[83]; + + + + + + _Bool + + validAgeResult[83]; + + + + int j=0; + + + + for(int i =18;i<101;i++){ + + + + validAge[j]= i; + + j++; + + } + + + + + + + + for(int i=0;i<83;i++){ + + + + validAgeResult[i] = agePermission(validAge[i]); + + } + + + + + + + + for(int i=0;i<83;i++){ + + + + do {if ((validAgeResult[i])) {} else {UnityFail( ((" Expected TRUE Was FALSE")), (UNITY_UINT)((UNITY_UINT)(44)));}} while(0); + + + + } + + + +} + + + +void test_agePermissionInvalidAge(void) + +{ + + + + + + + + + + + + int invalidAge[117]; + + + + + + _Bool + + invalidAgeResult[117]; + + + + int j=0; + + + + for(int i =-100;i<18;i++){ + + + + invalidAge[j]= i; + + j++; + + } + + + + + + + + for(int i=0;i<117;i++){ + + + + invalidAgeResult[i] = agePermission(invalidAge[i]); + + + + } + + + + + + + + for(int i=0;i<117;i++){ + + + + do {if (!(invalidAgeResult[i])) {} else {UnityFail( ((" Expected FALSE Was TRUE")), (UNITY_UINT)((UNITY_UINT)(81)));}} while(0); + + + + } + + + +} + + + +void test_IsInteger(void) + +{ + + + + + + + + char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; + + + + + + _Bool + + inputIsIntegerResult[19]; + + + + + + + + for(int i=0;i<19;i++) + + { + + + + inputIsIntegerResult[i] = checkIfInteger(inputIsInteger[i]); + + + + } + + + + + + + + for(int i=0;i<19;i++) + + { + + + + do {if ((inputIsIntegerResult[i])) {} else {UnityFail( ((" Expected TRUE Was FALSE")), (UNITY_UINT)((UNITY_UINT)(110)));}} while(0); + + + + } + + + +} + + + +void test_IsNotInteger(void) + +{ + + + + + + + + char* inputIsNotInteger[] = {"0.15","3.141592653589793238","5.3254f","-6.264","-7878.3261","foo","Bar","FIZZ","buzZ","joHN","jAnE","foo-bar","3,15","2k13",""," ","-","+","/*-+.,/=" }; + + + + + + _Bool + + inputIsNotIntegerResult[19]; + + + + + + + + for(int i=0;i<19;i++) + + { + + + + inputIsNotIntegerResult[i] = checkIfInteger(inputIsNotInteger[i]); + + + + } + + + + + + + + for(int i=0;i<19;i++) + + { + + + + do {if (!(inputIsNotIntegerResult[i])) {} else {UnityFail( ((" Expected FALSE Was TRUE")), (UNITY_UINT)((UNITY_UINT)(139)));}} while(0); + + + + } + + + +} diff --git a/build/test/dependencies/force_build b/build/test/dependencies/force_build new file mode 100644 index 0000000..e69de29 diff --git a/build/test/dependencies/mainMenu.d b/build/test/dependencies/mainMenu.d new file mode 100644 index 0000000..fbd8a70 --- /dev/null +++ b/build/test/dependencies/mainMenu.d @@ -0,0 +1 @@ +build/test/out/c/mainMenu.o: src/mainMenu.c src/mainMenu.h diff --git a/build/test/dependencies/test_mainMenu.d b/build/test/dependencies/test_mainMenu.d new file mode 100644 index 0000000..5ee8500 --- /dev/null +++ b/build/test/dependencies/test_mainMenu.d @@ -0,0 +1,4 @@ +build/test/out/c/test_mainMenu.o: tests/test_mainMenu.c \ + /var/lib/gems/3.0.0/gems/ceedling-0.31.1/vendor/unity/src/unity.h \ + /var/lib/gems/3.0.0/gems/ceedling-0.31.1/vendor/unity/src/unity_internals.h \ + src/mainMenu.h diff --git a/build/test/dependencies/test_mainMenu_runner.d b/build/test/dependencies/test_mainMenu_runner.d new file mode 100644 index 0000000..0ac0cdd --- /dev/null +++ b/build/test/dependencies/test_mainMenu_runner.d @@ -0,0 +1,4 @@ +build/test/out/c/test_mainMenu_runner.o: \ + build/test/runners/test_mainMenu_runner.c \ + /var/lib/gems/3.0.0/gems/ceedling-0.31.1/vendor/unity/src/unity.h \ + /var/lib/gems/3.0.0/gems/ceedling-0.31.1/vendor/unity/src/unity_internals.h diff --git a/build/test/out/c/mainMenu.o b/build/test/out/c/mainMenu.o new file mode 100644 index 0000000..5666f15 Binary files /dev/null and b/build/test/out/c/mainMenu.o differ diff --git a/build/test/out/c/test_mainMenu.o b/build/test/out/c/test_mainMenu.o new file mode 100644 index 0000000..c7879b1 Binary files /dev/null and b/build/test/out/c/test_mainMenu.o differ diff --git a/build/test/out/c/test_mainMenu_runner.o b/build/test/out/c/test_mainMenu_runner.o new file mode 100644 index 0000000..1666f41 Binary files /dev/null and b/build/test/out/c/test_mainMenu_runner.o differ diff --git a/build/test/out/test_mainMenu.out b/build/test/out/test_mainMenu.out new file mode 100755 index 0000000..b4dffd7 Binary files /dev/null and b/build/test/out/test_mainMenu.out differ diff --git a/build/test/preprocess/files/test_mainMenu.c b/build/test/preprocess/files/test_mainMenu.c new file mode 100644 index 0000000..c8bdf9f --- /dev/null +++ b/build/test/preprocess/files/test_mainMenu.c @@ -0,0 +1,299 @@ +#include "src/mainMenu.h" +#include "/var/lib/gems/3.0.0/gems/ceedling-0.31.1/vendor/unity/src/unity.h" + + + + + + + + +void setUp(void) + +{ + +} + + + +void tearDown(void) + +{ + +} + + + +void test_agePermissionValidAge(void) + +{ + + + + + + + + + + int validAge[83]; + + + + + + _Bool + + validAgeResult[83]; + + + + int j=0; + + + + for(int i =18;i<101;i++){ + + + + validAge[j]= i; + + j++; + + } + + + + + + + + for(int i=0;i<83;i++){ + + + + validAgeResult[i] = agePermission(validAge[i]); + + } + + + + + + + + for(int i=0;i<83;i++){ + + + + do {if ((validAgeResult[i])) {} else {UnityFail( ((" Expected TRUE Was FALSE")), (UNITY_UINT)((UNITY_UINT)(44)));}} while(0); + + + + } + + + +} + + + +void test_agePermissionInvalidAge(void) + +{ + + + + + + + + + + + + int invalidAge[117]; + + + + + + _Bool + + invalidAgeResult[117]; + + + + int j=0; + + + + for(int i =-100;i<18;i++){ + + + + invalidAge[j]= i; + + j++; + + } + + + + + + + + for(int i=0;i<117;i++){ + + + + invalidAgeResult[i] = agePermission(invalidAge[i]); + + + + } + + + + + + + + for(int i=0;i<117;i++){ + + + + do {if (!(invalidAgeResult[i])) {} else {UnityFail( ((" Expected FALSE Was TRUE")), (UNITY_UINT)((UNITY_UINT)(81)));}} while(0); + + + + } + + + +} + + + +void test_IsInteger(void) + +{ + + + + + + + + char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; + + + + + + _Bool + + inputIsIntegerResult[19]; + + + + + + + + for(int i=0;i<19;i++) + + { + + + + inputIsIntegerResult[i] = checkIfInteger(inputIsInteger[i]); + + + + } + + + + + + + + for(int i=0;i<19;i++) + + { + + + + do {if ((inputIsIntegerResult[i])) {} else {UnityFail( ((" Expected TRUE Was FALSE")), (UNITY_UINT)((UNITY_UINT)(110)));}} while(0); + + + + } + + + +} + + + +void test_IsNotInteger(void) + +{ + + + + + + + + char* inputIsNotInteger[] = {"0.15","3.141592653589793238","5.3254f","-6.264","-7878.3261","foo","Bar","FIZZ","buzZ","joHN","jAnE","foo-bar","3,15","2k13",""," ","-","+","/*-+.,/=" }; + + + + + + _Bool + + inputIsNotIntegerResult[19]; + + + + + + + + for(int i=0;i<19;i++) + + { + + + + inputIsNotIntegerResult[i] = checkIfInteger(inputIsNotInteger[i]); + + + + } + + + + + + + + for(int i=0;i<19;i++) + + { + + + + do {if (!(inputIsNotIntegerResult[i])) {} else {UnityFail( ((" Expected FALSE Was TRUE")), (UNITY_UINT)((UNITY_UINT)(139)));}} while(0); + + + + } + + + +} diff --git a/build/test/preprocess/includes/test_mainMenu.c b/build/test/preprocess/includes/test_mainMenu.c new file mode 100644 index 0000000..fb4dc93 --- /dev/null +++ b/build/test/preprocess/includes/test_mainMenu.c @@ -0,0 +1,3 @@ +--- +- "/var/lib/gems/3.0.0/gems/ceedling-0.31.1/vendor/unity/src/unity.h" +- src/mainMenu.h diff --git a/build/test/results/test_mainMenu.pass b/build/test/results/test_mainMenu.pass new file mode 100644 index 0000000..d5c35cb --- /dev/null +++ b/build/test/results/test_mainMenu.pass @@ -0,0 +1,30 @@ +--- +:source: + :path: tests + :file: test_mainMenu.c +:successes: +- :test: test_agePermissionValidAge + :line: 15 + :message: '' + :unity_test_time: 0 +- :test: test_agePermissionInvalidAge + :line: 50 + :message: '' + :unity_test_time: 0 +- :test: test_IsInteger + :line: 87 + :message: '' + :unity_test_time: 0 +- :test: test_IsNotInteger + :line: 116 + :message: '' + :unity_test_time: 0 +:failures: [] +:ignores: [] +:counts: + :total: 4 + :passed: 4 + :failed: 0 + :ignored: 0 +:stdout: [] +:time: 0.0009556771256029606 diff --git a/build/test/runners/test_updateCustomerAccountBalance_runner.c b/build/test/runners/test_mainMenu_runner.c similarity index 100% rename from build/test/runners/test_updateCustomerAccountBalance_runner.c rename to build/test/runners/test_mainMenu_runner.c diff --git a/src/updateCustomerAccountBalance.c b/src/updateCustomerAccountBalance.c index 2ad9ffb..2040a6d 100644 --- a/src/updateCustomerAccountBalance.c +++ b/src/updateCustomerAccountBalance.c @@ -2,48 +2,25 @@ #include "currentCustomerAccountBalance.c" #include "lineReplacer.h" -void troubleShoot(int errorCode){ - printf("Error! The requested operation was terminated because of an issue. Here are some details about the error:\n---------------\n"); - - switch(errorCode){ - case 0: - printf("Requested file could not be opened. Are you sure it exists?"); - break; - case 1: - printf("A temporary file could not be generated. Are you sure the bank management system has the required authorization to create new files?"); - break; - case 2: - printf("Replacement of the old file failed. Are you sure the bank management system has the required authorization to delete files?"); - break; - case 3: - printf("Renaming of a file failed. Are you sure the bank management system has the required authorization to rename files?"); - break; - case 4: - printf("Could not find the customer. Please contact customer support."); - break; - } -} - void replaceLineInFile(const char* file_name, int line, const char* new_line){ FILE* file = fopen(file_name, "r"); if (file == NULL) { - troubleShoot(0); + printf("Error opening file!\n"); return; } - char current_string[100]; + char current_string[1024]; int current_line = 1; - char *temp_file_name = calloc(8+1, sizeof(char)); - temp_file_name = "src/temp.txt"; + char *temp_file_name = "temp.txt"; FILE* temp_file = fopen(temp_file_name, "w"); if (temp_file == NULL) { - troubleShoot(1); + printf("Error creating temp file!\n"); fclose(file); return; } - while (fgets(current_string, sizeof(current_string),file) != NULL) { + while (fgets(current_string, sizeof(current_string), file) != NULL) { if (current_line == line) { - fprintf(temp_file, "%s\n", new_line); - //fputs("\n", temp_file); + fprintf(temp_file, "%s", new_line); + fputs("\n", temp_file); } else { fprintf(temp_file, "%s", current_string); } @@ -52,38 +29,36 @@ void replaceLineInFile(const char* file_name, int line, const char* new_line){ fclose(file); fclose(temp_file); if(remove(file_name)!=0){ - troubleShoot(2); + printf("could not remove the original file!"); } // Remove the original file if(rename(temp_file_name, file_name)!=0){ - troubleShoot(3); + printf("could not rename!"); } // Rename the temp file to the original file } void replaceBalanceInString(float replacementBalance, int currentLine) { char newBalanceLine[MAX_LENGTH] = "balance="; char balance_as_string[MAX_LENGTH]; - sprintf(balance_as_string, "%f", replacementBalance); //converts replacement balance to string + sprintf(balance_as_string, "%g", replacementBalance); //converts replacement balance to string strcat(newBalanceLine, balance_as_string); - replaceLineInFile(CUSTOMER_DATA_FILE,currentLine,newBalanceLine); + replaceLineInFile("CustomerData.txt",currentLine,newBalanceLine); } -float updateAvailableAccountBalance(int user_id, float newBalance){ +bool updateAvailableAccountBalance(int user_id, float newBalance){ bool keep_reading = true; - bool foundCustomer = false; - char *buffer = malloc(MAX_LENGTH * sizeof(char)); - char *stringID = malloc(MAX_LENGTH * sizeof(char)); - stringID = "ID="; - char *user_id_as_string =malloc(MAX_LENGTH * sizeof(char)); - char *balance_String = malloc(MAX_LENGTH * sizeof(char)); + char buffer[MAX_LENGTH]; + char stringID[MAX_LENGTH] = "ID="; + char user_id_as_string[MAX_LENGTH]; + char balance_String[MAX_LENGTH]; int currentLine=0; sprintf(user_id_as_string, "%d", user_id); // converts user_id to string strcat(stringID, user_id_as_string); - FILE *file = fopen(CUSTOMER_DATA_FILE, "r"); - //printf(stringID); + FILE *file = fopen("CustomerData.txt", "r+"); + printf(stringID); while(keep_reading) { fgets(buffer, MAX_LENGTH, file); currentLine++; @@ -91,25 +66,16 @@ float updateAvailableAccountBalance(int user_id, float newBalance){ keep_reading = false; } else if(strstr(buffer, stringID)) { //found the customer - for (int i = 0; i < 4; i++) { - fgets(buffer, MAX_LENGTH, file); - currentLine++; - } + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); + fgets(buffer, MAX_LENGTH, file); strcpy(balance_String, buffer); + currentLine+=4; keep_reading = false; - foundCustomer=true; } } - fclose(file); - - if(foundCustomer){ - replaceBalanceInString(newBalance,currentLine); - return newBalance; - }else{ - troubleShoot(4); - return -1; - } - - + fclose(file);; + replaceBalanceInString(newBalance,currentLine); } diff --git a/tests/test_LoginCustomer.c b/tests/test_LoginCustomer.c new file mode 100644 index 0000000..610a847 --- /dev/null +++ b/tests/test_LoginCustomer.c @@ -0,0 +1,21 @@ +#include +#include "LoginCustomer.h" +void setUp(){}; +void tearDown(){}; +void test_checkLogin() +{ + /*arrange*/ + bool expected_test_values_compute_to_true[] = {4==4,true==true, 1==1, false==false, 'z'=='z', '='=='=',0x1A==0x1A}; + int length_1 = sizeof(expected_test_values_compute_to_true)/sizeof(bool); + + bool expected_test_values_compute_to_false[] = {4!=4,true==false,1==0,false==true,'z'=='x','!'==')',0x1A==0x2B}; + int length_2 = sizeof(expected_test_values_compute_to_false)/sizeof(bool); + + /*act and assertions*/ + for(int i=0;i<7;++i) { + TEST_ASSERT_TRUE(checkLogin(expected_test_values_compute_to_true[i])); + } + for(int i=0;i<7;++i){ + TEST_ASSERT_FALSE(checkLogin(expected_test_values_compute_to_false[i])); + } +} diff --git a/tests/test_mainMenu.c b/tests/test_mainMenu.c new file mode 100644 index 0000000..be1fb40 --- /dev/null +++ b/tests/test_mainMenu.c @@ -0,0 +1,145 @@ +#ifdef TEST + +#include "unity.h" + +#include "mainMenu.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_agePermissionValidAge(void) +{ + //Test case : 0 + + //Arrange + + int validAge[83]; + + bool validAgeResult[83]; + + int j=0; + + for(int i =18;i<101;i++){ + + validAge[j]= i; + j++; + } + + //Act + + for(int i=0;i<83;i++){ + + validAgeResult[i] = agePermission(validAge[i]); + } + + //Assert + + for(int i=0;i<83;i++){ + + TEST_ASSERT_TRUE(validAgeResult[i]); + + } + +} + +void test_agePermissionInvalidAge(void) +{ + + //Test case : 1 + + //Arrange + + int invalidAge[117]; + + bool invalidAgeResult[117]; + + int j=0; + + for(int i =-100;i<18;i++){ + + invalidAge[j]= i; + j++; + } + + //Act + + for(int i=0;i<117;i++){ + + invalidAgeResult[i] = agePermission(invalidAge[i]); + + } + + //Assert + + for(int i=0;i<117;i++){ + + TEST_ASSERT_FALSE(invalidAgeResult[i]); + + } + +} + +void test_IsInteger(void) +{ + + //Arrange + + char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; + + bool inputIsIntegerResult[19]; + + //Act + + for(int i=0;i<19;i++) + { + + inputIsIntegerResult[i] = checkIfInteger(inputIsInteger[i]); + + } + + //Assert + + for(int i=0;i<19;i++) + { + + TEST_ASSERT_TRUE(inputIsIntegerResult[i]); + + } + +} + +void test_IsNotInteger(void) +{ + + //Arrange + + char* inputIsNotInteger[] = {"0.15","3.141592653589793238","5.3254f","-6.264","-7878.3261","foo","Bar","FIZZ","buzZ","joHN","jAnE","foo-bar","3,15","2k13",""," ","-","+","/*-+.,/=" }; + + bool inputIsNotIntegerResult[19]; + + //Act + + for(int i=0;i<19;i++) + { + + inputIsNotIntegerResult[i] = checkIfInteger(inputIsNotInteger[i]); + + } + + //Assert + + for(int i=0;i<19;i++) + { + + TEST_ASSERT_FALSE(inputIsNotIntegerResult[i]); + + } + +} + +#endif // TEST