File: TestVariantConversionFromString.cxx

package info (click to toggle)
vtk9 9.5.2%2Bdfsg3-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 205,984 kB
  • sloc: cpp: 2,336,570; ansic: 327,116; python: 111,200; yacc: 4,104; java: 3,977; sh: 3,032; xml: 2,771; perl: 2,189; lex: 1,787; makefile: 181; javascript: 165; objc: 153; tcl: 59
file content (236 lines) | stat: -rw-r--r-- 8,326 bytes parent folder | download | duplicates (4)
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
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause

#include "vtkVariant.h"

#include <array>
#include <cmath>
#include <iostream>
#include <limits>
#include <string>
#include <type_traits>

namespace
{

#define Check(expr, message)                                                                       \
  do                                                                                               \
  {                                                                                                \
    if (!(expr))                                                                                   \
    {                                                                                              \
      std::cout << __FILE__ << " L." << __LINE__ << " | " << #expr << " failed: \n"                \
                << message << std::endl;                                                           \
      return false;                                                                                \
    }                                                                                              \
  } while (false)

template <typename T>
T ToNumeric(const vtkVariant& v, bool* valid = nullptr);

#define DefineToNumeric(type, name)                                                                \
  template <>                                                                                      \
  type ToNumeric<type>(const vtkVariant& v, bool* valid)                                           \
  {                                                                                                \
    return v.To##name(valid);                                                                      \
  }

DefineToNumeric(short, Short);
DefineToNumeric(unsigned short, UnsignedShort);
DefineToNumeric(int, Int);
DefineToNumeric(unsigned int, UnsignedInt);
DefineToNumeric(long, Long);
DefineToNumeric(unsigned long, UnsignedLong);
DefineToNumeric(long long, LongLong);
DefineToNumeric(unsigned long long, UnsignedLongLong);
DefineToNumeric(float, Float);
DefineToNumeric(double, Double);

#undef DefineToNumeric

template <typename T, typename std::enable_if<std::is_signed<T>::value>::type* = nullptr>
std::array<T, 7> IntegersValues() noexcept
{
  return std::array<T, 7>{ 0, 1, 42, -1, -42, std::numeric_limits<T>::min(),
    std::numeric_limits<T>::max() };
}

template <typename T, typename std::enable_if<!std::is_signed<T>::value>::type* = nullptr>
std::array<T, 4> IntegersValues() noexcept
{
  return std::array<T, 4>{ 0, 1, 42, std::numeric_limits<T>::max() };
}

template <typename T>
bool CheckIntConversionHelper()
{
  bool valid = false;

  for (auto value : IntegersValues<T>())
  {
    vtkVariant variant = std::to_string(value).c_str();
    Check(ToNumeric<T>(variant, &valid) == value && valid, "Conversion failed");
  }

  return true;
}

bool CheckIntConversion()
{
  Check(CheckIntConversionHelper<short>(), "Failed to perform integer conversion");
  Check(CheckIntConversionHelper<unsigned short>(), "Failed to perform integer conversion");
  Check(CheckIntConversionHelper<int>(), "Failed to perform integer conversion");
  Check(CheckIntConversionHelper<unsigned int>(), "Failed to perform integer conversion");
  Check(CheckIntConversionHelper<long>(), "Failed to perform integer conversion");
  Check(CheckIntConversionHelper<unsigned long>(), "Failed to perform integer conversion");
  Check(CheckIntConversionHelper<long long>(), "Failed to perform integer conversion");
  Check(CheckIntConversionHelper<unsigned long long>(), "Failed to perform integer conversion");

  return true;
}

template <typename T>
bool AlmostEqual(T value, T expected, T tolerance = std::numeric_limits<T>::epsilon())
{
  return expected - tolerance < value && value < expected + tolerance;
}

template <typename T>
bool CheckFloatConversionHelper()
{
  bool valid = false;

  Check(ToNumeric<T>("0.0", &valid) == static_cast<T>(0.0) && valid, "Conversion failed");
  Check(ToNumeric<T>("-1.0", &valid) == static_cast<T>(-1.0) && valid, "Conversion failed");
  Check(ToNumeric<T>("1.0", &valid) == static_cast<T>(1.0) && valid, "Conversion failed");
  Check(
    AlmostEqual(ToNumeric<T>("25.12", &valid), static_cast<T>(25.12), static_cast<T>(0.00001)) &&
      valid,
    "Conversion failed");
  Check(AlmostEqual(
          ToNumeric<T>("-62354.2812", &valid), static_cast<T>(-62354.2812), static_cast<T>(0.01)) &&
      valid,
    "Conversion failed");

  return true;
}

bool CheckFloatConversion()
{
  Check(CheckFloatConversionHelper<float>(), "Checks failed for float");
  Check(CheckFloatConversionHelper<double>(), "Checks failed for double");

  return true;
}

bool CheckCharConversion()
{
  bool valid = false;

  vtkVariant c = "V";
  Check(c.ToChar(&valid) == 'V' && valid, "ToChar must return the first non whitespace char");
  Check(c.ToSignedChar(&valid) == static_cast<signed char>('V') && valid,
    "ToSignedChar must return the first non whitespace char");
  Check(c.ToUnsignedChar(&valid) == static_cast<unsigned char>('V') && valid,
    "ToUnsignedChar must return the first non whitespace char");

  return true;
}

bool CheckNonFiniteConversion()
{
  bool valid = false;

  vtkVariant nan = "Nan";
  Check(std::isnan(nan.ToDouble(&valid)) && valid, "Failed to convert Nan to double");
  Check(std::isnan(nan.ToFloat(&valid)) && valid, "Failed to convert Nan to float");

  vtkVariant inf = "InF";
  Check(std::isinf(inf.ToDouble(&valid)) && valid, "Failed to parse Inf to double");
  Check(inf.ToDouble(&valid) > 0 && valid, "Inf must be positive");
  Check(std::isinf(inf.ToFloat(&valid)) && valid, "Failed to parse Inf to float");
  Check(inf.ToFloat(&valid) > 0 && valid, "Inf must be positive");
  Check(inf.ToInt(&valid) == 0 && !valid, "Inf can not be converted to int");

  vtkVariant ninf = "-InF";
  Check(std::isinf(ninf.ToDouble(&valid)) && valid, "Failed to parse -Inf to double");
  Check(ninf.ToDouble(&valid) < 0 && valid, "-Inf must be negative");
  Check(std::isinf(ninf.ToFloat(&valid)) && valid, "Failed to parse -Inf to float");
  Check(ninf.ToFloat(&valid) < 0 && valid, "-Inf must be negative");
  Check(ninf.ToInt(&valid) == 0 && !valid, "-Inf can not be converted to int");

  vtkVariant word = "Hello";
  word.ToDouble(&valid);
  Check(!valid, "\"Hello\" can not be converted to double");
  word.ToFloat(&valid);
  Check(!valid, "\"Hello\" can not be converted to float");
  word.ToInt(&valid);
  Check(!valid, "\"Hello\" can not be converted to int");

  return true;
}

bool CheckTrimming()
{
  bool valid = false;

  vtkVariant c = "\r\t\n\f\v V\r\t\n\f\v ";
  Check(c.ToChar(&valid) == 'V' && valid, "Trimming not performed for chars");

  vtkVariant integer = "\r\t\n\f\v 42\r\t\n\f\v ";
  Check(integer.ToInt(&valid) == 42 && valid, "Trimming not performed for integers");
  vtkVariant floating = "\r\t\n\f\v 42.3\r\t\n\f\v ";
  const auto floating_value = floating.ToDouble(&valid);
  Check(
    43.01 > floating_value && floating_value > 42.29 && valid, "Trimming not performed for floats");

  vtkVariant nan = "\r\t\n\f\v nan\r\t\n\f\v ";
  Check(std::isnan(nan.ToFloat(&valid)) && valid, "Trimming not performed for non-finite floats");

  vtkVariant cMultiple = "\r\t\n\f\v V\r\t\n\f\v tk \r\t\n\f\v";
  cMultiple.ToChar(&valid);
  Check(!valid, "Must fail because string contains multiple values");

  vtkVariant intMultiple = "\r\t\n\f\v 42\r\t\n\f\v 12 \r\t\n\f\v";
  intMultiple.ToInt(&valid);
  Check(!valid, "Must fail because string contains multiple values");
  intMultiple.ToFloat(&valid);
  Check(!valid, "Must fail because string contains multiple values");

  vtkVariant nonfiniteMultiple = "\r\t\n\f\v nan\r\t\n\f\v 12 \r\t\n\f\v";
  nonfiniteMultiple.ToInt(&valid);
  Check(!valid, "Must fail because string contains multiple values");

  return true;
}

}

int TestVariantConversionFromString(int, char*[])
{
  if (!CheckIntConversion())
  {
    return EXIT_FAILURE;
  }

  if (!CheckFloatConversion())
  {
    return EXIT_FAILURE;
  }

  if (!CheckCharConversion())
  {
    return EXIT_FAILURE;
  }

  if (!CheckNonFiniteConversion())
  {
    return EXIT_FAILURE;
  }

  if (!CheckTrimming())
  {
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}