Browse Source
implement unit tests for the functions agePermission() and checkIfInteger()
remotes/origin/feature/the-main-menu
implement unit tests for the functions agePermission() and checkIfInteger()
remotes/origin/feature/the-main-menu
fdai7207
2 years ago
7 changed files with 303 additions and 3 deletions
-
5build-project.sh
-
101project.yml
-
2src/main.c
-
42src/mainMenu.c
-
13src/mainMenu.h
-
0test/support/.gitkeep
-
143test/test_mainMenu.c
@ -1,5 +1,6 @@ |
|||
clear |
|||
ceedling test:all |
|||
cd src/ |
|||
gcc main.c |
|||
gcc main.c mainMenu.c |
|||
./a.out |
|||
rm a.out |
|||
rm a.out |
@ -0,0 +1,101 @@ |
|||
--- |
|||
|
|||
# Notes: |
|||
# Sample project C code is not presently written to produce a release artifact. |
|||
# As such, release build options are disabled. |
|||
# This sample, therefore, only demonstrates running a collection of unit tests. |
|||
|
|||
:project: |
|||
:use_exceptions: FALSE |
|||
:use_test_preprocessor: TRUE |
|||
:use_auxiliary_dependencies: TRUE |
|||
:build_root: build |
|||
# :release_build: TRUE |
|||
:test_file_prefix: test_ |
|||
:which_ceedling: gem |
|||
:ceedling_version: 0.31.1 |
|||
:default_tasks: |
|||
- test:all |
|||
|
|||
#:test_build: |
|||
# :use_assembly: TRUE |
|||
|
|||
#:release_build: |
|||
# :output: MyApp.out |
|||
# :use_assembly: FALSE |
|||
|
|||
:environment: |
|||
|
|||
:extension: |
|||
:executable: .out |
|||
|
|||
:paths: |
|||
:test: |
|||
- +:test/** |
|||
- -:test/support |
|||
:source: |
|||
- src/** |
|||
:support: |
|||
- test/support |
|||
:libraries: [] |
|||
|
|||
:defines: |
|||
# in order to add common defines: |
|||
# 1) remove the trailing [] from the :common: section |
|||
# 2) add entries to the :common: section (e.g. :test: has TEST defined) |
|||
:common: &common_defines [] |
|||
:test: |
|||
- *common_defines |
|||
- TEST |
|||
:test_preprocess: |
|||
- *common_defines |
|||
- TEST |
|||
|
|||
:cmock: |
|||
:mock_prefix: mock_ |
|||
:when_no_prototypes: :warn |
|||
:enforce_strict_ordering: TRUE |
|||
:plugins: |
|||
- :ignore |
|||
- :callback |
|||
:treat_as: |
|||
uint8: HEX8 |
|||
uint16: HEX16 |
|||
uint32: UINT32 |
|||
int8: INT8 |
|||
bool: UINT8 |
|||
|
|||
# Add -gcov to the plugins list to make sure of the gcov plugin |
|||
# You will need to have gcov and gcovr both installed to make it work. |
|||
# For more information on these options, see docs in plugins/gcov |
|||
:gcov: |
|||
:reports: |
|||
- HtmlDetailed |
|||
:gcovr: |
|||
:html_medium_threshold: 75 |
|||
:html_high_threshold: 90 |
|||
|
|||
#:tools: |
|||
# Ceedling defaults to using gcc for compiling, linking, etc. |
|||
# As [:tools] is blank, gcc will be used (so long as it's in your system path) |
|||
# See documentation to configure a given toolchain for use |
|||
|
|||
# LIBRARIES |
|||
# These libraries are automatically injected into the build process. Those specified as |
|||
# common will be used in all types of builds. Otherwise, libraries can be injected in just |
|||
# tests or releases. These options are MERGED with the options in supplemental yaml files. |
|||
:libraries: |
|||
:placement: :end |
|||
:flag: "-l${1}" |
|||
:path_flag: "-L ${1}" |
|||
:system: [] # for example, you might list 'm' to grab the math library |
|||
:test: [] |
|||
:release: [] |
|||
|
|||
:plugins: |
|||
:load_paths: |
|||
- "#{Ceedling.load_path}" |
|||
:enabled: |
|||
- stdout_pretty_tests_report |
|||
- module_generator |
|||
... |
@ -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; |
|||
|
|||
} |
|||
} |
|||
|
@ -0,0 +1,13 @@ |
|||
#ifndef MAINMENU_H_ |
|||
#define MAINMENU_H_ |
|||
|
|||
#include<stdio.h> |
|||
#include<stdlib.h> |
|||
#include<stdbool.h> |
|||
#include<string.h> |
|||
|
|||
bool agePermission(int age); |
|||
bool checkIfInteger(char* userInput); |
|||
|
|||
#endif |
|||
|
@ -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 |
Write
Preview
Loading…
Cancel
Save
Reference in new issue