From 699b221661fb9358ac703c3fb3412908c6b2b9a1 Mon Sep 17 00:00:00 2001 From: Zhe Feng Date: Thu, 19 Mar 2026 17:27:59 +0800 Subject: [PATCH 1/2] test: Add tests for Iterator --- tests/chapter2/xmake.lua | 13 +++++++++++ tests/test_iterator.cpp | 49 ++++++++++++++++++++++++++++++++++++++++ tests/xmake.lua | 4 ++-- 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 tests/chapter2/xmake.lua create mode 100644 tests/test_iterator.cpp diff --git a/tests/chapter2/xmake.lua b/tests/chapter2/xmake.lua new file mode 100644 index 0000000..8c0ddb4 --- /dev/null +++ b/tests/chapter2/xmake.lua @@ -0,0 +1,13 @@ +target("chapter2_tests") + add_files("../test_main.cpp") + add_files( + "../test_metaInfo.cpp", + "../test_array.cpp", + "../test_iterator.cpp" + ) + if is_mode("debug") then + add_files("../../src/chapter2/**.cppm") + else + add_files("../../mystl/chapter2/**.cppm") + end + add_deps("chapter2_modules") \ No newline at end of file diff --git a/tests/test_iterator.cpp b/tests/test_iterator.cpp new file mode 100644 index 0000000..d79a778 --- /dev/null +++ b/tests/test_iterator.cpp @@ -0,0 +1,49 @@ +#include + +import mySTL.iterator; + +namespace { + +using TestPointerBaseTypes = + ::testing::Types; + +template +class IteratorTest : public ::testing::Test {}; + +} // namespace + +TYPED_TEST_SUITE(IteratorTest, TestPointerBaseTypes); + +TYPED_TEST(IteratorTest, IteratorConcept) { + using PointerType = TypeParam*; + using ConstPointerType = const TypeParam*; + + EXPECT_TRUE(mySTL::iterator::Iterator); + EXPECT_TRUE(mySTL::iterator::Iterator); +} + +TYPED_TEST(IteratorTest, FowardIteratorConcept) { + using PointerType = TypeParam*; + using ConstPointerType = const TypeParam*; + + EXPECT_TRUE(mySTL::iterator::ForwardIterator); + EXPECT_TRUE(mySTL::iterator::ForwardIterator); +} + +TYPED_TEST(IteratorTest, BidirectionalIteratorConcept) { + using PointerType = TypeParam*; + using ConstPointerType = const TypeParam*; + + EXPECT_TRUE(mySTL::iterator::BidirectionalIterator); + EXPECT_TRUE(mySTL::iterator::BidirectionalIterator); +} + +TYPED_TEST(IteratorTest, RandomAccessIteratorConcept) { + using PointerType = TypeParam*; + using ConstPointerType = const TypeParam*; + + EXPECT_TRUE(mySTL::iterator::RandomAccessIterator); + EXPECT_TRUE(mySTL::iterator::RandomAccessIterator); +} diff --git a/tests/xmake.lua b/tests/xmake.lua index 4eac509..529d413 100644 --- a/tests/xmake.lua +++ b/tests/xmake.lua @@ -3,5 +3,5 @@ add_packages("gtest") includes("chapter0/xmake.lua") includes("chapter1/xmake.lua") ---includes("chapter2/xmake.lua") -includes("chapter3/xmake.lua") \ No newline at end of file +includes("chapter2/xmake.lua") +--includes("chapter3/xmake.lua") \ No newline at end of file From 13baab18873716edfd05098e2e975bc6c50499fa Mon Sep 17 00:00:00 2001 From: Zhe Feng Date: Thu, 19 Mar 2026 18:08:23 +0800 Subject: [PATCH 2/2] test: Add tests for Range --- tests/chapter3/xmake.lua | 14 +++++++++ tests/test_range.cpp | 68 ++++++++++++++++++++++++++++++++++++++++ tests/xmake.lua | 2 +- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/chapter3/xmake.lua create mode 100644 tests/test_range.cpp diff --git a/tests/chapter3/xmake.lua b/tests/chapter3/xmake.lua new file mode 100644 index 0000000..1e1521f --- /dev/null +++ b/tests/chapter3/xmake.lua @@ -0,0 +1,14 @@ +target("chapter3_tests") + add_files("../test_main.cpp") + add_files( + "../test_metaInfo.cpp", + "../test_array.cpp", + "../test_iterator.cpp", + "../test_range.cpp" + ) + if is_mode("debug") then + add_files("../../src/chapter3/**.cppm") + else + add_files("../../mystl/chapter3/**.cppm") + end + add_deps("chapter3_modules") \ No newline at end of file diff --git a/tests/test_range.cpp b/tests/test_range.cpp new file mode 100644 index 0000000..beeb506 --- /dev/null +++ b/tests/test_range.cpp @@ -0,0 +1,68 @@ +#include + +import mySTL.range; +import mySTL.array; + +namespace { + +using TestPointerBaseTypes = + ::testing::Types; + +template +class RangeTest : public ::testing::Test {}; + +} // namespace + +TYPED_TEST_SUITE(RangeTest, TestPointerBaseTypes); + +TYPED_TEST(RangeTest, CArrayRange) { + EXPECT_FALSE(mySTL::range::Range); + EXPECT_TRUE(mySTL::range::Range); +} + +TYPED_TEST(RangeTest, CArrayIterRange) { + EXPECT_FALSE(mySTL::range::IterRange); + EXPECT_TRUE(mySTL::range::IterRange); +} + +TYPED_TEST(RangeTest, MySTLArrayRange) { + EXPECT_TRUE((mySTL::range::Range>)); + EXPECT_TRUE((mySTL::range::Range>)); +} + +TYPED_TEST(RangeTest, MySTLArrayIterRange) { + EXPECT_TRUE((mySTL::range::IterRange>)); + EXPECT_TRUE((mySTL::range::IterRange>)); +} + +TYPED_TEST(RangeTest, MySTLArrayRangeFor) { + mySTL::Array arr {}; + + for ([[maybe_unused]] auto& e : arr) { + } + + for ([[maybe_unused]] const auto& e : arr) { + } + + for ([[maybe_unused]] auto&& e : arr) { + } + + SUCCEED(); +} + +TYPED_TEST(RangeTest, MySTLArraySize0RangeFor) { + mySTL::Array arr {}; + + for ([[maybe_unused]] auto& e : arr) { + } + + for ([[maybe_unused]] const auto& e : arr) { + } + + for ([[maybe_unused]] auto&& e : arr) { + } + + SUCCEED(); +} diff --git a/tests/xmake.lua b/tests/xmake.lua index 529d413..b8e9ba3 100644 --- a/tests/xmake.lua +++ b/tests/xmake.lua @@ -4,4 +4,4 @@ add_packages("gtest") includes("chapter0/xmake.lua") includes("chapter1/xmake.lua") includes("chapter2/xmake.lua") ---includes("chapter3/xmake.lua") \ No newline at end of file +includes("chapter3/xmake.lua") \ No newline at end of file