Skip to content

Commit

Permalink
add gtest mini project
Browse files Browse the repository at this point in the history
  • Loading branch information
fangshuai03 committed Jan 19, 2024
1 parent 92f0b16 commit a9bf5fe
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 0 deletions.
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# CMake generated files
CMakeCache.txt
CMakeFiles
cmake_install.cmake
Makefile
*.make
*.cmake

# Build artifacts
build/
Release/
Debug/
*.o
*.a
*.so
*.dylib
*.lib
*.dll
*.exe
*.out

# Dependency directories (comment out if dependencies are tracked)
# libs/

# Editor backups and temporary files
*~
*.swp
*.tmp

# IDE specific files
.vs/
.vscode/
*.code-workspace
*.user
*.suo
*.userosscache
*.sln
*.opensdf
*.ipch
*.db
*.opendb
*.VC.db
*.VC.VC.opendb
_deps/


# Other files to ignore
.DS_Store
Thumbs.db
11 changes: 11 additions & 0 deletions c++/gtest_mini_project/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.10)
project(your_project)

set(CMAKE_CXX_STANDARD 11)

# 添加src目录
add_subdirectory(src)

# 启用测试
enable_testing()
add_subdirectory(tests)
8 changes: 8 additions & 0 deletions c++/gtest_mini_project/CTestTestfile.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# CMake generated Testfile for
# Source directory: /Users/fangshuai03/mini_project/c++/gtest_mini_project
# Build directory: /Users/fangshuai03/mini_project/c++/gtest_mini_project
#
# This file includes the relevant testing commands required for
# testing this directory and lists subdirectories to be tested as well.
subdirs("src")
subdirs("tests")
Empty file.
5 changes: 5 additions & 0 deletions c++/gtest_mini_project/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 创建一个库
add_library(my_even my_even.cpp)

# 指定这个库的头文件路径
target_include_directories(my_even PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
5 changes: 5 additions & 0 deletions c++/gtest_mini_project/src/my_even.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "my_even.h"

bool is_even(int num) {
return (num & 1) == 0;
}
6 changes: 6 additions & 0 deletions c++/gtest_mini_project/src/my_even.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef MY_EVEN_HPP
#define MY_EVEN_HPP

bool is_even(int number);

#endif
17 changes: 17 additions & 0 deletions c++/gtest_mini_project/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 下载和解压 Google Test
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/release-1.10.0.zip
)
FetchContent_MakeAvailable(googletest)

# 添加 Google Test 测试可执行文件
add_executable(test_demo test_demo.cpp)

# 链接测试执行文件与库
target_link_libraries(test_demo gtest_main my_even)

# 包含测试
include(GoogleTest)
gtest_discover_tests(test_demo)
Binary file added c++/gtest_mini_project/tests/test_demo
Binary file not shown.
8 changes: 8 additions & 0 deletions c++/gtest_mini_project/tests/test_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

#include "../src/my_even.h"
#include <gtest/gtest.h>

TEST(YourTest, IsEven) {
EXPECT_TRUE(is_even(2));
EXPECT_FALSE(is_even(3));
}

0 comments on commit a9bf5fe

Please sign in to comment.