test_clock_validator/main.cc

84 lines
2.3 KiB
C++
Raw Permalink Normal View History

2024-10-02 17:33:38 +09:00
#include <cmath>
#include <cstdint>
#include <cstdlib>
// #include <format>
#include <fstream>
#include <print>
#include <regex>
#include <sstream>
#include <string>
#include <tuple>
auto parse_line(std::string line) -> std::tuple<double_t, double_t> {
auto is = std::stringstream{line};
double_t time, high, low;
is >> time >> high >> low;
return std::tuple{time, high};
}
int main(int argc, char **argv) {
if (argc != 2) {
// std::println("Usage: {} <file>", argv[0]);
exit(0);
}
auto file = std::ifstream{argv[1]};
// main logic
auto re = std::regex{"^\\d+\\.\\d+ \\d+\\.\\d+ \\d+\\.\\d+$"};
std::string expectedFirst = "10.000000000000000 0.000000 0.000000";
std::string expectedLast = "16.000000000000000 1000.000000 0.000000";
bool is_in_high = false;
bool is_first_line = true;
bool is_valid_last_line = false;
double_t last_low_time = 0;
double_t riseup_time = 0;
int32_t rise_count = 0;
std::string line;
while (std::getline(file, line)) {
if (is_first_line) {
if (line != expectedFirst) {
std::println("Invalid first line: {}", line);
return 2;
}
is_first_line = false;
}
if (!std::regex_match(line, re)) {
std::println("Invalid line!: {}", line);
return 2;
}
is_valid_last_line = (line == expectedLast);
auto temp = parse_line(line);
auto time = std::get<0>(temp);
auto high = std::get<1>(temp);
if (high == 0) {
last_low_time = time;
} else if (!is_in_high && high > 500) {
riseup_time = time;
rise_count++;
is_in_high = true;
} else if (is_in_high && high < 500) {
is_in_high = false;
}
}
if (!is_valid_last_line) {
std::println("Invalid last line: {}", line);
return 2;
}
if (rise_count > 1) {
std::println("Too many rise counts: {}", rise_count);
return 2;
}
auto transition_is_too_long = (riseup_time - last_low_time) > (1. / 57 * 6);
if (transition_is_too_long) {
std::println("Too long transition: {} ns", riseup_time - last_low_time);
return 2;
}
return 1;
}