MCPcopy Create free account
hub / github.com/ashvardanian/StringZilla / test_intersecting_algorithms

Function test_intersecting_algorithms

scripts/test_stringzilla.cpp:4362–4435  ·  view source on GitHub ↗

* @brief Tests array intersection functionality. */

Source from the content-addressed store, hash-verified

4360 * @brief Tests array intersection functionality.
4361 */
4362void test_intersecting_algorithms() {
4363 using strs_t = std::vector<std::string>;
4364 using result_t = sz::intersect_result_t;
4365
4366 // The mapping aren't guaranteed to be in any specific order, so we will sort them for comparisons.
4367 using idx_pair_t = std::pair<std::size_t, std::size_t>;
4368 using idx_pairs_t = std::set<idx_pair_t>;
4369 auto to_pairs = [](result_t const &result) -> idx_pairs_t {
4370 idx_pairs_t pairs;
4371 for (std::size_t i = 0; i < result.first_offsets.size(); ++i)
4372 pairs.insert({result.first_offsets[i], result.second_offsets[i]});
4373 return pairs;
4374 };
4375
4376 // Predetermined simple cases
4377 {
4378 strs_t abcd({"a", "b", "c", "d"});
4379 strs_t dcba({"d", "c", "b", "a"});
4380 strs_t abs({"a", "b", "s"});
4381 strs_t empty;
4382 result_t result;
4383 // Empty sets
4384 {
4385 result = sz::intersect(empty, empty);
4386 assert(result.first_offsets.size() == 0 && result.second_offsets.size() == 0);
4387 result = sz::intersect(abcd, empty);
4388 assert(result.first_offsets.size() == 0 && result.second_offsets.size() == 0);
4389 }
4390 // Identity check
4391 {
4392 result = sz::intersect(abcd, abcd);
4393 assert(result.first_offsets.size() == 4 && result.second_offsets.size() == 4);
4394 assert(to_pairs(result) == idx_pairs_t({{0u, 0u}, {1u, 1u}, {2u, 2u}, {3u, 3u}}));
4395 }
4396 // Identical size, different order
4397 {
4398 result = sz::intersect(abcd, dcba);
4399 assert(result.first_offsets.size() == 4 && result.second_offsets.size() == 4);
4400 assert(to_pairs(result) == idx_pairs_t({{0u, 3u}, {1u, 2u}, {2u, 1u}, {3u, 0u}}));
4401 }
4402 // Different sets
4403 {
4404 result = sz::intersect(abcd, abs);
4405 assert(result.first_offsets.size() == 2 && result.second_offsets.size() == 2);
4406 assert(to_pairs(result) == idx_pairs_t({{0u, 0u}, {1u, 1u}}));
4407 }
4408 }
4409
4410 // Generate random strings
4411 struct {
4412 std::size_t min_length;
4413 std::size_t max_length;
4414 std::size_t count_strings;
4415 } experiments[] = {
4416 {10, 10, 100},
4417 {15, 15, 1000},
4418 {5, 30, 2000},
4419 };

Callers 1

mainFunction · 0.85

Calls 6

intersectFunction · 0.85
random_stringFunction · 0.85
insertMethod · 0.80
sizeMethod · 0.45
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…