Files
lightgui/widgets/tests/ind_csv_harness.cpp
cnmcdee 90880928c9 Ind* CSV series API: load_csv/set_series/clear_series on all 250 indicators
Every Ind* widget recomputes its lines from a loaded OHLCV series:
private recompute_lines() (moved out of the constructor), public
set_series()/load_csv()/clear_series()/series() matching the Stock*
chart widgets, same loader and CSV spec (StockData.h). Constructor
defaults keep the seeded 250-bar template; failed loads leave the
series untouched; clear_series() empties lines and draws clean.
Params that were constructor-only (fast/slow/period/...) are now
stored as members so a reloaded series uses the same settings.

Tests: widgets/tests/ind_csv_harness.cpp + test_ind_csv.py — 250/250
harnesses build, 250/250 numeric PASS, 39/39 screenshots PASS, 250/250
demo mains build. Docs: docs/stock-market-csv-data.md Indicators
section rewritten; all 250 cdoc pages updated (update-ind-csv.py).
2026-12-02 11:00:19 +00:00

147 lines
5.9 KiB
C++

// ind_csv_harness.cpp — per-widget harness for the Ind* CSV series API.
//
// Compiled once per indicator with:
// g++ -std=c++17 -O2 -Wall -Wextra -I widgets/components
// -DIND_HEADER=IndSMA/IndSMA.h -DIND_NAME=IndSMA
// -DIND_LINE_FN=line_values
// widgets/tests/ind_csv_harness.cpp -lX11 -o /tmp/ind_harness_IndSMA
//
// IND_LINE_FN is the widget's line accessor: line_values (165 widgets) or
// line (85 widgets). Modes (argv[1]):
// numeric <csvA> <csvB> template dump, load_csv(A), set_series(B),
// bad-path load, clear_series + draw; prints
// machine-readable lines and exits.
// shot-template draw the template series, stay alive until the
// window is closed (driver screenshots it).
// shot-csv <csv> same, after load_csv(csv).
// Attribution: XFrame.
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <string>
#define _IND_STR(x) #x
#define IND_STR(x) _IND_STR(x)
#include IND_STR(IND_HEADER)
// 85 of the 250 widgets declare their class in the global namespace
// (their namespace xframe closes before the class); the other 165 are
// xframe::Ind*. The using-directive makes the unqualified name work for
// both shapes.
using namespace xframe;
static const char* kName = IND_STR(IND_NAME);
template <typename W>
static void dump_lines(const char* tag, W& w) {
std::printf("%s lines=%d items=%d\n", tag, w.line_count(),
(int)w.series().size());
for (int i = 0; i < w.line_count(); ++i) {
const std::vector<double>& v = w.IND_LINE_FN(i);
double last = std::numeric_limits<double>::quiet_NaN();
double sum = 0.0, sumsq = 0.0;
long finite = 0;
for (int j = (int)v.size() - 1; j >= 0; --j)
if (std::isfinite(v[(size_t)j])) { last = v[(size_t)j]; break; }
for (double x : v)
if (std::isfinite(x)) { sum += x; sumsq += x * x; ++finite; }
std::printf("%s line %d n=%d finite=%ld last=%.6g sum=%.6g sumsq=%.6g\n",
tag, i, (int)v.size(), finite, last, sum, sumsq);
}
std::fflush(stdout);
}
int main(int argc, char** argv) {
const char* mode = argc > 1 ? argv[1] : "numeric";
Display* display = XOpenDisplay(nullptr);
if (!display) { std::printf("NO DISPLAY\n"); return 1; }
int screen = DefaultScreen(display);
XID Xid = RootWindow(display, screen);
Window window = XCreateSimpleWindow(display, Xid, 100, 100, 800, 600, 1,
BlackPixel(display, screen),
WhitePixel(display, screen));
std::string title = std::string(kName) + " - XFrame demo";
XStoreName(display, window, title.c_str());
XSelectInput(display, window,
ExposureMask | KeyPressMask | StructureNotifyMask |
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
XMapWindow(display, window);
GC gc = DefaultGC(display, screen);
SStyle_Manager styles(display, window, screen, gc);
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
"-misc-fixed-medium-r-normal--13-*");
styles.set_active_style_byName("default");
(void)screen;
// Construct with the seeded template series (250 bars).
IND_NAME widget(display, screen, window, gc, &styles, 20, 20, 720, 480);
widget.draw();
if (std::strcmp(mode, "numeric") == 0) {
dump_lines("template", widget);
std::string err;
if (!widget.load_csv(argv[2], err)) {
std::printf("csv_load FAIL %s\n", err.c_str());
return 2;
}
std::printf("csv_load ok items=%d\n", (int)widget.series().size());
widget.draw();
dump_lines("csv", widget);
// set_series path with the second file.
std::string err2;
xframe::OHLCVSeries s2 = xframe::load_ohlcv_csv(argv[3], err2);
if (!err2.empty()) { std::printf("csvB_load FAIL %s\n", err2.c_str()); return 2; }
widget.set_series(s2);
std::printf("set_series ok items=%d\n", (int)widget.series().size());
widget.draw();
dump_lines("csvB", widget);
// A failed load must leave the series untouched.
std::string err3;
bool bad = widget.load_csv("/tmp/does-not-exist.csv", err3);
std::printf("badpath ok=%d err_set=%d items=%d\n", (int)!bad,
(int)!err3.empty(), (int)widget.series().size());
if (bad) return 3;
dump_lines("after_badpath", widget);
// Clear: empty series, empty lines, draw must not crash.
widget.clear_series();
std::printf("clear items=%d lines=%d\n", (int)widget.series().size(),
widget.line_count());
widget.draw();
dump_lines("clear", widget);
XFlush(display);
std::printf("NUMERIC DONE %s\n", kName);
XCloseDisplay(display);
return 0;
}
if (std::strcmp(mode, "shot-csv") == 0) {
std::string err;
if (!widget.load_csv(argv[2], err)) {
std::fprintf(stderr, "csv: %s\n", err.c_str());
return 2;
}
}
widget.draw();
widget.on_button_press(400, 300, 1);
widget.on_button_release(400, 300, 1);
widget.on_motion(410, 300);
widget.on_key(XK_Right, "");
widget.draw();
XFlush(display);
std::printf("SHOT READY %s %s\n", kName, mode);
std::fflush(stdout);
// Stay alive until the driver closes the window.
while (true) {
XEvent ev;
XNextEvent(display, &ev);
if (ev.type == Expose) widget.draw();
if (ev.type == ClientMessage) break;
}
XCloseDisplay(display);
return 0;
}