Browse Source

optimised the isValidEmployeeID() function and its unit tests

remotes/origin/feature/employees-infos-access
fdai7207 2 years ago
parent
commit
5e72e1f652
  1. 31
      src/createEmployeeAccount.c
  2. 4
      src/createEmployeeAccount.h
  3. 15
      tests/test_createEmployeeAccount.c

31
src/createEmployeeAccount.c

@ -3,19 +3,32 @@
#include "createEmployeeAccount.h" #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<employeeIdLength;i++)
{ {
return true;
if(employeeId[i]==' ')
{
return false;
}
} }
return employeeIdLength <= maximumLength ;
} }
bool createNewEmployee(char* employeeId, char* employeePassword) bool createNewEmployee(char* employeeId, char* employeePassword)
@ -32,11 +45,12 @@ bool createNewEmployee(char* employeeId, char* employeePassword)
fprintf(employeesFile,"\n%s %s\n",employeeId,employeePassword); fprintf(employeesFile,"\n%s %s\n",employeeId,employeePassword);
fclose(employeesFile); fclose(employeesFile);
/*used the checkEmployeeCredentials to check if the new id and password are created successfully*/
/*used the checkEmployeeCredentials to check if the new id and password are created successfully*/
return checkEmployeeCredentials(employeeId, employeePassword); return checkEmployeeCredentials(employeeId, employeePassword);
} }
void getNewEmployeeCredentials() void getNewEmployeeCredentials()
{ {
char newEmployeeId[20]; char newEmployeeId[20];
@ -50,3 +64,4 @@ void getNewEmployeeCredentials()
createNewEmployee(newEmployeeId,newEmployeePassword) ? printf("\n\n Account created successfully !\n\n") : printf("\n\n Could not create the Account please contact an employee of clearance 1 !\n\n"); createNewEmployee(newEmployeeId,newEmployeePassword) ? printf("\n\n Account created successfully !\n\n") : printf("\n\n Could not create the Account please contact an employee of clearance 1 !\n\n");
} }

4
src/createEmployeeAccount.h

@ -6,9 +6,11 @@
#include<stdbool.h> #include<stdbool.h>
#include<string.h> #include<string.h>
bool isValidEmployeeID(char* employee);
bool isValidEmployeeID(char* employee, int maximumLength);
bool createNewEmployee(char* employeeId, char* employeePassword); bool createNewEmployee(char* employeeId, char* employeePassword);
int StringLengthCounter(char* string);
void getNewEmployeeCredentials(); void getNewEmployeeCredentials();
#endif #endif

15
tests/test_createEmployeeAccount.c

@ -18,13 +18,19 @@ void test_isValidEmployeeID(void)
/*Arrange*/ /*Arrange*/
char* validEmployeeId [] = {"Atharva","Can","Haytham","Julius","Mohamed","Shivam","Fizz","Buzz","JohnDoe","Foobar","waz","Objectoriented","INSTITUTIONALISATIOL","Intercommunicational","1234","1.6"}; 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; bool validEmployeeIdExpected = true;
for(int i =0;i<15;i++)
{
validStringLengths[i] = 20;
}
/*Act and Assert*/ /*Act and Assert*/
for(int i=0; i<15; i++) for(int i=0; i<15; i++)
{ {
bool validEmployeeIdResult = isValidEmployeeID(validEmployeeId[i]);
bool validEmployeeIdResult = isValidEmployeeID(validEmployeeId[i],validStringLengths[i]);
TEST_ASSERT_EQUAL(validEmployeeIdExpected,validEmployeeIdResult); TEST_ASSERT_EQUAL(validEmployeeIdExpected,validEmployeeIdResult);
} }
} }
@ -35,13 +41,18 @@ void test_isNotValidEmployeeID(void)
/*Arrange*/ /*Arrange*/
char* invalidEmployeeId [] = {"Atha rva","Ca n","Geschwindigkeitsbegrenzungen","1234 15","John Doe","fizz Fuzz"}; char* invalidEmployeeId [] = {"Atha rva","Ca n","Geschwindigkeitsbegrenzungen","1234 15","John Doe","fizz Fuzz"};
int invalidStringLengths[6];
bool invalidEmployeeIdExpected = false; bool invalidEmployeeIdExpected = false;
for(int i =0;i<6;i++)
{
invalidStringLengths[i] = 20;
}
/*Act and Assert*/ /*Act and Assert*/
for(int i=0; i<6; i++) for(int i=0; i<6; i++)
{ {
bool invalidEmployeeIdResult = isValidEmployeeID(invalidEmployeeId[i]);
bool invalidEmployeeIdResult = isValidEmployeeID(invalidEmployeeId[i],invalidStringLengths[i]);
TEST_ASSERT_EQUAL(invalidEmployeeIdExpected,invalidEmployeeIdResult); TEST_ASSERT_EQUAL(invalidEmployeeIdExpected,invalidEmployeeIdResult);
} }

Loading…
Cancel
Save