File: catch2_change_event_on_nan.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 (474 lines) | stat: -rw-r--r-- 19,846 bytes parent folder | download | duplicates (3)
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <tango/tango.h>
#include <memory>
#include <cmath>
#include <catch2/matchers/catch_matchers_floating_point.hpp>

#include "catch2_common.h"

constexpr static Tango::DevDouble ATTR_INIT_VALUE = 0.0;
static const Tango::DevDouble ATTR_NAN_VALUE = std::nan("nan");

// Test device class for Tango::DevDouble
template <class Base>
class ChangeEventOnNanDev_Double : public Base
{
  public:
    using Base::Base;

    ~ChangeEventOnNanDev_Double() override { }

    void init_device() override
    {
        attr_abs_value = ATTR_INIT_VALUE;
        attr_rel_value = ATTR_INIT_VALUE;
    }

    void set_abs_nan()
    {
        attr_abs_value = ATTR_NAN_VALUE;
    }

    void set_rel_nan()
    {
        attr_rel_value = ATTR_NAN_VALUE;
    }

    void unset_abs_nan()
    {
        attr_abs_value = ATTR_INIT_VALUE;
    }

    void unset_rel_nan()
    {
        attr_rel_value = ATTR_INIT_VALUE;
    }

    void read_abs(Tango::Attribute &att)
    {
        att.set_value_date_quality(&attr_abs_value, std::chrono::system_clock::now(), Tango::ATTR_VALID);
    }

    void read_rel(Tango::Attribute &att)
    {
        att.set_value_date_quality(&attr_rel_value, std::chrono::system_clock::now(), Tango::ATTR_VALID);
    }

    static void attribute_factory(std::vector<Tango::Attr *> &attrs)
    {
        // attribute with absolute change
        auto attr_abs = new TangoTest::AutoAttr<&ChangeEventOnNanDev_Double::read_abs>("attr_abs", Tango::DEV_DOUBLE);
        Tango::UserDefaultAttrProp abs_props;
        abs_props.set_event_abs_change("0.01");
        attr_abs->set_default_properties(abs_props);
        attr_abs->set_polling_period(100);
        attrs.push_back(attr_abs);

        // attribute with relative change
        auto attr_rel = new TangoTest::AutoAttr<&ChangeEventOnNanDev_Double::read_rel>("attr_rel", Tango::DEV_DOUBLE);
        Tango::UserDefaultAttrProp rel_props;
        rel_props.set_event_rel_change("0.01");
        attr_rel->set_default_properties(rel_props);
        attr_rel->set_polling_period(100);
        attrs.push_back(attr_rel);
    }

    static void command_factory(std::vector<Tango::Command *> &cmds)
    {
        cmds.push_back(new TangoTest::AutoCommand<&ChangeEventOnNanDev_Double::set_abs_nan>("set_abs_nan"));
        cmds.push_back(new TangoTest::AutoCommand<&ChangeEventOnNanDev_Double::unset_abs_nan>("unset_abs_nan"));
        cmds.push_back(new TangoTest::AutoCommand<&ChangeEventOnNanDev_Double::set_rel_nan>("set_rel_nan"));
        cmds.push_back(new TangoTest::AutoCommand<&ChangeEventOnNanDev_Double::unset_rel_nan>("unset_rel_nan"));
    }

  private:
    Tango::DevDouble attr_abs_value;
    Tango::DevDouble attr_rel_value;
};

// Test device class for Tango::DevFloat
template <class Base>
class ChangeEventOnNanDev_Float : public Base
{
  public:
    using Base::Base;

    ~ChangeEventOnNanDev_Float() override { }

    void init_device() override
    {
        attr_abs_value = ATTR_INIT_VALUE;
        attr_rel_value = ATTR_INIT_VALUE;
    }

    void set_abs_nan()
    {
        attr_abs_value = ATTR_NAN_VALUE;
    }

    void set_rel_nan()
    {
        attr_rel_value = ATTR_NAN_VALUE;
    }

    void unset_abs_nan()
    {
        attr_abs_value = ATTR_INIT_VALUE;
    }

    void unset_rel_nan()
    {
        attr_rel_value = ATTR_INIT_VALUE;
    }

    void read_abs(Tango::Attribute &att)
    {
        att.set_value_date_quality(&attr_abs_value, std::chrono::system_clock::now(), Tango::ATTR_VALID);
    }

    void read_rel(Tango::Attribute &att)
    {
        att.set_value_date_quality(&attr_rel_value, std::chrono::system_clock::now(), Tango::ATTR_VALID);
    }

