Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
disjoint_sparse_table_test.cc File Reference

Comprehensive test suite for Disjoint Sparse Table implementation. More...

#include <cstdio>
#include <iostream>
#include <iomanip>
#include <chrono>
#include <cassert>
#include <cmath>
#include <string>
#include <sstream>
#include <random>
#include <numeric>
#include <climits>
#include <cfloat>
#include <functional>
#include <algorithm>
#include <vector>
#include <tpl_disjoint_sparse_table.H>
#include <tpl_sparse_table.H>
#include <tpl_array.H>
#include <tpl_dynList.H>
Include dependency graph for disjoint_sparse_table_test.cc:

Go to the source code of this file.

Classes

class  Timer
 
struct  Xor_Op
 
struct  String_Concat_Op
 

Macros

#define TEST(name)
 
#define PASS()
 
#define FAIL(msg)
 
#define CHECK(cond, msg)
 
#define CHECK_EQ(a, b, msg)
 
#define CHECK_THROWS(ExType, expr, msg)
 

Functions

template<typename T >
T brute_sum (const vector< T > &v, size_t l, size_t r)
 
template<typename T >
T brute_product (const vector< T > &v, size_t l, size_t r)
 
template<typename T >
T brute_min (const vector< T > &v, size_t l, size_t r)
 
unsigned brute_xor (const vector< unsigned > &v, size_t l, size_t r)
 
void test_empty_table ()
 
void test_single_element_sum ()
 
void test_single_element_product ()
 
void test_two_elements ()
 
void test_all_equal ()
 
void test_sorted_ascending ()
 
void test_sorted_descending ()
 
void test_power_of_two_sizes ()
 
void test_non_power_of_two_sizes ()
 
void test_known_sum_array ()
 
void test_known_product_array ()
 
void test_get_all_elements ()
 
void test_values_reconstruct ()
 
void test_stress_sum_small ()
 
void test_stress_product_small ()
 
void test_stress_sum_large ()
 
void test_stress_point_queries ()
 
void test_stress_exhaustive_small ()
 
void test_xor_known ()
 
void test_xor_stress ()
 
void test_min_cross_validation ()
 
void test_max_cross_validation ()
 
void test_construct_from_array ()
 
void test_construct_from_vector ()
 
void test_construct_from_dynlist ()
 
void test_construct_from_init_list ()
 
void test_construct_uniform ()
 
void test_construct_all_identical ()
 
void test_copy_constructor ()
 
void test_move_constructor ()
 
void test_copy_assignment ()
 
void test_move_assignment ()
 
void test_swap ()
 
void test_exception_r_out_of_range ()
 
void test_exception_l_greater_than_r ()
 
void test_exception_get_out_of_range ()
 
void test_boundary_queries_no_throw ()
 
void test_negative_values ()
 
void test_int_limits ()
 
void test_double_sum ()
 
void test_double_product ()
 
void test_stress_double_sum ()
 
void test_perf_build ()
 
void test_perf_queries ()
 
void test_perf_build_large ()
 
void test_non_idempotent_correctness ()
 
void test_num_levels ()
 
void test_self_copy_assignment ()
 
void test_self_swap ()
 
void test_get_equals_point_query ()
 
void test_splitting_composability ()
 
void test_splitting_product ()
 
void test_prefix_sum_consistency ()
 
void test_adversarial_zigzag ()
 
void test_adversarial_single_outlier ()
 
void test_adversarial_plateau_with_spike ()
 
void test_sliding_window ()
 
void test_string_concatenation ()
 
void test_string_stress ()
 
int main (int argc, char *argv[])
 

Variables

static int tests_passed = 0
 
static int tests_failed = 0
 
static int total_tests = 0
 
static mt19937 rng
 

Detailed Description

Comprehensive test suite for Disjoint Sparse Table implementation.

Tests Gen_Disjoint_Sparse_Table, Sum_Disjoint_Sparse_Table, and Product_Disjoint_Sparse_Table against brute-force baselines with random and adversarial inputs.

TEST CATEGORIES:

  1. Edge cases (empty, single element, two elements, all-equal, sorted)
  2. Basic correctness (small known arrays, point queries)
  3. Brute-force stress tests (random arrays, random queries)
  4. Custom operations (XOR, min cross-validation with Sparse_Table)
  5. Construction from all container types (Array, vector, DynList, init-list)
  6. Copy/move semantics and swap
  7. Exception safety (out-of-range, invalid ranges)
  8. Numerical edge cases (negative values, overflow-prone, doubles)
  9. Performance tests
  10. Cross-validation with classical Sparse Table (idempotent ops)

COMPILE: g++ -std=c++20 -O2 -I.. -o disjoint_sparse_table_test \ disjoint_sparse_table_test.cc

RUN: ./disjoint_sparse_table_test [seed]

If seed is omitted, a random seed based on time is used.

Definition in file disjoint_sparse_table_test.cc.

Macro Definition Documentation

◆ CHECK

#define CHECK (   cond,
  msg 
)
Value:
do { \
if (!(cond)) { FAIL(msg); return; } \
} while(0)
#define FAIL(msg)
DynList< T > maps(const C &c, Op op)
Classic map operation.

Definition at line 113 of file disjoint_sparse_table_test.cc.

◆ CHECK_EQ

#define CHECK_EQ (   a,
  b,
  msg 
)
Value:
do { \
if ((a) != (b)) { \
stringstream ss; \
ss << msg << " (expected " << (b) << ", got " << (a) << ")"; \
FAIL(ss.str()); \
return; \
} \
} while(0)

Definition at line 118 of file disjoint_sparse_table_test.cc.

◆ CHECK_THROWS

#define CHECK_THROWS (   ExType,
  expr,
  msg 
)
Value:
do { \
bool caught = false; \
try { expr; } catch (const ExType &) { caught = true; } \
if (!caught) { FAIL(msg); return; } \
} while(0)

Definition at line 128 of file disjoint_sparse_table_test.cc.

◆ FAIL

#define FAIL (   msg)
Value:
do { \
cout << "\033[31mFAIL\033[0m (" << msg << ")\n"; \
} while(0)
static int tests_failed

Definition at line 107 of file disjoint_sparse_table_test.cc.

◆ PASS

#define PASS ( )
Value:
do { \
cout << "\033[32mPASS\033[0m\n"; \
} while(0)
static int tests_passed

Definition at line 101 of file disjoint_sparse_table_test.cc.

◆ TEST

#define TEST (   name)
Value:
do { \
cout << " Testing: " << name << "... " << flush; \
} while(0)
static int total_tests

Definition at line 95 of file disjoint_sparse_table_test.cc.

Function Documentation

◆ brute_min()

template<typename T >
T brute_min ( const vector< T > &  v,
size_t  l,
size_t  r 
)

Definition at line 173 of file disjoint_sparse_table_test.cc.

References l, and min().

Referenced by scenario_sensor_monitoring().

◆ brute_product()

template<typename T >
T brute_product ( const vector< T > &  v,
size_t  l,
size_t  r 
)

Definition at line 164 of file disjoint_sparse_table_test.cc.

References l.

Referenced by test_stress_product_small().

◆ brute_sum()

◆ brute_xor()

unsigned brute_xor ( const vector< unsigned > &  v,
size_t  l,
size_t  r 
)

Definition at line 181 of file disjoint_sparse_table_test.cc.

References l.

Referenced by test_xor_stress().

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 1179 of file disjoint_sparse_table_test.cc.

References Aleph::maps(), rng, test_adversarial_plateau_with_spike(), test_adversarial_single_outlier(), test_adversarial_zigzag(), test_all_equal(), test_boundary_queries_no_throw(), test_construct_all_identical(), test_construct_from_array(), test_construct_from_dynlist(), test_construct_from_init_list(), test_construct_from_vector(), test_construct_uniform(), test_copy_assignment(), test_copy_constructor(), test_double_product(), test_double_sum(), test_empty_table(), test_exception_get_out_of_range(), test_exception_l_greater_than_r(), test_exception_r_out_of_range(), test_get_all_elements(), test_get_equals_point_query(), test_int_limits(), test_known_product_array(), test_known_sum_array(), test_max_cross_validation(), test_min_cross_validation(), test_move_assignment(), test_move_constructor(), test_negative_values(), test_non_idempotent_correctness(), test_non_power_of_two_sizes(), test_num_levels(), test_perf_build(), test_perf_build_large(), test_perf_queries(), test_power_of_two_sizes(), test_prefix_sum_consistency(), test_self_copy_assignment(), test_self_swap(), test_single_element_product(), test_single_element_sum(), test_sliding_window(), test_sorted_ascending(), test_sorted_descending(), test_splitting_composability(), test_splitting_product(), test_stress_double_sum(), test_stress_exhaustive_small(), test_stress_point_queries(), test_stress_product_small(), test_stress_sum_large(), test_stress_sum_small(), test_string_concatenation(), test_string_stress(), test_swap(), test_two_elements(), test_values_reconstruct(), test_xor_known(), test_xor_stress(), tests_failed, tests_passed, and total_tests.

◆ test_adversarial_plateau_with_spike()

void test_adversarial_plateau_with_spike ( )

◆ test_adversarial_single_outlier()

void test_adversarial_single_outlier ( )

◆ test_adversarial_zigzag()

void test_adversarial_zigzag ( )

◆ test_all_equal()

void test_all_equal ( )

Definition at line 256 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, l, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_boundary_queries_no_throw()

void test_boundary_queries_no_throw ( )

◆ test_construct_all_identical()

void test_construct_all_identical ( )

◆ test_construct_from_array()

◆ test_construct_from_dynlist()

◆ test_construct_from_init_list()

void test_construct_from_init_list ( )

Definition at line 583 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_construct_from_vector()

void test_construct_from_vector ( )

◆ test_construct_uniform()

void test_construct_uniform ( )

Definition at line 591 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_copy_assignment()

void test_copy_assignment ( )

◆ test_copy_constructor()

void test_copy_constructor ( )

Definition at line 631 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, Aleph::copy(), l, Aleph::maps(), PASS, Aleph::HTList::size(), and TEST.

Referenced by main().

◆ test_double_product()

void test_double_product ( )

◆ test_double_sum()

void test_double_sum ( )

◆ test_empty_table()

void test_empty_table ( )

Definition at line 205 of file disjoint_sparse_table_test.cc.

References CHECK, CHECK_EQ, CHECK_THROWS, PASS, and TEST.

Referenced by main().

◆ test_exception_get_out_of_range()

void test_exception_get_out_of_range ( )

◆ test_exception_l_greater_than_r()

void test_exception_l_greater_than_r ( )

◆ test_exception_r_out_of_range()

void test_exception_r_out_of_range ( )

◆ test_get_all_elements()

void test_get_all_elements ( )

Definition at line 363 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::get(), PASS, and TEST.

Referenced by main().

◆ test_get_equals_point_query()

void test_get_equals_point_query ( )

◆ test_int_limits()

void test_int_limits ( )

Definition at line 773 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_known_product_array()

void test_known_product_array ( )

Definition at line 348 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_known_sum_array()

void test_known_sum_array ( )

Definition at line 329 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_max_cross_validation()

void test_max_cross_validation ( )

◆ test_min_cross_validation()

void test_min_cross_validation ( )

◆ test_move_assignment()

◆ test_move_constructor()

void test_move_constructor ( )

Definition at line 645 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, Aleph::maps(), PASS, Aleph::HTList::size(), and TEST.

Referenced by main().

◆ test_negative_values()

void test_negative_values ( )

Definition at line 764 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_non_idempotent_correctness()

void test_non_idempotent_correctness ( )

Definition at line 896 of file disjoint_sparse_table_test.cc.

References brute_sum(), CHECK_EQ, l, Aleph::maps(), PASS, Aleph::HTList::size(), and TEST.

Referenced by main().

◆ test_non_power_of_two_sizes()

◆ test_num_levels()

void test_num_levels ( )

Definition at line 913 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, Aleph::maps(), PASS, and TEST.

Referenced by main().

◆ test_perf_build()

void test_perf_build ( )

◆ test_perf_build_large()

void test_perf_build_large ( )

◆ test_perf_queries()

void test_perf_queries ( )

◆ test_power_of_two_sizes()

◆ test_prefix_sum_consistency()

void test_prefix_sum_consistency ( )

◆ test_self_copy_assignment()

◆ test_self_swap()

◆ test_single_element_product()

void test_single_element_product ( )

Definition at line 234 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_single_element_sum()

◆ test_sliding_window()

void test_sliding_window ( )

◆ test_sorted_ascending()

void test_sorted_ascending ( )

◆ test_sorted_descending()

void test_sorted_descending ( )

Definition at line 281 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_splitting_composability()

void test_splitting_composability ( )

◆ test_splitting_product()

void test_splitting_product ( )

◆ test_stress_double_sum()

void test_stress_double_sum ( )

◆ test_stress_exhaustive_small()

void test_stress_exhaustive_small ( )

◆ test_stress_point_queries()

void test_stress_point_queries ( )

◆ test_stress_product_small()

void test_stress_product_small ( )

◆ test_stress_sum_large()

void test_stress_sum_large ( )

◆ test_stress_sum_small()

void test_stress_sum_small ( )

◆ test_string_concatenation()

◆ test_string_stress()

void test_string_stress ( )

◆ test_swap()

◆ test_two_elements()

void test_two_elements ( )

Definition at line 242 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_values_reconstruct()

void test_values_reconstruct ( )

◆ test_xor_known()

void test_xor_known ( )

Definition at line 475 of file disjoint_sparse_table_test.cc.

References CHECK_EQ, PASS, Aleph::Gen_Disjoint_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_xor_stress()

void test_xor_stress ( )

Variable Documentation

◆ rng

mt19937 rng
static

Definition at line 148 of file disjoint_sparse_table_test.cc.

Referenced by benchmark_comparison(), build_random_graph(), build_random_graph(), demo_dynset_linhash(), demo_performance(), example_parallel_aggregations(), example_parallel_find(), example_parallel_sort(), generate_random_data(), generate_random_graph(), main(), monte_carlo_simulation(), random_ascii_token(), random_string(), run_experiment(), RankTreeTest< Tree >::SetUp(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), test_get_equals_point_query(), test_max_cross_validation(), test_min_cross_validation(), test_non_power_of_two_sizes(), test_perf_build(), test_perf_build_large(), test_perf_queries(), test_power_of_two_sizes(), test_prefix_sum_consistency(), test_sliding_window(), test_splitting_composability(), test_splitting_product(), test_stress_double_sum(), test_stress_exhaustive_small(), test_stress_point_queries(), test_stress_product_small(), test_stress_sum_large(), test_stress_sum_small(), test_xor_stress(), and visual_demonstration().

◆ tests_failed

int tests_failed = 0
static

Definition at line 92 of file disjoint_sparse_table_test.cc.

Referenced by main().

◆ tests_passed

int tests_passed = 0
static

Definition at line 91 of file disjoint_sparse_table_test.cc.

Referenced by main().

◆ total_tests

int total_tests = 0
static

Definition at line 93 of file disjoint_sparse_table_test.cc.

Referenced by main().