diff --git a/src/checkLoanEligibility.c b/src/checkLoanEligibility.c new file mode 100644 index 0000000..e3ba64c --- /dev/null +++ b/src/checkLoanEligibility.c @@ -0,0 +1,30 @@ +#include "checkLoanEligibility.h" +#include "_file_information.h" + +bool checkLoanEligibility(int user_id) { + + // Eligible only when user is present in CustomerDataFile + + bool keep_reading = true; + bool eligibility = false; + char buffer[MAX_LENGTH]; + + FILE *file = fopen(CUSTOMER_DATA_FILE, "r"); + + while(keep_reading) { + fgets(buffer, MAX_LENGTH, file); + + if (feof(file)) { + keep_reading = false; + } + if(user_id == atoi(buffer)) { + eligibility = true; + keep_reading = false; + } + + } + + fclose(file); + return eligibility; + +} \ No newline at end of file diff --git a/src/checkLoanEligibility.h b/src/checkLoanEligibility.h new file mode 100644 index 0000000..4415c60 --- /dev/null +++ b/src/checkLoanEligibility.h @@ -0,0 +1,10 @@ +#ifndef CHECKLOANELIGIBILITY_H_ +#define CHECKLOANELIGIBILITY_H_ + +#include +#include +#include + +bool checkLoanEligibility(int user_id); + +#endif \ No newline at end of file diff --git a/tests/test_checkLoanEligibility.c b/tests/test_checkLoanEligibility.c new file mode 100644 index 0000000..4107a62 --- /dev/null +++ b/tests/test_checkLoanEligibility.c @@ -0,0 +1,57 @@ +#ifdef TEST + +#include "unity.h" +#include "checkLoanEligibility.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_checkLoanEligibilitySuccess(void) { + + /* Arrange */ + + int user_id[3] = {1234, 1327, 1666}; // user_ids from file for testing + + bool result[3]; + + /* Act */ + + for (int i = 0; i < 3; i++) { + result[i] = checkLoanEligibility(user_id[i]); + } + + /* Assert */ + + for (int i = 0; i < 3; i++) { + TEST_ASSERT_TRUE(result[i]); // Pass if user_id is found in the file + } +} + +void test_checkLoanEligibilityFailure(void) { + + /* Arrange */ + + int user_id[3] = {12314, 127, 166}; // Random wrong user_ids + + bool result[3]; + + /* Act */ + + for (int i = 0; i < 3; i++) { + result[i] = checkLoanEligibility(user_id[i]); + } + + /* Assert */ + + for (int i = 0; i < 3; i++) { + TEST_ASSERT_FALSE(result[i]); // Pass if the returned result is false + } +} + + +#endif // TEST \ No newline at end of file