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

Comprehensive test suite for 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_sparse_table.H>
#include <tpl_array.H>
#include <tpl_dynList.H>
Include dependency graph for sparse_table_test.cc:

Go to the source code of this file.

Classes

class  Timer
 
struct  Gcd_Op
 
struct  And_Op
 
struct  Or_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_min (const vector< T > &v, size_t l, size_t r)
 
template<typename T >
T brute_max (const vector< T > &v, size_t l, size_t r)
 
int brute_gcd (const vector< int > &v, size_t l, size_t r)
 
int brute_and (const vector< int > &v, size_t l, size_t r)
 
int brute_or (const vector< int > &v, size_t l, size_t r)
 
void test_empty_table ()
 
void test_single_element ()
 
void test_single_element_max ()
 
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_min_array ()
 
void test_known_max_array ()
 
void test_get_all_elements ()
 
void test_values_reconstruction ()
 
void test_stress_min_small ()
 
void test_stress_max_small ()
 
void test_stress_min_medium ()
 
void test_stress_all_point_queries ()
 
void test_stress_all_pairs_small ()
 
void test_gcd_known ()
 
void test_gcd_stress ()
 
void test_bitwise_and_stress ()
 
void test_bitwise_or_stress ()
 
void test_construct_from_array ()
 
void test_construct_from_vector ()
 
void test_construct_from_dynlist ()
 
void test_construct_from_initlist ()
 
void test_construct_uniform_value ()
 
void test_all_constructors_agree ()
 
void test_copy_constructor ()
 
void test_move_constructor ()
 
void test_copy_assignment ()
 
void test_move_assignment ()
 
void test_swap ()
 
void test_query_r_out_of_range ()
 
void test_query_l_greater_than_r ()
 
void test_get_out_of_range ()
 
void test_boundary_queries_valid ()
 
void test_negative_values ()
 
void test_int_extremes ()
 
void test_double_values ()
 
void test_double_stress ()
 
void test_long_long_values ()
 
void test_performance_build ()
 
void test_performance_queries ()
 
void test_performance_build_large ()
 
void test_overlapping_ranges_idempotent ()
 
void test_num_levels_correctness ()
 
void test_self_copy_assignment ()
 
void test_self_swap ()
 
void test_get_equals_point_query ()
 
void test_idempotent_overlap_split ()
 
void test_disjoint_split_min ()
 
void test_adversarial_zigzag ()
 
void test_adversarial_single_outlier ()
 
void test_adversarial_plateau_with_dip ()
 
void test_sliding_window ()
 
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 Sparse Table implementation.

Tests Gen_Sparse_Table, Sparse_Table (min), and Max_Sparse_Table (max) against brute-force baselines with random and adversarial inputs.

TEST CATEGORIES:

  1. Edge cases (single element, two elements, all-equal)
  2. Basic correctness (small known arrays, point queries)
  3. Brute-force stress tests (random arrays, random queries)
  4. Custom idempotent operations (GCD, bitwise AND, bitwise OR)
  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. Large-scale performance tests
  9. Numerical edge cases (negative values, INT_MIN/INT_MAX, doubles)

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

RUN: ./sparse_table_test [seed]

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

Definition in file sparse_table_test.cc.

Macro Definition Documentation

◆ CHECK

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

Definition at line 108 of file 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 113 of file 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 123 of file 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 102 of file sparse_table_test.cc.

◆ PASS

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

Definition at line 96 of file sparse_table_test.cc.

◆ TEST

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

Definition at line 90 of file sparse_table_test.cc.

Function Documentation

◆ brute_and()

int brute_and ( const vector< int > &  v,
size_t  l,
size_t  r 
)

Definition at line 175 of file sparse_table_test.cc.

References l.

Referenced by test_bitwise_and_stress().

◆ brute_gcd()

int brute_gcd ( const vector< int > &  v,
size_t  l,
size_t  r 
)

Definition at line 167 of file sparse_table_test.cc.

References l, and Aleph::maps().

Referenced by test_gcd_stress().

◆ brute_max()

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

Definition at line 159 of file sparse_table_test.cc.

References l, and max().

Referenced by test_double_stress(), test_stress_all_pairs_small(), and test_stress_max_small().

◆ brute_min()

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

◆ brute_or()

int brute_or ( const vector< int > &  v,
size_t  l,
size_t  r 
)

