File: cxx_nan_inf_in_prop.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (446 lines) | stat: -rw-r--r-- 16,804 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// NOLINTBEGIN(*)

#include <limits>

#include "cxx_common.h"

#undef SUITE_NAME
#define SUITE_NAME NanInfInPropSuite

class NanInfInPropSuite : public CxxTest::TestSuite
{
  protected:
    DeviceProxy *device1, *dserver;

  public:
    SUITE_NAME()
    {
        //
        // Arguments check -------------------------------------------------
        //

        string device1_name;

        // predefined mandatory parameters
        device1_name = CxxTest::TangoPrinter::get_param("device1");

        // always add this line, otherwise arguments will not be parsed correctly
        CxxTest::TangoPrinter::validate_args();

        //
        // Initialization --------------------------------------------------
        //

        try
        {
            device1 = new DeviceProxy(device1_name);
            device1->ping();
        }
        catch(CORBA::Exception &e)
        {
            Except::print_exception(e);
            exit(-1);
        }
    }

    virtual ~SUITE_NAME()
    {
        //
        // Clean up --------------------------------------------------------
        //

        // clean up in case test suite terminates before my_restore_point is restored to defaults
        if(CxxTest::TangoPrinter::is_restore_set("NanInArrayProp_restore_point"))
        {
            try
            {
                string prop_name = "NaNInfArrayProperty";
                device1->delete_property(prop_name);
            }
            catch(DevFailed &e)
            {
                TEST_LOG << endl << "Exception in suite tearDown():" << endl;
                Except::print_exception(e);
            }
        }
        if(CxxTest::TangoPrinter::is_restore_set("NanInScalarProp_restore_point"))
        {
            try
            {
                string prop_name = "NaNInfScalarProperty";
                device1->delete_property(prop_name);
            }
            catch(DevFailed &e)
            {
                TEST_LOG << endl << "Exception in suite tearDown():" << endl;
                Except::print_exception(e);
            }
        }

        // delete dynamically allocated objects
        delete device1;
    }

    static SUITE_NAME *createSuite()
    {
        return new SUITE_NAME();
    }

    static void destroySuite(SUITE_NAME *suite)
    {
        delete suite;
    }

    void set_property(const char *prop_name, const char *str_prop_val)
    {
        Tango::DbData db_data_w;
        Tango::DbDatum db_datum(prop_name);
        TS_ASSERT_THROWS_NOTHING(db_datum << str_prop_val);
        db_data_w.push_back(db_datum);
        TS_ASSERT_THROWS_NOTHING(device1->put_property(db_data_w));
    }

    template <typename T>
    void write_then_read_array_property_containing_nan_and_inf()
    {
        // Write NaNInfArrayProperty device property
        Tango::DbData db_data_w;
        vector<T> array_val;
        array_val.push_back(T(1.2));
        if(std::numeric_limits<T>::has_quiet_NaN)
        {
            array_val.push_back(std::numeric_limits<T>::quiet_NaN());
            array_val.push_back(-std::numeric_limits<T>::quiet_NaN());
        }
        array_val.push_back(T(3.4));
        if(std::numeric_limits<T>::has_infinity)
        {
            array_val.push_back(std::numeric_limits<T>::infinity());
            array_val.push_back(-std::numeric_limits<T>::infinity());
        }
        array_val.push_back(T(-9.78));

        Tango::DbDatum db_datum("NaNInfArrayProperty");
        TS_ASSERT_THROWS_NOTHING(db_datum << array_val);
        db_data_w.push_back(db_datum);
        TS_ASSERT_THROWS_NOTHING(device1->put_property(db_data_w));

        // Read NanInfArrayProperty device property
        Tango::DbData db_data_r;
        string prop_name = "NaNInfArrayProperty";
        TS_ASSERT_THROWS_NOTHING(device1->get_property(prop_name, db_data_r));

        // Extract as T array:
        size_t expected_size = 3;
        if(std::numeric_limits<T>::has_quiet_NaN)
        {
            expected_size += 2;
        }
        if(std::numeric_limits<T>::has_infinity)
        {
            expected_size += 2;
        }
        vector<T> read_array_val;
        size_t index = 0;
        TS_ASSERT(db_data_r[0] >> read_array_val);
        TS_ASSERT_EQUALS(read_array_val.size(), expected_size);
        TS_ASSERT_EQUALS(read_array_val[index], T(1.2));
        index++;
        if(std::numeric_limits<T>::has_quiet_NaN)
        {
            TS_ASSERT(std::isnan(read_array_val[index]));
            index++;
            // -NaN is extracted as NaN
            TS_ASSERT(std::isnan(read_array_val[index]));
            index++;
        }
        TS_ASSERT_EQUALS(read_array_val[index], T(3.4));
        index++;
        if(std::numeric_limits<T>::has_infinity)
        {
            TS_ASSERT(std::isinf(read_array_val[index]));
            TS_ASSERT_LESS_THAN(0, read_array_val[index]);
            index++;
            TS_ASSERT(std::isinf(read_array_val[index]));
            TS_ASSERT_LESS_THAN(read_array_val[index], 0);
            index++;
        }
        TS_ASSERT_EQUALS(read_array_val[index], T(-9.78));

        // Extract as long array:
        if((std::numeric_limits<T>::has_infinity) || (std::numeric_limits<T>::has_quiet_NaN))
        {
            vector<Tango::DevLong> long_array_val;
            TS_ASSERT(!(db_data_r[0] >> long_array_val));
        }
    }

