-
Notifications
You must be signed in to change notification settings - Fork 56
add googletest #3672
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
Open
shaomeng
wants to merge
3
commits into
main
Choose a base branch
from
googletest
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−0
Open
add googletest #3672
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Add an executable that tests a specific part of VAPOR. | ||
| add_executable( ptr_cache_test ptr_cache_test.cpp ) | ||
| target_include_directories( ptr_cache_test PRIVATE ${PROJECT_SOURCE_DIR}/include/vapor/ ) | ||
| target_link_libraries( ptr_cache_test PUBLIC GTest::gtest_main ) | ||
|
|
||
|
|
||
| include(GoogleTest) | ||
|
|
||
| # Enable this executable to be tested using `ctest .` | ||
| gtest_discover_tests( ptr_cache_test ) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| #include "gtest/gtest.h" | ||
|
|
||
| #include "ptr_cache.hpp" // Only include the module that's tested | ||
|
|
||
| namespace { | ||
|
|
||
| struct MyObj { | ||
| int i = 0, j = 1; | ||
| }; | ||
|
|
||
| // Besides correct insertion/query/eviction behaviors the `ptr_cache` has to be | ||
| // deleting objects correctly upon eviction. However, memory errors are not | ||
| // something GoogleTest can detect. As a result, one needs to run this | ||
| // executable in valgrind to make sure that there are no memory errors: | ||
| // valgrind ./googletest_scripts/ptr_cache_test | ||
|
|
||
| // Test the cache when queries don't count as a use. | ||
| // It needs to insert and evict correctly. | ||
| TEST(ptr_cache_query_false, insert_eviction) | ||
| { | ||
| VAPoR::ptr_cache<int, MyObj, 3, false> cache; | ||
| const auto* p = cache.query(1); | ||
| EXPECT_EQ(p, nullptr); // nothing in the cache yet, should get nullptr | ||
|
|
||
| cache.insert(1, new MyObj{1, 100}); | ||
| cache.insert(2, new MyObj{2, 200}); | ||
| cache.insert(3, new MyObj{3, 300}); | ||
|
|
||
| // Make sure that we have all 3 objects | ||
| p = cache.query(1); | ||
| EXPECT_NE(p, nullptr); // not nullptr | ||
| EXPECT_EQ(p->j, 100); | ||
| p = cache.query(2); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 200); | ||
| p = cache.query(3); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 300); | ||
| p = cache.query(4); | ||
| EXPECT_EQ(p, nullptr); | ||
|
|
||
| // Insert a new object; make sure that "1" is evicted. | ||
| cache.insert(4, new MyObj{4, 400}); | ||
| p = cache.query(4); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 400); | ||
| p = cache.query(1); | ||
| EXPECT_EQ(p, nullptr); | ||
|
|
||
| // Do a query on "2", but queries don't count as a use. | ||
| p = cache.query(2); | ||
|
|
||
| // Insert another object; make sure that "2" is evicted. | ||
| cache.insert(5, new MyObj{5, 500}); | ||
| p = cache.query(5); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 500); | ||
| p = cache.query(2); | ||
| EXPECT_EQ(p, nullptr); | ||
| } | ||
|
|
||
|
|
||
| // Test the cache when queries count as a use. | ||
| TEST(ptr_cache_query_true, insert_eviction) | ||
| { | ||
| VAPoR::ptr_cache<int, MyObj, 3, true> cache; | ||
|
|
||
| cache.insert(1, new MyObj{1, 100}); | ||
| cache.insert(2, new MyObj{2, 200}); | ||
| cache.insert(3, new MyObj{3, 300}); | ||
|
|
||
| // Make sure that we have all 3 objects | ||
| const auto* p = cache.query(1); | ||
| EXPECT_NE(p, nullptr); // not nullptr | ||
| EXPECT_EQ(p->j, 100); | ||
| p = cache.query(2); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 200); | ||
| p = cache.query(3); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 300); | ||
| p = cache.query(4); | ||
| EXPECT_EQ(p, nullptr); | ||
|
|
||
| // Insert a new object; make sure that "1" is evicted. | ||
| cache.insert(4, new MyObj{4, 400}); | ||
| p = cache.query(4); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 400); | ||
| p = cache.query(1); | ||
| EXPECT_EQ(p, nullptr); | ||
|
|
||
| // Do a query on "2", then insert, it should be "3" that's evicted. | ||
| p = cache.query(2); | ||
| cache.insert(5, new MyObj{5, 500}); | ||
| p = cache.query(5); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 500); | ||
| p = cache.query(3); | ||
| EXPECT_EQ(p, nullptr); | ||
|
|
||
| // Do another query on "2", then insert, it should be "4" that's evicted. | ||
| p = cache.query(2); | ||
| cache.insert(6, new MyObj{6, 600}); | ||
| p = cache.query(6); | ||
| EXPECT_NE(p, nullptr); | ||
| EXPECT_EQ(p->j, 600); | ||
| p = cache.query(4); | ||
| EXPECT_EQ(p, nullptr); | ||
| } | ||
|
|
||
| } // End of namespace |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @ifranda , could you remove the
if()andendif()lines (line 132 and 136) for me? The CMake version should be specified at the beginning of theCMakeLists.txt(Line 7,cmake_minimum_required()) function, instead of here.(I lost my access to update this PR.)