From 5e72e1f6524cfcfe17b9aec48b43717bf98ff0e8 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Sun, 5 Feb 2023 05:51:16 +0100 Subject: [PATCH] optimised the isValidEmployeeID() function and its unit tests --- src/createEmployeeAccount.c | 31 ++++++++++++++++++++++-------- src/createEmployeeAccount.h | 4 +++- tests/test_createEmployeeAccount.c | 15 +++++++++++++-- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/createEmployeeAccount.c b/src/createEmployeeAccount.c index ac1a26a..7abfb02 100644 --- a/src/createEmployeeAccount.c +++ b/src/createEmployeeAccount.c @@ -3,19 +3,32 @@ #include "createEmployeeAccount.h" -bool isValidEmployeeID(char* employeeId) +int StringLengthCounter(char* string) { - int characterLimit = 20; - - if(strlen(employeeId)>characterLimit || strchr(employeeId,' ')) + int characterCounter = 0; + int i = 0; + while(string[i] !='\0') { - return false; + characterCounter++; + ++i; } + string[characterCounter] = '\0'; + return characterCounter; +} + - else +bool isValidEmployeeID(char* employeeId, int maximumLength) +{ + int employeeIdLength = strlen(employeeId); + int i; + for(i=0;i #include -bool isValidEmployeeID(char* employee); +bool isValidEmployeeID(char* employee, int maximumLength); bool createNewEmployee(char* employeeId, char* employeePassword); +int StringLengthCounter(char* string); + void getNewEmployeeCredentials(); #endif \ No newline at end of file diff --git a/tests/test_createEmployeeAccount.c b/tests/test_createEmployeeAccount.c index 7ce99aa..dbd971d 100644 --- a/tests/test_createEmployeeAccount.c +++ b/tests/test_createEmployeeAccount.c @@ -18,13 +18,19 @@ void test_isValidEmployeeID(void) /*Arrange*/ char* validEmployeeId [] = {"Atharva","Can","Haytham","Julius","Mohamed","Shivam","Fizz","Buzz","JohnDoe","Foobar","waz","Objectoriented","INSTITUTIONALISATIOL","Intercommunicational","1234","1.6"}; + int validStringLengths[15]; bool validEmployeeIdExpected = true; + for(int i =0;i<15;i++) + { + validStringLengths[i] = 20; + } + /*Act and Assert*/ for(int i=0; i<15; i++) { - bool validEmployeeIdResult = isValidEmployeeID(validEmployeeId[i]); + bool validEmployeeIdResult = isValidEmployeeID(validEmployeeId[i],validStringLengths[i]); TEST_ASSERT_EQUAL(validEmployeeIdExpected,validEmployeeIdResult); } } @@ -35,13 +41,18 @@ void test_isNotValidEmployeeID(void) /*Arrange*/ char* invalidEmployeeId [] = {"Atha rva","Ca n","Geschwindigkeitsbegrenzungen","1234 15","John Doe","fizz Fuzz"}; + int invalidStringLengths[6]; bool invalidEmployeeIdExpected = false; + for(int i =0;i<6;i++) + { + invalidStringLengths[i] = 20; + } /*Act and Assert*/ for(int i=0; i<6; i++) { - bool invalidEmployeeIdResult = isValidEmployeeID(invalidEmployeeId[i]); + bool invalidEmployeeIdResult = isValidEmployeeID(invalidEmployeeId[i],invalidStringLengths[i]); TEST_ASSERT_EQUAL(invalidEmployeeIdExpected,invalidEmployeeIdResult); }