Skip to content
Snippets Groups Projects
Commit b2f0f624 authored by Oliver Heidmann's avatar Oliver Heidmann Committed by Uwe Schulzweida
Browse files

updated tests on convert_number

parent 8d45ed25
No related branches found
No related tags found
2 merge requests!326convert_to_number not called string_to_number, fixed usage of .fail enabling...,!322Fixing and cleaning up handling of operator argument sourrounding the use of scientific notations
......@@ -4,15 +4,124 @@
#include "../../src/util_string.h"
#include <string>
#include <limits> // std::reference_wrapper
#include <tuple>
#include <cmath>
using namespace snowhouse;
void
cdoExit()
{
}
template <typename T>
void
test_string_to_number_unsigned()
{
T num_max = std::numeric_limits<T>::max();
std::stringstream str_num_max;
str_num_max << std::setprecision(std::numeric_limits<T>::max_digits10) << num_max;
T num_min = std::numeric_limits<T>::min();
std::stringstream str_num_min;
str_num_min << std::setprecision(std::numeric_limits<T>::max_digits10) << num_min;
std::vector<std::string> to_be_tested;
std::vector<T> expected;
to_be_tested = { "1", "0", "120", str_num_max.str() };
expected = { 1, 0, 120, std::numeric_limits<T>::max() };
for (std::size_t i = 0; i < to_be_tested.size(); i++)
{
auto msg = "correctly converts " + to_be_tested[i] + " to " + std::to_string(expected[i]);
bandit::it(msg, [&]() {
bool success;
T result;
std::tie(success, result) = string_to_number<T>(to_be_tested[i]);
AssertThat(success, Equals(true));
AssertThat(result, Equals(expected[i]));
});
}
}
template <typename T>
void
test_string_to_number()
{
T num_max = std::numeric_limits<T>::max();
std::stringstream str_num_max;
str_num_max << std::setprecision(std::numeric_limits<T>::max_digits10) << num_max;
T num_min = std::numeric_limits<T>::min();
std::stringstream str_num_min;
str_num_min << std::setprecision(std::numeric_limits<T>::max_digits10) << num_min;
std::vector<std::string> to_be_tested;
std::vector<T> expected;
to_be_tested = { "1", "0", str_num_max.str(), "-1", str_num_min.str() };
expected = { 1, 0, std::numeric_limits<T>::max(), -1, std::numeric_limits<T>::min() };
for (std::size_t i = 0; i < to_be_tested.size(); i++)
{
auto msg = "correctly converts " + to_be_tested[i] + " to " + std::to_string(expected[i]);
bandit::it(msg, [&]() {
bool success;
T result;
std::tie(success, result) = string_to_number<T>(to_be_tested[i]);
AssertThat(success, Equals(true));
AssertThat(result, Equals(expected[i]));
});
}
}
template <typename T>
void
test_string_to_number_fl()
{
T num_max = std::numeric_limits<T>::max();
std::stringstream str_num_max;
str_num_max << std::setprecision(std::numeric_limits<T>::max_digits10) << num_max;
T num_min = std::numeric_limits<T>::min();
std::stringstream str_num_min;
str_num_min << std::setprecision(std::numeric_limits<T>::max_digits10) << num_min;
std::vector<std::string> to_be_tested
= { "0.0", "0", "0.0e1", "2.e3", "2.0e3", "2.0625", str_num_max.str(), str_num_min.str() };
std::vector<T> expected = { 0, 0, 0, 2000, 2000, 2.0625, num_max, num_min };
for (std::size_t i = 0; i < to_be_tested.size(); i++)
{
auto msg = "correctly converts " + to_be_tested[i] + " to " + get_scientific(expected[i]);
bandit::it(msg, [&]() {
bool success;
T result;
std::tie(success, result) = string_to_number<T>(to_be_tested[i]);
AssertThat(success, Equals(true));
AssertThat(result, Equals(expected[i]));
});
}
}
template <typename T>
void
test_string_to_number_failures()
{
std::vector<std::string> to_be_tested = { "a0.02", "2.e3a", "2.a0e3", "2ff.01f" };
for (std::size_t i = 0; i < to_be_tested.size(); i++)
{
auto msg = "correctly throws error on " + to_be_tested[i];
bandit::it(msg, [&]() {
auto [success, result] = string_to_number<T>(to_be_tested[i]);
AssertThat(success, Equals(false));
AssertThat(std::isnan(result), Equals(true));
});
}
}
go_bandit([]() {
//==============================================================================
//
//
bandit::describe("Testing trim functions", []() {
bandit::describe("Testing ltrim function", []() {
std::string to_be_trimmed = " spaces in front ";
......@@ -24,32 +133,19 @@ go_bandit([]() {
});
});
bandit::describe("Testing string_to_number", []() {
bandit::describe("Testing float", []() {
std::string test_str = "0.02";
float expected = 0.02;
bandit::it("correctly converts", [&]() {
auto [success, result] = string_to_number<float>(test_str);
AssertThat(success, Equals(true));
AssertThat(result, Equals(expected));
});
});
bandit::describe("Testing int", []() {
std::string test_str = "2";
int expected = 2;
bandit::it("correctly converts", [&]() {
auto [success, result] = string_to_number<int>(test_str);
AssertThat(success, Equals(true));
AssertThat(result, Equals(expected));
});
bandit::describe("Testing string_to_number positive", []() {
bandit::describe("Testing int", []() { test_string_to_number<int>(); });
bandit::describe("Testing long", []() { test_string_to_number<long>(); });
bandit::describe("Testing unsigned", []() { test_string_to_number_unsigned<unsigned>(); });
bandit::describe("Testing float", []() { test_string_to_number_fl<float>(); });
bandit::describe("Testing double", []() { test_string_to_number_fl<double>(); });
bandit::describe("Testing long double", []() { test_string_to_number_fl<long double>(); });
});
bandit::describe("Testing double", []() {
std::string test_str = "0.02";
double expected = 0.02;
bandit::it("correctly converts", [&]() {
auto [success, result] = string_to_number<double>(test_str);
AssertThat(success, Equals(true));
AssertThat(result, Equals(expected));
});
bandit::describe("Testing string_to_number negatives", []() {
bandit::describe("Testing errors for float", []() { test_string_to_number_failures<float>(); });
bandit::describe("Testing errors for double", []() { test_string_to_number_failures<double>(); });
bandit::describe("Testing errors for long double", []() { test_string_to_number_failures<long double>(); });
});
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment