Browse Source

Merge branch 'feature/loan-eligibility' into feature/balance-operations

remotes/origin/feature/balance-operations
Shivam Chaudhary 2 years ago
parent
commit
721d084e14
  1. 30
      src/checkLoanEligibility.c
  2. 10
      src/checkLoanEligibility.h
  3. 57
      tests/test_checkLoanEligibility.c

30
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;
}

10
src/checkLoanEligibility.h

@ -0,0 +1,10 @@
#ifndef CHECKLOANELIGIBILITY_H_
#define CHECKLOANELIGIBILITY_H_
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
bool checkLoanEligibility(int user_id);
#endif

57
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
Loading…
Cancel
Save