ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
CLI.integration.hpp
Go to the documentation of this file.
1
9#pragma once
10
11#include <gtest/gtest.h>
12
13#include "io/TempFile.hpp"
14
15#include <cstdio>
16#include <cstdlib>
17#include <memory>
18#include <string>
19
20#include <nlohmann/json.hpp>
21
22using json = nlohmann::json;
23
24#ifndef PPFOREST2_BINARY_PATH
25#error "PPFOREST2_BINARY_PATH must be defined"
26#endif
27
28#ifndef PPFOREST2_DATA_DIR
29#error "PPFOREST2_DATA_DIR must be defined"
30#endif
31
32#ifndef PPFOREST2_GOLDEN_DIR
33#error "PPFOREST2_GOLDEN_DIR must be defined"
34#endif
35
36inline const std::string BINARY = PPFOREST2_BINARY_PATH;
37inline std::string const DATA_DIR = PPFOREST2_DATA_DIR;
38inline std::string const GOLDEN_DIR = PPFOREST2_GOLDEN_DIR;
39// Classification datasets live under `${DATA_DIR}/classification/`, regression
40// under `${DATA_DIR}/regression/`. Path constants pin the subfolder explicitly
41// so tests don't have to know the layout convention.
42inline std::string const IRIS_CSV = DATA_DIR + "/classification/iris.csv";
43inline std::string const CRAB_CSV = DATA_DIR + "/classification/crab.csv";
44inline std::string const WINE_CSV = DATA_DIR + "/classification/wine.csv";
45inline std::string const GLASS_CSV = DATA_DIR + "/classification/glass.csv";
46inline std::string const MTCARS_CSV = DATA_DIR + "/regression/mtcars.csv";
47inline std::string const HOUSING_CSV = DATA_DIR + "/regression/california_housing.csv";
48
57 std::string stdout_output;
58 std::string stderr_output;
59};
60
70inline ProcessResult run_ppforest2(std::string const& args) {
71 ppforest2::io::TempFile stderr_file;
72
73 // clang-format off
74 #ifdef _WIN32
75 std::string cmd = BINARY + " " + args + " 2>\"" + stderr_file.path() + "\"";
76 FILE* pipe = _popen(cmd.c_str(), "r");
77 #else
78 std::string cmd = BINARY + " " + args + " 2>\"" + stderr_file.path() + "\"";
79 FILE* pipe = popen(cmd.c_str(), "r");
80 #endif
81 // clang-format on
82 if (pipe == nullptr) {
83 return {-1, "", ""};
84 }
85
86 std::string output;
87 char buffer[4096];
88
89 while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
90 output += buffer;
91 }
92
93 // clang-format off
94 #ifdef _WIN32
95 int exit_code = _pclose(pipe);
96 #else
97 int status = pclose(pipe);
98 int exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
99 #endif
100 // clang-format on
101
102 return {exit_code, output, stderr_file.read()};
103}
104
107
114class SavedModelTest : public ::testing::Test {
115protected:
116 void SetUp() override {
117 model_ = std::make_unique<TempFile>();
118 model_->clear();
119 auto result = run_ppforest2("-q train -d " + IRIS_CSV + " -n 5 -r 0 -s " + model_->path());
120 ASSERT_EQ(result.exit_code, 0);
121 model_json_ = json::parse(model_->read());
122 }
123
124 std::unique_ptr<TempFile> model_;
126};
std::string const DATA_DIR
Definition CLI.integration.hpp:37
std::string const HOUSING_CSV
Definition CLI.integration.hpp:47
std::string const CRAB_CSV
Definition CLI.integration.hpp:43
std::string const IRIS_CSV
Definition CLI.integration.hpp:42
ProcessResult run_ppforest2(std::string const &args)
Spawn the ppforest2 binary with the given argument string.
Definition CLI.integration.hpp:70
std::string const GLASS_CSV
Definition CLI.integration.hpp:45
nlohmann::json json
Definition CLI.integration.hpp:22
const std::string BINARY
Definition CLI.integration.hpp:36
std::string const MTCARS_CSV
Definition CLI.integration.hpp:46
std::string const WINE_CSV
Definition CLI.integration.hpp:44
std::string const GOLDEN_DIR
Definition CLI.integration.hpp:38
RAII temporary file and directory with automatic cleanup.
Test fixture that trains a forest model once in SetUp().
Definition CLI.integration.hpp:114
void SetUp() override
Definition CLI.integration.hpp:116
std::unique_ptr< TempFile > model_
Definition CLI.integration.hpp:124
json model_json_
Definition CLI.integration.hpp:125
RAII temporary directory with automatic cleanup.
Definition TempFile.hpp:134
RAII temporary file with automatic cleanup.
Definition TempFile.hpp:28
std::string read() const
Read the entire file contents as a string.
Definition TempFile.hpp:112
std::string const & path() const
Definition TempFile.hpp:106
Captured output of a child-process invocation.
Definition CLI.integration.hpp:55
int exit_code
Definition CLI.integration.hpp:56
std::string stdout_output
Definition CLI.integration.hpp:57
std::string stderr_output
Definition CLI.integration.hpp:58