    void NaN_in_scalar_property(const string &prop_value)
    {
        //
        // if your code modifies the default (device) configuration, append the following line straight after
        CxxTest::TangoPrinter::restore_set("NanInfScalarProp_restore_point");

        // Write NaNInfArrayProperty device property
        Tango::DbData db_data_r;

        // Write NaNInfScalarProperty device property
        set_property("NaNInfScalarProperty", prop_value.c_str());

        // Read NaNInfScalarProperty device property
        string prop_name = "NaNInfScalarProperty";
        TS_ASSERT_THROWS_NOTHING(device1->get_property(prop_name, db_data_r));
        // Extract as a string:
        string string_scalar_val;
        TS_ASSERT(db_data_r[0] >> string_scalar_val);
        TS_ASSERT_EQUALS(string_scalar_val, prop_value);

        // Extract as float:
        Tango::DevFloat float_val;
        TS_ASSERT(db_data_r[0] >> float_val);
        TS_ASSERT(std::isnan(float_val));

        // Extract as double:
        Tango::DevDouble double_val;
        TS_ASSERT(db_data_r[0] >> double_val);
        TS_ASSERT(std::isnan(double_val));

        // Extract as short
        Tango::DevShort short_val;
        TS_ASSERT(!(db_data_r[0] >> short_val));

        // Restore the default configuration
        TS_ASSERT_THROWS_NOTHING(device1->delete_property(prop_name));
        // if the test suite fails here, thanks to the restore point, the test suite TearDown method will restore the
        // defaults after you set back the default configuration, append the following line
        CxxTest::TangoPrinter::restore_unset("NanInfScalarProp_restore_point");
    }

    //
    // Tests -------------------------------------------------------
    //

    // test NaN and infinity in array property

    void test_NaN_and_infinity_in_array_property()
    {
        //
        // if your code modifies the default (device) configuration, append the following line straight after
        CxxTest::TangoPrinter::restore_set("NanInArrayProp_restore_point");

        // Write NaNInfArrayProperty device property
        Tango::DbData db_data_w;
        vector<string> str_val;
        str_val.push_back("1.2");
        str_val.push_back("NaN");
        str_val.push_back("3.4");
        str_val.push_back("inf");
        str_val.push_back("nan");
        str_val.push_back("-iNf");
        str_val.push_back("-9.78");
        str_val.push_back("-NaN");
        str_val.push_back("-89101112.1314");

        Tango::DbDatum db_datum("NaNInfArrayProperty");
        TS_ASSERT_THROWS_NOTHING(db_datum << str_val);
        db_data_w.push_back(db_datum);
        TS_ASSERT_THROWS_NOTHING(device1->put_property(db_data_w));

        // Read NanInfArrayProperty device property
        Tango::DbData db_data_r;
        string prop_name = "NaNInfArrayProperty";
        TS_ASSERT_THROWS_NOTHING(device1->get_property(prop_name, db_data_r));

        // Extract as a string array:
        vector<string> string_array_val;
        TS_ASSERT(db_data_r[0] >> string_array_val);
        TS_ASSERT_EQUALS(string_array_val.size(), (size_t) 9);
        TS_ASSERT_EQUALS(string_array_val[0], "1.2");
        TS_ASSERT_EQUALS(string_array_val[1], "NaN");
        TS_ASSERT_EQUALS(string_array_val[2], "3.4");
        TS_ASSERT_EQUALS(string_array_val[3], "inf");
        TS_ASSERT_EQUALS(string_array_val[4], "nan");
        TS_ASSERT_EQUALS(string_array_val[5], "-iNf");
        TS_ASSERT_EQUALS(string_array_val[6], "-9.78");
        TS_ASSERT_EQUALS(string_array_val[7], "-NaN");
        TS_ASSERT_EQUALS(string_array_val[8], "-89101112.1314");

        // Extract as a float array:
        vector<Tango::DevFloat> float_array_val;
        TS_ASSERT(db_data_r[0] >> float_array_val);
        TS_ASSERT_EQUALS(float_array_val.size(), (size_t) 9);
        TS_ASSERT_EQUALS(float_array_val[0], Tango::DevFloat(1.2));
        TS_ASSERT(std::isnan(float_array_val[1]));
        TS_ASSERT_EQUALS(float_array_val[2], Tango::DevFloat(3.4));
        TS_ASSERT(std::isinf(float_array_val[3]));
        TS_ASSERT(std::isnan(float_array_val[4]));
        TS_ASSERT(std::isinf(float_array_val[5]));
        TS_ASSERT_LESS_THAN(float_array_val[5], 0);
        TS_ASSERT_EQUALS(float_array_val[6], Tango::DevFloat(-9.78));
        TS_ASSERT(std::isnan(float_array_val[7]));
        TS_ASSERT_EQUALS(float_array_val[8], Tango::DevFloat(-89101112.1314));

        // Extract as a double array:
        vector<Tango::DevDouble> double_array_val;
        TS_ASSERT(db_data_r[0] >> double_array_val);
        TS_ASSERT_EQUALS(double_array_val.size(), (size_t) 9);
        TS_ASSERT_EQUALS(double_array_val[0], Tango::DevDouble(1.2));
        TS_ASSERT(std::isnan(double_array_val[1]));
        TS_ASSERT_EQUALS(double_array_val[2], Tango::DevDouble(3.4));
        TS_ASSERT(std::isinf(double_array_val[3]));
        TS_ASSERT(std::isnan(double_array_val[4]));
        TS_ASSERT(std::isinf(double_array_val[5]));
        TS_ASSERT_LESS_THAN(double_array_val[5], 0);
        TS_ASSERT_EQUALS(double_array_val[6], Tango::DevDouble(-9.78));
        TS_ASSERT(std::isnan(double_array_val[7]));
        TS_ASSERT_EQUALS(double_array_val[8], Tango::DevDouble(-89101112.1314));

        // Extract as long array:
        vector<Tango::DevLong> long_array_val;
        TS_ASSERT(!(db_data_r[0] >> long_array_val));

        // Restore the default configuration
        TS_ASSERT_THROWS_NOTHING(device1->delete_property(prop_name));
        // if the test suite fails here, thanks to the restore point, the test suite TearDown method will restore the
        // defaults after you set back the default configuration, append the following line
        CxxTest::TangoPrinter::restore_unset("NanInArrayProp_restore_point");
    }

    // test write then read array float property with nan and inf
    void test_write_then_read_array_float_property_with_nan_and_inf()
    {
        //
        // if your code modifies the default (device) configuration, append the following line straight after
        CxxTest::TangoPrinter::restore_set("NanInArrayProp_restore_point");

        write_then_read_array_property_containing_nan_and_inf<Tango::DevFloat>();

        // Restore the default configuration
        string prop_name = "NaNInfArrayProperty";
        TS_ASSERT_THROWS_NOTHING(device1->delete_property(prop_name));
        // if the test suite fails here, thanks to the restore point, the test suite TearDown method will restore the
        // defaults after you set back the default configuration, append the following line
        CxxTest::TangoPrinter::restore_unset("NanInArrayProp_restore_point");
    }

    // test write then read array double property with nan and inf
    void test_write_then_read_array_double_property_with_nan_and_inf()
    {
        //
        // if your code modifies the default (device) configuration, append the following line straight after
        CxxTest::TangoPrinter::restore_set("NanInArrayProp_restore_point");

        write_then_read_array_property_containing_nan_and_inf<Tango::DevDouble>();

        // Restore the default configuration
        string prop_name = "NaNInfArrayProperty";
        TS_ASSERT_THROWS_NOTHING(device1->delete_property(prop_name));
        // if the test suite fails here, thanks to the restore point, the test suite TearDown method will restore the
        // defaults after you set back the default configuration, append the following line
        CxxTest::TangoPrinter::restore_unset("NanInArrayProp_restore_point");
    }

    // test NaN in scalar property
    void test_NaN_in_scalar_property()
    {
        NaN_in_scalar_property("NaN");
        NaN_in_scalar_property("nan");
        NaN_in_scalar_property("Nan");
    }

    // test -NaN in scalar property
    void test_Negative_NaN_in_scalar_property()
    {
        NaN_in_scalar_property("-NaN");
        NaN_in_scalar_property("-nan");
        NaN_in_scalar_property("-naN");
    }

    // test infinity in scalar property
    void test_infinity_in_scalar_property()
    {
        //
        // if your code modifies the default (device) configuration, append the following line straight after
        CxxTest::TangoPrinter::restore_set("NanInfScalarProp_restore_point");

        // Write NaNInfArrayProperty device property
        Tango::DbData db_data_r;

        // Write NaNInfScalarProperty device property
        set_property("NaNInfScalarProperty", "InF");

        // Read NaNInfScalarProperty device property
        string prop_name = "NaNInfScalarProperty";
        TS_ASSERT_THROWS_NOTHING(device1->get_property(prop_name, db_data_r));
        // Extract as a string:
        string string_scalar_val;
        TS_ASSERT(db_data_r[0] >> string_scalar_val);
        TS_ASSERT_EQUALS(string_scalar_val, "InF");

        // Extract as float:
        Tango::DevFloat float_val;
        TS_ASSERT(db_data_r[0] >> float_val);
        TS_ASSERT(std::isinf(float_val));

        // Extract as double:
        Tango::DevDouble double_val;
        TS_ASSERT(db_data_r[0] >> double_val);
        TS_ASSERT(std::isinf(double_val));

        // Extract as short
        Tango::DevShort short_val;
        TS_ASSERT(!(db_data_r[0] >> short_val));

        // Restore the default configuration
        TS_ASSERT_THROWS_NOTHING(device1->delete_property(prop_name));
        // if the test suite fails here, thanks to the restore point, the test suite TearDown method will restore the
        // defaults after you set back the default configuration, append the following line
        CxxTest::TangoPrinter::restore_unset("NanInfScalarProp_restore_point");
    }

    // test -iinfinity in scalar property
    void test_minus_infinity_in_scalar_property()
    {
        //
        // if your code modifies the default (device) configuration, append the following line straight after
        CxxTest::TangoPrinter::restore_set("NanInfScalarProp_restore_point");

        // Write NaNInfArrayProperty device property
        Tango::DbData db_data_r;

        // Write NaNInfScalarProperty device property
        set_property("NaNInfScalarProperty", "-inF");

        // Read NaNInfScalarProperty device property
        string prop_name = "NaNInfScalarProperty";
        TS_ASSERT_THROWS_NOTHING(device1->get_property(prop_name, db_data_r));
        // Extract as a string:
        string string_scalar_val;
        TS_ASSERT(db_data_r[0] >> string_scalar_val);
        TS_ASSERT_EQUALS(string_scalar_val, "-inF");

        // Extract as float:
        Tango::DevFloat float_val;
        TS_ASSERT(db_data_r[0] >> float_val);
        TS_ASSERT(std::isinf(float_val));
        TS_ASSERT_LESS_THAN(float_val, 0);

        // Extract as double:
        Tango::DevDouble double_val;
        TS_ASSERT(db_data_r[0] >> double_val);
        TS_ASSERT(std::isinf(double_val));
        TS_ASSERT_LESS_THAN(double_val, 0);

        // Extract as short
        Tango::DevShort short_val;
        TS_ASSERT(!(db_data_r[0] >> short_val));

        // Restore the default configuration
        TS_ASSERT_THROWS_NOTHING(device1->delete_property(prop_name));
        // if the test suite fails here, thanks to the restore point, the test suite TearDown method will restore the
        // defaults after you set back the default configuration, append the following line
        CxxTest::TangoPrinter::restore_unset("NanInfScalarProp_restore_point");
    }
};

// NOLINTEND(*)