Last active
May 22, 2019 17:35
-
-
Save caiorss/eb2fc15b7ed322ccac5ee496585e54e9 to your computer and use it in GitHub Desktop.
Example - sample project with CMake and Google Test Gtest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# This .gitignore file was automatically created by Microsoft(R) Visual Studio. | |
################################################################################ | |
/.vs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cmake_minimum_required(VERSION 3.9) | |
project( GtestsExperiment | |
VERSION 0.1 | |
DESCRIPTION "Experiment with Gtest uni testing framework" | |
) | |
#============= User Functions ================================# | |
# Copy target file to current directory whenerver it is rebuilt | |
function(copy_after_build TARGET_NAME ) | |
# Note: CMAKE_CURRENT_LIST_DIR is the directory where is this | |
# CMakeLists.txt file. | |
set(DESTDIR ${CMAKE_CURRENT_LIST_DIR}/bin/) | |
file(MAKE_DIRECTORY ${DESTDIR}) | |
# Copy binary file to <CMakeLists.txt didctory>./bin | |
# after target is compiled. | |
add_custom_command(TARGET ${TARGET_NAME} POST_BUILD | |
COMMAND ${CMAKE_COMMAND} -E copy | |
$<TARGET_FILE:${TARGET_NAME}> ${DESTDIR} | |
) | |
endfunction() | |
#============= Find Packages ================================# | |
# Download automatically, you can also just copy the conan.cmake file | |
if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake") | |
message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan") | |
file(DOWNLOAD "https://github.com/conan-io/cmake-conan/raw/v0.13/conan.cmake" | |
"${CMAKE_BINARY_DIR}/conan.cmake") | |
endif() | |
include(${CMAKE_BINARY_DIR}/conan.cmake) | |
conan_cmake_run(REQUIRES | |
gtest/1.8.1@bincrafters/stable | |
BASIC_SETUP | |
BUILD missing | |
) | |
include(CTest) | |
find_package(GTest REQUIRED) | |
#---------------------------------------------------# | |
# Targets Settings # | |
#---------------------------------------------------# | |
add_executable(gtest-experiment gtest-experiment.cpp) | |
target_link_libraries(gtest-experiment GTest::GTest GTest::Main) | |
add_test(GTestExperiment gtest-experiment) | |
copy_after_build(gtest-experiment) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <cassert> | |
#include <gtest/gtest.h> | |
struct InitHook { | |
~InitHook() | |
{ | |
std::cout << "Enter RETURN to exit" << std::endl; | |
std::cin.get(); | |
} | |
}; | |
#ifdef _WIN32 | |
// InitHook inithook; | |
#endif | |
// ==================== Implementation ======================== | |
int factorial(int n){ | |
int prod = 1; | |
for(int i = 1; i <= n; i++) | |
prod *= i; | |
return prod; | |
} | |
struct Date | |
{ | |
int year; | |
int month; | |
int day; | |
Date(){} | |
Date(int year, int month, int day) | |
: year(year) | |
, month(month) | |
, day(day) | |
{ } | |
// Comparison operator required by EXPECT_EQ | |
bool operator==(Date const& rhs) const | |
{ | |
return year == rhs.year | |
&& month == rhs.month | |
&& day == rhs.day; | |
} | |
// Necessary for make class printable in GTest | |
friend std::ostream& operator<<(std::ostream& os, Date const& rhs) | |
{ | |
return os << "Date { " << rhs.year << " ; " | |
<< rhs.month << " ; " | |
<< rhs.day << " } "; | |
} | |
}; | |
Date GregorianEasterSunday(int y) | |
{ | |
int c = y / 100; | |
int n = y - 19 * ( y / 19 ); | |
int k = ( c - 17 ) / 25; | |
int i = c - c / 4 - ( c - k ) / 3 + 19 * n + 15; | |
i = i - 30 * ( i / 30 ); | |
i = i - ( i / 28 ) * ( 1 - ( i / 28 ) | |
* ( 29 / ( i + 1 ) ) | |
* ( ( 21 - n ) / 11 ) ); | |
int j = y + y / 4 + i + 2 - c + c / 4; | |
j = j - 7 * ( j / 7 ); | |
int l = i - j; | |
int m = 3 + ( l + 40 ) / 44; | |
int d = l + 28 - 31 * ( m / 4 ); | |
return Date(y, m, d); | |
} | |
//=============== Tests ====================================// | |
TEST(FactorialTest, test1){ | |
EXPECT_EQ(6, factorial(3)); | |
EXPECT_EQ(24, factorial(4)); | |
EXPECT_EQ(120, factorial(5)); | |
EXPECT_EQ(3628800, factorial(10)); | |
// Expect greater than | |
EXPECT_GT(10000000, factorial(10)); | |
// Expect not equal | |
EXPECT_NE(25, factorial(4)); | |
} | |
TEST(FactorialTestFailure, testFailure){ | |
// Deliberately fails for demonstration purposes | |
EXPECT_EQ(6, factorial(3)); | |
EXPECT_EQ(4, factorial(4)); | |
EXPECT_EQ(6, factorial(2)); | |
} | |
TEST(GregorianEaster, testdates){ | |
EXPECT_EQ(Date(2005, 3, 27), GregorianEasterSunday(2005)); | |
EXPECT_EQ(Date(2008, 3, 23), GregorianEasterSunday(2008)); | |
EXPECT_EQ(Date(2010, 4, 4), GregorianEasterSunday(2010)); | |
// Failure | |
EXPECT_EQ(Date(2010, 14, 4), GregorianEasterSunday(2010)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment