From: Andrei Pavel Date: Fri, 6 May 2022 05:56:19 +0000 (+0300) Subject: [#562] add an ordered_unique index to ClientClassContainer X-Git-Tag: Kea-2.1.6~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe76e0f0788560d89ee30fb1a30058da54248dcf;p=thirdparty%2Fkea.git [#562] add an ordered_unique index to ClientClassContainer --- diff --git a/src/lib/dhcp/classify.h b/src/lib/dhcp/classify.h index d304dee6cc..4f3a0c1401 100644 --- a/src/lib/dhcp/classify.h +++ b/src/lib/dhcp/classify.h @@ -7,10 +7,12 @@ #ifndef CLASSIFY_H #define CLASSIFY_H +#include #include #include +#include #include -#include + #include /// @file classify.h @@ -55,6 +57,10 @@ namespace dhcp { boost::multi_index::hashed_unique< boost::multi_index::tag, boost::multi_index::identity + >, + // Third index orders lexicographically. + boost::multi_index::ordered_unique< + boost::multi_index::identity > > > ClientClassContainer; @@ -131,6 +137,14 @@ namespace dhcp { } /// @} + /// @brief Returns an index that allows iteration through a sorted set + /// of all the client classes. + /// + /// @return the index iterable through range-based for looping + ClientClassContainer::nth_index<2>::type const& sorted() const { + return container_.get<2>(); + } + /// @brief returns if class x belongs to the defined classes /// /// @param x client class to be checked diff --git a/src/lib/dhcp/tests/pkt4_unittest.cc b/src/lib/dhcp/tests/pkt4_unittest.cc index d01849891e..7029abfbd2 100644 --- a/src/lib/dhcp/tests/pkt4_unittest.cc +++ b/src/lib/dhcp/tests/pkt4_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (C) 2011-2021 Internet Systems Consortium, Inc. ("ISC") +// Copyright (C) 2011-2022 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this @@ -1386,4 +1386,28 @@ TEST_F(Pkt4Test, testSkipThisOptionError) { EXPECT_EQ("def", opstr->getValue()); } +// Check that sorted() allows sorted iteration. +TEST_F(Pkt4Test, sortedClientClasses) { + // Add a bunch of classes to a packet. + Pkt4 packet(DHCPDISCOVER, 1234); + packet.addClass("foo"); + packet.addClass("bar"); + packet.addClass("2"); + packet.addClass("10"); + + // Check that the usual iteration yields items in order of insertion. + std::string client_classes; + for (ClientClass const& c : packet.getClasses()) { + client_classes += c + ','; + } + EXPECT_EQ(client_classes, "ALL,foo,bar,2,10,"); + + // Check sorted() yields sorted items. + std::string sorted_client_classes; + for (ClientClass const& c : packet.getClasses().sorted()) { + sorted_client_classes += c + ','; + } + EXPECT_EQ(sorted_client_classes, "10,2,ALL,bar,foo,"); +} + } // end of anonymous namespace diff --git a/src/lib/dhcp/tests/pkt6_unittest.cc b/src/lib/dhcp/tests/pkt6_unittest.cc index c4878f1595..9afd8f2671 100644 --- a/src/lib/dhcp/tests/pkt6_unittest.cc +++ b/src/lib/dhcp/tests/pkt6_unittest.cc @@ -2115,4 +2115,28 @@ TEST_F(Pkt6Test, relayDataOption) { EXPECT_EQ(orig_data, clone_data); } +// Check that sorted() allows sorted iteration. +TEST_F(Pkt6Test, sortedClientClasses) { + // Add a bunch of classes to a packet. + Pkt6 packet(DHCPV6_SOLICIT, 1234); + packet.addClass("foo"); + packet.addClass("bar"); + packet.addClass("2"); + packet.addClass("10"); + + // Check that the usual iteration yields items in order of insertion. + std::string client_classes; + for (ClientClass const& c : packet.getClasses()) { + client_classes += c + ','; + } + EXPECT_EQ(client_classes, "ALL,foo,bar,2,10,"); + + // Check sorted() yields sorted items. + std::string sorted_client_classes; + for (ClientClass const& c : packet.getClasses().sorted()) { + sorted_client_classes += c + ','; + } + EXPECT_EQ(sorted_client_classes, "10,2,ALL,bar,foo,"); } + +} // namespace