From 42077481816139f49cd99d2f6a1451a185996c47 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Fri, 6 Dec 2024 13:59:06 -0500 Subject: [PATCH] Add test file --- .../support/tests/TestFluentTreeObject.cpp | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 src/lib/support/tests/TestFluentTreeObject.cpp diff --git a/src/lib/support/tests/TestFluentTreeObject.cpp b/src/lib/support/tests/TestFluentTreeObject.cpp new file mode 100644 index 00000000000000..65caf5d9c54531 --- /dev/null +++ b/src/lib/support/tests/TestFluentTreeObject.cpp @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed 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 "pw_unit_test/framework.h" +#include + +#include +#include +#include + +namespace { + +using namespace chip; + +struct ClusterData +{ + const ClusterId id; + const char * name; +}; + +struct EndpointData +{ + const EndpointId id; + + const char * name; + Span serverClusters; + Span clientClusters; +}; + +struct EndpointItemsWrapper +{ + Span data; + + template + EndpointItemsWrapper(EndpointData (&d)[N]) : data(d) + {} +}; + +const ClusterData gClusterList1[] = { + { + .id = 100, + .name = "one hundred", + }, + { + .id = 200, + .name = "two hundred", + }, +}; + +const ClusterData gClusterList2[] = { + { + .id = 1, + .name = "just one", + }, +}; + +EndpointData gEndpointDataItems[] = { + { + .id = 123, + .name = "foo", + .serverClusters = Span(gClusterList1), + .clientClusters = Span(gClusterList2), + }, + { + .id = 456, + .name = "bar", + .serverClusters = Span(gClusterList2), + .clientClusters = Span(), + }, + { + .id = 1000, + .name = "Empty", + .serverClusters = Span(), + .clientClusters = Span(), + }, +}; + +/// search index definitions +struct ByEndpoint +{ + using Key = EndpointId; // the KEY inside a type │ │ │ + using Type = const EndpointData; // the values for a sub-search │ │ │ + static Span GetSpan(EndpointItemsWrapper & data) { return data.data; } + static bool HasKey(const Key & id, const Type & instance) { return instance.id == id; } +}; + +struct ByServerCluster +{ + using Key = ClusterId; // the KEY inside a type │ │ │ + using Type = const ClusterData; // the values for a sub-search │ │ │ + static Span GetSpan(const EndpointData & data) { return data.serverClusters; } + static bool HasKey(const Key & id, const Type & instance) { return instance.id == id; } +}; + +struct ByClientCluster +{ + using Key = ClusterId; // the KEY inside a type │ │ │ + using Type = const ClusterData; // the values for a sub-search │ │ │ + static Span GetSpan(const EndpointData & data) { return data.clientClusters; } + static bool HasKey(const Key & id, const Type & instance) { return instance.id == id; } +}; + +} // namespace + +TEST(TestFluentTreeObject, TestFunctionality) +{ + EndpointItemsWrapper wrapper(gEndpointDataItems); + FluentTreeObject tree(&wrapper); + + EXPECT_EQ(tree.Value(), &wrapper); // value getting to start matches + + // one level search, with hint + { + size_t hint = 0; + ASSERT_NE(tree.Find(123, hint).Value(), nullptr); + ASSERT_STREQ(tree.Find(123, hint).Value()->name, "foo"); + EXPECT_EQ(hint, 0u); + + ASSERT_NE(tree.Find(456, hint).Value(), nullptr); + EXPECT_EQ(hint, 1u); + EXPECT_STREQ(tree.Find(456, hint).Value()->name, "bar"); + EXPECT_EQ(hint, 1u); + + // hint is ignored here + EXPECT_STREQ(tree.Find(123, hint).Value()->name, "foo"); + EXPECT_EQ(hint, 0u); + + EXPECT_STREQ(tree.Find(1000, hint).Value()->name, "Empty"); + EXPECT_EQ(hint, 2u); + + // Invalid searches + EXPECT_EQ(tree.Find(12345, hint).Value(), nullptr); + EXPECT_EQ(tree.Find(0, hint).Value(), nullptr); + } + + // searches for "next" + { + size_t hint = 0; + auto next = tree.Next(123, hint); + + ASSERT_NE(next.Value(), nullptr); + EXPECT_EQ(hint, 1u); + EXPECT_EQ(next.Value()->id, 456u); + EXPECT_STREQ(next.Value()->name, "bar"); + + next = tree.Next(456, hint); + ASSERT_NE(next.Value(), nullptr); + EXPECT_EQ(hint, 2u); + EXPECT_EQ(next.Value()->id, 1000u); + EXPECT_STREQ(next.Value()->name, "Empty"); + + /// search at the end + next = tree.Next(1000, hint); + EXPECT_EQ(next.Value(), nullptr); + + // null value preserves the failure + size_t clusterHint = 0; + auto sub_item = next.Find(123, clusterHint); + EXPECT_EQ(sub_item.Value(), nullptr); + } +}