-
Notifications
You must be signed in to change notification settings - Fork 857
Add libswoc unit tests #11106
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
Add libswoc unit tests #11106
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
12f988a
Adding libswoc unit_tests and CMakeLists.txt update
bneradt 3a25c0d
swoc/unit_tests build and test fixes
bneradt c5d07a8
Put back in the IntrusiveHashMap::insert ordering
bneradt 4ffc766
TextView::suffix: Assure we don't access beyond bounds
bneradt da7aa2c
Have libswoc unit_tests use ATS catch instead of local
bneradt 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
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 |
|---|---|---|
|
|
@@ -1384,7 +1384,7 @@ TextView::suffix(int n) const noexcept { | |
| inline TextView | ||
| TextView::suffix_at(char c) const { | ||
| self_type zret; | ||
| if (auto n = this->rfind(c); n != npos) { | ||
| if (auto n = this->rfind(c); n != npos && n + 1 < this->size()) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This addresses a compiler warning from Centos that zret.assign referenced the byte after data() if n pointed to the last byte in the string. |
||
| ++n; | ||
| zret.assign(this->data() + n, this->size() - n); | ||
| } | ||
|
|
||
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,51 @@ | ||
| cmake_minimum_required(VERSION 3.12) | ||
| project(test_libswoc CXX) | ||
| set(CMAKE_CXX_STANDARD 17) | ||
|
|
||
| add_executable( | ||
| test_libswoc | ||
| unit_test_main.cc | ||
| test_BufferWriter.cc | ||
| test_bw_format.cc | ||
| test_Errata.cc | ||
| test_IntrusiveDList.cc | ||
| test_IntrusiveHashMap.cc | ||
| test_ip.cc | ||
| test_Lexicon.cc | ||
| test_MemSpan.cc | ||
| test_MemArena.cc | ||
| test_meta.cc | ||
| test_range.cc | ||
| test_TextView.cc | ||
| test_Scalar.cc | ||
| test_swoc_file.cc | ||
| test_Vectray.cc | ||
| ex_bw_format.cc | ||
| ex_IntrusiveDList.cc | ||
| ex_ipspace_properties.cc | ||
| ex_Lexicon.cc | ||
| ex_MemArena.cc | ||
| ex_TextView.cc | ||
| ex_UnitParser.cc | ||
| ) | ||
|
|
||
| target_link_libraries(test_libswoc PUBLIC libswoc PRIVATE catch2::catch2) | ||
| set_target_properties(test_libswoc PROPERTIES CLANG_FORMAT_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) | ||
| if(CMAKE_COMPILER_IS_GNUCXX) | ||
| target_compile_options( | ||
| test_libswoc | ||
| PRIVATE -Wall | ||
| -Wextra | ||
| -Werror | ||
| -Wno-unused-parameter | ||
| -Wno-format-truncation | ||
| -Wno-stringop-overflow | ||
| -Wno-invalid-offsetof | ||
| ) | ||
| # stop the compiler from complaining about unused variable in structured binding | ||
| if(GCC_VERSION VERSION_LESS 8.0) | ||
| target_compile_options(test_libswoc PRIVATE -Wno-unused-variable) | ||
| endif() | ||
| endif() | ||
|
|
||
| add_test(NAME test_libswoc COMMAND test_libswoc) |
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,215 @@ | ||
| /** @file | ||
|
|
||
| IntrusiveDList documentation examples. | ||
|
|
||
| This code is run during unit tests to verify that it compiles and runs correctly, but the primary | ||
| purpose of the code is for documentation, not testing per se. This means editing the file is | ||
| almost certain to require updating documentation references to code in this file. | ||
|
|
||
| @section license License | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one or more contributor license | ||
| agreements. See the NOTICE file distributed with this work for additional information regarding | ||
| copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 | ||
| (the "License"); you may not use this file except in compliance with the License. You may | ||
| obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the | ||
| License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| express or implied. See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| #include <iostream> | ||
| #include <string_view> | ||
| #include <string> | ||
| #include <algorithm> | ||
|
|
||
| #include "swoc/IntrusiveDList.h" | ||
| #include "swoc/bwf_base.h" | ||
|
|
||
| #include "catch.hpp" | ||
|
|
||
| using swoc::IntrusiveDList; | ||
|
|
||
| class Message { | ||
| using self_type = Message; ///< Self reference type. | ||
|
|
||
| public: | ||
| // Message severity level. | ||
| enum Severity { LVL_DEBUG, LVL_INFO, LVL_WARN, LVL_ERROR }; | ||
|
|
||
| protected: | ||
| std::string _text; // Text of the message. | ||
| Severity _severity{LVL_DEBUG}; | ||
| int _indent{0}; // indentation level for display. | ||
|
|
||
| // Intrusive list support. | ||
| struct Linkage { | ||
| static self_type *&next_ptr(self_type *); // Link accessor. | ||
| static self_type *&prev_ptr(self_type *); // Link accessor. | ||
|
|
||
| self_type *_next{nullptr}; // Forward link. | ||
| self_type *_prev{nullptr}; // Backward link. | ||
| } _link; | ||
|
|
||
| bool is_in_list() const; | ||
|
|
||
| friend class Container; | ||
| }; | ||
|
|
||
| auto | ||
| Message::Linkage::next_ptr(self_type *that) -> self_type *& { | ||
| return that->_link._next; | ||
| } | ||
| auto | ||
| Message::Linkage::prev_ptr(self_type *that) -> self_type *& { | ||
| return that->_link._prev; | ||
| } | ||
|
|
||
| bool | ||
| Message::is_in_list() const { | ||
| return _link._next || _link._prev; | ||
| } | ||
|
|
||
| class Container { | ||
| using self_type = Container; | ||
| using MessageList = IntrusiveDList<Message::Linkage>; | ||
|
|
||
| public: | ||
| ~Container(); | ||
|
|
||
| template <typename... Args> self_type &debug(std::string_view fmt, Args &&...args); | ||
|
|
||
| size_t count() const; | ||
| self_type &clear(); | ||
| Message::Severity max_severity() const; | ||
| void print() const; | ||
|
|
||
| protected: | ||
| MessageList _msgs; | ||
| }; | ||
|
|
||
| Container::~Container() { | ||
| this->clear(); // clean up memory. | ||
| } | ||
|
|
||
| auto | ||
| Container::clear() -> self_type & { | ||
| Message *msg; | ||
| while (nullptr != (msg = _msgs.take_head())) { | ||
| delete msg; | ||
| } | ||
| _msgs.clear(); | ||
| return *this; | ||
| } | ||
|
|
||
| size_t | ||
| Container::count() const { | ||
| return _msgs.count(); | ||
| } | ||
|
|
||
| template <typename... Args> | ||
| auto | ||
| Container::debug(std::string_view fmt, Args &&...args) -> self_type & { | ||
| Message *msg = new Message; | ||
| swoc::bwprint_v(msg->_text, fmt, std::forward_as_tuple(args...)); | ||
| msg->_severity = Message::LVL_DEBUG; | ||
| _msgs.append(msg); | ||
| return *this; | ||
| } | ||
|
|
||
| Message::Severity | ||
| Container::max_severity() const { | ||
| auto spot = std::max_element(_msgs.begin(), _msgs.end(), | ||
| [](Message const &lhs, Message const &rhs) { return lhs._severity < rhs._severity; }); | ||
| return spot == _msgs.end() ? Message::Severity::LVL_DEBUG : spot->_severity; | ||
| } | ||
|
|
||
| void | ||
| Container::print() const { | ||
| for (auto &&elt : _msgs) { | ||
| std::cout << static_cast<unsigned int>(elt._severity) << ": " << elt._text << std::endl; | ||
| } | ||
| } | ||
|
|
||
| TEST_CASE("IntrusiveDList Example", "[libswoc][IntrusiveDList]") { | ||
| Container container; | ||
|
|
||
| container.debug("This is message {}", 1); | ||
| REQUIRE(container.count() == 1); | ||
| // Destructor is checked for non-crashing as container goes out of scope. | ||
| } | ||
|
|
||
| struct Thing { | ||
| std::string _payload; | ||
| Thing *_next{nullptr}; | ||
| Thing *_prev{nullptr}; | ||
| using Linkage = swoc::IntrusiveLinkage<Thing, &Thing::_next, &Thing::_prev>; | ||
|
|
||
| Thing(std::string_view text) : _payload(text) {} | ||
| }; | ||
|
|
||
| // Just for you, @maskit ! Demonstrating non-public links and subclassing. | ||
| class PrivateThing : protected Thing { | ||
| using self_type = PrivateThing; | ||
| using super_type = Thing; | ||
|
|
||
| public: | ||
| PrivateThing(std::string_view text) : super_type(text) {} | ||
|
|
||
| struct Linkage { | ||
| static self_type *& | ||
| next_ptr(self_type *t) { | ||
| return swoc::ptr_ref_cast<self_type>(t->_next); | ||
| } | ||
| static self_type *& | ||
| prev_ptr(self_type *t) { | ||
| return swoc::ptr_ref_cast<self_type>(t->_prev); | ||
| } | ||
| }; | ||
|
|
||
| std::string const & | ||
| payload() const { | ||
| return _payload; | ||
| } | ||
| }; | ||
|
|
||
| class PrivateThing2 : protected Thing { | ||
| using self_type = PrivateThing2; | ||
| using super_type = Thing; | ||
|
|
||
| public: | ||
| PrivateThing2(std::string_view text) : super_type(text) {} | ||
| using Linkage = swoc::IntrusiveLinkageRebind<self_type, super_type::Linkage>; | ||
| friend Linkage; | ||
|
|
||
| std::string const & | ||
| payload() const { | ||
| return _payload; | ||
| } | ||
| }; | ||
|
|
||
| TEST_CASE("IntrusiveDList Inheritance", "[libswoc][IntrusiveDList][example]") { | ||
| IntrusiveDList<PrivateThing::Linkage> priv_list; | ||
| for (size_t i = 1; i <= 23; ++i) { | ||
| swoc::LocalBufferWriter<16> w; | ||
| w.print("Item {}", i); | ||
| priv_list.append(new PrivateThing(w.view())); | ||
| REQUIRE(priv_list.count() == i); | ||
| } | ||
| REQUIRE(priv_list.head()->payload() == "Item 1"); | ||
| REQUIRE(priv_list.tail()->payload() == "Item 23"); | ||
|
|
||
| IntrusiveDList<PrivateThing2::Linkage> priv2_list; | ||
| for (size_t i = 1; i <= 23; ++i) { | ||
| swoc::LocalBufferWriter<16> w; | ||
| w.print("Item {}", i); | ||
| priv2_list.append(new PrivateThing2(w.view())); | ||
| REQUIRE(priv2_list.count() == i); | ||
| } | ||
| REQUIRE(priv2_list.head()->payload() == "Item 1"); | ||
| REQUIRE(priv2_list.tail()->payload() == "Item 23"); | ||
| } |
Oops, something went wrong.
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.
This puts back what we reverted because origin side session pooling was broken by it. See #10992 where we pulled this out.
To address the functional concerns, see my patch below in HttpSessionManager.cc