From b9992a63adb71a6f4f60c922f03abdd92708fcc0 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Fri, 10 Feb 2023 12:34:04 +0100 Subject: [PATCH 1/3] Add feature to check eligibility for loan --- src/checkLoanEligibility.c | 30 ++++++++++++++++++++++++++++++ src/checkLoanEligibility.h | 10 ++++++++++ 2 files changed, 40 insertions(+) create mode 100644 src/checkLoanEligibility.c create mode 100644 src/checkLoanEligibility.h 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 From eb5fe068b8078daecaf5658c2717147e0626f875 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Fri, 10 Feb 2023 12:34:39 +0100 Subject: [PATCH 2/3] Add success test for loan eligibility --- tests/test_checkLoanEligibility.c | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/test_checkLoanEligibility.c diff --git a/tests/test_checkLoanEligibility.c b/tests/test_checkLoanEligibility.c new file mode 100644 index 0000000..b14ca3d --- /dev/null +++ b/tests/test_checkLoanEligibility.c @@ -0,0 +1,36 @@ +#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 + } +} + + +#endif // TEST \ No newline at end of file From b9a28f43b3c42e78e35d26467688d5ccee4c6e09 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Fri, 10 Feb 2023 12:37:32 +0100 Subject: [PATCH 3/3] Add failure test for loan eligibility --- tests/test_checkLoanEligibility.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_checkLoanEligibility.c b/tests/test_checkLoanEligibility.c index b14ca3d..4107a62 100644 --- a/tests/test_checkLoanEligibility.c +++ b/tests/test_checkLoanEligibility.c @@ -32,5 +32,26 @@ void test_checkLoanEligibilitySuccess(void) { } } +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