integrate gtest framwork

This commit is contained in:
2024-03-26 04:52:12 +00:00
parent 347cfcf8e9
commit 68d185f12a
5 changed files with 55 additions and 4 deletions

View File

@ -4,6 +4,13 @@ include(CTest)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2") set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -fsanitize=address") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -fsanitize=address")
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.zip
)
FetchContent_MakeAvailable(googletest)
include(GoogleTest)
add_subdirectory(vector) add_subdirectory(vector)
add_subdirectory(priority_queue) add_subdirectory(priority_queue)
add_subdirectory(map) add_subdirectory(map)

View File

@ -29,4 +29,5 @@ add_test(NAME mp_four_mem COMMAND sh -c "${CMAKE_CURRENT_BINARY_DIR}/mp_four_mem
add_test(NAME mp_five COMMAND sh -c "${CMAKE_CURRENT_BINARY_DIR}/mp_five >/tmp/five_out.txt\ add_test(NAME mp_five COMMAND sh -c "${CMAKE_CURRENT_BINARY_DIR}/mp_five >/tmp/five_out.txt\
&& diff -u ${CMAKE_CURRENT_SOURCE_DIR}/data/five/answer.txt /tmp/five_out.txt>/tmp/five_diff.txt") && diff -u ${CMAKE_CURRENT_SOURCE_DIR}/data/five/answer.txt /tmp/five_out.txt>/tmp/five_diff.txt")
add_test(NAME mp_five_mem COMMAND sh -c "${CMAKE_CURRENT_BINARY_DIR}/mp_five_mem >/tmp/five_mem_out.txt\ add_test(NAME mp_five_mem COMMAND sh -c "${CMAKE_CURRENT_BINARY_DIR}/mp_five_mem >/tmp/five_mem_out.txt\
&& diff -u ${CMAKE_CURRENT_SOURCE_DIR}/data/five/answer.txt /tmp/five_mem_out.txt>/tmp/five_mem_diff.txt") && diff -u ${CMAKE_CURRENT_SOURCE_DIR}/data/five/answer.txt /tmp/five_mem_out.txt>/tmp/five_mem_diff.txt")
add_subdirectory(test)

View File

@ -243,7 +243,15 @@ class map {
map *domain; map *domain;
public: public:
// Add some type traits
typedef std::bidirectional_iterator_tag iterator_category;
typedef pair<const Key, T> value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type *pointer;
typedef value_type &reference;
friend const_iterator; friend const_iterator;
friend map;
iterator() : raw_pointer(nullptr), domain(nullptr) {} iterator() : raw_pointer(nullptr), domain(nullptr) {}
iterator(const iterator &other) : raw_pointer(other.raw_pointer), domain(other.domain) {} iterator(const iterator &other) : raw_pointer(other.raw_pointer), domain(other.domain) {}
iterator(RedBlackTreeNodeType *raw_pointer, map *domain) : raw_pointer(raw_pointer), domain(domain) {} iterator(RedBlackTreeNodeType *raw_pointer, map *domain) : raw_pointer(raw_pointer), domain(domain) {}
@ -326,7 +334,15 @@ class map {
const map *domain; const map *domain;
public: public:
// Add some type traits
typedef std::bidirectional_iterator_tag iterator_category;
typedef pair<const Key, T> value_type;
typedef std::ptrdiff_t difference_type;
typedef value_type *pointer;
typedef value_type &reference;
friend iterator; friend iterator;
friend map;
const_iterator() : raw_pointer(nullptr), domain(nullptr) {} const_iterator() : raw_pointer(nullptr), domain(nullptr) {}
const_iterator(const const_iterator &other) : raw_pointer(other.raw_pointer), domain(other.domain) {} const_iterator(const const_iterator &other) : raw_pointer(other.raw_pointer), domain(other.domain) {}
const_iterator(const iterator &other) : raw_pointer(other.raw_pointer), domain(other.domain) {} const_iterator(const iterator &other) : raw_pointer(other.raw_pointer), domain(other.domain) {}
@ -409,7 +425,7 @@ class map {
other.tree_root = nullptr; other.tree_root = nullptr;
} }
/** /**
* TODO assignment operator * assignment operator
*/ */
map &operator=(const map &other) { map &operator=(const map &other) {
if (this == &other) return *this; if (this == &other) return *this;
@ -417,6 +433,7 @@ class map {
delete tree_root; delete tree_root;
node_count = other.node_count; node_count = other.node_count;
CopyFrom(tree_root, other.tree_root); CopyFrom(tree_root, other.tree_root);
return *this;
} }
map &operator=(map &&other) { map &operator=(map &&other) {
if (this == &other) return *this; if (this == &other) return *this;
@ -426,6 +443,7 @@ class map {
tree_root = other.tree_root; tree_root = other.tree_root;
other.node_count = 0; other.node_count = 0;
other.tree_root = nullptr; other.tree_root = nullptr;
return *this;
} }
~map() { ~map() {
if (tree_root) tree_root->ReleaseAll(); if (tree_root) tree_root->ReleaseAll();
@ -447,7 +465,6 @@ class map {
return result->val.second; return result->val.second;
} }
/** /**
* TODO
* access specified element * access specified element
* Returns a reference to the value that is mapped to a key equivalent to key, * Returns a reference to the value that is mapped to a key equivalent to key,
* performing an insertion if such key does not already exist. * performing an insertion if such key does not already exist.
@ -521,7 +538,9 @@ class map {
* *
* throw if pos pointed to a bad element (pos == this->end() || pos points an element out of this) * throw if pos pointed to a bad element (pos == this->end() || pos points an element out of this)
*/ */
void erase(iterator pos) {} void erase(iterator pos) {
if (pos.domain != this || pos.raw_pointer == nullptr) throw invalid_iterator();
}
/** /**
* Returns the number of elements with key * Returns the number of elements with key
* that compares equivalent to the specified argument, * that compares equivalent to the specified argument,

9
map/test/CMakeLists.txt Normal file
View File

@ -0,0 +1,9 @@
add_executable(
test_mp_basic
test_basic.cpp
)
target_link_libraries(
test_mp_basic
GTest::gtest_main
)
gtest_discover_tests(test_mp_basic)

15
map/test/test_basic.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <gtest/gtest.h>
#include "map.hpp"
TEST(BasicTests, GTestItSelf) {
// Expect two strings not to be equal.
EXPECT_STRNE("hello", "world");
// Expect equality.
EXPECT_EQ(7 * 6, 42);
}
TEST(BasicTests, ConstructorAndEmptySize) {
sjtu::map<int, int> map;
EXPECT_EQ(map.empty(), true);
EXPECT_EQ(map.size(), 0);
}