From 839c9aa5115c259f1560e6b68afa6ca1ce87cb49 Mon Sep 17 00:00:00 2001 From: fdai7207 Date: Sun, 29 Jan 2023 01:08:46 +0100 Subject: [PATCH] refactoring: removed redundant code in the unit tests to improve readability and maintainability. --- tests/test_mainMenu.c | 142 +++++++++++++++++++----------------------- 1 file changed, 64 insertions(+), 78 deletions(-) diff --git a/tests/test_mainMenu.c b/tests/test_mainMenu.c index 515237d..eb93bb7 100644 --- a/tests/test_mainMenu.c +++ b/tests/test_mainMenu.c @@ -6,138 +6,124 @@ void setUp(void) { + } void tearDown(void) { + } + void test_agePermissionValidAge(void) { //Test case : 0 - //Arrange - - int validAge[83]; - - bool validAgeResult[83]; + /*Arrange*/ - int j=0; + int Age = 18; - for(int i =18;i<101;i++){ + bool validAgeResult[83]; - validAge[j]= i; - j++; - } - - //Act + /*Act*/ - for(int i=0;i<83;i++){ - - validAgeResult[i] = agePermission(validAge[i]); + for(int i =0;i<83;i++){ + + validAgeResult[i]= Age + i; } - - //Assert + + + /*Assert*/ for(int i=0;i<83;i++){ - - TEST_ASSERT_TRUE(validAgeResult[i]); - - } + TEST_ASSERT_TRUE(validAgeResult[i]); + + } + } void test_agePermissionInvalidAge(void) { //Test case : 1 - - //Arrange - + + /*Arrange*/ + int invalidAge[117]; - + bool invalidAgeResult[117]; - - int j=0; - - for(int i =-100;i<18;i++){ - - invalidAge[j]= i; - j++; + + + for(int i =-100;i<18;i++) + { + + invalidAge[i+100]= i; } - - //Act - - for(int i=0;i<117;i++){ - + + /*Act*/ + + for(int i=0;i<117;i++) + { + invalidAgeResult[i] = agePermission(invalidAge[i]); } - - //Assert - - for(int i=0;i<117;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]; + //test case 0 - //Act - - for(int i=0;i<19;i++) - { + /*Arrange*/ - inputIsIntegerResult[i] = checkIfInteger(inputIsInteger[i]); - - } + char* inputIsInteger[] = {"-10000000","-2000000","-354698","-66667","-7878","-987","-64","-5","0","1","2","10","201","333","4321","56974","698751","7878989","88954621" }; + + bool inputIsIntegerExpected = true; - //Assert + /*Act and Assert*/ for(int i=0;i<19;i++) { + bool inputIsIntegerResult = checkIfInteger(inputIsInteger[i]); - TEST_ASSERT_TRUE(inputIsIntegerResult[i]); + TEST_ASSERT_EQUAL(inputIsIntegerExpected,inputIsIntegerResult); - } + } } void test_IsNotInteger(void) { + //test case 1 + + /*Arrange*/ - //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 - + + bool inputIsNotIntegerExpected = false; + + /*Act and Assert*/ + + for(int i=0;i<19;i++) { - - TEST_ASSERT_FALSE(inputIsNotIntegerResult[i]); - + bool inputIsNotIntegerResult = checkIfInteger(inputIsNotInteger[i]); + + TEST_ASSERT_EQUAL(inputIsNotIntegerExpected,inputIsNotIntegerResult); + } + } #endif // TEST