-
Notifications
You must be signed in to change notification settings - Fork 8
feat: Port Array.hpp from CLP into a new containers library; Add initializer list constructor; Resolve most clang-tidy violations.
#33
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a6ee90c
Add array
Bill-hbrhbr d7e6f37
Add initializer list
Bill-hbrhbr 497a251
Address coderabbtai
Bill-hbrhbr 36bcd1f
Merge branch 'main' into add-container-array
Bill-hbrhbr 924ff62
Rename lib container to containers
Bill-hbrhbr f931b04
Rename functions, variables and add comments
Bill-hbrhbr 39200a3
Reorder todo
Bill-hbrhbr b072f48
Address coderabbitai review
Bill-hbrhbr 8614065
Rename test
Bill-hbrhbr 3569423
Furthur refactor tests to reduce complexity
Bill-hbrhbr 9f5eb84
Minor fixes
Bill-hbrhbr 2a877e4
Update src/ystdlib/containers/test/test_Array.cpp
Bill-hbrhbr 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| add_subdirectory(containers) | ||
| add_subdirectory(error_handling) |
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,122 @@ | ||
| #ifndef YSTDLIB_CONTAINERS_ARRAY | ||
| #define YSTDLIB_CONTAINERS_ARRAY | ||
|
|
||
| #include <algorithm> | ||
| #include <concepts> | ||
| #include <cstddef> | ||
| #include <cstring> | ||
| #include <initializer_list> | ||
| #include <memory> | ||
| #include <stdexcept> | ||
| #include <type_traits> | ||
|
|
||
| namespace ystdlib::containers { | ||
| /** | ||
| * Class for a runtime fix-sized array. | ||
| * @tparam T The type of elements in the array. The type must be default initializable so that this | ||
| * class doesn't need to implement special memory allocation procedure for std::unique_ptr<T[]>. | ||
| * Note that when using constructor that takes an initializer list, T must be copy constructable. | ||
| */ | ||
| template <typename T> | ||
| requires(std::is_fundamental_v<T> || std::default_initializable<T>) | ||
| class Array { | ||
| public: | ||
| // Types | ||
| using Iterator = T*; | ||
| using ConstIterator = T const*; | ||
|
|
||
| // Constructors | ||
| // NOLINTNEXTLINE(*-avoid-c-arrays) | ||
| explicit Array(size_t size) : m_data{std::make_unique<T[]>(size)}, m_size{size} { | ||
| if constexpr (std::is_fundamental_v<T>) { | ||
| std::fill_n(m_data.get(), m_size, T{}); | ||
| } | ||
| } | ||
|
|
||
| Array(std::initializer_list<T> list) | ||
| // NOLINTNEXTLINE(*-avoid-c-arrays) | ||
| : m_data{std::make_unique<T[]>(list.size())}, | ||
| m_size{list.size()} { | ||
| std::ranges::copy(list, m_data.get()); | ||
| } | ||
|
|
||
| // Disable copy constructor and assignment operator | ||
| Array(Array const&) = delete; | ||
| auto operator=(Array const&) -> Array& = delete; | ||
|
|
||
| // Default move constructor and assignment operator | ||
| Array(Array&&) = default; | ||
| auto operator=(Array&&) -> Array& = default; | ||
|
|
||
| // Destructor | ||
| ~Array() = default; | ||
|
|
||
| // Methods | ||
| /** | ||
| * @return Whether the array is empty. | ||
| */ | ||
| [[nodiscard]] auto empty() const -> bool { return 0 == size(); } | ||
|
|
||
| /** | ||
| * @return The size of the array. | ||
| */ | ||
| [[nodiscard]] auto size() const -> size_t { return m_size; } | ||
|
|
||
| /** | ||
| * @return A pointer to the underlying data buffer. | ||
| */ | ||
| [[nodiscard]] auto data() -> T* { return m_data.get(); } | ||
|
|
||
| /** | ||
| * @return A pointer to the underlying data buffer. | ||
| */ | ||
| [[nodiscard]] auto data() const -> T const* { return m_data.get(); } | ||
|
|
||
| /** | ||
| * @param idx | ||
| * @return The element at the given index. | ||
| * @throw `std::out_of_range` if the given index is out of bound. | ||
| */ | ||
| [[nodiscard]] auto at(size_t idx) -> T& { | ||
| assert_is_in_range(idx); | ||
| return m_data[idx]; | ||
| } | ||
|
|
||
| /** | ||
| * @param idx | ||
| * @return The element at the given index. | ||
| * @throw `std::out_of_range` if the given index is out of bound. | ||
| */ | ||
| [[nodiscard]] auto at(size_t idx) const -> T const& { | ||
| assert_is_in_range(idx); | ||
| return m_data[idx]; | ||
| } | ||
|
|
||
| [[nodiscard]] auto begin() -> Iterator { return m_data.get(); } | ||
|
|
||
| [[nodiscard]] auto end() -> Iterator { return m_data.get() + m_size; } | ||
|
|
||
| [[nodiscard]] auto begin() const -> ConstIterator { return m_data.get(); } | ||
|
|
||
| [[nodiscard]] auto end() const -> ConstIterator { return m_data.get() + m_size; } | ||
|
|
||
| private: | ||
| /** | ||
| * @param idx | ||
| * @throw `std::out_of_range` if the given index is out of bound. | ||
| */ | ||
| auto assert_is_in_range(size_t idx) const -> void { | ||
| if (idx >= m_size) { | ||
| // TODO: Add ErrorCode class for `stdexcept` errors and switch to use TraceableException | ||
| throw std::out_of_range("ystdlib::containers::Array out-of-range access."); | ||
| } | ||
| } | ||
|
|
||
| // Variables | ||
| // NOLINTNEXTLINE(*-avoid-c-arrays) | ||
| std::unique_ptr<T[]> m_data; | ||
| size_t m_size; | ||
| }; | ||
| } // namespace ystdlib::containers | ||
|
|
||
| #endif // YSTDLIB_CONTAINERS_ARRAY |
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 @@ | ||
| cpp_library(NAME container NAMESPACE ystdlib TESTS_SOURCES test/test_Array.cpp) |
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,200 @@ | ||
| #include <algorithm> | ||
| #include <concepts> | ||
| #include <cstddef> | ||
| #include <ranges> | ||
| #include <stdexcept> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <type_traits> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include <ystdlib/containers/Array.hpp> | ||
|
|
||
| #include <catch2/catch_template_test_macros.hpp> | ||
| #include <catch2/catch_test_macros.hpp> | ||
|
|
||
| namespace { | ||
| constexpr size_t cBufferSize{1024}; | ||
|
|
||
| /** | ||
| * A class that requires an explicit argument for construction and disables the default constructor. | ||
| * | ||
| * Includes a data member `m_value` and an accessor function to verify successful initialization. | ||
| */ | ||
| class ExplicitConstructor { | ||
| public: | ||
| ExplicitConstructor() = delete; | ||
|
|
||
| ExplicitConstructor(size_t value) : m_value{value} {} | ||
|
|
||
| [[nodiscard]] auto get_value() const -> size_t { return m_value; } | ||
|
|
||
| private: | ||
| size_t m_value; | ||
| }; | ||
|
|
||
| /** | ||
| * A class that supports multiple initialization methods. | ||
| * | ||
| * This class is used to test `Array` list initialization with various constructor argument types. | ||
| * It includes a data member `m_value` and an accessor function to verify successful initialization. | ||
| */ | ||
| class PolymorphicConstructors { | ||
| public: | ||
| static constexpr size_t cDefaultValue = 3190; | ||
|
|
||
| PolymorphicConstructors() = default; | ||
|
|
||
| PolymorphicConstructors(size_t value) : m_value{value} {} | ||
|
|
||
| PolymorphicConstructors(std::string const& s) : m_value{s.size()} {} | ||
|
|
||
| PolymorphicConstructors(ExplicitConstructor const& obj) : m_value{obj.get_value()} {} | ||
|
|
||
| [[nodiscard]] auto get_value() const -> size_t { return m_value; } | ||
|
|
||
| private: | ||
| size_t m_value{cDefaultValue}; | ||
| }; | ||
|
|
||
| /** | ||
| * Tests whether list initialization works correctly for `Array` when an initializer list is passed | ||
| * as a function argument. | ||
| */ | ||
| void test_list_initialization_in_function_call( | ||
| ystdlib::containers::Array<PolymorphicConstructors> const& arr, | ||
| std::vector<size_t> const& data | ||
| ) { | ||
| REQUIRE(std::ranges::equal( | ||
| arr | std::views::transform([](auto const& obj) { return obj.get_value(); }), | ||
| data | ||
| )); | ||
| } | ||
| } // namespace | ||
|
|
||
| namespace ystdlib::containers::test { | ||
| TEST_CASE("test_array_empty", "[containers][Array]") { | ||
| Array<int> arr(0); | ||
| REQUIRE(arr.empty()); | ||
| // NOLINTNEXTLINE(readability-container-size-empty) | ||
| REQUIRE((0 == arr.size())); | ||
| REQUIRE((arr.begin() == arr.end())); | ||
| } | ||
|
|
||
| TEST_CASE("test_array_reference", "[containers][Array]") { | ||
| Array<int> arr(cBufferSize); | ||
| for (int idx{0}; idx < cBufferSize; ++idx) { | ||
| arr.at(idx) = idx; | ||
| } | ||
| auto const& arr_const_ref = arr; | ||
| REQUIRE(std::ranges::equal(arr, arr_const_ref)); | ||
| } | ||
|
|
||
| TEST_CASE("test_array_ranged_copy", "[containers][Array]") { | ||
| std::vector<int> vec; | ||
| for (int idx{0}; idx < cBufferSize; ++idx) { | ||
| vec.push_back(idx); | ||
| } | ||
| Array<int> arr(cBufferSize); | ||
| std::ranges::copy(vec, arr.begin()); | ||
| REQUIRE(std::ranges::equal(vec, arr)); | ||
| } | ||
|
|
||
| TEST_CASE("test_array_movable", "[containers][Array]") { | ||
| Array<int> arr(cBufferSize); | ||
| Array<int> reference_array(cBufferSize); | ||
| for (int idx{0}; idx < cBufferSize; ++idx) { | ||
| arr.at(idx) = idx; | ||
| reference_array.at(idx) = idx; | ||
| } | ||
| auto const arr_moved{std::move(arr)}; | ||
| REQUIRE(std::ranges::equal(reference_array, arr_moved)); | ||
| } | ||
|
|
||
| TEST_CASE("test_array_illegal_access", "[containers][Array]") { | ||
| Array<int> arr(cBufferSize); | ||
| REQUIRE_THROWS_AS(arr.at(-1), std::out_of_range); | ||
| REQUIRE_THROWS_AS(arr.at(cBufferSize), std::out_of_range); | ||
| } | ||
|
|
||
| TEMPLATE_TEST_CASE( | ||
| "test_array_fundamental_type_value_initialization", | ||
| "[containers][Array]", | ||
| bool, | ||
| char, | ||
| signed char, | ||
| unsigned char, | ||
| wchar_t, | ||
| char16_t, | ||
| char32_t, | ||
| short, | ||
| unsigned short, | ||
| int, | ||
| unsigned int, | ||
| long, | ||
| unsigned long, | ||
| long long, | ||
| unsigned long long, | ||
| float, | ||
| double, | ||
| long double, | ||
| std::nullptr_t | ||
| ) { | ||
| REQUIRE(std::is_fundamental_v<TestType>); | ||
| Array<TestType> arr(cBufferSize); | ||
| std::ranges::for_each(arr, [](auto const& p) -> void { | ||
| REQUIRE((static_cast<TestType>(0) == p)); | ||
| }); | ||
| } | ||
|
|
||
| TEST_CASE("test_array_list_initialization", "[containers][Array]") { | ||
| constexpr size_t cTestNum0{344}; | ||
| constexpr size_t cTestNum1{454}; | ||
| constexpr std::string_view cTestStr{"yscope"}; | ||
|
|
||
| // Test basic list initialization | ||
| std::vector<std::string> const | ||
| vec{"yscope", "clp", "ystdlib", "ystdlib::containers::Array", "default_initializable"}; | ||
| Array<std::string> const | ||
| arr{"yscope", "clp", "ystdlib", "ystdlib::containers::Array", "default_initializable"}; | ||
| REQUIRE(std::ranges::equal(vec, arr)); | ||
|
|
||
| // Test polymorphic list initialization | ||
| REQUIRE(std::is_copy_constructible_v<PolymorphicConstructors>); | ||
|
|
||
| // Expected values to verify correct initialization of `PolymorphicConstructors`. | ||
| // See the `PolymorphicConstructors` class for details on how constructor arguments are handled. | ||
| std::vector<size_t> const data | ||
| = {PolymorphicConstructors::cDefaultValue, cTestNum0, cTestStr.size(), cTestNum1}; | ||
|
|
||
| SECTION("Local variable") { | ||
| Array<PolymorphicConstructors> const list_init_arr | ||
| = {PolymorphicConstructors{}, | ||
| cTestNum0, | ||
| std::string{cTestStr}, | ||
| ExplicitConstructor{cTestNum1}}; | ||
| REQUIRE(std::ranges::equal( | ||
| list_init_arr | ||
| | std::views::transform([](auto const& obj) { return obj.get_value(); }), | ||
| data | ||
| )); | ||
| } | ||
|
|
||
| SECTION("Function argument passing") { | ||
| test_list_initialization_in_function_call( | ||
| {PolymorphicConstructors{}, | ||
| cTestNum0, | ||
| std::string{cTestStr}, | ||
| ExplicitConstructor{cTestNum1}}, | ||
| data | ||
| ); | ||
| } | ||
|
|
||
| // Test polymorphic list initialization with a non default-constructible class | ||
| REQUIRE_FALSE(std::default_initializable<ExplicitConstructor>); | ||
|
|
||
| // TODO: Test `Array` list initialization with this class. Currently, `Array` requires the | ||
| // template type to be a fundamental type or a default-initializable class type. | ||
| } | ||
| } // namespace ystdlib::containers::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
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.
Uh oh!
There was an error while loading. Please reload this page.