Browse Source

refactoring: prefixed input retrieval functions with user

remotes/origin/userinput
TheUltimateOptimist 11 months ago
parent
commit
7cf9f0d312
  1. 4
      src/add/main.c
  2. 22
      src/userinput.c
  3. 60
      src/userinput.h
  4. 56
      test/test_userinput.c

4
src/add/main.c

@ -4,7 +4,7 @@
int main() {
printf("add two numbers:\n");
int firstNum = getd("first number: ", NULL, NULL);
int secondNum = getd("second number: ", NULL, NULL);
int firstNum = usergetd("first number: ", NULL, NULL);
int secondNum = usergetd("second number: ", NULL, NULL);
printf("The result is: %d\n", add(firstNum, secondNum));
}

22
src/userinput.c

@ -97,7 +97,7 @@ char *readInput() {
return buffer;
}
char *gets(char *message, unsigned long *minLength, unsigned long *maxLength) {
char *usergets(char *message, unsigned long *minLength, unsigned long *maxLength) {
printText(message);
char *result = readInput();
while (minLength != NULL && strlen(result) < *minLength || maxLength != NULL && strlen(result) > *maxLength) {
@ -178,7 +178,7 @@ void printInvalidNumberMessage(long double value, long double *min, long double
long double getNumber(char *message, long double *min, long double *max, long double *greaterThan, long double *smallerThan, long double leftBorder, long double rightBorder) {
while (true) {
char *input = gets(message, NULL, NULL);
char *input = usergets(message, NULL, NULL);
if (strlen(input) == 0) {
printText("Fehlende Eingabe!\n");
free(input);
@ -199,55 +199,55 @@ long double getNumber(char *message, long double *min, long double *max, long do
}
}
short gethd(char *message, short *min, short *max) {
short usergethd(char *message, short *min, short *max) {
LD(min)
LD(max)
return getNumber(message, minld, maxld, NULL, NULL, SHRT_MIN, SHRT_MAX);
}
int getd(char *message, int *min, int *max) {
int usergetd(char *message, int *min, int *max) {
LD(min)
LD(max)
return getNumber(message, minld, maxld, NULL, NULL, INT_MIN, INT_MAX);
}
long getld(char *message, long *min, long *max) {
long usergetld(char *message, long *min, long *max) {
LD(min)
LD(max)
return getNumber(message, minld, maxld, NULL, NULL, LONG_MIN, LONG_MAX);
}
long long getlld(char *message, long long *min, long long *max) {
long long usergetlld(char *message, long long *min, long long *max) {
LD(min)
LD(max)
return getNumber(message, minld, maxld, NULL, NULL, LLONG_MIN, LLONG_MAX);
}
unsigned short gethu(char *message, unsigned short *min, unsigned short *max) {
unsigned short usergethu(char *message, unsigned short *min, unsigned short *max) {
LD(min)
LD(max)
return getNumber(message, minld, maxld, NULL, NULL, 0, USHRT_MAX);
}
unsigned int getu(char *message, unsigned int *min, unsigned int *max) {
unsigned int usergetu(char *message, unsigned int *min, unsigned int *max) {
LD(min)
LD(max)
return getNumber(message, minld, maxld, NULL, NULL, 0, UINT_MAX);
}
unsigned long getlu(char *message, unsigned long *min, unsigned long *max) {
unsigned long usergetlu(char *message, unsigned long *min, unsigned long *max) {
LD(min)
LD(max)
return getNumber(message, minld, maxld, NULL, NULL, 0, ULONG_MAX);
}
unsigned long long getllu(char *message, unsigned long long *min, unsigned long long *max) {
unsigned long long usergetllu(char *message, unsigned long long *min, unsigned long long *max) {
LD(min)
LD(max)
return getNumber(message, minld, maxld, NULL, NULL, 0, ULLONG_MAX);
}
double getlf(char *message, double *min, double *max, double *greaterThan, double *smallerThan) {
double usergetlf(char *message, double *min, double *max, double *greaterThan, double *smallerThan) {
LD(min)
LD(max)
LD(greaterThan)

60
src/userinput.h

@ -3,77 +3,77 @@
/// userinput.h provides functions for retrieving user input.
/// The functions are named get + placeholder used by printf for the given datatype.
/// string -> gets
/// short -> gethd
/// int -> getd
/// long -> getld
/// long long -> getlld
/// unsigned short -> gethu
/// unsigned int -> getu
/// unsigned long -> getlu
/// unsigned long long -> getllu
/// double -> getlf
/// string -> usergets
/// short -> usergethd
/// int -> usergetd
/// long -> usergetld
/// long long -> usergetlld
/// unsigned short -> usergethu
/// unsigned int -> usergetu
/// unsigned long -> usergetlu
/// unsigned long long -> usergetllu
/// double -> usergetlf
/// The provided functions handle validating the user input and ask again for input
/// with the given message until it is valid.
/// Using the given message gets asks the user to enter a string repeatedly
/// Using the given message usergets asks the user to enter a string repeatedly
/// until its length is between minLength and maxLength (both inclusive).
/// The obtained string is then returned.
/// If you don't want to enforce a minLength or a maxLength you can assign it to NULL.
char *gets(char *message, unsigned long *minLength, unsigned long *maxLength);
char *usergets(char *message, unsigned long *minLength, unsigned long *maxLength);
/// Using the given message gethd asks the user to enter a short repeatedly
/// Using the given message usergethd asks the user to enter a short repeatedly
/// until it is in the valid range for a short and between min and max (both inclusive) if provided.
/// The obtained short is then returned.
/// If you don't want to enforce a min or max you can assign it to NULL.
short gethd(char *message, short *min, short *max);
short usergethd(char *message, short *min, short *max);
/// Using the given message getd asks the user to enter an int repeatedly
/// Using the given message usergetd asks the user to enter an int repeatedly
/// until it is in the valid range for an int and between min and max (both inclusive) if provided.
/// The obtained int is then returned.
/// If you don't want to enforce a min or max you can assign it to NULL.
int getd(char *message, int *min, int *max);
int usergetd(char *message, int *min, int *max);
/// Using the given message getld asks the user to enter a long repeatedly
/// Using the given message usergetld asks the user to enter a long repeatedly
/// until it is in the valid range for a long and between min and max (both inclusive) if provided.
/// The obtained long is then returned.
/// If you don't want to enforce a min or max you can assign it to NULL.
long getld(char *message, long *min, long *max);
long usergetld(char *message, long *min, long *max);
/// Using the given message getlld asks the user to enter a long long repeatedly
/// Using the given message usergetlld asks the user to enter a long long repeatedly
/// until it is in the valid range for a long long and between min and max (both inclusive) if provided.
/// The obtained long is then returned.
/// If you don't want to enforce a min or max you can assign it to NULL.
long long getlld(char *message, long long *min, long long *max);
long long usergetlld(char *message, long long *min, long long *max);
/// Using the given message gethu asks the user to enter an unsigned short repeatedly
/// Using the given message usergethu asks the user to enter an unsigned short repeatedly
/// until it is in the valid range for an unsigned short and between min and max (both inclusive) if provided.
/// The obtained unsigned short is then returned.
/// If you don't want to enforce a min or max you can assign it to NULL.
unsigned short gethu(char *message, unsigned short *min, unsigned short *max);
unsigned short usergethu(char *message, unsigned short *min, unsigned short *max);
/// Using the given message getu asks the user to enter an unsigned int repeatedly
/// Using the given message usergetu asks the user to enter an unsigned int repeatedly
/// until it is in the valid range for an unsigned int and between min and max (both inclusive) if provided.
/// The obtained unsigned int is then returned.
/// If you don't want to enforce a min or max you can assign it to NULL.
unsigned int getu(char *message, unsigned int *min, unsigned int *max);
unsigned int usergetu(char *message, unsigned int *min, unsigned int *max);
/// Using the given message getlu asks the user to enter an unsigned long repeatedly
/// Using the given message usergetlu asks the user to enter an unsigned long repeatedly
/// until it is in the valid range for an unsigned long and between min and max (both inclusive) if provided.
/// The obtained unsigned long is then returned.
/// If you don't want to enforce a min or max you can assign it to NULL.
unsigned long getlu(char *message, unsigned long *min, unsigned long *max);
unsigned long usergetlu(char *message, unsigned long *min, unsigned long *max);
/// Using the given message getllu asks the user to enter an unsigned long long repeatedly
/// Using the given message usergetllu asks the user to enter an unsigned long long repeatedly
/// until it is in the valid range for an unsigned long long and between min and max (both inclusive) if provided.
/// The obtained unsigned long long is then returned.
/// If you don't want to enforce a min or max you can assign it to NULL.
unsigned long long getllu(char *message, unsigned long long *min, unsigned long long *max);
unsigned long long usergetllu(char *message, unsigned long long *min, unsigned long long *max);
/// Using the given message getlf asks the user to enter a double repeatedly
/// Using the given message usergetlf asks the user to enter a double repeatedly
/// until it is in the valid range for a double, between min and max (both inclusive),
/// greater than the given greaterThan and smaller than the given smallerThan.
/// The obtained double is then returned.
/// If you don't want to enforce a min, max, greaterThan or smallerThan you can assign them to NULL.
double getlf(char *message, double *min, double *max, double *greaterThan, double *smallerThan);
double usergetlf(char *message, double *min, double *max, double *greaterThan, double *smallerThan);
#endif

56
test/test_userinput.c

@ -6,112 +6,112 @@
void setUp(void){}
void tearDown(void){}
void test_gets_WithMinLengthAndMaxLength(void) {
void test_usergets_WithMinLengthAndMaxLength(void) {
unsigned long minLength = 3;
unsigned long maxLength = 10;
char *input[] = {"ei", "e", "sddfdfdfdfdf", "dddd", NULL};
fakeInput = input;
char *actual = gets("", &minLength, &maxLength);
char *actual = usergets("", &minLength, &maxLength);
TEST_ASSERT_EQUAL_CHAR_ARRAY("dddd", actual, strlen(actual));
}
void test_gets_WithMinLength(void) {
void test_usergets_WithMinLength(void) {
unsigned long minLength = 3;
char *input[] = {"", "ei", "e", "dddd", NULL};
fakeInput = input;
char *actual = gets("", &minLength, NULL);
char *actual = usergets("", &minLength, NULL);
TEST_ASSERT_EQUAL_CHAR_ARRAY("dddd", actual, strlen(actual));
}
void test_gets_WithMaxLength(void) {
void test_usergets_WithMaxLength(void) {
unsigned long maxLength = 6;
char *input[] = {"dfdfdfdf", "dddd", NULL};
fakeInput = input;
char *actual = gets("", NULL, &maxLength);
char *actual = usergets("", NULL, &maxLength);
TEST_ASSERT_EQUAL_CHAR_ARRAY("dddd", actual, strlen(actual));
}
void test_gets_WithAnyLength(void) {
void test_usergets_WithAnyLength(void) {
char *input[] = {"dfdfdfdf", NULL};
fakeInput = input;
char *actual = gets("", NULL, NULL);
char *actual = usergets("", NULL, NULL);
TEST_ASSERT_EQUAL_CHAR_ARRAY("dfdfdfdf", actual, strlen(actual));
}
void test_gethd_WithMinAndMax(void) {
void test_usergethd_WithMinAndMax(void) {
short min = 10;
short max = 100;
char *input[] = {"sdf", "1", "101", "50", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_INT64(50, gethd("", &min, &max));
TEST_ASSERT_EQUAL_INT64(50, usergethd("", &min, &max));
}
void test_getd_WithMinAndMax(void) {
void test_usergetd_WithMinAndMax(void) {
int min = 10;
int max = 100000;
char *input[] = {"sdf", "4", "10000000", "1167", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_INT64(1167, getd("", &min, &max));
TEST_ASSERT_EQUAL_INT64(1167, usergetd("", &min, &max));
}
void test_getd_WithMin(void) {
void test_usergetd_WithMin(void) {
int min = 10;
char *input[] = {"sdf", "4", "10000000", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_INT64(10000000, getd("", &min, NULL));
TEST_ASSERT_EQUAL_INT64(10000000, usergetd("", &min, NULL));
}
void test_getd_WithMax(void) {
void test_usergetd_WithMax(void) {
int max = 1000;
char *input[] = {"sdf", "4000", "10000000", "-200", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_INT64(-200, getd("", NULL, &max));
TEST_ASSERT_EQUAL_INT64(-200, usergetd("", NULL, &max));
}
void test_getld_WithMinAndMax(void) {
void test_usergetld_WithMinAndMax(void) {
long min = -100;
long max = 100000000;
char *input[] = {"sdf", "-400", "10000000000000", "11001100", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_INT64(11001100, getld("", &min, &max));
TEST_ASSERT_EQUAL_INT64(11001100, usergetld("", &min, &max));
}
void test_getlld_WithMinAndMax(void) {
void test_usergetlld_WithMinAndMax(void) {
long long min = -100;
long long max = 100000000;
char *input[] = {"sdf", "-400", "10000000000000", "11001100", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_INT64(11001100, getlld("", &min, &max));
TEST_ASSERT_EQUAL_INT64(11001100, usergetlld("", &min, &max));
}
void test_gethu_WithMinAndMax(void) {
void test_usergethu_WithMinAndMax(void) {
unsigned short min = 10;
unsigned short max = 100;
char *input[] = {"sdf", "-1", "1", "101", "50", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_UINT64(50, gethu("", &min, &max));
TEST_ASSERT_EQUAL_UINT64(50, usergethu("", &min, &max));
}
void test_getu_WithMinAndMax(void) {
void test_usergetu_WithMinAndMax(void) {
unsigned int min = 10;
unsigned int max = 100000;
char *input[] = {"sdf", "4", "10000000", "1167", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_UINT64(1167, getu("", &min, &max));
TEST_ASSERT_EQUAL_UINT64(1167, usergetu("", &min, &max));
}
void test_getlu_WithMinAndMax(void) {
void test_usergetlu_WithMinAndMax(void) {
unsigned long min = 100;
unsigned long max = 100000000;
char *input[] = {"sdf", "-400", "10000000000000", "11001100", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_UINT64(11001100, getlu("", &min, &max));
TEST_ASSERT_EQUAL_UINT64(11001100, usergetlu("", &min, &max));
}
void test_getllu_WithMinAndMax(void) {
void test_usergetllu_WithMinAndMax(void) {
unsigned long long min = 100;
unsigned long long max = 100000000;
char *input[] = {"sdf", "-400", "10000000000000", "11001100", NULL};
fakeInput = input;
TEST_ASSERT_EQUAL_UINT64(11001100, getllu("", &min, &max));
TEST_ASSERT_EQUAL_UINT64(11001100, usergetllu("", &min, &max));
}
Loading…
Cancel
Save