From b74e9567fd3bf7e5e587afac0808ecc933453e26 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Tue, 27 Aug 2024 22:07:23 +0200 Subject: [PATCH] More tests --- src/tl/unit_tests/tlBitSetMapTests.cc | 80 +++++++++++++++++++++++++-- 1 file changed, 76 insertions(+), 4 deletions(-) diff --git a/src/tl/unit_tests/tlBitSetMapTests.cc b/src/tl/unit_tests/tlBitSetMapTests.cc index 272a31428..af5be63c8 100644 --- a/src/tl/unit_tests/tlBitSetMapTests.cc +++ b/src/tl/unit_tests/tlBitSetMapTests.cc @@ -25,6 +25,8 @@ #include "tlString.h" #include "tlTimer.h" +#include + static tl::BitSet bs (const char *s) { tl::BitSet res; @@ -66,11 +68,8 @@ struct SetInserter std::set *ps; }; -static std::string match (const tl::bit_set_map &bsm, const tl::BitSet &bs) +static std::string s2s (const std::set &values) { - std::set values; - bsm.lookup (bs, SetInserter (values)); - std::string res; for (auto i = values.begin (); i != values.end (); ++i) { if (!res.empty ()) { @@ -81,6 +80,13 @@ static std::string match (const tl::bit_set_map &bsm, const tl::BitSet &bs) return res; } +static std::string match (const tl::bit_set_map &bsm, const tl::BitSet &bs) +{ + std::set values; + bsm.lookup (bs, SetInserter (values)); + return s2s (values); +} + namespace { @@ -162,4 +168,70 @@ TEST(2_Regular) } } +TEST(3_IrregularTest) +{ + srand (0); + + tl::bit_set_map map; + + unsigned int num = 10000; + unsigned int nbits_min = 10; + unsigned int nbits_max = 20; + + for (unsigned int i = 0; i < num; ++i) { + std::string s; + unsigned int n = nbits_min + (rand () % (nbits_max - nbits_min)); + for (unsigned int j = 0; j < n; ++j) { + // this pattern gives roughly 5 matches per entry with 10k entries + s += "010101X"[rand () % 7]; + } + map.insert (bsm (s.c_str ()), int (i)); + } + + std::vector test_vectors; + for (unsigned int i = 0; i < num; ++i) { + std::string s; + unsigned int n = nbits_min + (rand () % (nbits_max - nbits_min)); + for (unsigned int j = 0; j < n; ++j) { + s += "01"[rand () % 2]; + } + test_vectors.push_back (bs (s.c_str ())); + } + + { + tl::SelfTimer timer ("sorting"); + map.sort (); + } + + std::vector matches; + + { + tl::SelfTimer timer ("match method"); + for (auto i = test_vectors.begin (); i != test_vectors.end (); ++i) { + matches.push_back (match (map, *i)); + } + } + + size_t max_matches = 0; + + // brute force + { + tl::SelfTimer timer ("brute force"); + for (auto i = test_vectors.begin (); i != test_vectors.end (); ++i) { + std::set values; + for (auto j = map.begin (); j != map.end (); ++j) { + if (j->mask.match (*i)) { + values.insert(j->value); + } + } + max_matches = std::max (max_matches, values.size ()); + EXPECT_EQ (s2s (values), matches [i - test_vectors.begin ()]); + } + } + + // sanity check + tl::info << "Max. matches: " << max_matches; + EXPECT_EQ (max_matches > 5, true); +} + }