Skip to content

Commit 022750f

Browse files
committed
Put RCI workspace regression in the required TA unit target
Move the newly added constructor allocation regression into test_ta and add an independent pairwise ranking reference for live and intrabar values. Restore the unregistered legacy source to its original contents.
1 parent c35a483 commit 022750f

2 files changed

Lines changed: 80 additions & 33 deletions

File tree

‎tests/test_ta.cpp‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
#define PINEFORGE_TEST_ALLOCATION_BYTES
2+
#include "global_allocation_replacement.hpp"
3+
14
#include <cmath>
25
#include <cstdio>
6+
#include <deque>
37
#include <limits>
48
#include <vector>
59

@@ -426,14 +430,89 @@ static int test_tr_subsequent_bars_match_regardless_of_handle_na() {
426430
return fails;
427431
}
428432

433+
static std::size_t rci_constructor_bytes(int length) {
434+
global_allocation::live_bytes = 0;
435+
global_allocation::peak_bytes = 0;
436+
global_allocation::recording = true;
437+
std::size_t retained = 0;
438+
{
439+
ta::RCI indicator(length);
440+
retained = global_allocation::live_bytes;
441+
}
442+
global_allocation::recording = false;
443+
return retained;
444+
}
445+
446+
static int test_rci_constructor_workspace() {
447+
int fails = 0;
448+
for (int length : {7, 37, 129}) {
449+
const auto small = rci_constructor_bytes(length);
450+
const auto large = rci_constructor_bytes(2 * length);
451+
const auto expected = static_cast<std::size_t>(length)
452+
* (sizeof(int) + sizeof(double));
453+
const bool valid = small > 0 && large > small && large - small == expected;
454+
std::printf("RCI workspace length=%d small=%zu large=%zu delta=%zu expected=%zu\n",
455+
length, small, large, large >= small ? large - small : 0, expected);
456+
if (!valid) {
457+
std::printf("FAIL RCI constructor workspace length=%d\n", length);
458+
++fails;
459+
}
460+
}
461+
return fails;
462+
}
463+
464+
static double rci_pairwise_reference(const std::deque<double>& window) {
465+
const int n = static_cast<int>(window.size());
466+
double squared = 0.0;
467+
for (int i = 0; i < n; ++i) {
468+
int less = 0, equal = 0;
469+
for (double value : window) {
470+
less += value < window[i];
471+
equal += value == window[i];
472+
}
473+
const double rank = less + (equal - 1) / 2.0;
474+
const double difference = static_cast<double>(i) - rank;
475+
squared += difference * difference;
476+
}
477+
return (1.0 - 6.0 * squared / (n * (n * n - 1))) * 100.0;
478+
}
479+
480+
static int test_rci_live_window_values() {
481+
int fails = 0;
482+
const double values[] = {3.0, 1.0, 1.0, 2.0, 7.0, -0.0, 0.0,
483+
5.0, -2.0, 5.0, 9.0, 4.0, 4.0, 2.0, 8.0};
484+
for (int length : {2, 5, 9}) {
485+
ta::RCI indicator(length);
486+
std::deque<double> window;
487+
for (double value : values) {
488+
window.push_back(value);
489+
if (static_cast<int>(window.size()) > length) window.pop_front();
490+
const auto expected = [&] {
491+
return static_cast<int>(window.size()) < length
492+
? na<double>() : rci_pairwise_reference(window);
493+
};
494+
if (!near(indicator.compute(value), expected())) ++fails;
495+
for (int tick = 0; tick < 4; ++tick) {
496+
window.back() = value + (tick - 1) * 0.25;
497+
if (!near(indicator.recompute(window.back()), expected())) ++fails;
498+
}
499+
if (!is_na(indicator.compute(na<double>()))) ++fails;
500+
}
501+
}
502+
if (fails) std::printf("FAIL RCI live-window pairwise comparisons: %d\n", fails);
503+
return fails;
504+
}
505+
429506
int main() {
430507
int fails = test_linreg_na_in_window()
431508
+ test_percentrank_pine_semantics()
432509
+ test_ema_seeds_from_first_value()
433510
+ test_ema_ignores_na_inputs()
434511
+ test_sar_matches_pine_reference_initialization()
435512
+ test_tr_handle_na_default_returns_na_on_first_bar()
436-
+ test_tr_subsequent_bars_match_regardless_of_handle_na();
513+
+ test_tr_subsequent_bars_match_regardless_of_handle_na()
514+
+ test_rci_constructor_workspace()
515+
+ test_rci_live_window_values();
437516
if (fails > 0) {
438517
std::printf("test_ta: %d TA checks failed\n", fails);
439518
return 1;

‎tests/test_ta_window_optimizations.cpp‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -951,37 +951,6 @@ void check_window_allocations(const char* name, int length) {
951951
check_true(old_counts.other >= static_cast<std::size_t>(kAllocationCalls), label);
952952
}
953953

954-
std::size_t rci_constructor_bytes(int length) {
955-
// Every recorded object is destroyed before the next recording window.
956-
global_allocation::live_bytes = 0;
957-
global_allocation::peak_bytes = 0;
958-
global_allocation::recording = true;
959-
std::size_t retained = 0;
960-
{
961-
ta::RCI indicator(length);
962-
retained = global_allocation::live_bytes;
963-
}
964-
global_allocation::recording = false;
965-
return retained;
966-
}
967-
968-
void test_rci_constructor_workspace() {
969-
// Before values arrive, only the permutation and rank workspaces should
970-
// scale with length. The empty live deque has length-independent storage.
971-
// Measure real requested heap bytes, not a timer or a reported constant.
972-
for (int length : {7, 37, 129}) {
973-
const auto small = rci_constructor_bytes(length);
974-
const auto large = rci_constructor_bytes(2 * length);
975-
const auto expected = static_cast<std::size_t>(length)
976-
* (sizeof(int) + sizeof(double));
977-
char label[160];
978-
std::snprintf(label, sizeof(label),
979-
"RCI constructor length=%d measured workspace delta=%zu expected=%zu",
980-
length, large >= small ? large - small : 0, expected);
981-
check_true(small > 0 && large > small && large - small == expected, label);
982-
}
983-
}
984-
985954
void test_allocations() {
986955
for (int length : {5, 50}) {
987956
check_window_allocations<ta::RCI, RefRCI>("RCI", length);
@@ -1006,7 +975,6 @@ int main() {
1006975
test_mfi_cmo();
1007976
test_recompute_paths();
1008977
test_allocations();
1009-
test_rci_constructor_workspace();
1010978

1011979
std::printf("test_ta_window_optimizations: checks=%d failures=%d\n",
1012980
checks, failures);

0 commit comments

Comments
 (0)