    static void attribute_factory(std::vector<Tango::Attr *> &attrs)
    {
        // attribute with absolute change
        auto attr_abs = new TangoTest::AutoAttr<&ChangeEventOnNanDev_Float::read_abs>("attr_abs", Tango::DEV_FLOAT);
        Tango::UserDefaultAttrProp abs_props;
        abs_props.set_event_abs_change("0.01");
        attr_abs->set_default_properties(abs_props);
        attr_abs->set_polling_period(100);
        attrs.push_back(attr_abs);

        // attribute with relative change
        auto attr_rel = new TangoTest::AutoAttr<&ChangeEventOnNanDev_Float::read_rel>("attr_rel", Tango::DEV_FLOAT);
        Tango::UserDefaultAttrProp rel_props;
        rel_props.set_event_rel_change("0.01");
        attr_rel->set_default_properties(rel_props);
        attr_rel->set_polling_period(100);
        attrs.push_back(attr_rel);
    }

    static void command_factory(std::vector<Tango::Command *> &cmds)
    {
        cmds.push_back(new TangoTest::AutoCommand<&ChangeEventOnNanDev_Float::set_abs_nan>("set_abs_nan"));
        cmds.push_back(new TangoTest::AutoCommand<&ChangeEventOnNanDev_Float::unset_abs_nan>("unset_abs_nan"));
        cmds.push_back(new TangoTest::AutoCommand<&ChangeEventOnNanDev_Float::set_rel_nan>("set_rel_nan"));
        cmds.push_back(new TangoTest::AutoCommand<&ChangeEventOnNanDev_Float::unset_rel_nan>("unset_rel_nan"));
    }

  private:
    Tango::DevFloat attr_abs_value;
    Tango::DevFloat attr_rel_value;
};

TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(ChangeEventOnNanDev_Double, 4)

