|
| 1 | +#include <pineforge/detail/feed_projection.hpp> |
| 2 | +#include <pineforge/report_sink.h> |
| 3 | +#include <pineforge/source/pine_strategy_host.hpp> |
| 4 | +#include <pineforge/timeframe.hpp> |
| 5 | + |
| 6 | +#include <array> |
| 7 | +#include <cerrno> |
| 8 | +#include <cstdint> |
| 9 | +#include <cstdio> |
| 10 | +#include <cstdlib> |
| 11 | +#include <cstring> |
| 12 | +#include <ctime> |
| 13 | +#include <stdexcept> |
| 14 | +#include <string> |
| 15 | +#include <sys/inotify.h> |
| 16 | +#include <unistd.h> |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace feed_projection_scratch_directory_test { |
| 20 | +using namespace pineforge; |
| 21 | +using TapeRow = source::RecordedSeriesRow; |
| 22 | +using Tape = detail::FeedProjection<TapeRow>; |
| 23 | +using Calendar = detail::FeedProjection<std::int64_t>; |
| 24 | + |
| 25 | +constexpr std::int64_t first_stamp = 1704067200000LL; |
| 26 | +constexpr std::int64_t day_ms = 24 * 60 * 60 * 1000LL; |
| 27 | +int failures = 0; |
| 28 | + |
| 29 | +#define CHECK(condition) do { if (!(condition)) { \ |
| 30 | + std::printf("FAIL line %d: %s\n", __LINE__, #condition); ++failures; \ |
| 31 | +} } while (false) |
| 32 | + |
| 33 | +void require(bool condition, const char* message) { |
| 34 | + if (!condition) throw std::runtime_error(message); |
| 35 | +} |
| 36 | + |
| 37 | +struct Directory { |
| 38 | + std::string path; |
| 39 | + |
| 40 | + explicit Directory(const char* prefix) { |
| 41 | + std::string pattern = std::string("/var/tmp/") + prefix + "-XXXXXX"; |
| 42 | + std::vector<char> bytes(pattern.begin(), pattern.end()); |
| 43 | + bytes.push_back('\0'); |
| 44 | + require(::mkdtemp(bytes.data()) != nullptr, "cannot create projection directory"); |
| 45 | + path = bytes.data(); |
| 46 | + } |
| 47 | + ~Directory() { ::rmdir(path.c_str()); } |
| 48 | + Directory(const Directory&) = delete; |
| 49 | + Directory& operator=(const Directory&) = delete; |
| 50 | +}; |
| 51 | + |
| 52 | +struct OwnedFile { |
| 53 | + std::string path; |
| 54 | + ~OwnedFile() { |
| 55 | + if (!path.empty()) ::unlink(path.c_str()); |
| 56 | + } |
| 57 | +}; |
| 58 | + |
| 59 | +OwnedFile make_report_path(const Directory& directory) { |
| 60 | + std::string pattern = directory.path + "/report-XXXXXX"; |
| 61 | + std::vector<char> bytes(pattern.begin(), pattern.end()); |
| 62 | + bytes.push_back('\0'); |
| 63 | + const int fd = ::mkstemp(bytes.data()); |
| 64 | + require(fd >= 0, "cannot create report path"); |
| 65 | + ::close(fd); |
| 66 | + return {bytes.data()}; |
| 67 | +} |
| 68 | + |
| 69 | +class Environment final { |
| 70 | +public: |
| 71 | + Environment(const char* name, const std::string& value) : name_(name) { |
| 72 | + if (const char* previous = std::getenv(name_)) { |
| 73 | + had_previous_ = true; |
| 74 | + previous_ = previous; |
| 75 | + } |
| 76 | + require(::setenv(name_, value.c_str(), 1) == 0, "cannot set scratch environment"); |
| 77 | + } |
| 78 | + ~Environment() { |
| 79 | + if (had_previous_) ::setenv(name_, previous_.c_str(), 1); |
| 80 | + else ::unsetenv(name_); |
| 81 | + } |
| 82 | + Environment(const Environment&) = delete; |
| 83 | + Environment& operator=(const Environment&) = delete; |
| 84 | +private: |
| 85 | + const char* name_; |
| 86 | + bool had_previous_ = false; |
| 87 | + std::string previous_; |
| 88 | +}; |
| 89 | + |
| 90 | +class CreationWatch final { |
| 91 | +public: |
| 92 | + CreationWatch(const std::string& configured, const std::string& default_directory) |
| 93 | + : fd_(::inotify_init1(IN_NONBLOCK | IN_CLOEXEC)) { |
| 94 | + require(fd_ >= 0, "cannot create projection directory watch"); |
| 95 | + configured_watch_ = ::inotify_add_watch(fd_, configured.c_str(), IN_CREATE); |
| 96 | + default_watch_ = ::inotify_add_watch(fd_, default_directory.c_str(), IN_CREATE); |
| 97 | + require(configured_watch_ >= 0 && default_watch_ >= 0, |
| 98 | + "cannot watch projection directories"); |
| 99 | + } |
| 100 | + ~CreationWatch() { |
| 101 | + if (configured_watch_ >= 0) ::inotify_rm_watch(fd_, configured_watch_); |
| 102 | + if (default_watch_ >= 0) ::inotify_rm_watch(fd_, default_watch_); |
| 103 | + if (fd_ >= 0) ::close(fd_); |
| 104 | + } |
| 105 | + CreationWatch(const CreationWatch&) = delete; |
| 106 | + CreationWatch& operator=(const CreationWatch&) = delete; |
| 107 | + |
| 108 | + void collect() { |
| 109 | + std::array<char, 16384> bytes{}; |
| 110 | + for (;;) { |
| 111 | + const ssize_t count = ::read(fd_, bytes.data(), bytes.size()); |
| 112 | + if (count < 0 && errno == EINTR) continue; |
| 113 | + if (count < 0 && errno == EAGAIN) return; |
| 114 | + require(count > 0, "cannot read projection directory watch"); |
| 115 | + std::size_t offset = 0; |
| 116 | + while (offset < static_cast<std::size_t>(count)) { |
| 117 | + require(static_cast<std::size_t>(count) - offset >= sizeof(inotify_event), |
| 118 | + "short projection directory event"); |
| 119 | + const auto* event = reinterpret_cast<const inotify_event*>( |
| 120 | + bytes.data() + offset); |
| 121 | + require(event->len <= static_cast<std::size_t>(count) - offset |
| 122 | + - sizeof(inotify_event), |
| 123 | + "invalid projection directory event"); |
| 124 | + if ((event->mask & IN_CREATE) && event->len != 0) { |
| 125 | + const std::string name(event->name); |
| 126 | + if (name.rfind("pineforge-feed-", 0) == 0) { |
| 127 | + if (event->wd == configured_watch_) ++configured_feed_creations_; |
| 128 | + if (event->wd == default_watch_) ++default_feed_creations_; |
| 129 | + } |
| 130 | + } |
| 131 | + offset += sizeof(inotify_event) + event->len; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + std::size_t configured_feed_creations() const noexcept { |
| 136 | + return configured_feed_creations_; |
| 137 | + } |
| 138 | + std::size_t default_feed_creations() const noexcept { |
| 139 | + return default_feed_creations_; |
| 140 | + } |
| 141 | +private: |
| 142 | + int fd_ = -1; |
| 143 | + int configured_watch_ = -1; |
| 144 | + int default_watch_ = -1; |
| 145 | + std::size_t configured_feed_creations_ = 0; |
| 146 | + std::size_t default_feed_creations_ = 0; |
| 147 | +}; |
| 148 | + |
| 149 | +double tape_value(std::size_t index) { |
| 150 | + if (index == 0) return -0.0; |
| 151 | + return static_cast<double>((index * 37) % 257 - 128) * 0.125; |
| 152 | +} |
| 153 | + |
| 154 | +bool same_bits(double left, double right) { |
| 155 | + return std::memcmp(&left, &right, sizeof(left)) == 0; |
| 156 | +} |
| 157 | + |
| 158 | +Bar bar_at(std::int64_t timestamp, std::size_t index) { |
| 159 | + const double price = 100.0 + static_cast<double>(index) * 0.25; |
| 160 | + return {price, price + 1.0, price - 1.0, price + 0.5, 1.0, timestamp}; |
| 161 | +} |
| 162 | + |
| 163 | +struct Input { |
| 164 | + std::vector<std::int64_t> stamps; |
| 165 | + std::vector<Bar> daily; |
| 166 | + std::vector<std::int64_t> tape_opens; |
| 167 | + std::vector<double> tape_values; |
| 168 | +}; |
| 169 | + |
| 170 | +Input make_input(std::size_t calendar_rows, std::size_t tape_rows) { |
| 171 | + Input input; |
| 172 | + input.stamps.reserve(calendar_rows); |
| 173 | + input.daily.reserve(calendar_rows); |
| 174 | + for (std::size_t i = 0; i < calendar_rows; ++i) { |
| 175 | + const auto timestamp = first_stamp + static_cast<std::int64_t>(i) * day_ms; |
| 176 | + input.stamps.push_back(timestamp); |
| 177 | + input.daily.push_back(bar_at(timestamp, i)); |
| 178 | + } |
| 179 | + input.tape_opens.reserve(tape_rows); |
| 180 | + input.tape_values.reserve(tape_rows); |
| 181 | + for (std::size_t i = 0; i < tape_rows; ++i) { |
| 182 | + input.tape_opens.push_back(first_stamp + static_cast<std::int64_t>(i) * day_ms); |
| 183 | + input.tape_values.push_back(tape_value(i)); |
| 184 | + } |
| 185 | + return input; |
| 186 | +} |
| 187 | + |
| 188 | +class ProjectionHost final : public source::PineStrategyHost { |
| 189 | +public: |
| 190 | + ProjectionHost() { |
| 191 | + set_syminfo_timezone("UTC"); |
| 192 | + set_syminfo_session("24x7"); |
| 193 | + set_syminfo_type("crypto"); |
| 194 | + set_syminfo_string("tickerid", "TEST:BASE"); |
| 195 | + set_syminfo_mintick(0.01); |
| 196 | + source::PineStrategyConfig config; |
| 197 | + config.initial_capital = 100000.0; |
| 198 | + configure_pine_strategy(config); |
| 199 | + } |
| 200 | + |
| 201 | + void configure_security_evaluators() override { |
| 202 | + security_eval_states_.clear(); |
| 203 | + register_security_eval(0, "D", "360", false, false); |
| 204 | + } |
| 205 | + void evaluate_security(int, const Bar&, bool) override {} |
| 206 | + void clear_security(int) override {} |
| 207 | + void on_source_bar(const Bar&) override {} |
| 208 | + |
| 209 | + const NativeDayPartition& calendar() const noexcept { return chart_day_partition_; } |
| 210 | + double tape_at(std::int64_t timestamp) { |
| 211 | + current_bar_.timestamp = timestamp; |
| 212 | + return recorded_series_value("tape"); |
| 213 | + } |
| 214 | +}; |
| 215 | + |
| 216 | +void check_tape(const ProjectionHost& host, const Input& input) { |
| 217 | + auto& readable = const_cast<ProjectionHost&>(host); |
| 218 | + for (std::size_t i = 0; i < input.tape_opens.size(); ++i) |
| 219 | + CHECK(same_bits(readable.tape_at(input.tape_opens[i]), input.tape_values[i])); |
| 220 | +} |
| 221 | + |
| 222 | +void check_calendar(const ProjectionHost& host, const Input& input) { |
| 223 | + const auto& partition = host.calendar(); |
| 224 | + CHECK(partition.stamps == input.stamps); |
| 225 | + CHECK(partition.trade_day.size() == input.stamps.size()); |
| 226 | + CHECK(partition.week_open.size() == input.stamps.size()); |
| 227 | + CHECK(partition.month_open.size() == input.stamps.size()); |
| 228 | + for (std::size_t i = 0; i < input.stamps.size(); ++i) { |
| 229 | + const auto day = input.stamps[i] / kMsPerDay; |
| 230 | + const auto week = input.stamps[i - i % 7]; |
| 231 | + const time_t seconds = static_cast<time_t>(input.stamps[i] / 1000); |
| 232 | + struct tm date {}; |
| 233 | + require(::gmtime_r(&seconds, &date) != nullptr, "cannot derive calendar row"); |
| 234 | + const auto month = input.stamps[i] |
| 235 | + - static_cast<std::int64_t>(date.tm_mday - 1) * kMsPerDay; |
| 236 | + CHECK(partition.trade_day[i] == day); |
| 237 | + CHECK(partition.week_open[i] == week); |
| 238 | + CHECK(partition.month_open[i] == month); |
| 239 | + } |
| 240 | +} |
| 241 | + |
| 242 | +void configured_directory_case() { |
| 243 | + Directory configured("pineforge-feed-configured"); |
| 244 | + Directory default_directory("pineforge-feed-default"); |
| 245 | + Directory report_directory("pineforge-feed-report"); |
| 246 | + OwnedFile report = make_report_path(report_directory); |
| 247 | + Environment temporary("TMPDIR", default_directory.path); |
| 248 | + CreationWatch watch(configured.path, default_directory.path); |
| 249 | + |
| 250 | + const auto calendar_rows = Calendar::rows_per_page() + 1; |
| 251 | + const auto tape_rows = Tape::rows_per_page() + 1; |
| 252 | + const auto input = make_input(calendar_rows, tape_rows); |
| 253 | + ProjectionHost host; |
| 254 | + const pf_report_sink_v1 descriptor{ |
| 255 | + sizeof(descriptor), 1, report.path.c_str(), "feed-projection-directory", |
| 256 | + configured.path.c_str()}; |
| 257 | + CHECK(host.set_report_sink_v1(&descriptor) == 0); |
| 258 | + CHECK(host.set_native_security_feed("D", input.daily.data(), |
| 259 | + static_cast<int>(input.daily.size()))); |
| 260 | + CHECK(host.set_recorded_series("tape", input.tape_opens.data(), input.tape_values.data(), |
| 261 | + static_cast<int>(input.tape_opens.size()))); |
| 262 | + host.run(input.daily.data(), static_cast<int>(input.daily.size()), "360", "360", |
| 263 | + false, 4, MagnifierDistribution::ENDPOINTS); |
| 264 | + CHECK(host.last_error().empty()); |
| 265 | + watch.collect(); |
| 266 | + |
| 267 | + // One raw recorded-series builder plus the three calendar builders must |
| 268 | + // each spill: the row counts above are one past their derived page sizes. |
| 269 | + CHECK(watch.configured_feed_creations() == 4); |
| 270 | + CHECK(watch.default_feed_creations() == 0); |
| 271 | + check_tape(host, input); |
| 272 | + check_calendar(host, input); |
| 273 | +} |
| 274 | + |
| 275 | +void default_small_case() { |
| 276 | + Directory default_directory("pineforge-feed-default-small"); |
| 277 | + Environment temporary("TMPDIR", default_directory.path); |
| 278 | + CreationWatch watch(default_directory.path, default_directory.path); |
| 279 | + |
| 280 | + const auto input = make_input(1, 1); |
| 281 | + ProjectionHost host; |
| 282 | + CHECK(host.set_native_security_feed("D", input.daily.data(), |
| 283 | + static_cast<int>(input.daily.size()))); |
| 284 | + CHECK(host.set_recorded_series("tape", input.tape_opens.data(), input.tape_values.data(), |
| 285 | + static_cast<int>(input.tape_opens.size()))); |
| 286 | + host.run(input.daily.data(), static_cast<int>(input.daily.size()), "360", "360", |
| 287 | + false, 4, MagnifierDistribution::ENDPOINTS); |
| 288 | + CHECK(host.last_error().empty()); |
| 289 | + watch.collect(); |
| 290 | + |
| 291 | + // The recommendation keeps a one-page projection resident, so this |
| 292 | + // nonempty default run must not attempt a projection backing file. |
| 293 | + CHECK(watch.default_feed_creations() == 0); |
| 294 | + check_tape(host, input); |
| 295 | + check_calendar(host, input); |
| 296 | +} |
| 297 | +} // namespace feed_projection_scratch_directory_test |
| 298 | + |
| 299 | +int main() { |
| 300 | + using namespace feed_projection_scratch_directory_test; |
| 301 | + try { |
| 302 | + configured_directory_case(); |
| 303 | + default_small_case(); |
| 304 | + } catch (const std::exception& error) { |
| 305 | + std::fprintf(stderr, "projection scratch-directory setup failed: %s\n", error.what()); |
| 306 | + return EXIT_FAILURE; |
| 307 | + } |
| 308 | + return failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 309 | +} |
0 commit comments