File: TestNumberToString.cxx

package info (click to toggle)
vtk9 9.3.0%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 267,116 kB
  • sloc: cpp: 2,195,914; ansic: 285,452; python: 104,858; sh: 4,061; yacc: 4,035; java: 3,977; xml: 2,771; perl: 2,189; lex: 1,762; objc: 153; makefile: 150; javascript: 90; tcl: 59
file content (242 lines) | stat: -rw-r--r-- 7,246 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
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
187
188
189
190
191
192
193
194
195
196
197
198
199
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
234
235
236
237
238
239
240
241
242
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkSmartPointer.h"

#include "vtkNumberToString.h"
#include "vtkTypeTraits.h"
#include <limits>
#include <sstream>
#include <vtkMinimalStandardRandomSequence.h>
namespace
{
template <typename T>
int TestConvertPrecision(unsigned int samples);
template <typename T>
int TestConvertLowHigh(unsigned int samples);
template <typename T>
int ConvertNumericLimitsValue(const char* t, T);
}
int TestNumberToString(int, char*[])
{
  int status = EXIT_SUCCESS;

  std::cout << "Testing <numeric_limits>..." << std::endl;
  if (ConvertNumericLimitsValue("unsigned short", static_cast<unsigned short>(0)) == EXIT_FAILURE)
  {
    status = EXIT_FAILURE;
  }
  if (ConvertNumericLimitsValue("short", static_cast<short>(0)) == EXIT_FAILURE)
  {
    status = EXIT_FAILURE;
  }
  if (ConvertNumericLimitsValue("unsigned int", static_cast<unsigned int>(0)) == EXIT_FAILURE)
  {
    status = EXIT_FAILURE;
  }
  if (ConvertNumericLimitsValue("int", static_cast<int>(0)) == EXIT_FAILURE)
  {
    status = EXIT_FAILURE;
  }
  if (ConvertNumericLimitsValue("unsigned long", static_cast<unsigned long>(0)) == EXIT_FAILURE)
  {
    status = EXIT_FAILURE;
  }
  if (ConvertNumericLimitsValue("long", static_cast<int>(0)) == EXIT_FAILURE)
  {
    status = EXIT_FAILURE;
  }
  if (ConvertNumericLimitsValue("float", static_cast<float>(0)) == EXIT_FAILURE)
  {
    status = EXIT_FAILURE;
  }
  if (ConvertNumericLimitsValue("double", static_cast<double>(0)) == EXIT_FAILURE)
  {
    status = EXIT_FAILURE;
  }

  if (status == EXIT_FAILURE)
  {
    return status;
  }

  unsigned int samples = 10000;
  if (TestConvertPrecision<float>(samples) || TestConvertPrecision<double>(samples))
  {
    return EXIT_FAILURE;
  }

  if (TestConvertLowHigh<float>(samples) || TestConvertLowHigh<double>(samples))
  {
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

namespace
{
template <typename T>
int TestConvertPrecision(unsigned int samples)
{
  std::cout << "Testing type: " << typeid(T).name() << std::endl;
  vtkNumberToString converter;
  for (int p = 5; p < 20; ++p)
  {
    unsigned int matches = 0;
    unsigned int mismatches = 0;

    // Now convert numbers to strings. Read the strings as floats and doubles
    // and compare the results with the original values.
    {
      vtkSmartPointer<vtkMinimalStandardRandomSequence> randomSequence =
        vtkSmartPointer<vtkMinimalStandardRandomSequence>::New();
      for (unsigned int i = 0; i < samples; ++i)
      {
        randomSequence->Next();
        T value = randomSequence->GetRangeValue(-1.0, 1.0);
        std::stringstream convertedStr;
        convertedStr << converter.Convert(value);
        std::stringstream rawStr;
        rawStr << std::setprecision(p) << value;

        T convertedValue;
        std::istringstream convertedStream(convertedStr.str());
        convertedStream >> convertedValue;
        if (convertedValue != value)
        {
          std::cout << "ERROR: " << value << " != " << convertedValue << std::endl;
          ++mismatches;
        }
        T rawValue;
        std::istringstream rawStream(rawStr.str());
        rawStream >> rawValue;
        if (rawValue == value)
        {
          ++matches;
        }
      }
    }
    std::cout << "For precision " << p << " Matches without conversion: " << matches << std::endl;
    std::cout << "                 MisMatches with conversion: " << mismatches << std::endl;
    if (mismatches)
    {
      return EXIT_FAILURE;
    }
    if (matches == samples)
    {
      std::cout << "The minimum precision for type " << typeid(T).name() << " is " << p
                << std::endl;
      break;
    }
  }
  return EXIT_SUCCESS;
}

template <typename T>
int TestConvertLowHigh(unsigned int samples)
{
  // Now convert numbers to strings. Read the strings as floats and doubles
  // and compare the results with the original values.
  for (int iLow = -20; iLow <= 0; iLow++)
  {
    for (int iHigh = 0; iHigh <= 20; iHigh++)
    {
      std::cout << "Testing low exponent: " << iLow << ", high exponent: " << iHigh << "."
                << std::endl;
      vtkNumberToString converter;
      converter.SetLowExponent(iLow);
      converter.SetHighExponent(iHigh);
      vtkSmartPointer<vtkMinimalStandardRandomSequence> randomSequence =
        vtkSmartPointer<vtkMinimalStandardRandomSequence>::New();
      for (unsigned int i = 0; i < samples; ++i)
      {
        randomSequence->Next();
        T value = randomSequence->GetRangeValue(
          std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
        std::string str = converter.Convert(value);
        T convertedValue = std::stod(str);
        if (convertedValue != value)
        {
          std::cout << "ERROR: " << value << " != " << convertedValue << std::endl;
          return EXIT_FAILURE;
        }
      }
    }
  }

  return EXIT_SUCCESS;
}

template <typename T>
int ConvertNumericLimitsValue(const char* t, T)
{
  vtkNumberToString converter;
  int status = EXIT_SUCCESS;
  {
    T value = std::numeric_limits<T>::max();
    std::stringstream convertedStr;
    convertedStr << converter.Convert(value);
    std::istringstream convertedStream(convertedStr.str());
    std::cout << t << "(max) "
              << "raw: " << value << " converted: " << convertedStream.str() << std::endl;

    T convertedValue;
    convertedStream >> convertedValue;
    if (value != convertedValue)
    {
      std::cout << "ERROR: Bad conversion of std::max" << std::endl;
      status = EXIT_FAILURE;
    }
  }
  {
    T value = std::numeric_limits<T>::min();
    std::stringstream convertedStr;
    convertedStr << converter.Convert(value);
    std::istringstream convertedStream(convertedStr.str());
    std::cout << t << "(min) "
              << "raw: " << value << " converted: " << convertedStream.str() << std::endl;

    T convertedValue;
    convertedStream >> convertedValue;
    if (value != convertedValue)
    {
      std::cout << "ERROR: Bad conversion of std::min" << std::endl;
      status = EXIT_FAILURE;
    }
  }
  {
    T value = std::numeric_limits<T>::lowest();
    std::stringstream convertedStr;
    convertedStr << converter.Convert(value);
    std::istringstream convertedStream(convertedStr.str());
    std::cout << t << "(lowest) "
              << "raw: " << value << " converted: " << convertedStream.str() << std::endl;

    T convertedValue;
    convertedStream >> convertedValue;
    if (value != convertedValue)
    {
      std::cout << "ERROR: Bad conversion of std::lowest" << std::endl;
      status = EXIT_FAILURE;
    }
  }
  {
    T value = std::numeric_limits<T>::epsilon();
    std::stringstream convertedStr;
    convertedStr << converter.Convert(value);
    std::istringstream convertedStream(convertedStr.str());
    std::cout << t << "(epsilon) "
              << "raw: " << value << " converted: " << convertedStream.str() << std::endl;

    T convertedValue;
    convertedStream >> convertedValue;
    if (value != convertedValue)
    {
      std::cout << "ERROR: Bad conversion of std::epsilon" << std::endl;
      status = EXIT_FAILURE;
    }
  }
  return status;
}
}