[gtest] First commit of gtest, and first UT for SaveStorageValue() function

* Include README.md with some basic information about preparing the setup
* UT for SaveStorageValue() function
This commit is contained in:
danimartin82 2020-03-27 13:30:36 +01:00
parent a16cf8c140
commit 45776a651c
5 changed files with 187 additions and 0 deletions

66
gtest/README.md Normal file
View File

@ -0,0 +1,66 @@
## Unit test for raylib using Google Test
We are going to use the Open source Tool Google Test for generating and launching Unit Tests. Please consider reading some information before start:
* https://en.wikipedia.org/wiki/Unit_testing
* https://github.com/google/googletest
* https://github.com/google/googletest/blob/master/googletest/docs/primer.md
## Steps for preparing the Google Test environment for Raylib
1) Intall **MinGW** if it's not already installed.
2) Install **Cmake** from: https://cmake.org/download/
3) Clone **Google Test** repository https://github.com/google/googletest
The expected download directory for google test is:
`C:\GitHub\googletest`
If a different one is choosen, please update the path in the bat file.
4) Create a directory for the build:
`C:\GitHub\googletest\mybuild`
5) Configure and generate with CMake, as describe in image
![Cmake configuration](docs\configure_and_generate_with_cmake.png "Cmake configuration")
6) Compile google test, using **make**:
`C:\GitHub\googletest\mybuild>mingw32-make`
## Compile and run unit test ##
The raylib library contains a folder called gtest with the implemented Unit Test, and a batch file for its compilation and execution.
**IMPORTANT**:
* The batch file contains the absolute paths to the Google test static library compiled in the previous steps. If the paths are different, please update them.
* As we are going to test **raylib** and its included in the compilation via `-lraylib` sure that MINGGW contains your last compilation of raylib, so you are testing the latest changes in raylib. Copy libraylib.a from `raylib\src` to `C:\MinGW\i686-w64-mingw32\lib`
* The compilation of the Unit Test has not the option `-mwindows` so al the **TRACELOG** from raylib will be seen on the console.
For compiling and test execution run the bach file for one unit test file:
`C:\GitHub\raylib\gtest>raylib_compile_gtest.bat core_ut.cpp`
The result shall looks something like this:
![Example execution gtest](docs\example_execution_gtest.png "Example execution gtest")

81
gtest/core_ut.cpp Normal file
View File

@ -0,0 +1,81 @@
#include "gtest/gtest.h"
#include "raylib.h"
// We expect storing and loading OK in position 0
TEST(SaveStorageValue, test_basic_save_load) {
int in=19;
int out=0;
int position=0;
SaveStorageValue(position,in);
out=LoadStorageValue(position);
EXPECT_EQ (in,out);
}
// We expect to create file storage.data and save/load OK
TEST(SaveStorageValue, create_storage_data_file) {
int in=19;
int out=0;
int position=0;
system("del storage.data");
SaveStorageValue(position,in);
out=LoadStorageValue(position);
EXPECT_EQ (in,out);
}
// We expect to create and increase the size of the file storage.data save/load OK
TEST(SaveStorageValue, increase_storage_data_file) {
int in=19;
int out=0;
int position=0;
system("del storage.data");
SaveStorageValue(position,in);
out=LoadStorageValue(position);
EXPECT_EQ (in,out);
position=23;
SaveStorageValue(position,in);
out=LoadStorageValue(position);
EXPECT_EQ (in,out);
}
// We expect failure in SaveStorageValue, and so save is not performed. Load gives different data than save
TEST(SaveStorageValue, test_negative_position) {
int in=19;
int out=0;
int position=-1;
SaveStorageValue(position,in);
out=LoadStorageValue(position);
EXPECT_NE (in,out);
}
// We expect a failure in SaveStorageValue, and so save is not performed. Load gives different data than save
TEST(SaveStorageValue, fail_realloc_in_SaveStorageValue) {
int in=19;
int out=0;
int position=INT_MAX;
SaveStorageValue(position,in);
out=LoadStorageValue(position);
EXPECT_NE (in,out);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

View File

@ -0,0 +1,40 @@
::@echo off
:: .
:: Compile your test using : raylib_compile_gtest.bat core_ut.cpp
:: .
:: > Setup required Environment
:: -------------------------------------
set GTEST_INCLUDE_DIR=C:\GitHub\googletest\googletest\include\
set GTEST_LIB=C:\GitHub\googletest\mybuild\lib\libgtest.a
set GTEST_LIB_MAIN=C:\GitHub\googletest\mybuild\lib\libgtest_main.a
:: Get full filename path for input file %1
set FILENAME=%~f1
set NAMEPART=%FILENAME:~0,-4%
cd %~dp0
:: .
:: > Cleaning latest build
:: ---------------------------
cmd /c if exist %NAMEPART%.exe del /F %NAMEPART%.exe
:: > Compiling program
:: --------------------------
g++ -c %NAMEPART%.cpp -I%GTEST_INCLUDE_DIR%
:: .
:: > Linking program
:: --------------------------
:: -s : Remove all symbol table and relocation information from the executable
:: -O2 : Optimization Level 2, this option increases both compilation time and the performance of the generated code
:: -std=c99 : Use C99 language standard
:: -Wall : Enable all compilation Warnings
g++ -pthread -o %NAMEPART%.exe %NAMEPART%.o %GTEST_LIB% %GTEST_LIB_MAIN% -lraylib -lopengl32 -lgdi32 -lwinmm -std=c99 -Wall
:: .
:: > Executing program
:: -------------------------
cmd /c if exist %NAMEPART%.exe %NAMEPART%.exe