Browse Source
Merge remote-tracking branch 'origin/development-backup' into Alpha
remotes/origin/Alpha
Merge remote-tracking branch 'origin/development-backup' into Alpha
remotes/origin/Alpha
fdai7207
2 years ago
31 changed files with 1096 additions and 78 deletions
-
41build-project.sh
-
9project.yml
-
129src/createCustomer.c
-
13src/createCustomer.h
-
1src/createEmployeeAccount.c
-
72src/customerMenu.c
-
6src/customerMenu.h
-
11src/customerProperties.h
-
14src/docs.txt
-
6src/employeeLogin.c
-
30src/employeesCredentialsList.txt
-
60src/employeesData.txt
-
59src/error.c
-
3src/error.h
-
151src/helperFunctions.c
-
18src/helperFunctions.h
-
87src/loginCustomer.c
-
14src/loginCustomer.h
-
BINsrc/main
-
12src/main.c
-
55src/mainMenu.c
-
2src/mainMenu.h
-
29tests/test_CreateCustomer.c
-
24tests/test_CustomerMenu.c
-
81tests/test_Error.c
-
24tests/test_LoginCustomer.c
-
2tests/test_createEmployeeAccount.c
-
4tests/test_employeeLogin.c
-
213tests/test_helperFunctions.c
-
2tests/test_mainMenu.c
-
2tests/test_showGeneralInfoEmployee.c
@ -0,0 +1,129 @@ |
|||
#include "createCustomer.h" |
|||
/*Code written by Julius Philipp Engel, fdai7057*/ |
|||
int generateID() |
|||
{ |
|||
srand(clock()); |
|||
const int MIN = 1000000, MAX = 9000001; |
|||
int pseudoRandomIDForCustomer = (rand() % MAX) + MIN; |
|||
return pseudoRandomIDForCustomer; |
|||
} |
|||
|
|||
void collectCustomerProperties() |
|||
{ |
|||
customer_t instance; |
|||
instance.forename = calloc(15+1,sizeof(char)); |
|||
instance.surname = calloc(15+1,sizeof(char)); |
|||
instance.password = calloc(20+1,sizeof(char)); |
|||
instance.ID = generateID(); |
|||
int letterCounter = 0; |
|||
int letterMaximum = 15; |
|||
int errorResult = 0; |
|||
char userInput=' '; |
|||
bool inputTooLong = false, foundComma = false; |
|||
printf("To create a new user, enter the information required below.\n"); |
|||
printf("Enter forename (max. 15 letters):\n"); |
|||
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ |
|||
*(instance.forename+letterCounter) = userInput; |
|||
++letterCounter; |
|||
if(letterCounter>=letterMaximum){ |
|||
inputTooLong = true; |
|||
break; |
|||
} |
|||
} |
|||
if(inputTooLong){ |
|||
errorResult = errorMessage(-7); |
|||
if(errorResult==-7) exit(-1); |
|||
} |
|||
else{ |
|||
*(instance.forename+letterCounter) = '\0'; |
|||
letterCounter = 0; |
|||
} |
|||
if(!isLetterOfAlphabet(instance.forename)){ |
|||
errorResult = errorMessage(-10); |
|||
exit(-1); |
|||
} |
|||
printf("Enter surname (max. 15 letters):\n"); |
|||
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ |
|||
*(instance.surname+letterCounter) = userInput; |
|||
++letterCounter; |
|||
if(letterCounter>=letterMaximum){ |
|||
inputTooLong = true; |
|||
break; |
|||
} |
|||
} |
|||
if(inputTooLong){ |
|||
errorResult = errorMessage(-8); |
|||
if(errorResult==-8) exit(-1); |
|||
}else{ |
|||
*(instance.surname+letterCounter) = '\0'; |
|||
letterCounter = 0; |
|||
} |
|||
if(!isLetterOfAlphabet(instance.surname)){ |
|||
errorResult = errorMessage(-11); |
|||
if(errorResult==-11) exit(-1); |
|||
} |
|||
printf("Enter password (max. 20 letters):\n"); |
|||
letterMaximum = 20; |
|||
while(letterCounter<letterMaximum && (userInput=getchar())!='\n'){ |
|||
*(instance.password+letterCounter) = userInput; |
|||
++letterCounter; |
|||
if(letterCounter>=letterMaximum){ |
|||
inputTooLong = true; |
|||
break; |
|||
} |
|||
} |
|||
if(inputTooLong){ |
|||
errorResult=errorMessage(-9); |
|||
if(errorResult==-9) exit(-1); |
|||
}else{ |
|||
*(instance.password+letterCounter) = '\0'; |
|||
} |
|||
|
|||
letterCounter = 0; |
|||
printf("Enter balance (max. 10 digits):\n"); |
|||
char *balanceCharacters = calloc(10+1+1+1,sizeof(char)); |
|||
letterMaximum = 10; |
|||
while(letterCounter<=letterMaximum && (userInput=getchar())!='\n'){ |
|||
*(balanceCharacters+letterCounter) = userInput; |
|||
++letterCounter; |
|||
if(letterCounter>letterMaximum){ |
|||
inputTooLong = true; |
|||
break; |
|||
} |
|||
} |
|||
if(inputTooLong){ |
|||
errorResult = errorMessage(-12); |
|||
if(errorResult==-12) exit(-1); |
|||
}else{ |
|||
if(!foundComma){ |
|||
*(balanceCharacters+letterCounter) = '.'; |
|||
++letterCounter; |
|||
*(balanceCharacters+letterCounter) = '0'; |
|||
++letterCounter; |
|||
} |
|||
*(balanceCharacters+letterCounter) = '\0'; |
|||
} |
|||
if(!everyCharacterIsDigit(balanceCharacters)){ |
|||
puts("You have entered a character that is not a number. This is not allowed. Aborting!"); |
|||
exit(-1); |
|||
} |
|||
instance.balance = balanceToDouble(balanceCharacters); |
|||
printf("Account successfully created. Your ID is: %d. Note it somewhere!\n",instance.ID); |
|||
customer_t *referenceToCustomerInstance = &instance; |
|||
writeCustomerPropertiesToFile(referenceToCustomerInstance); |
|||
} |
|||
|
|||
void writeCustomerPropertiesToFile(customer_t *referenceToCustomerInstance) |
|||
{ |
|||
FILE *customerData = fopen("CustomerData.txt","a"); |
|||
int errorResult = 0; |
|||
if(customerData!=NULL){ |
|||
fprintf(customerData,"%s\nID=%d\nForename=%s\nSurname=%s\nPassword=%s\nBalance=%.4f€\n\n", |
|||
generateCheckString(referenceToCustomerInstance->ID, referenceToCustomerInstance->password),referenceToCustomerInstance->ID,referenceToCustomerInstance->forename,referenceToCustomerInstance->surname,referenceToCustomerInstance->password, referenceToCustomerInstance->balance); |
|||
fclose(customerData); |
|||
} |
|||
else{ |
|||
errorResult = errorMessage(-6); |
|||
if(errorResult==-6) exit(-1); |
|||
} |
|||
} |
@ -0,0 +1,13 @@ |
|||
#ifndef CREATE_CUSTOMER_H |
|||
#define CREATE_CUSTOMER_H |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <stdbool.h> |
|||
#include <time.h> |
|||
#include "customerProperties.h" |
|||
#include "helperFunctions.h" |
|||
#include "error.h" |
|||
int generateID(); |
|||
void collectCustomerProperties(); |
|||
void writeCustomerPropertiesToFile(customer_t *); |
|||
#endif |
@ -0,0 +1,72 @@ |
|||
#include "customerMenu.h" |
|||
|
|||
int customerChoiceForMenuItem(int numberOfMenuItem) |
|||
{ |
|||
int returnStatus = 0; |
|||
switch(numberOfMenuItem){ |
|||
case 1: |
|||
puts("You have chosen to send money.\n"); |
|||
returnStatus = 1; |
|||
//call sendMoney(); |
|||
break; |
|||
case 2: |
|||
puts("You have chosen to withdraw money.\n"); |
|||
returnStatus = 2; |
|||
//call withDrawMoney(); |
|||
break; |
|||
case 3: |
|||
puts("You have chosen to deposit money.\n"); |
|||
returnStatus = 3; |
|||
//call depositMoney(); |
|||
break; |
|||
case 4: |
|||
puts("You have chosen to request a loan.\n"); |
|||
returnStatus = 4; |
|||
//call requestLoan(); |
|||
break; |
|||
default: |
|||
puts("Invalid input."); |
|||
returnStatus = -1; |
|||
} |
|||
return returnStatus; |
|||
} |
|||
|
|||
void showAllMenuEntries() |
|||
{ |
|||
|
|||
int userDecision = 0; |
|||
|
|||
puts("\n\n"); |
|||
puts("CUSTOMER MENU"); |
|||
|
|||
puts("\n"); |
|||
puts("Select between (1-4):"); |
|||
puts("\n\n\n"); |
|||
|
|||
puts("->->->1. send money<-<-<-"); |
|||
puts("\n\n\n"); |
|||
|
|||
puts("->->->2. withdraw money<-<-<-"); |
|||
puts("\n\n\n"); |
|||
|
|||
puts("->->->3. deposit money<-<-<-"); |
|||
puts("\n\n\n"); |
|||
|
|||
puts("->->->4. request loan<-<-<-"); |
|||
puts("\n\n\n"); |
|||
|
|||
puts("Your decision: "); |
|||
scanf("%d",&userDecision); |
|||
customerChoiceForMenuItem(userDecision); |
|||
} |
|||
|
|||
void menu(customer_t *c) |
|||
{ |
|||
if(c==NULL){ |
|||
puts("Invalid pointer. Aborting!"); |
|||
exit(-1); |
|||
}else{ |
|||
puts("Welcome!"); |
|||
showAllMenuEntries(); |
|||
} |
|||
} |
@ -0,0 +1,6 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include "customerProperties.h" |
|||
int customerChoiceForMenuItem(int); |
|||
void showAllMenuEntries(); |
|||
void menu(customer_t *); |
@ -0,0 +1,11 @@ |
|||
#ifndef CUSTOMER_PROPERTIES_H |
|||
#define CUSTOMER_PROPERTIES_H |
|||
typedef struct Customer |
|||
{ |
|||
unsigned int ID; |
|||
char *IDAsString; |
|||
char *password; |
|||
char *forename, *surname; |
|||
double balance; |
|||
}customer_t; |
|||
#endif |
@ -0,0 +1,14 @@ |
|||
char *stringConcatenation(char string_1*, char string_2*): |
|||
|
|||
This function takes two char pointers. In this function a new string is created. This new string is the concatenation of string_1 and string_2. The size of the new string is the length of string_1 plus the length of string_2 plus one for '\0'. This function is needed when creating an unique check string for a customer. |
|||
|
|||
char *to_string(int number): |
|||
|
|||
This function takes an integer variable as its argument. It returns an string that contains the digits of number. For example to_string(176) would return "176\0". This function is needed to write data in form of characters into the customer file. |
|||
|
|||
|
|||
char *generateCheckString(int customerID, char *password): |
|||
|
|||
The purpose of this function is to generate a string that is needed when a user wants to login. This string is searched for in the customer-data file and if it is found, the login is successful. This function is needed when a new user is created because then it is written in the file for the first time. |
|||
The format of the returned string is [ID]=[PASSWORD]. |
|||
For example generateCheckString(177,"tree") would return "177=tree\0". |
@ -0,0 +1,59 @@ |
|||
#include "error.h" |
|||
|
|||
int errorMessage(int errorCode) |
|||
{ |
|||
int returnValue=0; |
|||
switch(errorCode){ |
|||
case -1: |
|||
puts("Login not successful."); |
|||
returnValue = -1; |
|||
break; |
|||
case -2: |
|||
puts("Maximum number of attempts reached."); |
|||
returnValue = -2; |
|||
break; |
|||
case -3: |
|||
puts("No menu entry available for this number."); |
|||
returnValue = -3; |
|||
break; |
|||
case -4: |
|||
puts("CustomerData.* not found. Make sure that you've created an user account before logging in for the first time. Without users there is no file. Aborting!"); |
|||
returnValue = -4; |
|||
break; |
|||
case -5: |
|||
puts("You should be at least 18 years old to create a bank account!"); |
|||
returnValue = -5; |
|||
break; |
|||
case -6: |
|||
puts("Error when trying to open a file to create a customer account."); |
|||
returnValue = -6; |
|||
break; |
|||
case -7: |
|||
puts("Forename too long. (length > 15 characters) Aborting!"); |
|||
returnValue = -7; |
|||
break; |
|||
case -8: |
|||
puts("Surname too long. (length > 15 characters) Aborting!"); |
|||
returnValue = -8; |
|||
break; |
|||
case -9: |
|||
puts("Password too long. (length > 20 characters) Aboring!"); |
|||
returnValue = -9; |
|||
break; |
|||
case -10: |
|||
puts("You have entered an invalid character [ä,ö,ü, special characters] for your forename. This is not allowed. Aborting!"); |
|||
returnValue = -10; |
|||
break; |
|||
case -11: |
|||
puts("You have entered an invalid character [ä,ö,ü, special characters] for your surname. This is not allowed. Aborting!"); |
|||
returnValue = -11; |
|||
break; |
|||
case -12: |
|||
puts("You entered too many digits."); |
|||
returnValue = -12; |
|||
break; |
|||
default: |
|||
puts("Error code unknown."); |
|||
} |
|||
return returnValue; |
|||
} |
@ -0,0 +1,3 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
int errorMessage(int); |
@ -0,0 +1,151 @@ |
|||
#include "helperFunctions.h" |
|||
/*Code written by Julius Philipp Engel, fdai7057*/ |
|||
|
|||
char *stringConcatenation(char *string_1, char *string_2) |
|||
{ |
|||
int lengthStringOne = strlen(string_1); |
|||
int lengthStringTwo = strlen(string_2); |
|||
|
|||
if(lengthStringOne==0||lengthStringTwo==0){ |
|||
printf("Empty strings are not allowed. Aborting.\n"); |
|||
exit(-1); |
|||
//call error(); |
|||
} |
|||
|
|||
const int totalLength = lengthStringOne + lengthStringTwo + 1; |
|||
char *result = calloc(totalLength, sizeof(char)); |
|||
int i,j; |
|||
for(i=0,j=0;i<totalLength;++i){ |
|||
if(i<lengthStringOne){ |
|||
*(result+i) = *(string_1+i); |
|||
}else{ |
|||
*(result+i) = *(string_2+j); |
|||
++j; |
|||
} |
|||
} |
|||
*(result+i) = '\0'; |
|||
return result; |
|||
} |
|||
|
|||
|
|||
char *to_string(int number) |
|||
{ |
|||
if(number==0) |
|||
{ |
|||
char *str = calloc(2,sizeof(char)); |
|||
*(str) = '0'; |
|||
*(str+1) = '\0'; |
|||
return str; |
|||
} |
|||
else |
|||
{ |
|||
int cpy = number, len = 0; |
|||
while(number>0){ |
|||
++len; |
|||
number /= 10; |
|||
} |
|||
char *str = calloc(len+1, sizeof(char)); |
|||
for(int i=0,j=len-1;i<len;++i,--j){ |
|||
*(str+j) = '0' + (cpy % 10); |
|||
cpy /= 10; |
|||
} |
|||
*(str+len) = '\0'; |
|||
return str; |
|||
} |
|||
} |
|||
|
|||
unsigned int power(unsigned int base, unsigned int exponent){ |
|||
if(base==0&&exponent==0) return 0; |
|||
else if(base>=1&&exponent==0) return 1; |
|||
else{ |
|||
unsigned int result = 1, counter = 0; |
|||
while(counter<exponent){ |
|||
result *= base; |
|||
++counter; |
|||
} |
|||
return result; |
|||
} |
|||
} |
|||
|
|||
bool everyCharacterIsDigit(char *string) |
|||
{ |
|||
bool onlyDigits = true; |
|||
for(int i=0;*(string+i)!='\0';++i){ |
|||
if( !(*(string+i)>='0'&&*(string+i)<='9') && *(string+i)!='.'){ |
|||
onlyDigits = false; |
|||
break; |
|||
} |
|||
} |
|||
return onlyDigits; |
|||
} |
|||
|
|||
bool isLetterOfAlphabet(char *string){ |
|||
bool everyCharIsInAlphabet = true; |
|||
for(int i=0;*(string+i)!='\0';++i){ |
|||
if(!(*(string+i)>='A'&&*(string+i)<='Z') && !(*(string+i)>='a'&&*(string+i)<='z')){ |
|||
everyCharIsInAlphabet = false; |
|||
break; |
|||
} |
|||
} |
|||
return everyCharIsInAlphabet; |
|||
} |
|||
|
|||
unsigned int toUnsignedInteger(char *ID) |
|||
{ |
|||
if(everyCharacterIsDigit(ID)){ |
|||
unsigned int result = 0; |
|||
int IDLength = strlen(ID); |
|||
for(int i=0, j = IDLength - 1;i<IDLength;++i,--j){ |
|||
result += (*(ID+j) - '0') * power(10,i); |
|||
} |
|||
return result; |
|||
} else { |
|||
return 0; |
|||
} |
|||
} |
|||
|
|||
char *generateCheckString(unsigned int customerID, char *customerPassword) |
|||
{ |
|||
char *IDString = to_string(customerID); |
|||
int IDLength = strlen(IDString); |
|||
int passwordLength = strlen(customerPassword); |
|||
int checkStringLength = IDLength + 1 + passwordLength + 1; |
|||
char *checkString = calloc(checkStringLength, sizeof(char)); |
|||
checkString = strcat(IDString, "="); |
|||
checkString = strcat(checkString, customerPassword); |
|||
*(checkString+checkStringLength) = '\0'; |
|||
return checkString; |
|||
} |
|||
|
|||
double balanceToDouble(char *balanceAsString) |
|||
{ |
|||
double result, power; |
|||
int i=0, sign; |
|||
sign = (*(balanceAsString) == '-') ? -1 : 1; |
|||
if(*(balanceAsString)=='+'||*(balanceAsString)=='-'){ |
|||
++i; |
|||
} |
|||
for(result = 0.0; *(balanceAsString+i)!='.';++i){ |
|||
result = 10.0 * result + (*(balanceAsString+i) - '0'); |
|||
} |
|||
if(*(balanceAsString+i)=='.'){ |
|||
++i; |
|||
} |
|||
for(power = 1.0;*(balanceAsString+i)!='\0';++i){ |
|||
result = 10.0 * result + (*(balanceAsString+i)- '0'); |
|||
power *= 10.0; |
|||
} |
|||
return sign * result / power; |
|||
} |
|||
|
|||
unsigned int calculateStringLength(char *string){ |
|||
int length = 0; |
|||
while(*(string+length)!='\0') ++length; |
|||
return length; |
|||
} |
|||
|
|||
bool characterIsUpperCase(char inputCharacter) |
|||
{ |
|||
bool result = (inputCharacter>='A'&&inputCharacter<='Z') ? true : false; |
|||
return result; |
|||
} |
@ -0,0 +1,18 @@ |
|||
#ifndef STRING_MANIPULATION_H |
|||
#define STRING_MANIPULATION_H |
|||
#include <stdlib.h> |
|||
#include <stdio.h> |
|||
#include <string.h> |
|||
#include <stdbool.h> |
|||
#define UNITY_INCLUDE_CONFIG_H |
|||
char *stringConcatenation(char *, char *); |
|||
char *to_string(int); |
|||
char *generateCheckString(unsigned int, char *); |
|||
unsigned int toUnsignedInteger(char *); |
|||
unsigned int power(unsigned int, unsigned int); |
|||
bool everyCharacterIsDigit(char *); |
|||
bool isLetterOfAlphabet(char *); |
|||
double balanceToDouble(char *); |
|||
unsigned int calculateStringLength(char *); |
|||
bool charIsUpperCase(char ); |
|||
#endif |
@ -0,0 +1,87 @@ |
|||
#include "loginCustomer.h" |
|||
|
|||
bool checkLogin(bool loginSuccessful) |
|||
{ |
|||
return (loginSuccessful) ? true : false; |
|||
} |
|||
|
|||
void collectCustomerDataForLogin(int attempts) |
|||
{ |
|||
customer_t c; |
|||
c.IDAsString = calloc(15+1, sizeof(char)); |
|||
c.password = calloc(20+1, sizeof(char)); |
|||
int digitCharacterFromUser, passwordCharacterFromUser; |
|||
int IDLengthCounter = 0, passwordLengthCounter = 0; |
|||
int errorResult = 0; |
|||
const int IDMaxLength = 16, passwordMaxLength = 21; |
|||
printf("Enter ID:\n"); |
|||
while((digitCharacterFromUser=getchar())!='\n'&&IDLengthCounter<IDMaxLength){ |
|||
if(digitCharacterFromUser>='0'&&digitCharacterFromUser<='9'){ |
|||
*(c.IDAsString+IDLengthCounter) = digitCharacterFromUser; |
|||
} |
|||
else{ |
|||
printf("Character entered is not a digit. Aborting.\n"); |
|||
exit(-1); |
|||
} |
|||
++IDLengthCounter; |
|||
} |
|||
*(c.IDAsString+IDLengthCounter) = '\0'; |
|||
if(IDLengthCounter>=IDMaxLength){ |
|||
printf("ID entered is too long. Aborting.\n"); |
|||
exit(-1); |
|||
} |
|||
printf("Enter password:\n"); |
|||
while((passwordCharacterFromUser=getchar())!='\n'&&passwordLengthCounter<passwordMaxLength){ |
|||
*(c.password+passwordLengthCounter) = passwordCharacterFromUser; |
|||
++passwordLengthCounter; |
|||
} |
|||
*(c.password+passwordLengthCounter) = '\0'; |
|||
|
|||
if(passwordLengthCounter>=passwordMaxLength){ |
|||
printf("Password entered is too long. Aborting.\n"); |
|||
exit(-1); |
|||
} |
|||
bool loginSuccessful = loginCustomer(&c); |
|||
free(c.IDAsString); |
|||
free(c.password); |
|||
if(loginSuccessful) { |
|||
//call menu(); |
|||
menu(&c); |
|||
}else if(!loginSuccessful && attempts < MAX_LOGIN_ATTEMPTS){ |
|||
printf("You have %d attempts left.\n", MAX_LOGIN_ATTEMPTS - attempts); |
|||
collectCustomerDataForLogin(++attempts); |
|||
}else{ |
|||
errorResult = errorMessage(-2); |
|||
if(errorResult==-2) exit(-1); |
|||
} |
|||
} |
|||
|
|||
bool loginCustomer(customer_t *c) |
|||
{ |
|||
bool foundCustomerEntryInFile = false; |
|||
unsigned int IDAsNumber = toUnsignedInteger(c->IDAsString); |
|||
char *searchForThisString = generateCheckString(IDAsNumber,c->password); |
|||
char *lineFromCustomerFile = calloc(40,sizeof(char)); |
|||
int errorResult = 0; |
|||
FILE *readCustomerFile = fopen("CustomerData.txt", "r"); |
|||
if(readCustomerFile==NULL){ |
|||
errorResult = errorMessage(-4); |
|||
if(errorResult==-4) exit(-1); |
|||
} |
|||
while((fscanf(readCustomerFile,"%s",lineFromCustomerFile)!=EOF)){ |
|||
if(strcmp(searchForThisString,lineFromCustomerFile)==0){ |
|||
foundCustomerEntryInFile = true; |
|||
break; |
|||
} |
|||
} |
|||
free(lineFromCustomerFile); |
|||
if(checkLogin(foundCustomerEntryInFile)){ |
|||
printf("Login successful.\n"); |
|||
fclose(readCustomerFile); |
|||
return foundCustomerEntryInFile; |
|||
}else{ |
|||
errorResult = errorMessage(-1); |
|||
} |
|||
fclose(readCustomerFile); |
|||
return foundCustomerEntryInFile; |
|||
} |
@ -0,0 +1,14 @@ |
|||
#ifndef LOGIN_CUSTOMER_H |
|||
#define LOGIN_CUSTOMER_H |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <stdbool.h> |
|||
#include <string.h> |
|||
#include "createCustomer.h" |
|||
#include "customerMenu.h" |
|||
#include "error.h" |
|||
#define MAX_LOGIN_ATTEMPTS 3 |
|||
bool checkLogin(bool); |
|||
void collectCustomerDataForLogin(int); |
|||
bool loginCustomer(customer_t *); |
|||
#endif |
@ -0,0 +1,29 @@ |
|||
#include <unity.h> |
|||
#include <limits.h> |
|||
#include <math.h> |
|||
#include "../src/helperFunctions.c" |
|||
#include "../src/error.c" |
|||
#include "../src/createCustomer.c" |
|||
|
|||
void setUp(){} |
|||
void tearDown(){} |
|||
|
|||
void test_generateID(){ |
|||
|
|||
const int test_values = USHRT_MAX; |
|||
|
|||
/*initialize blocks by calling generateID()*/ |
|||
int *numbers = calloc(test_values, sizeof(int)); |
|||
for(int i=0;i<test_values;++i) |
|||
{ |
|||
*(numbers+i) = generateID(); |
|||
//printf("%d\n", *(numbers+i)); |
|||
} |
|||
|
|||
/*assertions, range checking*/ |
|||
int delta = 5000000, expected = 5000000; |
|||
for(int i=0;i<test_values;++i) |
|||
{ |
|||
TEST_ASSERT_INT_WITHIN(delta, expected,*(numbers+i)); |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
#include <unity.h> |
|||
#include "../src/customerMenu.c" |
|||
|
|||
void test_customerChoiceForMenuEntry() |
|||
{ |
|||
int decision = 1; |
|||
/*customer choses to send money, return 1*/ |
|||
TEST_ASSERT_EQUAL_INT(1, customerChoiceForMenuItem(decision)); |
|||
/*customer choses to withdraw money, return 2*/ |
|||
decision = 2; |
|||
TEST_ASSERT_EQUAL_INT(2, customerChoiceForMenuItem(decision)); |
|||
/*customer choses to deposit money, return 3*/ |
|||
decision = 3; |
|||
TEST_ASSERT_EQUAL_INT(3, customerChoiceForMenuItem(decision)); |
|||
/*customer choses to request a loan, return 4*/ |
|||
decision = 4; |
|||
TEST_ASSERT_EQUAL_INT(4, customerChoiceForMenuItem(decision)); |
|||
/*invalid input values, the return value should always be -1*/ |
|||
int arrayOfInvalidValues[] = {-1,-2,-3,-4,-5,-6,-7,-8,-9,-10,-11,-12,-13,-14,-15,-16,-17,-18,-19,-20,0, 123,3247,6839,38593,3033,55055}; |
|||
int length = sizeof(arrayOfInvalidValues)/sizeof(int); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(-1, customerChoiceForMenuItem(arrayOfInvalidValues[i])); |
|||
} |
|||
} |
@ -0,0 +1,81 @@ |
|||
#include <unity.h> |
|||
#include <time.h> |
|||
#include <stdlib.h> |
|||
#include "../src/error.c" |
|||
|
|||
void setUp(){} |
|||
void tearDown(){} |
|||
|
|||
|
|||
void test_error() |
|||
{ |
|||
/*arrange*/ |
|||
srand(time(0)); |
|||
int bound = 1000; |
|||
int invalidErrorCodes_1[bound]; |
|||
int invalidErrorCodesLarge_2[bound]; |
|||
int invalidErrorCodesLargest_3[bound]; |
|||
|
|||
int validErrorCodeUnsuccessfulLogin[bound]; |
|||
int validErrorCodeMaximumNumberOfAttempts[bound]; |
|||
int validErrorCodeNoMenuEntryForNumber[bound]; |
|||
/*new test cases*/ |
|||
int validErrorCodeNoCustomerDataFile[bound]; |
|||
int validErrorCodeTooYoung[bound]; |
|||
int validErrorCodeCreatingFile[bound]; |
|||
int validErrorCodeForenameTooLong[bound]; |
|||
int validErrorCodeSurnameTooLong[bound]; |
|||
int validErrorCodePasswordTooLong[bound]; |
|||
|
|||
int validErrorCodeInvalidCharacterForename[bound]; |
|||
int validErrorCodeInvalidCharacterSurname[bound]; |
|||
int validErrorCodeTooManyDigits[bound]; |
|||
|
|||
|
|||
for(int i=0;i<bound;++i){ |
|||
/*1000 numbers in the range from 1 to 2000 */ |
|||
invalidErrorCodes_1[i] = rand() % 2000 + 1; |
|||
/*1000 numbers in the range from 1000.000 to 100.999.999*/ |
|||
invalidErrorCodesLarge_2[i] = (rand() % 100000000) + 1000000; |
|||
/*1000 numbers in the range from 1.000.000.000 to 2.000.000.000*/ |
|||
invalidErrorCodesLargest_3[i] = (rand() % 1000000001) + 1000000000 ; |
|||
/*1000 times -1 in array*/ |
|||
validErrorCodeUnsuccessfulLogin[i] = -1; |
|||
/*1000 times -2 in array*/ |
|||
validErrorCodeMaximumNumberOfAttempts[i] = -2; |
|||
/*1000 times -3 in array*/ |
|||
validErrorCodeNoMenuEntryForNumber[i] = -3; |
|||
validErrorCodeNoCustomerDataFile[i] = -4; |
|||
validErrorCodeTooYoung[i] = -5; |
|||
validErrorCodeCreatingFile[i] = -6; |
|||
validErrorCodeForenameTooLong[i] = -7; |
|||
validErrorCodeSurnameTooLong[i] = -8; |
|||
validErrorCodePasswordTooLong[i] = -9; |
|||
validErrorCodeInvalidCharacterForename[i] = -10; |
|||
validErrorCodeInvalidCharacterSurname[i] = -11; |
|||
validErrorCodeTooManyDigits[i] = -12; |
|||
} |
|||
|
|||
/*act and assertions for invalid error codes*/ |
|||
for(int i=0;i<bound;++i){ |
|||
TEST_ASSERT_EQUAL_INT(0,errorMessage(invalidErrorCodes_1[i])); |
|||
TEST_ASSERT_EQUAL_INT(0,errorMessage(invalidErrorCodesLarge_2[i])); |
|||
TEST_ASSERT_EQUAL_INT(0,errorMessage(invalidErrorCodesLargest_3[i])); |
|||
} |
|||
/*act and assertions for valid error codes*/ |
|||
for(int i=0;i<bound;++i){ |
|||
TEST_ASSERT_EQUAL_INT(-1, errorMessage(validErrorCodeUnsuccessfulLogin[i])); |
|||
TEST_ASSERT_EQUAL_INT(-2, errorMessage(validErrorCodeMaximumNumberOfAttempts[i])); |
|||
TEST_ASSERT_EQUAL_INT(-3, errorMessage(validErrorCodeNoMenuEntryForNumber[i])); |
|||
/*new test cases*/ |
|||
TEST_ASSERT_EQUAL_INT(-4, errorMessage(validErrorCodeNoCustomerDataFile[i])); |
|||
TEST_ASSERT_EQUAL_INT(-5, errorMessage(validErrorCodeTooYoung[i])); |
|||
TEST_ASSERT_EQUAL_INT(-6, errorMessage(validErrorCodeCreatingFile[i])); |
|||
TEST_ASSERT_EQUAL_INT(-7, errorMessage(validErrorCodeForenameTooLong[i])); |
|||
TEST_ASSERT_EQUAL_INT(-8, errorMessage(validErrorCodeSurnameTooLong[i])); |
|||
TEST_ASSERT_EQUAL_INT(-9, errorMessage(validErrorCodePasswordTooLong[i])); |
|||
TEST_ASSERT_EQUAL_INT(-10, errorMessage(validErrorCodeInvalidCharacterForename[i])); |
|||
TEST_ASSERT_EQUAL_INT(-11, errorMessage(validErrorCodeInvalidCharacterSurname[i])); |
|||
TEST_ASSERT_EQUAL_INT(-12, errorMessage(validErrorCodeTooManyDigits[i])); |
|||
} |
|||
} |
@ -0,0 +1,24 @@ |
|||
#include <unity.h> |
|||
#include "../src/loginCustomer.c" |
|||
#include "../src/customerMenu.c" |
|||
#include "../src/helperFunctions.c" |
|||
#include "../src/error.c" |
|||
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])); |
|||
} |
|||
} |
@ -0,0 +1,213 @@ |
|||
#include <stdlib.h> |
|||
#include <time.h> |
|||
#include <string.h> |
|||
#include <unity.h> |
|||
#include "../src/helperFunctions.c" |
|||
|
|||
void test_calculateStringLength() |
|||
{ |
|||
char *testStrings[] = {"linux","table","book","men","woman","boy","girl","computer","old","new","water","fire","bright","dark","black","white"}; int expectedResults[] = {5,5,4,3,5,3,4,8,3,3,5,4,6,4,5,5}; |
|||
int numberOfValues= sizeof(expectedResults) / sizeof(int); |
|||
for(int i=0;i<numberOfValues;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expectedResults[i], calculateStringLength(testStrings[i])); |
|||
} |
|||
} |
|||
|
|||
void test_isLetterOfAlphabet() |
|||
{ |
|||
/*test block 1*/ |
|||
char *testStringsToTrue[] = {"adhj","kasdlwq","vbeuqor","kalkwyynmakj","kakswxl","akljlxjkcyxyklj","asdjhuwpwe","xbcvddd","klajksdjkl","ghghgtie","kajsd"}; |
|||
unsigned int numberOfElements = sizeof(testStringsToTrue) / sizeof(char *); |
|||
for(int i=0;i<numberOfElements;++i){ |
|||
TEST_ASSERT_TRUE(isLetterOfAlphabet(testStringsToTrue[i])); |
|||
} |
|||
/*test block 2*/ |
|||
char *testStringsToTrue_2[] = {"bcjyxhkjyxchjqwo","tree","garden","thinker","philosophy","linux","computer","lesson","teacher","table","drink","water","every", "Frank","city","economic","programming","task","smart","smarter","smartest","dumb","wood","animals","forest","display","hot","cold","ice","bear","keyboard","pair","pencil"}; |
|||
numberOfElements = sizeof(testStringsToTrue_2) / sizeof(char *); |
|||
for(int i=0;i<numberOfElements;++i){ |
|||
TEST_ASSERT_TRUE(isLetterOfAlphabet(testStringsToTrue_2[i])); |
|||
} |
|||
/*test block 3*/ |
|||
char *testStringsToFalse[] = {"ashjdkj32123","4213jashj","laskdj2","1sbabjsdh","askjasdkjd0","123932131a","abcd2hutz","81287asjk231jkhs","aslkjasdlkjsd123","cbc451873"}; |
|||
numberOfElements = sizeof(testStringsToFalse) / sizeof(char *); |
|||
for(int i=0;i<numberOfElements;++i){ |
|||
TEST_ASSERT_FALSE(isLetterOfAlphabet(testStringsToFalse[i])); |
|||
} |
|||
/*test block 4*/ |
|||
char *testStringsToFalse_2[] = {"1234","56789","00000010101010","3748927398273498","757575757","1726371238726","19237182937192837","875378612873621","128973192837","99494949499929292929292938382828","1827391237981273","7481263871236782136"}; |
|||
numberOfElements = sizeof(testStringsToFalse_2) / sizeof(char *); |
|||
for(int i=0;i<numberOfElements;++i){ |
|||
TEST_ASSERT_FALSE(isLetterOfAlphabet(testStringsToFalse_2[i])); |
|||
} |
|||
} |
|||
|
|||
|
|||
void test_toUnsignedInteger() |
|||
{ |
|||
/*test block 1*/ |
|||
char *strings[] = {"111","123","542","994","9000","8384","6473","12345","57837","78387","93276","1000","8444","48484"}; |
|||
int expected[] = {111,123,542,994,9000,8384,6473,12345,57837,78387,93276,1000,8444,48484}; |
|||
int length = sizeof(expected)/sizeof(int); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expected[i], toUnsignedInteger(strings[i])); |
|||
} |
|||
|
|||
/*test block 2*/ |
|||
char *strings_2[] = {"9999","99999","9","99","999","0","19","10","90","8765"}; |
|||
int expected_2[] = {9999,99999,9,99,999,0,19,10,90,8765}; |
|||
length = sizeof(expected_2)/sizeof(int); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expected_2[i], toUnsignedInteger(strings_2[i])); |
|||
} |
|||
|
|||
/*test block 3*/ |
|||
char *strings_3[] = {"0","1","1","2","3","5","8","13","21","34","55","89","144","233"}; |
|||
int expected_3[] = {0,1,1,2,3,5,8,13,21,34,55,89,144,233}; |
|||
length = sizeof(expected_3)/sizeof(int); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expected_3[i], toUnsignedInteger(strings_3[i])); |
|||
} |
|||
|
|||
} |
|||
void test_everyCharacterIsDigit() |
|||
{ |
|||
/*test block 1*/ |
|||
char *expectTrue[] = {"0","11","222","3333","4444","134132","12352378","12847273","1237873","9992475","987232","34723873278","578347823783","758378723","44293884742", |
|||
"3184123872873","8912892383","18282828","55757575757528282","123823883282383282575757283832","99999999999999999999999999999999999","128321378","81293982139823","21412323" |
|||
"575757575754646464648383838383","1298557648298219821398129381928391283918238912831283928391283129839281391283918238912391238912839182391239857517828"}; |
|||
int length = sizeof(expectTrue)/sizeof(char *); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_TRUE(everyCharacterIsDigit(expectTrue[i])); |
|||
} |
|||
|
|||
/*test block 2*/ |
|||
char *expectFalse[] = {"a","bcd","dhdd","3asad87","askj","nxbdj","489sjk2kj","kjasjkd38234","aksjlas","bcbc838ch","akjsjkdjkq919191","askjsdakj492","kasjcncn","9919a19","cbajsh","askjajkd","ajshdasjh","jyxhyxjchyx","kasjdakj","vbvb88888888888888828282828282828askjh"}; |
|||
length = sizeof(expectFalse)/sizeof(char *); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_FALSE(everyCharacterIsDigit(expectFalse[i])); |
|||
} |
|||
} |
|||
|
|||
void test_power() |
|||
{ |
|||
/*test block 1*/ |
|||
int testValues[] = {1,2,3,4,5,6,7,8,9,10}; |
|||
int expectedValues[] = {1,4,9,16,25,36,49,64,81,100}; |
|||
int length = sizeof(testValues)/sizeof(int); |
|||
const int exponent = 2; |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expectedValues[i], power(testValues[i],exponent)); |
|||
} |
|||
|
|||
/*test block 2*/ |
|||
int testValues_2[] = {11,12,13,14,15,16,17,18,19,20}; |
|||
int expectedValues_2[] = {121,144,169,196,225,256,289,324,361,400}; |
|||
length = sizeof(testValues_2)/sizeof(int); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expectedValues_2[i],power(testValues_2[i],exponent)); |
|||
} |
|||
|
|||
/*test block 3*/ |
|||
int testValues_3[] = {1,2,3,4,5,6,7,8,9,10}; |
|||
int expectedValues_3[] = {1,8,27,64,125,216,343,512,729,1000}; |
|||
const int exponent_2 = 3; |
|||
length = sizeof(testValues_3)/sizeof(int); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expectedValues_3[i],power(testValues_3[i],exponent_2)); |
|||
} |
|||
/*test block 4*/ |
|||
int testValues_4[] = {11,12,13,14,15,16,17,18,19,20}; |
|||
int expectedValues_4[] = {1331,1728,2197,2744,3375,4096,4913,5832,6859,8000}; |
|||
length = sizeof(testValues_4)/sizeof(int); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expectedValues_4[i],power(testValues_4[i],exponent_2)); |
|||
} |
|||
/*test block 5*/ |
|||
int testValues_5[] = {0,0,19,2,4,5,11,54,32,12,77}; |
|||
int exponents[] = {0,1,2,7,4,2,0,1,2,4,2}; |
|||
int expectedValues_5[] = {0, 0, 361,128,256,25,1,54,1024,20736,5929}; |
|||
length = sizeof(testValues_5)/sizeof(int); |
|||
for(int i=0;i<length;++i){ |
|||
TEST_ASSERT_EQUAL_INT(expectedValues_5[i], power(testValues_5[i],exponents[i])); |
|||
} |
|||
} |
|||
|
|||
void test_to_string() |
|||
{ |
|||
/*initializing test values*/ |
|||
char *result_1[] = {"0","1","2","3","4","5","6","7","8","9","10"}; |
|||
char *result_2[] = {"500","502","504","506","508","510","512","514","516","518"}; |
|||
char *result_3[] = {"1000","2000","3000","4000","5000","6000","7000","8000","9000","10000"}; |
|||
char *result_4[] = {"9999","8999","7999","6999","5999","4999","3999","2999","1999","999"}; |
|||
char *result_5[] = {"1000000","2000000","3000000","4000000","5000000","6000000","7000000", |
|||
"8000000","9000000","10000000"}; |
|||
|
|||
/*assertions*/ |
|||
for(int i=0;i<=10;++i){ |
|||
TEST_ASSERT_EQUAL_STRING(result_1[i],to_string(i)); |
|||
} |
|||
|
|||
for(int i=0, j=500;i<10;++i,j+=2){ |
|||
TEST_ASSERT_EQUAL_STRING(result_2[i],to_string(j)); |
|||
} |
|||
|
|||
for(int i=0, j=1000;i<10;++i,j+=1000){ |
|||
TEST_ASSERT_EQUAL_STRING(result_3[i],to_string(j)); |
|||
} |
|||
|
|||
for(int i=0, j=9999;i<10;++i,j-=1000){ |
|||
TEST_ASSERT_EQUAL_STRING(result_4[i], to_string(j)); |
|||
} |
|||
for(int i=0, j=1000000;i<10;++i,j+=1000000){ |
|||
TEST_ASSERT_EQUAL_STRING(result_5[i],to_string(j)); |
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
void test_generateCheckString() |
|||
{ |
|||
/*test block 1*/ |
|||
int numbers_1[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25}; |
|||
char *strings_1[] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; |
|||
char *result_1[] = {"0=a","1=b","2=c","3=d","4=e","5=f","6=g","7=h","8=i","9=j","10=k","11=l","12=m","13=n","14=o","15=p","16=q","17=r", "18=s","19=t","20=u","21=v","22=w","23=x","24=y","25=z"}; |
|||
for(int i=0;i<26;++i){ |
|||
TEST_ASSERT_EQUAL_STRING(result_1[i],generateCheckString(numbers_1[i],*(strings_1+i))); |
|||
} |
|||
/*test block 2*/ |
|||
int numbers_2[] = {0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025}; |
|||
char *strings_2[] = {"z","zy","zyx","zyxw","zyxwv","zyxwvu","zyxwvut","zyxwvuts","zyxwvutsr","zyxwvutsrq","zyxwvutsrqp", |
|||
"zyxwvutsrqpo","zyxwvutsrqpon","zyxwvutsrqponm","zyxwvutsrqponml","zyxwvutsrqponmlk", |
|||
"zyxwvutsrqponmlkj","zyxwvutsrqponmlkji","zyxwvutsrqponmlkjih","zyxwvutsrqponmlkjihg","zyxwvutsrqponmlkjihgf", |
|||
"zyxwvutsrqponmlkjihgfe","zyxwvutsrqponmlkjihgfed","zyxwvutsrqponmlkjihgfedc","zyxwvutsrqponmlkjihgfedcb", |
|||
"zyxwvutsrqponmlkjihgfedcba"}; |
|||
char *result_2[] = {"0=z","1=zy","1=zyx","2=zyxw","3=zyxwv","5=zyxwvu","8=zyxwvut","13=zyxwvuts","21=zyxwvutsr","34=zyxwvutsrq", |
|||
"55=zyxwvutsrqp","89=zyxwvutsrqpo","144=zyxwvutsrqpon","233=zyxwvutsrqponm","377=zyxwvutsrqponml", |
|||
"610=zyxwvutsrqponmlk","987=zyxwvutsrqponmlkj","1597=zyxwvutsrqponmlkji","2584=zyxwvutsrqponmlkjih", |
|||
"4181=zyxwvutsrqponmlkjihg","6765=zyxwvutsrqponmlkjihgf","10946=zyxwvutsrqponmlkjihgfe", |
|||
"17711=zyxwvutsrqponmlkjihgfed","28657=zyxwvutsrqponmlkjihgfedc","46368=zyxwvutsrqponmlkjihgfedcb", |
|||
"75025=zyxwvutsrqponmlkjihgfedcba"}; |
|||
for(int i=0;i<26;++i){ |
|||
TEST_ASSERT_EQUAL_STRING(result_2[i],generateCheckString(numbers_2[i],*(strings_2+i))); |
|||
} |
|||
/*test block 3*/ |
|||
srand(time(0)); |
|||
int random_number=0; |
|||
char *random_numbers_strings[20]; |
|||
int random_numbers[20]; |
|||
for(int i=0;i<20;++i){ |
|||
random_number = (rand() % 100) + 1; |
|||
random_numbers_strings[i] = to_string(random_number); |
|||
random_numbers[i] = random_number; |
|||
} |
|||
char *strings_3[] = {"tree","plant","tea","programming","assembler","unix","BSD","snow","mountain","table","wood","forest", "calculator","book","light","keyboard","old","paper","pencil","voltage"}; |
|||
char *result_3[20]; |
|||
for(int i=0;i<20;++i){ |
|||
random_numbers_strings[i] = strcat(random_numbers_strings[i],"="); |
|||
result_3[i] = strcat(random_numbers_strings[i],strings_3[i]); |
|||
printf("%s\n",result_3[i]); |
|||
} |
|||
for(int i=0;i<20;++i){ |
|||
TEST_ASSERT_EQUAL_STRING(result_3[i],generateCheckString(random_numbers[i],strings_3[i])); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue