File: testRDValue.cpp

package info (click to toggle)
rdkit 202209.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 203,880 kB
  • sloc: cpp: 334,239; python: 80,247; ansic: 24,579; java: 7,667; sql: 2,123; yacc: 1,884; javascript: 1,358; lex: 1,260; makefile: 576; xml: 229; fortran: 183; cs: 181; sh: 101
file content (344 lines) | stat: -rw-r--r-- 9,027 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <RDGeneral/test.h>
#include "RDValue.h"
#include "RDProps.h"
#include "Invariant.h"
#include "StreamOps.h"
#include <limits>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
#include <boost/any.hpp>

using namespace RDKit;

template <class T>
void testLimits() {
  BOOST_LOG(rdErrorLog) << "Test limits" << std::endl;
  // check numeric limits
  {
    RDValue v(std::numeric_limits<T>::min());
    std::cerr << "min: " << std::numeric_limits<T>::min() << " "
              << rdvalue_cast<T>(v) << std::endl;
    CHECK_INVARIANT(rdvalue_cast<T>(v) == std::numeric_limits<T>::min(),
                    "bad min");
    CHECK_INVARIANT(
        rdvalue_cast<T>(RDValue(v)) == std::numeric_limits<T>::min(),
        "bad min");
    v = std::numeric_limits<T>::max();
    CHECK_INVARIANT(rdvalue_cast<T>(v) == std::numeric_limits<T>::max(),
                    "bad max");
    CHECK_INVARIANT(
        rdvalue_cast<T>(RDValue(v)) == std::numeric_limits<T>::max(),
        "bad max");
  }
  {
    RDValue v(std::numeric_limits<T>::max());
    CHECK_INVARIANT(rdvalue_cast<T>(v) == std::numeric_limits<T>::max(),
                    "bad max");
    RDValue vv(v);
    CHECK_INVARIANT(rdvalue_cast<T>(vv) == std::numeric_limits<T>::max(),
                    "bad max");

    v = std::numeric_limits<T>::min();
    RDValue vvv(v);
    CHECK_INVARIANT(rdvalue_cast<T>(v) == std::numeric_limits<T>::min(),
                    "bad min");
    CHECK_INVARIANT(rdvalue_cast<T>(vvv) == std::numeric_limits<T>::min(),
                    "bad min");
  }
  BOOST_LOG(rdErrorLog) << "..done" << std::endl;
}

void testPOD() {
  BOOST_LOG(rdErrorLog) << "Test POD" << std::endl;
  testLimits<int>();
  testLimits<unsigned int>();
  testLimits<double>();
  testLimits<float>();
  testLimits<bool>();
  BOOST_LOG(rdErrorLog) << "..done" << std::endl;
}

template <class T>
void testVector() {
  T minv = std::numeric_limits<T>::min();
  T maxv = std::numeric_limits<T>::max();
  std::vector<T> data;
  data.push_back(minv);
  data.push_back(maxv);
  data.push_back(T());

  RDValue v(data);
  CHECK_INVARIANT(rdvalue_cast<std::vector<T>>(v) == data, "bad vec");
  RDValue vv;
  copy_rdvalue(vv, v);
  CHECK_INVARIANT(rdvalue_cast<std::vector<T>>(vv) == data,
                  "bad copy constructor");
  RDValue::cleanup_rdvalue(v);  // desctructor...
  RDValue::cleanup_rdvalue(vv);
}

void testPODVectors() {
  BOOST_LOG(rdErrorLog) << "Test String Vect" << std::endl;
  testVector<int>();
  testVector<unsigned int>();
  testVector<double>();
  testVector<float>();
  testVector<long double>();  // stored in anys
  BOOST_LOG(rdErrorLog) << "..done" << std::endl;
}

void testStringVect() {
  BOOST_LOG(rdErrorLog) << "Test String Vect" << std::endl;
  std::vector<std::string> vecs;
  vecs.emplace_back("my");
  vecs.emplace_back("dog");
  vecs.emplace_back("has");
  vecs.emplace_back("fleas");
  RDValue v(vecs);
  CHECK_INVARIANT(rdvalue_cast<std::vector<std::string>>(v) == vecs,
                  "bad vect");
  RDValue vc;
  copy_rdvalue(vc, v);
  CHECK_INVARIANT(rdvalue_cast<std::vector<std::string>>(vc) == vecs,
                  "bad vect");
  RDValue vv = vecs;
  RDValue vvc;
  copy_rdvalue(vvc, vv);
  CHECK_INVARIANT(rdvalue_cast<std::vector<std::string>>(vv) == vecs,
                  "bad vect");
  CHECK_INVARIANT(rdvalue_cast<std::vector<std::string>>(vvc) == vecs,
                  "bad vect");

  RDValue::cleanup_rdvalue(v);    // desctructor...
  RDValue::cleanup_rdvalue(vc);   // desctructor...
  RDValue::cleanup_rdvalue(vv);   // desctructor...
  RDValue::cleanup_rdvalue(vvc);  // desctructor...
  BOOST_LOG(rdErrorLog) << "..done" << std::endl;
}

void testMapsAndLists() {
  BOOST_LOG(rdErrorLog) << "Test Maps And Lists" << std::endl;
  {
    typedef std::map<std::string, int> listtype;
    listtype m;
    m["foo"] = 1;
    m["bar"] = 2;
    RDValue v(m);
    CHECK_INVARIANT(rdvalue_cast<listtype>(v) == m, "bad map cast");
    RDValue::cleanup_rdvalue(v);
  }
  {
    std::list<std::string> m;
    m.emplace_back("foo");
    m.emplace_back("bar");
    RDValue v(m);
    CHECK_INVARIANT(rdvalue_cast<std::list<std::string>>(v) == m,
                    "bad map cast");
    RDValue::cleanup_rdvalue(v);
  }
  BOOST_LOG(rdErrorLog) << "..done" << std::endl;
}

void testNaN() {
  // make a NaN
  BOOST_LOG(rdErrorLog) << "Test NaN" << std::endl;
  double nan = sqrt(-1.0);
  RDValue v(nan);
  TEST_ASSERT(v.getTag() == RDTypeTag::DoubleTag);

  CHECK_INVARIANT(std::isnan(rdvalue_cast<double>(v)),
                  "Oops, can't store NaNs!");

  RDValue vv(2.0);
  TEST_ASSERT(rdvalue_is<double>(vv));
  TEST_ASSERT(vv.getTag() == RDTypeTag::DoubleTag);
  BOOST_LOG(rdErrorLog) << "..done" << std::endl;
}

template <class T>
std::vector<T> makeVec() {
  std::vector<T> vec;
  vec.push_back((T)0);
  vec.push_back((T)1);
  vec.push_back((T)2);
  vec.push_back((T)3);
  vec.push_back((T)4);
  vec.push_back((T)5);
  return vec;
}

template <class T>
void testProp(T val) {
  std::stringstream ss;

  {
    RDProps p;
    p.setProp<T>("foo", val);
    TEST_ASSERT(streamWriteProps(ss, p));
  }

  {
    RDProps p2;
    streamReadProps(ss, p2);
    TEST_ASSERT(p2.getProp<T>("foo") == val);
  }
};

void testPropertyPickler() {
  BOOST_LOG(rdErrorLog) << "Test Property Pickler" << std::endl;
  std::cerr << "== int" << std::endl;
  testProp<int>(1234);
  std::cerr << "== double" << std::endl;
  testProp<double>(1234.);
  std::cerr << "== float" << std::endl;
  testProp<float>(1234.0f);
  std::cerr << "== unsigned int" << std::endl;
  testProp<unsigned int>(1234u);
  std::cerr << "== bool" << std::endl;
  testProp<bool>(true);
  std::cerr << "== std::string" << std::endl;
  testProp<std::string>(
      std::string("the quick brown fox jumps over the lazy dog"));

  testProp(0);
  testProp(0.);
  testProp(0.0f);
  testProp(0u);
  testProp(false);

  /*
  testProp(makeVec<int>());
  testProp(makeVec<int>());
  testProp(makeVec<int>());
  testProp(makeVec<unsigned int>());

  {
    std::vector<std::string> v;
    v.push_back("a");
    v.push_back("b");
    v.push_back("c");
    v.push_back("d");
    v.push_back("e");
    testProp(v);
  }
  */
  BOOST_LOG(rdErrorLog) << "..done" << std::endl;
}

void testPickleBinaryString() {
  BOOST_LOG(rdErrorLog) << "Pickle Binary String" << std::endl;
  char buf[10];
  for (int i = 0; i < 10; ++i) {
    buf[i] = (char)i;
  }
  std::string str(buf, 10);
  std::stringstream ss;

  {
    RDProps p;
    p.setProp<std::string>("foo", str);
    TEST_ASSERT(streamWriteProps(ss, p));
  }

  {
    RDProps p2;
    streamReadProps(ss, p2);
    TEST_ASSERT(p2.getProp<std::string>("foo") == str);
  }
  BOOST_LOG(rdErrorLog) << "..done" << std::endl;
}

void testIntConversions() {
  RDProps p;
  p.setProp<int>("foo", 1);
  p.getProp<std::int64_t>("foo");
  p.getProp<std::int8_t>("foo");
  p.getProp<std::int16_t>("foo");
  p.getProp<std::uint16_t>("foo");

  p.setProp<int64_t>("foo", 1);
  p.getProp<int64_t>("foo");

  p.setProp<unsigned int>("foo", 1);
  p.getProp<std::uint64_t>("foo");
  p.getProp<std::uint8_t>("foo");
  p.getProp<std::uint16_t>("foo");

  p.getProp<std::int16_t>("foo");

  // Test that min/max values of smaller types do not under/overflow
  p.setProp<unsigned int>("foo", 0);
  p.getProp<std::uint8_t>("foo");
  p.getProp<std::uint16_t>("foo");

  p.setProp<unsigned int>("foo", 255);
  p.getProp<std::uint8_t>("foo");

  p.setProp<unsigned int>("foo", 65535);
  p.getProp<std::uint16_t>("foo");

  p.setProp<int>("foo", -128);
  p.getProp<std::int8_t>("foo");

  p.setProp<int>("foo", -32768);
  p.getProp<std::int16_t>("foo");

  p.setProp<int>("foo", 127);
  p.getProp<std::int8_t>("foo");

  p.setProp<int>("foo", 32767);
  p.getProp<std::int16_t>("foo");

  // Test some overflows
  p.setProp<int>("foo", 32767 + 1);
  try {
    p.getProp<std::int8_t>("foo");  // should fail
    TEST_ASSERT(0);
  } catch (boost::numeric::positive_overflow&) {
  }
  try {
    p.getProp<std::uint8_t>("foo");  // should fail
    TEST_ASSERT(0);
  } catch (boost::numeric::positive_overflow&) {
  }
  try {
    p.getProp<std::int16_t>("foo");  // should fail
    TEST_ASSERT(0);
  } catch (boost::numeric::positive_overflow&) {
  }
  p.setProp<int>("foo", 65535 + 1);
  try {
    p.getProp<std::uint16_t>("foo");  // should fail
    TEST_ASSERT(0);
  } catch (boost::numeric::positive_overflow&) {
  }

  p.setProp<int>("foo", -1);
  try {
    p.getProp<std::uint8_t>("foo");  // should fail
    TEST_ASSERT(0);
  } catch (boost::numeric::negative_overflow&) {
  }

  p.getProp<std::int16_t>("foo");  // should pass
  try {
    p.getProp<std::uint16_t>("foo");  // should fail
    TEST_ASSERT(0);
  } catch (boost::numeric::negative_overflow&) {
  }
}
int main() {
  std::cerr << "-- running tests -- " << std::endl;
  testPOD();
  testPODVectors();
  testStringVect();
  testNaN();
  testPropertyPickler();
  testPickleBinaryString();
  testIntConversions();
}