/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * 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 #include #include namespace apache { namespace thrift { template struct AllSamePolicy : BaseDistinctTablePolicy { struct Hash { size_t operator()(const T&) const { return 0; } }; struct Equal { bool operator()(const T&, const T&) const { return true; } }; }; template struct OneByteIndexPolicy : BaseDistinctTablePolicy { template using Index = std::unordered_set; }; TEST(DistinctTable, Basic) { std::vector words; DistinctTable wordTable(&words); EXPECT_EQ(0, wordTable.add("hi")); EXPECT_EQ(1, wordTable.add("bye")); EXPECT_EQ(0, wordTable.add("hi")); EXPECT_EQ(2, wordTable.add("what")); EXPECT_EQ(1, wordTable.add("bye")); EXPECT_EQ(2, wordTable.add("what")); EXPECT_EQ(0, wordTable.add("hi")); EXPECT_EQ("hi", words[0]); EXPECT_EQ("bye", words[1]); EXPECT_EQ("what", words[2]); EXPECT_EQ(3, words.size()); } TEST(DistinctTable, AllSamePolicy) { std::vector words; DistinctTable wordTable(&words); EXPECT_EQ(0, wordTable.add("hi")); // Null hash/equal functions think everything is the same EXPECT_EQ(0, wordTable.add("bye")); EXPECT_EQ(0, wordTable.add("hi")); } TEST(DistinctTable, ByteIndexPolicy) { std::vector nums; DistinctTable numTable(&nums); for (int i = 0; i < 500; ++i) { if (i < 256) { EXPECT_EQ(i, numTable.add(sqrt(i))); } else { EXPECT_LT(numTable.add(sqrt(i)), 256); } } } } // namespace thrift } // namespace apache