From 328eee9f8547259f7dc95779ce8150f4b016bde5 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Sat, 28 Jan 2023 21:38:46 +0100 Subject: [PATCH] implement unit tests for the functions agePermission() and checkIfInteger() --- build-project.sh | 3 +- src/main.c | 9 +- src/mainMenu.c | 42 ++++++++++ src/mainMenu.h | 13 +++ tests/test_StringManipulation.c | 2 +- tests/test_mainMenu.c | 143 ++++++++++++++++++++++++++++++++ 6 files changed, 205 insertions(+), 7 deletions(-) create mode 100644 src/mainMenu.c create mode 100644 src/mainMenu.h create mode 100644 tests/test_mainMenu.c diff --git a/build-project.sh b/build-project.sh index e044018..420b964 100755 --- a/build-project.sh +++ b/build-project.sh @@ -1,5 +1,6 @@ +clear ceedling test:all cd src/ -gcc main.c -o main +gcc main.c mainMenu.c -o main ./main rm main diff --git a/src/main.c b/src/main.c index 2e0147f..3f845e6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,6 @@ #include -int main() -{ - - return 0; -} +int main() { + + return 0; +} diff --git a/src/mainMenu.c b/src/mainMenu.c new file mode 100644 index 0000000..54d20ad --- /dev/null +++ b/src/mainMenu.c @@ -0,0 +1,42 @@ +#include"mainMenu.h" + + +bool agePermission(int age){ + + if(age >= 18) + { + + return true; + + } + + else + { + + return false; + + } + +} + +bool checkIfInteger(char* input){ + + char *end_pointer; + + strtol(input, &end_pointer, 10); + + if (end_pointer == input || *end_pointer != '\0') + { + + return false; + + } + + else + { + + return true; + + } +} + diff --git a/src/mainMenu.h b/src/mainMenu.h new file mode 100644 index 0000000..61192be --- /dev/null +++ b/src/mainMenu.h @@ -0,0 +1,13 @@ +#ifndef MAINMENU_H_ +#define MAINMENU_H_ + +#include +#include +#include +#include + +bool agePermission(int age); +bool checkIfInteger(char* userInput); + +#endif + diff --git a/tests/test_StringManipulation.c b/tests/test_StringManipulation.c index accd414..ecae147 100644 --- a/tests/test_StringManipulation.c +++ b/tests/test_StringManipulation.c @@ -75,7 +75,7 @@ void test_generateCheckString() 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]); + //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])); diff --git a/tests/test_mainMenu.c b/tests/test_mainMenu.c new file mode 100644 index 0000000..515237d --- /dev/null +++ b/tests/test_mainMenu.c @@ -0,0 +1,143 @@ +#ifdef TEST + +#include "unity.h" + +#include "mainMenu.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} +void test_agePermissionValidAge(void) +{ + //Test case : 0 + + //Arrange + + int validAge[83]; + + bool validAgeResult[83]; + + int j=0; + + for(int i =18;i<101;i++){ + + validAge[j]= i; + j++; + } + + //Act + + for(int i=0;i<83;i++){ + + validAgeResult[i] = agePermission(validAge[i]); + } + + //Assert + + for(int i=0;i<83;i++){ + + TEST_ASSERT_TRUE(validAgeResult[i]); + + } + +} + +void test_agePermissionInvalidAge(void) +{ + + //Test case : 1 + + //Arrange + + int invalidAge[117]; + + bool invalidAgeResult[117]; + + int j=0; + + for(int i =-100;i<18;i++){ + + invalidAge[j]= i; + j++; + } + + //Act + + for(int i=0;i<117;i++){ + + invalidAgeResult[i] = agePermission(invalidAge[i]); + + } + + //Assert + + for(int i=0;i<117;i++){ + + TEST_ASSERT_FALSE(invalidAgeResult[i]); + + } + +} + +void test_IsInteger(void) +{ + + //Arrange + + char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; + + bool inputIsIntegerResult[19]; + + //Act + + for(int i=0;i<19;i++) + { + + inputIsIntegerResult[i] = checkIfInteger(inputIsInteger[i]); + + } + + //Assert + + for(int i=0;i<19;i++) + { + + TEST_ASSERT_TRUE(inputIsIntegerResult[i]); + + } + +} + +void test_IsNotInteger(void) +{ + + //Arrange + + char* inputIsNotInteger[] = {"0.15","3.141592653589793238","5.3254f","-6.264","-7878.3261","foo","Bar","FIZZ","buzZ","joHN","jAnE","foo-bar","3,15","2k13",""," ","-","+","/*-+.,/=" }; + + bool inputIsNotIntegerResult[19]; + + //Act + + for(int i=0;i<19;i++) + { + + inputIsNotIntegerResult[i] = checkIfInteger(inputIsNotInteger[i]); + + } + + //Assert + + for(int i=0;i<19;i++) + { + + TEST_ASSERT_FALSE(inputIsNotIntegerResult[i]); + + } + +} +#endif // TEST