|
| 1 | +#define PINEFORGE_TEST_ALLOCATION_BYTES |
| 2 | +#include "global_allocation_replacement.hpp" |
| 3 | + |
1 | 4 | #include <cmath> |
2 | 5 | #include <cstdio> |
| 6 | +#include <deque> |
3 | 7 | #include <limits> |
4 | 8 | #include <vector> |
5 | 9 |
|
@@ -426,14 +430,89 @@ static int test_tr_subsequent_bars_match_regardless_of_handle_na() { |
426 | 430 | return fails; |
427 | 431 | } |
428 | 432 |
|
| 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 | + |
429 | 506 | int main() { |
430 | 507 | int fails = test_linreg_na_in_window() |
431 | 508 | + test_percentrank_pine_semantics() |
432 | 509 | + test_ema_seeds_from_first_value() |
433 | 510 | + test_ema_ignores_na_inputs() |
434 | 511 | + test_sar_matches_pine_reference_initialization() |
435 | 512 | + 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(); |
437 | 516 | if (fails > 0) { |
438 | 517 | std::printf("test_ta: %d TA checks failed\n", fails); |
439 | 518 | return 1; |
|
0 commit comments