strtonumbertest.cc 5.37 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
// -*- 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,
124
    unsigned short, unsigned int, unsigned long, unsigned long long
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    >;

  TestSuite test;

  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
144
      test.check(bool(value == val1), "Random values");
145 146 147 148 149 150 151
    }

    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());
152
    test.check(cmp(value2, val2), "numeric_limits::max");
153 154 155

    bool exception = false;
    try {
156
      DUNE_UNUSED T val3 = strTo<T>(("1" + oss2.str()).c_str()); // add one more digit in front of number
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    } 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;

179
    setlocale(LC_NUMERIC, "C");
180 181 182
    T val0 = strTo<T>("1.5");
    test.check(cmp(value, val0), "Locale_C");

183
    char* new_loc = setlocale(LC_NUMERIC, "de_DE.UTF-8");
184 185
    if (new_loc && Std::is_detected<Impl::HasFromChars, T>::value) {
      T val1 = strTo<T>("1.5");
186 187 188 189 190
      test.check(cmp(value, val1), "Locale_de_DE");
    } else {
      std::cout << "### skipped locale de_DE.UTF-8 test\n";
    }

191
    setlocale(LC_NUMERIC, "C");
192 193
    bool exception = false;
    try {
194
      DUNE_UNUSED T val2 = strTo<T>("1,5");
195 196 197 198 199 200 201
    } catch(Dune::InvalidArgument const&) {
      exception = true;
    }
    test.check(exception, "InvalidArgument Exception: locale");

    exception = false;
    try {
202
      DUNE_UNUSED T val2 = strTo<T>("1.5__");
203 204 205 206 207 208 209
    } catch(Dune::InvalidArgument const&) {
      exception = true;
    }
    test.check(exception, "InvalidArgument Exception: trailing characters");

    exception = false;
    try {
210
      DUNE_UNUSED T val2 = strTo<T>("1.5 ");
211 212 213
    } catch(Dune::InvalidArgument const&) {
      exception = true;
    }
214
    test.check(exception, "InvalidArgument Exception: trailing whitespace");
215 216 217

    exception = false;
    try {
218
      DUNE_UNUSED T val2 = strTo<T>(" 1.5");
219 220 221 222 223 224 225
    } catch(Dune::InvalidArgument const&) {
      exception = true;
    }
    test.check(!exception, "InvalidArgument Exception: leading whitespace");
  });

}