SCENARIO("Change events for DevDouble are generated on NaN with absolute change")
{
    int idlver = GENERATE(TangoTest::idlversion(4));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        TangoTest::Context ctx{"change_event_on_nan", "ChangeEventOnNanDev_Double", idlver};
        auto device = ctx.get_proxy();

        REQUIRE(idlver == device->get_idl_version());

        AND_GIVEN("a polled attribute with absolute change")
        {
            std::string att{"attr_abs"};

            REQUIRE(device->is_attribute_polled(att));

            AND_GIVEN("a change event subscription")
            {
                TangoTest::CallbackMock<Tango::EventData> callback;
                REQUIRE_NOTHROW(device->subscribe_event(att, Tango::CHANGE_EVENT, &callback));

                THEN("we receive some events with the initial value")
                {
                    using Catch::Matchers::IsNaN;
                    using Catch::Matchers::WithinAbs;
                    using TangoTest::AnyLikeMatches;
                    // We get the following two initial events (the fact there
                    // are two is a side effect of the fix for #369):
                    //
                    // 1. In `subscribe_event` we do a `read_attribute` to
                    // generate the first event
                    // 2. Because we are the first subscriber to `"attr"`, the
                    // polling loop starts and sends an event because it is the
                    // first time it has read the attribute

                    auto maybe_initial_event = callback.pop_next_event();
                    REQUIRE(maybe_initial_event.has_value());
                    REQUIRE(!maybe_initial_event->err);
                    REQUIRE(maybe_initial_event->attr_value != nullptr);
                    REQUIRE_THAT(*maybe_initial_event->attr_value,
                                 AnyLikeMatches(WithinAbs(ATTR_INIT_VALUE, 0.0000001)));

                    maybe_initial_event = callback.pop_next_event();

                    WHEN("we set the attribute value to NaN")
                    {
                        REQUIRE_NOTHROW(device->command_inout("set_abs_nan"));

                        THEN("a change event is generated")
                        {
                            auto maybe_new_event = callback.pop_next_event();
                            REQUIRE(maybe_new_event.has_value());
                            REQUIRE(!maybe_new_event->err);
                            REQUIRE(maybe_new_event->attr_value != nullptr);
                            REQUIRE_THAT(*maybe_new_event->attr_value, AnyLikeMatches(IsNaN()));
                            AND_WHEN("we unset the attribute value from NaN")
                            {
                                REQUIRE_NOTHROW(device->command_inout("unset_abs_nan"));

                                THEN("a change event is generated")
                                {
                                    auto maybe_new_event = callback.pop_next_event();
                                    REQUIRE(maybe_new_event.has_value());
                                    REQUIRE(!maybe_new_event->err);
                                    REQUIRE(maybe_new_event->attr_value != nullptr);
                                    REQUIRE_THAT(*maybe_new_event->attr_value,
                                                 AnyLikeMatches(WithinAbs(ATTR_INIT_VALUE, 0.0000001)));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

SCENARIO("Change events for DevDouble are generated on NaN with relative change")
{
    int idlver = GENERATE(TangoTest::idlversion(4));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        TangoTest::Context ctx{"change_event_on_nan", "ChangeEventOnNanDev_Double", idlver};
        auto device = ctx.get_proxy();

        REQUIRE(idlver == device->get_idl_version());

        AND_GIVEN("a polled attribute with relative change")
        {
            std::string att{"attr_rel"};

            REQUIRE(device->is_attribute_polled(att));

            AND_GIVEN("a change event subscription")
            {
                TangoTest::CallbackMock<Tango::EventData> callback;
                REQUIRE_NOTHROW(device->subscribe_event(att, Tango::CHANGE_EVENT, &callback));
                THEN("we receive some events with the initial value")
                {
                    using Catch::Matchers::IsNaN;
                    using Catch::Matchers::WithinAbs;
                    using TangoTest::AnyLikeMatches;

                    // We get the following two initial events (the fact there
                    // are two is a side effect of the fix for #369):
                    //
                    // 1. In `subscribe_event` we do a `read_attribute` to
                    // generate the first event
                    // 2. Because we are the first subscriber to `"attr"`, the
                    // polling loop starts and sends an event because it is the
                    // first time it has read the attribute

                    auto maybe_initial_event = callback.pop_next_event();
                    REQUIRE(maybe_initial_event.has_value());
                    REQUIRE(!maybe_initial_event->err);
                    REQUIRE(maybe_initial_event->attr_value != nullptr);
                    REQUIRE_THAT(*maybe_initial_event->attr_value,
                                 AnyLikeMatches(WithinAbs(ATTR_INIT_VALUE, 0.0000001)));

                    maybe_initial_event = callback.pop_next_event();

                    WHEN("we set the attribute value to NaN")
                    {
                        REQUIRE_NOTHROW(device->command_inout("set_rel_nan"));
                        THEN("a change event is generated")
                        {
                            auto maybe_new_event = callback.pop_next_event();
                            REQUIRE(maybe_new_event.has_value());
                            REQUIRE(!maybe_new_event->err);
                            REQUIRE(maybe_new_event->attr_value != nullptr);
                            REQUIRE_THAT(*maybe_new_event->attr_value, AnyLikeMatches(IsNaN()));

                            AND_WHEN("we unset the attribute value from NaN")
                            {
                                REQUIRE_NOTHROW(device->command_inout("unset_rel_nan"));
                                THEN("a change event is generated")
                                {
                                    auto maybe_new_event = callback.pop_next_event();
                                    REQUIRE(maybe_new_event.has_value());
                                    REQUIRE(!maybe_new_event->err);
                                    REQUIRE(maybe_new_event->attr_value != nullptr);
                                    REQUIRE_THAT(*maybe_new_event->attr_value,
                                                 AnyLikeMatches(WithinAbs(ATTR_INIT_VALUE, 0.0000001)));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(ChangeEventOnNanDev_Float, 4)

SCENARIO("Change events for DevFloat are generated on NaN with absolute change")
{
    int idlver = GENERATE(TangoTest::idlversion(4));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        TangoTest::Context ctx{"change_event_on_nan", "ChangeEventOnNanDev_Float", idlver};
        auto device = ctx.get_proxy();

        REQUIRE(idlver == device->get_idl_version());

        AND_GIVEN("a polled attribute with absolute change")
        {
            std::string att{"attr_abs"};

            REQUIRE(device->is_attribute_polled(att));

            AND_GIVEN("a change event subscription")
            {
                TangoTest::CallbackMock<Tango::EventData> callback;
                REQUIRE_NOTHROW(device->subscribe_event(att, Tango::CHANGE_EVENT, &callback));

                THEN("we receive some events with the initial value")
                {
                    using Catch::Matchers::IsNaN;
                    using Catch::Matchers::WithinAbs;
                    using TangoTest::AnyLikeMatches;

                    // We get the following two initial events (the fact there
                    // are two is a side effect of the fix for #369):
                    //
                    // 1. In `subscribe_event` we do a `read_attribute` to
                    // generate the first event
                    // 2. Because we are the first subscriber to `"attr"`, the
                    // polling loop starts and sends an event because it is the
                    // first time it has read the attribute

                    auto maybe_initial_event = callback.pop_next_event();
                    REQUIRE(maybe_initial_event.has_value());
                    REQUIRE(!maybe_initial_event->err);
                    REQUIRE(maybe_initial_event->attr_value != nullptr);
                    REQUIRE_THAT(*maybe_initial_event->attr_value,
                                 AnyLikeMatches<float>(WithinAbs(ATTR_INIT_VALUE, 0.0000001f)));

                    maybe_initial_event = callback.pop_next_event();

                    WHEN("we set the attribute value to NaN")
                    {
                        REQUIRE_NOTHROW(device->command_inout("set_abs_nan"));

                        THEN("a change event is generated")
                        {
                            auto maybe_new_event = callback.pop_next_event();
                            REQUIRE(maybe_new_event.has_value());
                            REQUIRE(!maybe_new_event->err);
                            REQUIRE(maybe_new_event->attr_value != nullptr);
                            REQUIRE_THAT(*maybe_new_event->attr_value, AnyLikeMatches<float>(IsNaN()));
                            AND_WHEN("we unset the attribute value from NaN")
                            {
                                REQUIRE_NOTHROW(device->command_inout("unset_abs_nan"));

                                THEN("a change event is generated")
                                {
                                    auto maybe_new_event = callback.pop_next_event();
                                    REQUIRE(maybe_new_event.has_value());
                                    REQUIRE(!maybe_new_event->err);
                                    REQUIRE(maybe_new_event->attr_value != nullptr);
                                    REQUIRE_THAT(*maybe_new_event->attr_value,
                                                 AnyLikeMatches<float>(WithinAbs(ATTR_INIT_VALUE, 0.0000001f)));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

SCENARIO("Change events for DevFloat are generated on NaN with relative change")
{
    int idlver = GENERATE(TangoTest::idlversion(4));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        TangoTest::Context ctx{"change_event_on_nan", "ChangeEventOnNanDev_Float", idlver};
        auto device = ctx.get_proxy();

        REQUIRE(idlver == device->get_idl_version());

        AND_GIVEN("a polled attribute with relative change")
        {
            std::string att{"attr_rel"};

            REQUIRE(device->is_attribute_polled(att));

            AND_GIVEN("a change event subscription")
            {
                TangoTest::CallbackMock<Tango::EventData> callback;
                REQUIRE_NOTHROW(device->subscribe_event(att, Tango::CHANGE_EVENT, &callback));
                THEN("we receive some events with the initial value")
                {
                    using Catch::Matchers::IsNaN;
                    using Catch::Matchers::WithinAbs;
                    using TangoTest::AnyLikeMatches;

                    // We get the following two initial events (the fact there
                    // are two is a side effect of the fix for #369):
                    //
                    // 1. In `subscribe_event` we do a `read_attribute` to
                    // generate the first event
                    // 2. Because we are the first subscriber to `"attr"`, the
                    // polling loop starts and sends an event because it is the
                    // first time it has read the attribute

                    auto maybe_initial_event = callback.pop_next_event();
                    REQUIRE(maybe_initial_event.has_value());
                    REQUIRE(!maybe_initial_event->err);
                    REQUIRE(maybe_initial_event->attr_value != nullptr);
                    REQUIRE_THAT(*maybe_initial_event->attr_value,
                                 AnyLikeMatches<float>(WithinAbs(ATTR_INIT_VALUE, 0.0000001f)));

                    maybe_initial_event = callback.pop_next_event();

                    WHEN("we set the attribute value to NaN")
                    {
                        REQUIRE_NOTHROW(device->command_inout("set_rel_nan"));
                        THEN("a change event is generated")
                        {
                            auto maybe_new_event = callback.pop_next_event();
                            REQUIRE(maybe_new_event.has_value());
                            REQUIRE(!maybe_new_event->err);
                            REQUIRE(maybe_new_event->attr_value != nullptr);
                            REQUIRE_THAT(*maybe_new_event->attr_value, AnyLikeMatches<float>(IsNaN()));

                            AND_WHEN("we unset the attribute value from NaN")
                            {
                                REQUIRE_NOTHROW(device->command_inout("unset_rel_nan"));
                                THEN("a change event is generated")
                                {
                                    auto maybe_new_event = callback.pop_next_event();
                                    REQUIRE(maybe_new_event.has_value());
                                    REQUIRE(!maybe_new_event->err);
                                    REQUIRE(maybe_new_event->attr_value != nullptr);
                                    REQUIRE_THAT(*maybe_new_event->attr_value,
                                                 AnyLikeMatches<float>(WithinAbs(ATTR_INIT_VALUE, 0.0000001f)));
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}