From a98742945b5accb9b6216bf72d25fb2e9fbb73de Mon Sep 17 00:00:00 2001 From: ado Date: Sat, 29 Jul 2023 13:56:00 +0200 Subject: [PATCH] Update test helpers --- include/ss/parser.hpp | 2 - test/test_helpers.hpp | 99 +++++++++++++++++-------------------------- 2 files changed, 39 insertions(+), 62 deletions(-) diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index 74967cf..4ad4df4 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -1,7 +1,5 @@ #pragma once -#define _CRT_SECURE_NO_WARNINGS - #include "common.hpp" #include "converter.hpp" #include "extract.hpp" diff --git a/test/test_helpers.hpp b/test/test_helpers.hpp index 1da01f5..e99852a 100644 --- a/test/test_helpers.hpp +++ b/test/test_helpers.hpp @@ -1,9 +1,6 @@ #pragma once -#define _CRT_SECURE_NO_WARNINGS - -#include -#include +#include #ifdef CMAKE_GITHUB_CI #include @@ -12,73 +9,55 @@ #endif struct buffer { - char* data_{nullptr}; + std::string data_; - char* operator()(const char* data) { - if (data_) { - delete[] data_; - } - data_ = new char[strlen(data) + 1]; - strcpy(data_, data); - return data_; - } + char *operator()(const std::string &data) { + data_ = data; + return data_.data(); + } - char* append(const char* data) { - if (data_) { - char* new_data_ = new char[strlen(data_) + strlen(data) + 1]; - strcpy(new_data_, data_); - strcat(new_data_, data); - delete[] data_; - data_ = new_data_; - return data_; - } else { - return operator()(data); - } - } + char *append(const std::string &data) { + data_ += data; + return data_.data(); + } - char* append_overwrite_last(const char* data, size_t size) { - data_[strlen(data_) - size] = '\0'; - return append(data); - } - - ~buffer() { - if (data_) { - delete[] data_; - } - } + char *append_overwrite_last(const std::string &data, size_t size) { + data_.resize(data_.size() - size); + return append(data); + } }; [[maybe_unused]] inline buffer buff; #define CHECK_FLOATING_CONVERSION(input, type) \ - { \ - auto eps = std::numeric_limits::min(); \ - std::string s = #input; \ - auto t = ss::to_num(s.c_str(), s.c_str() + s.size()); \ - REQUIRE(t.has_value()); \ - CHECK_LT(std::abs(t.value() - type(input)), eps); \ - } \ - { \ - /* check negative too */ \ - auto eps = std::numeric_limits::min(); \ - auto s = std::string("-") + #input; \ - auto t = ss::to_num(s.c_str(), s.c_str() + s.size()); \ - REQUIRE(t.has_value()); \ - CHECK_LT(std::abs(t.value() - type(-input)), eps); \ - } + { \ + auto eps = std::numeric_limits::min(); \ + std::string s = #input; \ + auto t = ss::to_num(s.c_str(), s.c_str() + s.size()); \ + REQUIRE(t.has_value()); \ + CHECK_LT(std::abs(t.value() - type(input)), eps); \ + } \ + { \ + /* check negative too */ \ + auto eps = std::numeric_limits::min(); \ + auto s = std::string("-") + #input; \ + auto t = ss::to_num(s.c_str(), s.c_str() + s.size()); \ + REQUIRE(t.has_value()); \ + CHECK_LT(std::abs(t.value() - type(-input)), eps); \ + } #define CHECK_INVALID_CONVERSION(input, type) \ - { \ - std::string s = input; \ - auto t = ss::to_num(s.c_str(), s.c_str() + s.size()); \ - CHECK_FALSE(t.has_value()); \ - } + { \ + std::string s = input; \ + auto t = ss::to_num(s.c_str(), s.c_str() + s.size()); \ + CHECK_FALSE(t.has_value()); \ + } #define REQUIRE_VARIANT(var, el, type) \ - { \ - auto ptr = std::get_if(&var); \ - REQUIRE(ptr); \ - REQUIRE_EQ(el, *ptr); \ - } + { \ + auto ptr = std::get_if(&var); \ + REQUIRE(ptr); \ + REQUIRE_EQ(el, *ptr); \ + } #define CHECK_NOT_VARIANT(var, type) CHECK(!std::holds_alternative(var));