- xframe::load_ohlcv_csv / parse_ohlcv_csv_text in StockData.h: date,open,high,low,close,volume, optional header matched by name (Massive exports load as-is), YYYY-MM-DD / ISO / epoch s-ms-ns dates, volume optional, malformed rows skipped + counted, error string out. - 15 single-series widgets gain load_csv() + clear_series(); 8 comparative widgets gain add_series_csv(); failure leaves the series untouched. Drawing tools intentionally excluded (coordinates, not series); Ind* widgets take CSV only via the constructor. - Fixtures tests/data/ohlcv_sample.csv (+ _b, 30 rows each) and widgets/tests/csv_unit_test.cpp + test_stock_csv.py (23/23 PASS: CSV shot differs from template shot, 30 items, bad path exits 2). - docs/stock-market-csv-data.md + CSV sections in the stock group docs.
101 lines
5.3 KiB
C++
101 lines
5.3 KiB
C++
// Unit test for xframe::load_ohlcv_csv / parse_ohlcv_csv_text (StockData.h).
|
|
// Compile: g++ -std=c++17 -Wall -Wextra -I widgets/components csv_unit_test.cpp -o /tmp/csv_unit_test
|
|
#include "StockData.h"
|
|
|
|
#include <cstdio>
|
|
#include <string>
|
|
|
|
static int failures = 0;
|
|
#define CHECK(cond, msg) do { \
|
|
if (!(cond)) { std::printf("FAIL: %s\n", msg); ++failures; } \
|
|
} while (0)
|
|
|
|
int main() {
|
|
std::string err;
|
|
int skipped = -1;
|
|
|
|
// 1. The 30-row sample fixture (header, YYYY-MM-DD dates).
|
|
auto s = xframe::load_ohlcv_csv("tests/data/ohlcv_sample.csv", err, &skipped);
|
|
CHECK(err.empty(), "sample: unexpected error");
|
|
CHECK(s.size() == 30, "sample: expected 30 rows");
|
|
CHECK(s.size() == 30 && s[0].timestamp == 1793577600LL, "sample: first date 2026-11-02 UTC");
|
|
CHECK(s.size() == 30 && s[0].close == 150.60, "sample: first close 150.60");
|
|
CHECK(s.size() == 30 && s[29].close == 152.25, "sample: last close 152.25");
|
|
CHECK(skipped == 0, "sample: no rows skipped");
|
|
bool sane = true;
|
|
for (auto& b : s)
|
|
if (!(b.high >= std::max(b.open, b.close) && b.low <= std::min(b.open, b.close) && b.volume > 0)) sane = false;
|
|
CHECK(sane, "sample: high/low/volume sane");
|
|
|
|
// 2. Second fixture differs (multi-series B symbol).
|
|
auto sb = xframe::load_ohlcv_csv("tests/data/ohlcv_sample_b.csv", err);
|
|
CHECK(err.empty() && sb.size() == 30, "sample_b: 30 rows");
|
|
CHECK(sb.size() == 30 && sb[29].close == 311.07, "sample_b: last close 311.07");
|
|
|
|
// 3. Massive aggregates format (header-matched, window_start ns).
|
|
auto m = xframe::load_ohlcv_csv("tests/data/SPY_fake.csv", err, &skipped);
|
|
CHECK(err.empty(), "massive: unexpected error");
|
|
CHECK(!m.empty(), "massive: rows loaded");
|
|
CHECK(!m.empty() && m[0].open == 660.00 && m[0].close == 659.49, "massive: first bar OHLC");
|
|
CHECK(!m.empty() && m[0].volume == 17171, "massive: first bar volume");
|
|
CHECK(!m.empty() && m[0].timestamp == 1794493800LL, "massive: window_start ns -> s");
|
|
CHECK(skipped == 0, "massive: no rows skipped");
|
|
|
|
// 4. Headerless 6-column positional.
|
|
auto h6 = xframe::parse_ohlcv_csv_text(
|
|
"2026-01-05,100.0,101.5,99.5,101.0,5000\n2026-01-06,101.0,102.0,100.5,101.8,6000\n", err);
|
|
CHECK(err.empty() && h6.size() == 2, "headerless6: 2 rows");
|
|
CHECK(h6.size() == 2 && h6[1].timestamp == 1767657600LL, "headerless6: second date 2026-01-06");
|
|
|
|
// 5. Headerless 5-column (no date): sequential daily timestamps from 2026-01-01.
|
|
auto h5 = xframe::parse_ohlcv_csv_text("100.0,101.5,99.5,101.0,5000\n101.0,102.0,100.5,101.8,6000\n", err);
|
|
CHECK(err.empty() && h5.size() == 2, "headerless5: 2 rows");
|
|
CHECK(h5.size() == 2 && h5[0].timestamp == 1767225600LL && h5[1].timestamp == 1767312000LL,
|
|
"headerless5: sequential dates from 2026-01-01");
|
|
|
|
// 6. Reordered header + extra columns ignored + volume missing.
|
|
auto rh = xframe::parse_ohlcv_csv_text(
|
|
"ticker,close,low,open,high\nACME,105.0,99.0,100.0,106.0\n", err);
|
|
CHECK(err.empty() && rh.size() == 1, "reordered: 1 row");
|
|
CHECK(rh.size() == 1 && rh[0].open == 100.0 && rh[0].close == 105.0 && rh[0].volume == 0,
|
|
"reordered: values by name, volume defaults 0");
|
|
|
|
// 7. Date formats: slash date, ISO datetime, epoch ms, epoch s.
|
|
auto df = xframe::parse_ohlcv_csv_text(
|
|
"date,open,high,low,close,volume\n"
|
|
"2026/01/05,100,101,99,100.5,10\n"
|
|
"2026-01-06T09:30:00Z,100,101,99,100.5,10\n"
|
|
"1767657600000,100,101,99,100.5,10\n"
|
|
"1767744000,100,101,99,100.5,10\n", err);
|
|
CHECK(err.empty() && df.size() == 4, "dates: 4 rows");
|
|
CHECK(df.size() == 4 && df[0].timestamp == 1767571200LL, "dates: slash date");
|
|
CHECK(df.size() == 4 && df[1].timestamp == 1767691800LL, "dates: ISO datetime 09:30");
|
|
CHECK(df.size() == 4 && df[2].timestamp == 1767657600LL, "dates: epoch ms");
|
|
CHECK(df.size() == 4 && df[3].timestamp == 1767744000LL, "dates: epoch s");
|
|
|
|
// 8. Malformed rows skipped and counted; CRLF + BOM + quoted field tolerated.
|
|
auto bad = xframe::parse_ohlcv_csv_text(
|
|
"\xEF\xBB\xBF""date,open,high,low,close,volume\r\n"
|
|
"2026-01-05,100,101,99,100.5,10\r\n"
|
|
"2026-01-06,not-a-number,101,99,100.5,10\r\n"
|
|
"2026-01-07,100,101\r\n"
|
|
"\"2026-01-08\",100,101,99,100.5,10\r\n", err, &skipped);
|
|
CHECK(err.empty() && bad.size() == 2, "malformed: 2 valid rows");
|
|
CHECK(skipped == 2, "malformed: 2 rows skipped");
|
|
CHECK(bad.size() == 2 && bad[1].timestamp == 1767830400LL, "malformed: quoted date row kept");
|
|
|
|
// 9. Errors: missing file, empty text, header missing close, no valid rows.
|
|
auto nf = xframe::load_ohlcv_csv("tests/data/does-not-exist.csv", err);
|
|
CHECK(!err.empty() && nf.empty(), "missing file: error set, empty series");
|
|
auto em = xframe::parse_ohlcv_csv_text("", err);
|
|
CHECK(!err.empty() && em.empty(), "empty text: error set");
|
|
auto mc = xframe::parse_ohlcv_csv_text("date,open,high,low\n2026-01-05,100,101,99\n", err);
|
|
CHECK(!err.empty() && mc.empty(), "missing close column: error set");
|
|
auto nv = xframe::parse_ohlcv_csv_text("date,open,high,low,close,volume\njunk,junk,junk,junk,junk,junk\n", err);
|
|
CHECK(!err.empty() && nv.empty(), "no valid rows: error set");
|
|
|
|
if (failures == 0) std::printf("csv_unit_test: ALL PASS\n");
|
|
else std::printf("csv_unit_test: %d FAILURES\n", failures);
|
|
return failures == 0 ? 0 : 1;
|
|
}
|