Definition at line 183 of file sparse_table_test.cc.

References l.

Referenced by test_bitwise_or_stress().

◆ main()

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

Definition at line 1238 of file sparse_table_test.cc.

References Aleph::maps(), rng, test_adversarial_plateau_with_dip(), test_adversarial_single_outlier(), test_adversarial_zigzag(), test_all_constructors_agree(), test_all_equal(), test_bitwise_and_stress(), test_bitwise_or_stress(), test_boundary_queries_valid(), test_construct_from_array(), test_construct_from_dynlist(), test_construct_from_initlist(), test_construct_from_vector(), test_construct_uniform_value(), test_copy_assignment(), test_copy_constructor(), test_disjoint_split_min(), test_double_stress(), test_double_values(), test_empty_table(), test_gcd_known(), test_gcd_stress(), test_get_all_elements(), test_get_equals_point_query(), test_get_out_of_range(), test_idempotent_overlap_split(), test_int_extremes(), test_known_max_array(), test_known_min_array(), test_long_long_values(), test_move_assignment(), test_move_constructor(), test_negative_values(), test_non_power_of_two_sizes(), test_num_levels_correctness(), test_overlapping_ranges_idempotent(), test_performance_build(), test_performance_build_large(), test_performance_queries(), test_power_of_two_sizes(), test_query_l_greater_than_r(), test_query_r_out_of_range(), test_self_copy_assignment(), test_self_swap(), test_single_element(), test_single_element_max(), test_sliding_window(), test_sorted_ascending(), test_sorted_descending(), test_stress_all_pairs_small(), test_stress_all_point_queries(), test_stress_max_small(), test_stress_min_medium(), test_stress_min_small(), test_swap(), test_two_elements(), test_values_reconstruction(), tests_failed, tests_passed, and total_tests.

◆ test_adversarial_plateau_with_dip()

void test_adversarial_plateau_with_dip ( )

Definition at line 1198 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_adversarial_single_outlier()

void test_adversarial_single_outlier ( )

Definition at line 1178 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_adversarial_zigzag()

void test_adversarial_zigzag ( )

Definition at line 1162 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_all_constructors_agree()

void test_all_constructors_agree ( )

◆ test_all_equal()

void test_all_equal ( )

Definition at line 246 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_bitwise_and_stress()

void test_bitwise_and_stress ( )

◆ test_bitwise_or_stress()

void test_bitwise_or_stress ( )

◆ test_boundary_queries_valid()

void test_boundary_queries_valid ( )

◆ test_construct_from_array()

◆ test_construct_from_dynlist()

void test_construct_from_dynlist ( )

◆ test_construct_from_initlist()

void test_construct_from_initlist ( )

◆ test_construct_from_vector()

void test_construct_from_vector ( )

◆ test_construct_uniform_value()

void test_construct_uniform_value ( )

◆ test_copy_assignment()

void test_copy_assignment ( )

◆ test_copy_constructor()

void test_copy_constructor ( )

Definition at line 749 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_disjoint_split_min()

void test_disjoint_split_min ( )

Definition at line 1141 of file sparse_table_test.cc.

References CHECK_EQ, l, Aleph::maps(), min(), PASS, Aleph::Gen_Sparse_Table< T, Op >::query(), rng, and TEST.

Referenced by main().

◆ test_double_stress()

void test_double_stress ( )

Definition at line 899 of file sparse_table_test.cc.

References brute_max(), brute_min(), FAIL, Aleph::maps(), N, PASS, rng, Aleph::swap(), and TEST.

Referenced by main().

◆ test_double_values()

void test_double_values ( )

Definition at line 887 of file sparse_table_test.cc.

References CHECK, Aleph::maps(), PASS, Aleph::Gen_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_empty_table()

void test_empty_table ( )

Definition at line 195 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_gcd_known()

void test_gcd_known ( )

Definition at line 554 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_gcd_stress()

void test_gcd_stress ( )

◆ test_get_all_elements()

void test_get_all_elements ( )

Definition at line 354 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_get_equals_point_query()

void test_get_equals_point_query ( )

◆ test_get_out_of_range()

void test_get_out_of_range ( )

Definition at line 834 of file sparse_table_test.cc.

References CHECK_THROWS, Aleph::Gen_Sparse_Table< T, Op >::get(), PASS, and TEST.

