strtonumbertest.cc 5.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:

#include "config.h"

#include <cassert>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <limits>
#include <random>
#include <sstream>
#include <tuple>
#include <type_traits>
#include <iomanip>

#include <dune/common/classname.hh>
#include <dune/common/hybridutilities.hh>
#include <dune/common/float_cmp.hh>
#include <dune/common/strtonumber.hh>
#include <dune/common/quadmath.hh>
#include <dune/common/timer.hh>
#include <dune/common/test/testsuite.hh>

using namespace Dune;

template <class T, class = void>
struct Distribution
{
  using type = std::uniform_real_distribution<T>;
};

template <class T>
struct Distribution<T, std::enable_if_t<std::is_integral<T>::value>>
{
  using type = std::uniform_int_distribution<T>;
};

// Random number generator used to generate test data
template <class T, class = void>
struct Generator
{
  Generator(T lo = std::numeric_limits<T>::min(),
            T hi = std::numeric_limits<T>::max())
    : gen{}
    , dis(lo, hi)
  {}

  T operator()()
  {
    return dis(gen);
  }

  std::mt19937 gen;
  typename Distribution<T>::type dis;
};

// specialization for bool, since uniform_int_distribution not implemented for bool
template <>
struct Generator<bool,void>
{
  using T = signed short;

  Generator(T lo = std::numeric_limits<T>::min(),
            T hi = std::numeric_limits<T>::max())
    : gen{}
    , dis(lo, hi)
  {}

  bool operator()()
  {
    return dis(gen) >= 0;
  }

  std::mt19937 gen;
  typename Distribution<T>::type dis;
};

// fall-back implementatin for all types that are neither
// is_floating_point nor is_integral, using std::rand()
template <class T>
struct Generator<T, std::enable_if_t<not std::is_arithmetic<T>::value
#ifdef HAVE_QUADMATH
  || std::is_same<T,__float128>::value || std::is_same<T,Float128>::value
#endif
  >>
{
  Generator(T lo = std::numeric_limits<T>::min(),
            T hi = std::numeric_limits<T>::max())
    : lo_(lo)
    , hi_(hi)
  {
    std::srand(5489);
  }

  T operator()() const
  {
    T x = T(std::rand())/RAND_MAX;
    return lo_ + hi_*x - lo_*x;
  }

  T lo_, hi_;
};


template <class T, class = void>
struct Comparator
{
  bool operator()(T const& x, T const& y) { return Dune::FloatCmp::eq(x, y); }
};

template <class T>
struct Comparator<T, std::enable_if_t<std::is_integral<T>::value>>
{
  bool operator()(T const& x, T const& y) { return x == y; }
};


int main()
{
  std::srand(std::time(nullptr));

  using Types = std::tuple<bool, signed short, signed int, signed long, signed long long,
    unsigned short, unsigned int, unsigned long, unsigned long long,
    float, double, long double
#if HAVE_QUADMATH
    , Float128
#endif
    >;

  TestSuite test;

  test.check(strTo<char>("0") == '\0');
  test.check(strTo<signed char>("0") == '\0');
  test.check(strTo<unsigned char>("0") == '\0');

  Hybrid::forEach(Types{}, [&test](auto value)
  {
    using namespace Dune;
    using T = decltype(value);
    std::cout << "test<" << className<T>() << ">...\n";

    Comparator<T> cmp{};
    Generator<T> generate{};
    for (int i = 0; i < 100; ++i) { // repeat the test 100 times
      value = generate();
      std::ostringstream oss;
      oss << std::setprecision(std::numeric_limits<T>::digits10 + 3) << value;
      T val1 = strTo<T>(oss.str().c_str());

      // compare with original value
152
      test.check(bool(value == val1), "Random values");
153 154 155 156 157 158 159
    }

    T value2 = std::numeric_limits<T>::max();
    std::ostringstream oss2;
    oss2 << std::setprecision(std::numeric_limits<T>::digits10 + 4) << value2;

    T val2 = strTo<T>(oss2.str().c_str());
160
    test.check(cmp(value2, val2), "numeric_limits::max");
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

    bool exception = false;
    try {
      T val3 = strTo<T>(("1" + oss2.str()).c_str()); // add one more digit in front of number
    } catch(Dune::RangeError const&) {
      exception = true;
    }
    test.check(exception, "RangeError Exception");
  });


  using FloatingPointTypes = std::tuple<float, double, long double
#if HAVE_QUADMATH
    , Float128
#endif
    >;

  Hybrid::forEach(FloatingPointTypes{}, [&test](auto value)
  {
    using namespace Dune;
    using T = decltype(value);
    Comparator<T> cmp{};
    std::cout << "test<" << className<T>() << ">...\n";

    value = 1.5;

187
    setlocale(LC_NUMERIC, "C");
188 189 190
    T val0 = strTo<T>("1.5");
    test.check(cmp(value, val0), "Locale_C");

191
    char* new_loc = setlocale(LC_NUMERIC, "de_DE.UTF-8");
192 193 194 195 196 197 198
    if (new_loc) {
      T val1 = strTo<T>("1,5");
      test.check(cmp(value, val1), "Locale_de_DE");
    } else {
      std::cout << "### skipped locale de_DE.UTF-8 test\n";
    }

199
    setlocale(LC_NUMERIC, "C");
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
    bool exception = false;
    try {
      T val2 = strTo<T>("1,5");
    } catch(Dune::InvalidArgument const&) {
      exception = true;
    }
    test.check(exception, "InvalidArgument Exception: locale");

    exception = false;
    try {
      T val2 = strTo<T>("1.5__");
    } catch(Dune::InvalidArgument const&) {
      exception = true;
    }
    test.check(exception, "InvalidArgument Exception: trailing characters");

    exception = false;
    try {
      T val2 = strTo<T>("1.5 ");
    } catch(Dune::InvalidArgument const&) {
      exception = true;
    }
    test.check(!exception, "InvalidArgument Exception: trailing whitespace");

    exception = false;
    try {
      T val2 = strTo<T>(" 1.5");
    } catch(Dune::InvalidArgument const&) {
      exception = true;
    }
    test.check(!exception, "InvalidArgument Exception: leading whitespace");
  });

}