|
| 1 | +/* |
| 2 | + * test_native_c_api_c99.c -- the public C headers as a strict C99 consumer |
| 3 | + * sees them (R5 lane F4, item 9). |
| 4 | + * |
| 5 | + * ADR-0001's P2c ruling keeps `sharpe_tv` / `sortino_tv` as deprecated |
| 6 | + * spellings of `sharpe_monthly` / `sortino_monthly`: one double behind an |
| 7 | + * anonymous union, which is a C11 construct. Its portability note promised a |
| 8 | + * strict C99 consumer a warning; under -pedantic-errors it was an error |
| 9 | + * (audit AUDIT3 §2.2 P2c). tests/CMakeLists.txt compiles this translation unit |
| 10 | + * as C99 with extensions off and -pedantic-errors, so any C11-only construct |
| 11 | + * in <pineforge/pineforge.h> or <pineforge/native_c_api.h> fails the build. |
| 12 | + * Its body executes what such a caller relies on: the alias reads one storage |
| 13 | + * both ways, and a native C host configures through the typed enumerations, |
| 14 | + * runs, and reads its words back by their C names. |
| 15 | + * |
| 16 | + * No assert(): the Release gate defines NDEBUG and would no-op every row. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <pineforge/pineforge.h> |
| 20 | + |
| 21 | +#include <stddef.h> |
| 22 | +#include <stdio.h> |
| 23 | +#include <string.h> |
| 24 | + |
| 25 | +static int failures = 0; |
| 26 | + |
| 27 | +#define CHECK(cond, msg) \ |
| 28 | + do { \ |
| 29 | + if (!(cond)) { \ |
| 30 | + fprintf(stderr, "FAIL %s:%d %s\n", __FILE__, __LINE__, (msg)); \ |
| 31 | + ++failures; \ |
| 32 | + } \ |
| 33 | + } while (0) |
| 34 | + |
| 35 | +/* open, high, low, close, volume, timestamp (Unix milliseconds). */ |
| 36 | +static const pf_bar_t kBars[] = { |
| 37 | + {100.0, 102.0, 99.0, 101.0, 4.0, 0}, |
| 38 | + {101.0, 103.0, 100.0, 102.0, 4.0, 300000}, |
| 39 | + {102.0, 104.0, 101.0, 103.0, 4.0, 600000}, |
| 40 | + {103.0, 105.0, 102.0, 104.0, 4.0, 900000} |
| 41 | +}; |
| 42 | + |
| 43 | +struct c99_host { |
| 44 | + pf_strategy_t handle; |
| 45 | + int calculations; |
| 46 | + int unnamed; |
| 47 | +}; |
| 48 | + |
| 49 | +static int c99_on_bar(void* user, const pf_bar_t* bar, const pf_native_decision_v1* at) { |
| 50 | + struct c99_host* host = (struct c99_host*)user; |
| 51 | + pf_native_request_v1 request; |
| 52 | + (void)bar; |
| 53 | + if (at->provenance != PF_NATIVE_PROVENANCE_CALCULATION |
| 54 | + || at->path_phase != PF_NATIVE_PATH_PHASE_NONE |
| 55 | + || at->completion != PF_NATIVE_COMPLETION_KIND_CONFIRMED) { |
| 56 | + ++host->unnamed; |
| 57 | + } |
| 58 | + if (++host->calculations != 1) return 0; |
| 59 | + memset(&request, 0, sizeof(request)); |
| 60 | + request.struct_size = (uint32_t)sizeof(request); |
| 61 | + request.version = PF_NATIVE_API_VERSION; |
| 62 | + request.intent = PF_NATIVE_INTENT_TRANSACT; |
| 63 | + request.intent_value = 1.0; |
| 64 | + request.trigger = PF_NATIVE_TRIGGER_MARKET; |
| 65 | + return strategy_native_submit_v1(host->handle, &request, NULL, NULL) == PF_NATIVE_OK ? 0 : 1; |
| 66 | +} |
| 67 | + |
| 68 | +static void check_deprecated_alias(void) { |
| 69 | + pf_equity_stats_t stats; |
| 70 | + CHECK(offsetof(pf_equity_stats_t, sharpe_tv) == offsetof(pf_equity_stats_t, sharpe_monthly), |
| 71 | + "sharpe_tv is not sharpe_monthly's storage"); |
| 72 | + CHECK(offsetof(pf_equity_stats_t, sortino_tv) |
| 73 | + == offsetof(pf_equity_stats_t, sortino_monthly), |
| 74 | + "sortino_tv is not sortino_monthly's storage"); |
| 75 | + memset(&stats, 0, sizeof(stats)); |
| 76 | + stats.sharpe_monthly = 1.25; |
| 77 | + stats.sortino_tv = -0.5; |
| 78 | + CHECK(stats.sharpe_tv == 1.25, "a write through sharpe_monthly does not read as sharpe_tv"); |
| 79 | + CHECK(stats.sortino_monthly == -0.5, |
| 80 | + "a write through sortino_tv does not read as sortino_monthly"); |
| 81 | +} |
| 82 | + |
| 83 | +static void check_native_run(void) { |
| 84 | + struct c99_host host; |
| 85 | + pf_native_callbacks_v1 callbacks; |
| 86 | + pf_native_run_spec_v1 spec; |
| 87 | + pf_native_state_v1 state; |
| 88 | + pf_native_event_v1 events[16]; |
| 89 | + double units = 0.0; |
| 90 | + int written, i, applied = 0; |
| 91 | + |
| 92 | + memset(&host, 0, sizeof(host)); |
| 93 | + memset(&callbacks, 0, sizeof(callbacks)); |
| 94 | + callbacks.struct_size = (uint32_t)sizeof(callbacks); |
| 95 | + callbacks.version = PF_NATIVE_API_VERSION; |
| 96 | + callbacks.user = &host; |
| 97 | + callbacks.on_bar = c99_on_bar; |
| 98 | + host.handle = strategy_native_host_create_v1(&callbacks); |
| 99 | + CHECK(host.handle != NULL, "a C99 caller's callback table was refused"); |
| 100 | + if (!host.handle) return; |
| 101 | + |
| 102 | + memset(&spec, 0, sizeof(spec)); |
| 103 | + spec.struct_size = (uint32_t)sizeof(spec); |
| 104 | + spec.session_key = "native-c-api-c99"; |
| 105 | + spec.run_number = 1; |
| 106 | + spec.input_tf = "5"; |
| 107 | + spec.script_tf = "5"; |
| 108 | + spec.ticker = "MOCK"; |
| 109 | + spec.tickerid = "TEST:MOCK"; |
| 110 | + spec.type = "crypto"; |
| 111 | + spec.currency = "USDT"; |
| 112 | + spec.basecurrency = "ETH"; |
| 113 | + spec.description = ""; |
| 114 | + spec.volumetype = ""; |
| 115 | + spec.timezone = "UTC"; |
| 116 | + spec.session = "24x7"; |
| 117 | + spec.chart_timezone = ""; |
| 118 | + spec.initial_capital = 10000.0; |
| 119 | + spec.point_value = 1.0; |
| 120 | + spec.account_fx = 1.0; |
| 121 | + spec.price_tick = 0.01; |
| 122 | + spec.fee_kind = PF_NATIVE_FEE_PERCENT; |
| 123 | + spec.close_execution = PF_NATIVE_CLOSE_EXECUTION_NEXT_ELIGIBLE_POINT; |
| 124 | + spec.allowed_open_directions = PF_NATIVE_OPEN_DIRECTIONS_BOTH; |
| 125 | + spec.optional_mask = PF_NATIVE_SPEC_OPTIONAL_QUANTITY_GRID; |
| 126 | + spec.quantity_grid = 1.0; |
| 127 | + CHECK(strategy_configure_native_v1(host.handle, &spec) == 0, "the C99 spec was refused"); |
| 128 | + CHECK(strategy_execution_contract(host.handle) == PF_EXECUTION_CONTRACT_NATIVE_MARKET_V1, |
| 129 | + "a native host is not on the native market contract"); |
| 130 | + CHECK(strategy_native_run_v1(host.handle, kBars, 4, NULL) == PF_NATIVE_OK, |
| 131 | + "the C99 run did not complete"); |
| 132 | + CHECK(host.calculations == 4 && host.unnamed == 0, |
| 133 | + "a C99 calculation was not the named calculation point"); |
| 134 | + |
| 135 | + memset(&state, 0, sizeof(state)); |
| 136 | + state.struct_size = (uint32_t)sizeof(state); |
| 137 | + CHECK(strategy_native_state_v1(host.handle, &state) == PF_NATIVE_OK, "C99 state read"); |
| 138 | + CHECK(state.lifecycle == PF_NATIVE_LIFECYCLE_COMPLETED |
| 139 | + && state.completion == PF_NATIVE_COMPLETION_BATCH_COMPLETE |
| 140 | + && state.failure_code == PF_NATIVE_FAILURE_NONE, |
| 141 | + "the C99 run did not end as a completed batch"); |
| 142 | + memset(events, 0, sizeof(events)); |
| 143 | + written = strategy_native_events_v1(host.handle, 0, events, 16); |
| 144 | + for (i = 0; i < written; ++i) { |
| 145 | + if (events[i].kind == PF_NATIVE_EVENT_APPLIED && events[i].terminal |
| 146 | + && events[i].reason == PF_NATIVE_TERMINAL_WORKING_UNITS_SATISFIED) { |
| 147 | + ++applied; |
| 148 | + } |
| 149 | + } |
| 150 | + CHECK(applied == 1, "the C99 entry did not fill as one satisfied execution"); |
| 151 | + CHECK(strategy_native_position_v1(host.handle, &units, NULL, NULL) == PF_NATIVE_OK |
| 152 | + && units == 1.0, |
| 153 | + "the C99 entry did not leave a long 1"); |
| 154 | + strategy_native_host_free(host.handle); |
| 155 | +} |
| 156 | + |
| 157 | +int main(void) { |
| 158 | + check_deprecated_alias(); |
| 159 | + check_native_run(); |
| 160 | + printf("test_native_c_api_c99: %s\n", failures == 0 ? "ok" : "FAILED"); |
| 161 | + return failures == 0 ? 0 : 1; |
| 162 | +} |
0 commit comments