Referenced by main().

◆ test_idempotent_overlap_split()

void test_idempotent_overlap_split ( )

Definition at line 1119 of file sparse_table_test.cc.

References CHECK_EQ, l, Aleph::maps(), min(), PASS, Aleph::Gen_Sparse_Table< T, Op >::query(), rng, and TEST.

Referenced by main().

◆ test_int_extremes()

void test_int_extremes ( )

Definition at line 874 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_known_max_array()

void test_known_max_array ( )

Definition at line 340 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_known_min_array()

void test_known_min_array ( )

Definition at line 321 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_long_long_values()

void test_long_long_values ( )

Definition at line 931 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_move_assignment()

void test_move_assignment ( )

◆ test_move_constructor()

void test_move_constructor ( )

Definition at line 762 of file 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 863 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_non_power_of_two_sizes()

void test_non_power_of_two_sizes ( )

◆ test_num_levels_correctness()

void test_num_levels_correctness ( )

Definition at line 1054 of file sparse_table_test.cc.

References FAIL, Aleph::maps(), Aleph::Gen_Sparse_Table< T, Op >::num_levels(), PASS, and TEST.

Referenced by main().

◆ test_overlapping_ranges_idempotent()

void test_overlapping_ranges_idempotent ( )

Definition at line 1026 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_performance_build()

void test_performance_build ( )

Definition at line 950 of file sparse_table_test.cc.

References CHECK, CHECK_EQ, Aleph::maps(), N, PASS, rng, Aleph::Gen_Sparse_Table< T, Op >::size(), and TEST.

Referenced by main().

◆ test_performance_build_large()

void test_performance_build_large ( )

◆ test_performance_queries()

void test_performance_queries ( )

Definition at line 968 of file sparse_table_test.cc.

References CHECK, Aleph::maps(), N, PASS, Aleph::Gen_Sparse_Table< T, Op >::query(), rng, Aleph::swap(), and TEST.

Referenced by main().

◆ test_power_of_two_sizes()

void test_power_of_two_sizes ( )

◆ test_query_l_greater_than_r()

void test_query_l_greater_than_r ( )

Definition at line 825 of file sparse_table_test.cc.

References CHECK_THROWS, PASS, Aleph::Gen_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_query_r_out_of_range()

void test_query_r_out_of_range ( )

Definition at line 814 of file sparse_table_test.cc.

References CHECK_THROWS, PASS, Aleph::Gen_Sparse_Table< T, Op >::query(), and TEST.

Referenced by main().

◆ test_self_copy_assignment()

◆ test_self_swap()

◆ test_single_element()

◆ test_single_element_max()

void test_single_element_max ( )

Definition at line 224 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_sliding_window()

void test_sliding_window ( )

Definition at line 1219 of file sparse_table_test.cc.

References brute_min(), CHECK_EQ, l, PASS, Aleph::Gen_Sparse_Table< T, Op >::query(), rng, TEST, and w.

Referenced by main().

◆ test_sorted_ascending()

void test_sorted_ascending ( )

Definition at line 257 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_sorted_descending()

void test_sorted_descending ( )

Definition at line 271 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_stress_all_pairs_small()

void test_stress_all_pairs_small ( )

Definition at line 490 of file sparse_table_test.cc.

References brute_max(), brute_min(), FAIL, l, Aleph::maps(), N, PASS, rng, and TEST.

Referenced by main().

◆ test_stress_all_point_queries()

void test_stress_all_point_queries ( )

◆ test_stress_max_small()

void test_stress_max_small ( )

◆ test_stress_min_medium()

void test_stress_min_medium ( )

◆ test_stress_min_small()

void test_stress_min_small ( )

◆ test_swap()

◆ test_two_elements()

void test_two_elements ( )

Definition at line 232 of file sparse_table_test.cc.

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

Referenced by main().

◆ test_values_reconstruction()

void test_values_reconstruction ( )

Variable Documentation

◆ rng

◆ tests_failed

int tests_failed = 0
static

Definition at line 87 of file sparse_table_test.cc.

Referenced by main().

◆ tests_passed

int tests_passed = 0
static

Definition at line 86 of file sparse_table_test.cc.

Referenced by main().

◆ total_tests

int total_tests = 0
static

Definition at line 88 of file sparse_table_test.cc.

Referenced by main().