diff options
| author | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:42:50 +0300 |
|---|---|---|
| committer | Mehmet Samet Duman <yongdohyun@projecttick.org> | 2026-04-02 18:42:50 +0300 |
| commit | 5fad10f89c485cfdc7b99011f07609f8871160d4 (patch) | |
| tree | 1860b39753b652dfe54d3cbbc80c875f40198d1f /json4cpp/tests/src/unit-user_defined_input.cpp | |
| parent | 292baed7ac0cf84263263966ed32ed113cae857f (diff) | |
| parent | 9a737481aed085fd289f82dff1fa8c3c66627a7e (diff) | |
| download | Project-Tick-5fad10f89c485cfdc7b99011f07609f8871160d4.tar.gz Project-Tick-5fad10f89c485cfdc7b99011f07609f8871160d4.zip | |
Add 'json4cpp/' from commit '9a737481aed085fd289f82dff1fa8c3c66627a7e'
git-subtree-dir: json4cpp
git-subtree-mainline: 292baed7ac0cf84263263966ed32ed113cae857f
git-subtree-split: 9a737481aed085fd289f82dff1fa8c3c66627a7e
Diffstat (limited to 'json4cpp/tests/src/unit-user_defined_input.cpp')
| -rw-r--r-- | json4cpp/tests/src/unit-user_defined_input.cpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/json4cpp/tests/src/unit-user_defined_input.cpp b/json4cpp/tests/src/unit-user_defined_input.cpp new file mode 100644 index 0000000000..56446ea94c --- /dev/null +++ b/json4cpp/tests/src/unit-user_defined_input.cpp @@ -0,0 +1,130 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ (supporting code) +// | | |__ | | | | | | version 3.12.0 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me> +// SPDX-License-Identifier: MIT + +#include "doctest_compatibility.h" + +#include <nlohmann/json.hpp> +using nlohmann::json; + +#include <list> + +namespace +{ +TEST_CASE("Use arbitrary stdlib container") +{ + std::string raw_data = "[1,2,3,4]"; + std::list<char> data(raw_data.begin(), raw_data.end()); + + json as_json = json::parse(data.begin(), data.end()); + CHECK(as_json.at(0) == 1); + CHECK(as_json.at(1) == 2); + CHECK(as_json.at(2) == 3); + CHECK(as_json.at(3) == 4); +} + +struct MyContainer +{ + const char* data; +}; + +const char* begin(const MyContainer& c) +{ + return c.data; +} + +const char* end(const MyContainer& c) +{ + return c.data + strlen(c.data); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) +} + +TEST_CASE("Custom container non-member begin/end") +{ + + const MyContainer data{"[1,2,3,4]"}; + json as_json = json::parse(data); + CHECK(as_json.at(0) == 1); + CHECK(as_json.at(1) == 2); + CHECK(as_json.at(2) == 3); + CHECK(as_json.at(3) == 4); + +} + +TEST_CASE("Custom container member begin/end") +{ + struct MyContainer2 + { + const char* data; + + const char* begin() const noexcept + { + return data; + } + + const char* end() const noexcept + { + return data + strlen(data); // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + } + }; + + const MyContainer2 data{"[1,2,3,4]"}; + json as_json = json::parse(data); + CHECK(as_json.at(0) == 1); + CHECK(as_json.at(1) == 2); + CHECK(as_json.at(2) == 3); + CHECK(as_json.at(3) == 4); +} + +TEST_CASE("Custom iterator") +{ + const char* raw_data = "[1,2,3,4]"; + + struct MyIterator + { + using difference_type = std::size_t; + using value_type = char; + using pointer = const char*; + using reference = const char&; + using iterator_category = std::input_iterator_tag; + + MyIterator& operator++() + { + ++ptr; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + return *this; + } + + reference operator*() const + { + return *ptr; + } + + bool operator!=(const MyIterator& rhs) const + { + return ptr != rhs.ptr; + } + + const char* ptr; + }; + + // avoid -Wunused-local-typedefs + CHECK(std::is_same<MyIterator::difference_type, std::size_t>::value); + CHECK(std::is_same<MyIterator::value_type, char>::value); + CHECK(std::is_same<MyIterator::pointer, const char*>::value); + CHECK(std::is_same<MyIterator::reference, const char&>::value); + CHECK(std::is_same<MyIterator::iterator_category, std::input_iterator_tag>::value); + + const MyIterator begin{raw_data}; + const MyIterator end{raw_data + strlen(raw_data)}; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + + json as_json = json::parse(begin, end); + CHECK(as_json.at(0) == 1); + CHECK(as_json.at(1) == 2); + CHECK(as_json.at(2) == 3); + CHECK(as_json.at(3) == 4); +} + +} // namespace |
