From 57292f4b64a1c9e58ce932337fcb4701dd68dedc Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Wed, 8 Feb 2023 02:12:04 +0100 Subject: [PATCH] implement unit tests for the function validPhoneNumber(). --- src/createEmployeeAccount.c | 19 +++++++++++++++ src/createEmployeeAccount.h | 1 + src/mainMenu.c | 34 +++++++++++++------------- tests/test_createEmployeeAccount.c | 38 ++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 17 deletions(-) diff --git a/src/createEmployeeAccount.c b/src/createEmployeeAccount.c index 3fb19e5..f8668bd 100644 --- a/src/createEmployeeAccount.c +++ b/src/createEmployeeAccount.c @@ -67,6 +67,25 @@ bool isValidName(char* name,const int minimalLength) return true; } +bool isValidPhoneNumber(char *phoneNumber) +{ + const int validNumberLength = 14; + int numberLength = strlen(phoneNumber); + + if(numberLength != validNumberLength) + { + return false; + } + + if(phoneNumber[0]!='+' || phoneNumber[1]!='4' || phoneNumber[2]!='9') + { + return false; + } + + return true; + +} + int StringLengthCounter(char* string) { int characterCounter = 0; diff --git a/src/createEmployeeAccount.h b/src/createEmployeeAccount.h index 33b5f97..03cd582 100644 --- a/src/createEmployeeAccount.h +++ b/src/createEmployeeAccount.h @@ -26,6 +26,7 @@ bool createNewEmployee(char* employeeId, char* employeePassword); bool verifyPassword(char* enteredPassword,char* passwordConfirmation); bool storeEmployeeData(const char *name,const char *lastName,const char *adress,const char *phoneNumber); bool isValidName(char* name,int minimalLength); +bool isValidPhoneNumber(char *phoneNumber); int StringLengthCounter(char* string); diff --git a/src/mainMenu.c b/src/mainMenu.c index fdc2f21..8fcb882 100644 --- a/src/mainMenu.c +++ b/src/mainMenu.c @@ -45,42 +45,42 @@ void ageInput() while (true) { - /*the userInput string is changed to a number with the strtol function*/ + /*the userInput string is changed to a number with the strtol function*/ - age = strtol(userInput,&userInputPointer,10); + age = strtol(userInput,&userInputPointer,10); if((checkIfInteger(userInput))&& (agePermission(age))) - { + { - printf("Access granted!\n\n\n\n"); + printf("Access granted!\n\n\n\n"); - showMenu(); + showMenu(); - menuInput(); + menuInput(); - break; + break; - } + } - else if((checkIfInteger(userInput)) && !(agePermission(age))) - { + else if((checkIfInteger(userInput)) && !(agePermission(age))) + { printf("You should be at least 18 years old to create a bank account!\n"); - break; + break; - } + } - else - { - printf("input invalid! try again!\n"); + else + { + printf("input invalid! try again!\n"); scanf("%s",userInput); - } + } -} + } } void menuInput() diff --git a/tests/test_createEmployeeAccount.c b/tests/test_createEmployeeAccount.c index 4b66ccf..d1c369e 100644 --- a/tests/test_createEmployeeAccount.c +++ b/tests/test_createEmployeeAccount.c @@ -226,4 +226,42 @@ void test_invalidName(void) } +void test_validPhoneNumber(void) +{ + /*Arrange*/ + char* validPhoneNumbers[] = {"+4903584736198","+4912345678912","+4987541024534","+4932145784236","+4987264287139"}; + bool validPhoneNumbersResult[5]; + + /*Act*/ + for(int i =0;i<5;i++) + { + validPhoneNumbersResult[i] = isValidPhoneNumber(validPhoneNumbers[i]); + } + + /*Assert*/ + for(int i=0;i<5;i++) + { + TEST_ASSERT_TRUE(validPhoneNumbersResult[i]); + } +} + +void test_invalidPhoneNumber(void) +{ + /*Arrange*/ + char* invalidPhoneNumbers[] = {"+490358473619812","+6112345678912","+498754","-4932145784236","123"}; + bool invalidPhoneNumbersResult[2]; + + /*Act*/ + for(int i =0;i<5;i++) + { + invalidPhoneNumbersResult[i] = isValidPhoneNumber(invalidPhoneNumbers[i]); + } + + /*Assert*/ + for(int i=0;i<5;i++) + { + TEST_ASSERT_FALSE(invalidPhoneNumbersResult[i]); + } +} + #endif // TEST