Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gtest setup #30

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*.fls
*.fdb_latexmk
.vscode/
build/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "googletest"]
path = googletest
url = https://github.com/google/googletest
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.13)

project(booklet LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include(CTest)
enable_testing()

add_subdirectory(tests)
add_subdirectory(googletest)
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ all:
mv main.pdf "$(PDFNAME)"

clean:
-rm main.aux main.log main.out main.toc
-rm -r main.aux main.log main.out main.toc build

distclean: clean
-rm "$(PDFNAME)"

print-todos:
grep --color '\\todo{' main.tex content/*

test:
mkdir -p build
cd build && cmake .. && make -j8 && ./tests/tests
1 change: 1 addition & 0 deletions googletest
Submodule googletest added at 6b63c9
28 changes: 28 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.13)

include(GoogleTest)

set(TEST_SOURCES
test_fenwick.cpp
test_mod_inverse.cpp
)

add_executable(tests
${TEST_SOURCES}
)

target_include_directories(tests
PUBLIC
../source
../googletest/include
)

set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

target_link_libraries(tests
PUBLIC
gtest
gtest_main
)

gtest_discover_tests(tests)
10 changes: 10 additions & 0 deletions tests/test_fenwick.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "gtest/gtest.h"
#include "fenwick.h"

TEST(fenwick, works) {
Fenwick ft(5);
EXPECT_EQ(ft.sum(0, 4), 0);
ft.add(2, 5);
EXPECT_EQ(ft.sum(0, 4), 5);
EXPECT_EQ(ft.sum(0, 1), 0);
}
21 changes: 21 additions & 0 deletions tests/test_mod_inverse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "gtest/gtest.h"
#include "mod_inverse.h"

TEST(mod_inverse, correct) {
auto check = [](ll x, ll mod) {
auto inv = mod_inverse(x, mod);
return ((x * inv) % mod) == 1;
};

for(ll mod : {17, 1000000007, 1000000009}) {
for(ll x : {2, 50, 1000000000}) {
EXPECT_TRUE(check(x, mod));
}
}
}

TEST(mod_inverse, fails_if_not_coprime) {
EXPECT_EQ(mod_inverse(5, 10), -1);
EXPECT_EQ(mod_inverse(1000, 1000000), -1);
EXPECT_EQ(mod_inverse(1000000000, 8000000000), -1);
}