public-api.cpp 36.3 KB
Newer Older
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1
2
3
4
5
// Copyright (c) 2020 INRA Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#include <irritator/core.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
6
#include <irritator/io.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
7
8
9
10
11

#include <boost/ut.hpp>

#include <fmt/format.h>

12
#include <iostream>
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
struct file_output
{
    file_output(const char* file_path) noexcept
      : os(std::fopen(file_path, "w"))
    {}

    ~file_output() noexcept
    {
        if (os)
            std::fclose(os);
    }

    std::FILE* os = nullptr;
};

void
file_output_initialize(const irt::observer& obs, const irt::time /*t*/) noexcept
{
    if (!obs.user_data)
        return;

    auto* output = reinterpret_cast<file_output*>(obs.user_data);
    fmt::print(output->os, "t,{}\n", obs.name.c_str());
}

void
file_output_observe(const irt::observer& obs,
                    const irt::time t,
                    const irt::message& msg) noexcept
{
    if (!obs.user_data)
        return;

    auto* output = reinterpret_cast<file_output*>(obs.user_data);
48
    fmt::print(output->os, "{},{}\n", t, msg.real[0]);
49
50
}

Gauthier Quesnel's avatar
Gauthier Quesnel committed
51
52
53
54
55
56
57
58
59
60
61
62
int
main()
{
    using namespace boost::ut;

    "model_constepxr"_test = [] {
        expect(irt::is_detected_v<irt::initialize_function_t, irt::counter> ==
               true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::counter> ==
               false);
        expect(irt::is_detected_v<irt::transition_function_t, irt::counter> ==
               true);
63
        expect(irt::is_detected_v<irt::has_input_port_t, irt::counter> == true);
64
65
        expect(irt::is_detected_v<irt::has_output_port_t, irt::counter> ==
               false);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
66

67
68
        expect(irt::is_detected_v<irt::initialize_function_t, irt::generator> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
69
70
        expect(irt::is_detected_v<irt::lambda_function_t, irt::generator> ==
               true);
71
        expect(irt::is_detected_v<irt::transition_function_t, irt::generator> ==
72
               true);
73
74
75
76
        expect(irt::is_detected_v<irt::has_input_port_t, irt::generator> ==
               false);
        expect(irt::is_detected_v<irt::has_output_port_t, irt::generator> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
77
78
79
80
81
82
83

        expect(irt::is_detected_v<irt::initialize_function_t, irt::adder_2> ==
               true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::adder_2> ==
               true);
        expect(irt::is_detected_v<irt::transition_function_t, irt::adder_2> ==
               true);
84
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_2> == true);
85
86
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_2> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
87
88
89
90
91
92
93

        expect(irt::is_detected_v<irt::initialize_function_t, irt::adder_3> ==
               true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::adder_3> ==
               true);
        expect(irt::is_detected_v<irt::transition_function_t, irt::adder_3> ==
               true);
94
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_3> == true);
95
96
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_3> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
97
98
99
100
101
102
103

        expect(irt::is_detected_v<irt::initialize_function_t, irt::adder_4> ==
               true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::adder_4> ==
               true);
        expect(irt::is_detected_v<irt::transition_function_t, irt::adder_4> ==
               true);
104
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_4> == true);
105
106
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_4> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
107
108
109

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_2> ==
               true);
110
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_2> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
111
112
113
        expect(irt::is_detected_v<irt::transition_function_t, irt::mult_2> ==
               true);
        expect(irt::is_detected_v<irt::has_input_port_t, irt::mult_2> == true);
114
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_2> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
115
116
117

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_3> ==
               true);
118
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_3> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
119
120
121
        expect(irt::is_detected_v<irt::transition_function_t, irt::mult_3> ==
               true);
        expect(irt::is_detected_v<irt::has_input_port_t, irt::mult_3> == true);
122
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_3> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
123
124
125

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_4> ==
               true);
126
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_4> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
127
128
129
        expect(irt::is_detected_v<irt::transition_function_t, irt::mult_4> ==
               true);
        expect(irt::is_detected_v<irt::has_input_port_t, irt::mult_4> == true);
130
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_4> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
131
132
133
134
135
136
137
138
139

        expect(
          irt::is_detected_v<irt::initialize_function_t, irt::integrator> ==
          true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::integrator> ==
               true);
        expect(
          irt::is_detected_v<irt::transition_function_t, irt::integrator> ==
          true);
140
141
142
143
        expect(irt::is_detected_v<irt::has_input_port_t, irt::integrator> ==
               true);
        expect(irt::is_detected_v<irt::has_output_port_t, irt::integrator> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
144
145
146
147
148
149
150
151
152

        expect(
          irt::is_detected_v<irt::initialize_function_t, irt::quantifier> ==
          true);
        expect(irt::is_detected_v<irt::lambda_function_t, irt::quantifier> ==
               true);
        expect(
          irt::is_detected_v<irt::transition_function_t, irt::quantifier> ==
          true);
153
154
155
156
        expect(irt::is_detected_v<irt::has_input_port_t, irt::quantifier> ==
               true);
        expect(irt::is_detected_v<irt::has_output_port_t, irt::quantifier> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
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
    };

    "status"_test = [] {
        irt::status s1 = irt::status::success;
        expect(irt::is_success(s1) == true);
        expect(irt::is_bad(s1) == false);

        irt::status s2 = irt::status::block_allocator_not_enough_memory;
        expect(irt::is_success(s2) == false);
        expect(irt::is_bad(s2) == true);
    };

    "time"_test = [] {
        expect(irt::time_domain<irt::time>::infinity >
               irt::time_domain<irt::time>::zero);
        expect(irt::time_domain<irt::time>::zero >
               irt::time_domain<irt::time>::negative_infinity);
    };

    "small_string"_test = [] {
        irt::small_string<8> f1;
        expect(f1.capacity() == 8_ul);
        expect(f1 == "");
        expect(f1.size() == 0_ul);

        f1.append("ok");
        expect(f1 == "ok");
        expect(f1.size() == 2_ul);

        f1.append("ok");
        expect(f1 == "okok");
        expect(f1.size() == 4_ul);

        f1.append("1234");
        expect(f1 == "okok123");
        expect(f1.size() == 7_ul);

        irt::small_string<8> f2(f1);
        expect(f2 == "okok123");
        expect(f2.size() == 7_ul);

        expect(f1.c_str() != f2.c_str());

        irt::small_string<8> f3("012345678");
        expect(f3 == "0123456");
        expect(f3.size() == 7_ul);

        f3.clear();
        expect(f3 == "");
        expect(f3.size() == 0_ul);

        f3 = f2;
        expect(f3 == "okok123");
        expect(f3.size() == 7_ul);
    };

    "list"_test = [] {
        irt::flat_list<int>::allocator_type allocator;
        expect(is_success(allocator.init(32)));

        irt::flat_list<int> lst(&allocator);

        lst.emplace_front(5);
        lst.emplace_front(4);
        lst.emplace_front(3);
        lst.emplace_front(2);
        lst.emplace_front(1);

        {
            int i = 1;
            for (auto it = lst.begin(); it != lst.end(); ++it)
                expect(*it == i++);
        }

        lst.pop_front();

        {
            int i = 2;
            for (auto it = lst.begin(); it != lst.end(); ++it)
                expect(*it == i++);
        }
    };

    "double_list"_test = [] {
        irt::flat_double_list<int>::allocator_type allocator;
        expect(is_success(allocator.init(32)));

        irt::flat_double_list<int> lst(&allocator);

        expect(lst.empty());
        expect(lst.begin() == lst.end());

        lst.emplace_front(0);
        expect(lst.begin() == --lst.end());
        expect(++lst.begin() == lst.end());

        lst.clear();
        expect(lst.empty());
        expect(lst.begin() == lst.end());

        lst.emplace_front(5);
        lst.emplace_front(4);
        lst.emplace_front(3);
        lst.emplace_front(2);
        lst.emplace_front(1);
        lst.emplace_back(6);
        lst.emplace_back(7);
        lst.emplace_back(8);

        {
            int i = 1;
            for (auto it = lst.begin(); it != lst.end(); ++it)
                expect(*it == i++);
        }

        lst.pop_front();

        {
            int i = 2;
            for (auto it = lst.begin(); it != lst.end(); ++it)
                expect(*it == i++);
        }

        {
            auto it = lst.begin();
            expect(*it == 2);

            --it;
            expect(it == lst.end());

            --it;
            expect(it == --lst.end());
        }

        {
            auto it = lst.end();
            expect(it == lst.end());

            --it;
            expect(*it == 8);

            --it;
            expect(*it == 7);
        }
    };

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
    "array_api"_test = [] {
        struct position
        {
            position() noexcept = default;

            position(int x_, int y_)
              : x(x_)
              , y(y_)
            {}

            int x = 0, y = 0;
        };

        irt::array<position> positions;
        !expect(irt::status::success == positions.init(16u));

        expect(positions.size() == 16_u);
        expect(positions.capacity() == 16u);

        for (int i = 0; i != 16; ++i) {
            positions[i].x = i;
            positions[i].y = i;
        }

        for (int i = 0; i != 16; ++i) {
            expect(positions[i].x == i);
            expect(positions[i].y == i);
        }
    };

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
    "vector_api"_test = [] {
        struct position
        {
            position() noexcept = default;

            position(int x_, int y_)
              : x(x_)
              , y(y_)
            {}

            int x = 0, y = 0;
        };

        irt::vector<position> positions;
        !expect(irt::status::success == positions.init(16u));

        expect(positions.size() == 0_u);
        expect(positions.capacity() == 16u);

        {
            expect(positions.can_alloc(1u));
            auto it = positions.emplace_back(0, 1);
            expect(positions.size() == 1_u);
            expect(positions.begin() == it);
            expect(it->x == 0);
            expect(it->y == 1);
        }

        {
            auto ret = positions.try_emplace_back(2, 3);
            expect(ret.first == true);
            expect(ret.second == positions.begin() + 1);
            expect(positions.size() == 2_u);
            expect(positions.begin()->x == 0);
            expect(positions.begin()->y == 1);
            expect((positions.begin() + 1)->x == 2);
            expect((positions.begin() + 1)->y == 3);
        }

        {
            auto it = positions.erase_and_swap(positions.begin());
            expect(positions.size() == 1_u);
            expect(it == positions.begin());
            expect(it->x == 2);
            expect(it->y == 3);
        }

        positions.clear();
        expect(positions.size() == 0_ul);
        expect(positions.capacity() == 16_u);

        expect(positions.can_alloc(16u));
        for (int i = 0; i != 16; ++i)
            positions.emplace_back(i, i);

        auto ret = positions.try_emplace_back(16, 16);
        expect(ret.first == false);

        expect(positions.size() == 16_ul);
        expect(positions.capacity() == 16_u);

        auto it = positions.begin();
        while (it != positions.end())
            it = positions.erase_and_swap(it);

        expect(positions.size() == 0_ul);
        expect(positions.capacity() == 16_u);
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
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
    "data_array_api"_test = [] {
        struct position
        {
            position() = default;
            constexpr position(float x_)
              : x(x_)
            {}

            float x;
        };

        enum class position_id : std::uint64_t
        {
        };

        irt::data_array<position, position_id> array;

        expect(array.max_size() == 0);
        expect(array.max_used() == 0);
        expect(array.capacity() == 0);
        expect(array.next_key() == 1);
        expect(array.is_free_list_empty());

        bool is_init = irt::is_success(array.init(3));

        expect(array.max_size() == 0);
        expect(array.max_used() == 0);
        expect(array.capacity() == 3);
        expect(array.next_key() == 1);
        expect(array.is_free_list_empty());

        expect(is_init);

        {
            auto& first = array.alloc();
            first.x = 0.f;
            expect(array.max_size() == 1);
            expect(array.max_used() == 1);
            expect(array.capacity() == 3);
            expect(array.next_key() == 2);
            expect(array.is_free_list_empty());

            auto& second = array.alloc();
            expect(array.max_size() == 2);
            expect(array.max_used() == 2);
            expect(array.capacity() == 3);
            expect(array.next_key() == 3);
            expect(array.is_free_list_empty());

            second.x = 1.f;

            auto& third = array.alloc();
            expect(array.max_size() == 3);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(array.is_free_list_empty());

            third.x = 2.f;

            expect(array.full());
        }

        array.clear();

        expect(array.max_size() == 0);
        expect(array.max_used() == 0);
469
        expect(array.capacity() == 3);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
        expect(array.next_key() == 1);
        expect(array.is_free_list_empty());

        is_init = irt::is_success(array.init(3));

        {
            auto& d1 = array.alloc(1.f);
            auto& d2 = array.alloc(2.f);
            auto& d3 = array.alloc(3.f);

            expect(array.max_size() == 3);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(array.is_free_list_empty());

            array.free(d1);

            expect(array.max_size() == 2);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(!array.is_free_list_empty());

            array.free(d2);

            expect(array.max_size() == 1);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(!array.is_free_list_empty());

            array.free(d3);
            expect(array.max_size() == 0);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 4);
            expect(!array.is_free_list_empty());

            auto& n1 = array.alloc();
            auto& n2 = array.alloc();
            auto& n3 = array.alloc();

            expect(irt::get_index(array.get_id(n1)) == 2_u);
            expect(irt::get_index(array.get_id(n2)) == 1_u);
            expect(irt::get_index(array.get_id(n3)) == 0_u);

            expect(array.max_size() == 3);
            expect(array.max_used() == 3);
            expect(array.capacity() == 3);
            expect(array.next_key() == 7);
            expect(array.is_free_list_empty());
        }
    };

    "message"_test = [] {
        {
527
528
529
530
531
            irt::message vdouble;
            expect(vdouble.real[0] == 0.0);
            expect(vdouble.real[1] == 0.0);
            expect(vdouble.real[2] == 0.0);
            expect(vdouble.size() == 0_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
532
        }
533

Gauthier Quesnel's avatar
Gauthier Quesnel committed
534
        {
535
536
537
            irt::message vdouble(1.0);
            expect(vdouble[0] == 1.0);
            expect(vdouble.size() == 1_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
538
        }
539

Gauthier Quesnel's avatar
Gauthier Quesnel committed
540
        {
541
542
543
544
            irt::message vdouble(0.0, 1.0);
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 1.0);
            expect(vdouble.size() == 2_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
545
        }
546

Gauthier Quesnel's avatar
Gauthier Quesnel committed
547
        {
548
549
550
551
552
            irt::message vdouble(1.0, 2.0, 3.0);
            expect(vdouble[0] == 1.0);
            expect(vdouble[1] == 2.0);
            expect(vdouble[2] == 3.0);
            expect(vdouble.size() == 3_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
        }
    };

    "heap_order"_test = [] {
        irt::heap h;
        h.init(4u);

        irt::heap::handle i1 = h.insert(0.0, irt::model_id{ 0 });
        irt::heap::handle i2 = h.insert(1.0, irt::model_id{ 1 });
        irt::heap::handle i3 = h.insert(-1.0, irt::model_id{ 2 });
        irt::heap::handle i4 = h.insert(2.0, irt::model_id{ 3 });
        expect(h.full());

        expect(i1->tn == 0.0_d);
        expect(i2->tn == 1.0_d);
        expect(i3->tn == -1.0_d);
        expect(i4->tn == 2.0_d);

        expect(h.top() == i3);
        h.pop();
        expect(h.top() == i1);
        h.pop();
        expect(h.top() == i2);
        h.pop();
        expect(h.top() == i4);
        h.pop();

        expect(h.empty());
        expect(!h.full());
    };

    "heap_insert_pop"_test = [] {
        irt::heap h;
        h.init(4u);

        irt::heap::handle i1 = h.insert(0.0, irt::model_id{ 0 });
        irt::heap::handle i2 = h.insert(1.0, irt::model_id{ 1 });
        irt::heap::handle i3 = h.insert(-1.0, irt::model_id{ 2 });
        irt::heap::handle i4 = h.insert(2.0, irt::model_id{ 3 });

        expect(i1 != nullptr);
        expect(i2 != nullptr);
        expect(i3 != nullptr);
        expect(i4 != nullptr);

        expect(!h.empty());
        expect(h.top() == i3);

        h.pop(); // remove i3
        h.pop(); // rmeove i1

        expect(h.top() == i2);

        i3->tn = -10.0;
        h.insert(i3);

        i1->tn = -1.0;
        h.insert(i1);

        expect(h.top() == i3);
        h.pop();

        expect(h.top() == i1);
        h.pop();

        expect(h.top() == i2);
        h.pop();

        expect(h.top() == i4);
        h.pop();

        expect(h.empty());
    };

    "heap_with_equality"_test = [] {
        irt::heap h;
        h.init(256u);

        for (double t = 0; t < 100.0; ++t)
            h.insert(t, irt::model_id{ static_cast<unsigned>(t) });

        expect(h.size() == 100_ul);

        h.insert(50.0, irt::model_id{ 502 });
        h.insert(50.0, irt::model_id{ 503 });
        h.insert(50.0, irt::model_id{ 504 });

        expect(h.size() == 103_ul);

        for (double t = 0.0; t < 50.0; ++t) {
            expect(h.top()->tn == t);
            h.pop();
        }

        expect(h.top()->tn == 50.0_d);
        h.pop();
        expect(h.top()->tn == 50.0_d);
        h.pop();
        expect(h.top()->tn == 50.0_d);
        h.pop();
        expect(h.top()->tn == 50.0_d);
        h.pop();

        for (double t = 51.0; t < 100.0; ++t) {
            expect(h.top()->tn == t);
            h.pop();
        }
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
662
663
664
665
666
667
668
669
670
671
672
    "constant_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.constant_models.can_alloc(2));
        expect(sim.counter_models.can_alloc(1));

        auto& cnt = sim.counter_models.alloc();
        auto& c1 = sim.constant_models.alloc();
        auto& c2 = sim.constant_models.alloc();

Gauthier Quesnel's avatar
Gauthier Quesnel committed
673
674
        c1.default_value = 0.0;
        c2.default_value = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
675
676
677
678
679
680
681
682
683
684

        expect(sim.models.can_alloc(3));
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
        expect(irt::is_success(sim.alloc(c1, sim.constant_models.get_id(c1))));
        expect(irt::is_success(sim.alloc(c2, sim.constant_models.get_id(c2))));

        expect(sim.connect(c1.y[0], cnt.x[0]) == irt::status::success);
        expect(sim.connect(c2.y[0], cnt.x[0]) == irt::status::success);

        irt::time t = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
685
686
        expect(sim.initialize(t) == irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
687
688
689
690
691
692
693
694
695
696
        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
        } while (t < sim.end);

        expect(cnt.number == 2_ul);
    };

697
698
699
700
701
702
703
704
705
706
707
708
    "cross_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.counter_models.can_alloc(1));
        expect(sim.cross_models.can_alloc(1));
        expect(sim.constant_models.can_alloc(1));

        auto& cnt = sim.counter_models.alloc();
        auto& cross1 = sim.cross_models.alloc();
        auto& c1 = sim.constant_models.alloc();

709
710
        // auto& value = sim.messages.alloc(3.0);
        // auto& threshold = sim.messages.alloc(0.0);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
711
        //
712
713
        // c1.init[0] = sim.messages.get_id(value);
        // cross1.init[0] = sim.messages.get_id(threshold);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
714
715
716

        c1.default_value = 3.0;
        cross1.default_threshold = 0.0;
717
718
719

        expect(sim.models.can_alloc(3));
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
720
721
        expect(
          irt::is_success(sim.alloc(cross1, sim.cross_models.get_id(cross1))));
722
723
724
725
726
727
728
729
        expect(irt::is_success(sim.alloc(c1, sim.constant_models.get_id(c1))));

        expect(sim.connect(c1.y[0], cross1.x[0]) == irt::status::success);
        expect(sim.connect(c1.y[0], cross1.x[1]) == irt::status::success);
        expect(sim.connect(c1.y[0], cross1.x[2]) == irt::status::success);
        expect(sim.connect(cross1.y[0], cnt.x[0]) == irt::status::success);

        irt::time t = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
730
731
        expect(sim.initialize(t) == irt::status::success);

732
733
        irt::status st;

734
        do {
735
736
737
738
739
740
741
            st = sim.run(t);
            expect(irt::is_success(st));
        } while (t < sim.end);

        expect(cnt.number == 2_ul);
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
742
743
744
745
746
747
748
749
750
751
752
    "generator_counter_simluation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.generator_models.can_alloc(1));
        expect(sim.counter_models.can_alloc(1));

        auto& gen = sim.generator_models.alloc();
        auto& cnt = sim.counter_models.alloc();

        expect(sim.models.can_alloc(2));
753
754
        expect(
          irt::is_success(sim.alloc(gen, sim.generator_models.get_id(gen))));
755
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
756
757
758
759
760
761
762
763
764

        expect(sim.connect(gen.y[0], cnt.x[0]) == irt::status::success);

        expect(sim.begin == irt::time_domain<irt::time>::zero);
        expect(sim.end == irt::time_domain<irt::time>::infinity);

        sim.end = 10.0;

        irt::time t = sim.begin;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
765
766
        expect(sim.initialize(t) == irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
767
768
769
770
771
        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
772
            expect(cnt.number <= static_cast<irt::i64>(t));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
773
774
775
776
        } while (t < sim.end);

        expect(cnt.number == 9_ul);
    };
777

K-H-Ismail's avatar
K-H-Ismail committed
778
779
780
781
782
783
784
785
786
787
    "time_func"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
        expect(sim.time_func_models.can_alloc(1));
        expect(sim.counter_models.can_alloc(1));

        auto& time_fun = sim.time_func_models.alloc();
        auto& cnt = sim.counter_models.alloc();

788
        time_fun.default_f = &irt::square_time_function;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
789

K-H-Ismail's avatar
K-H-Ismail committed
790
        expect(sim.models.can_alloc(2));
791
792
        expect(irt::is_success(
          sim.alloc(time_fun, sim.time_func_models.get_id(time_fun))));
K-H-Ismail's avatar
K-H-Ismail committed
793
794
795
796
        expect(irt::is_success(sim.alloc(cnt, sim.counter_models.get_id(cnt))));

        expect(sim.connect(time_fun.y[0], cnt.x[0]) == irt::status::success);

Gauthier Quesnel's avatar
Gauthier Quesnel committed
797
798
        irt::time t{ 0 };
        !expect(sim.initialize(t) == irt::status::success);
K-H-Ismail's avatar
K-H-Ismail committed
799
        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
800
801
802
            auto st = sim.run(t);
            !expect(irt::is_success(st));
            expect(time_fun.value == t * t);
803
            expect(cnt.number <= static_cast<irt::i64>(t));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
804
        } while (t < 30);
K-H-Ismail's avatar
K-H-Ismail committed
805
806
    };

807
808
809
    "lotka_volterra_simulation"_test = [] {
        irt::simulation sim;

810
        expect(irt::is_success(sim.init(32lu, 512lu)));
811
812
813
814
815
816
817
818
819
820
821
822
823
        expect(sim.adder_2_models.can_alloc(2));
        expect(sim.mult_2_models.can_alloc(2));
        expect(sim.integrator_models.can_alloc(2));
        expect(sim.quantifier_models.can_alloc(2));

        auto& sum_a = sim.adder_2_models.alloc();
        auto& sum_b = sim.adder_2_models.alloc();
        auto& product = sim.mult_2_models.alloc();
        auto& integrator_a = sim.integrator_models.alloc();
        auto& integrator_b = sim.integrator_models.alloc();
        auto& quantifier_a = sim.quantifier_models.alloc();
        auto& quantifier_b = sim.quantifier_models.alloc();

Gauthier Quesnel's avatar
Gauthier Quesnel committed
824
        integrator_a.default_current_value = 18.0;
825

826
827
        quantifier_a.default_adapt_state =
          irt::quantifier::adapt_state::possible;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
828
829
830
        quantifier_a.default_zero_init_offset = true;
        quantifier_a.default_step_size = 0.01;
        quantifier_a.default_past_length = 3;
831

Gauthier Quesnel's avatar
Gauthier Quesnel committed
832
        integrator_b.default_current_value = 7.0;
833

834
835
        quantifier_b.default_adapt_state =
          irt::quantifier::adapt_state::possible;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
836
837
838
        quantifier_b.default_zero_init_offset = true;
        quantifier_b.default_step_size = 0.01;
        quantifier_b.default_past_length = 3;
839

Gauthier Quesnel's avatar
Gauthier Quesnel committed
840
841
842
843
844
845
        product.default_input_coeffs[0] = 1.0;
        product.default_input_coeffs[1] = 1.0;
        sum_a.default_input_coeffs[0] = 2.0;
        sum_a.default_input_coeffs[1] = -0.4;
        sum_b.default_input_coeffs[0] = -1.0;
        sum_b.default_input_coeffs[1] = 0.1;
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891

        expect(sim.models.can_alloc(10));
        !expect(irt::is_success(
          sim.alloc(sum_a, sim.adder_2_models.get_id(sum_a), "sum_a")));
        !expect(irt::is_success(
          sim.alloc(sum_b, sim.adder_2_models.get_id(sum_b), "sum_b")));
        !expect(irt::is_success(
          sim.alloc(product, sim.mult_2_models.get_id(product), "prod")));
        !expect(irt::is_success(sim.alloc(
          integrator_a, sim.integrator_models.get_id(integrator_a), "int_a")));
        !expect(irt::is_success(sim.alloc(
          integrator_b, sim.integrator_models.get_id(integrator_b), "int_b")));
        !expect(irt::is_success(sim.alloc(
          quantifier_a, sim.quantifier_models.get_id(quantifier_a), "qua_a")));
        !expect(irt::is_success(sim.alloc(
          quantifier_b, sim.quantifier_models.get_id(quantifier_b), "qua_b")));

        !expect(sim.models.size() == 7_ul);

        expect(sim.connect(sum_a.y[0], integrator_a.x[1]) ==
               irt::status::success);
        expect(sim.connect(sum_b.y[0], integrator_b.x[1]) ==
               irt::status::success);

        expect(sim.connect(integrator_a.y[0], sum_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], sum_b.x[0]) ==
               irt::status::success);

        expect(sim.connect(integrator_a.y[0], product.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], product.x[1]) ==
               irt::status::success);

        expect(sim.connect(product.y[0], sum_a.x[1]) == irt::status::success);
        expect(sim.connect(product.y[0], sum_b.x[1]) == irt::status::success);

        expect(sim.connect(quantifier_a.y[0], integrator_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(quantifier_b.y[0], integrator_b.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_a.y[0], quantifier_a.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], quantifier_b.x[0]) ==
               irt::status::success);

892
        irt::dot_writer dw(std::cout);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
893
        dw(sim);
894

895
896
897
898
899
        file_output fo_a("lotka-volterra_a.csv");
        file_output fo_b("lotka-volterra_b.csv");
        expect(fo_a.os != nullptr);
        expect(fo_b.os != nullptr);

900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
        auto& obs_a = sim.observers.alloc(0.01,
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);
        auto& obs_b = sim.observers.alloc(0.01,
                                          "B",
                                          static_cast<void*>(&fo_b),
                                          file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator_a.id), obs_a);
        sim.observe(sim.models.get(integrator_b.id), obs_b);

916
        irt::time t = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
917
918

        expect(sim.initialize(t) == irt::status::success);
919
        !expect(sim.sched.size() == 7_ul);
920
921

        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
922
            auto st = sim.run(t);
923
924
            expect(st == irt::status::success);
        } while (t < 15.0);
925
    };
Gauthier Quesnel's avatar
Gauthier Quesnel committed
926

927
    "izhikevitch_simulation"_test = [] {
928
        irt::simulation sim;
929

930
        expect(irt::is_success(sim.init(64lu, 256lu)));
931
        expect(sim.constant_models.can_alloc(3));
932
933
934
935
936
937
938
        expect(sim.adder_2_models.can_alloc(3));
        expect(sim.adder_4_models.can_alloc(1));
        expect(sim.mult_2_models.can_alloc(1));
        expect(sim.integrator_models.can_alloc(2));
        expect(sim.quantifier_models.can_alloc(2));
        expect(sim.cross_models.can_alloc(2));

K-H-Ismail's avatar
K-H-Ismail committed
939
940
        auto& constant = sim.constant_models.alloc();
        auto& constant2 = sim.constant_models.alloc();
941
        auto& constant3 = sim.constant_models.alloc();
942
943
944
945
946
947
948
949
950
951
952
953
        auto& sum_a = sim.adder_2_models.alloc();
        auto& sum_b = sim.adder_2_models.alloc();
        auto& sum_c = sim.adder_4_models.alloc();
        auto& sum_d = sim.adder_2_models.alloc();
        auto& product = sim.mult_2_models.alloc();
        auto& integrator_a = sim.integrator_models.alloc();
        auto& integrator_b = sim.integrator_models.alloc();
        auto& quantifier_a = sim.quantifier_models.alloc();
        auto& quantifier_b = sim.quantifier_models.alloc();
        auto& cross = sim.cross_models.alloc();
        auto& cross2 = sim.cross_models.alloc();

954
955
956
957
        double a = 0.2;
        double b = 2.0;
        double c = -56.0;
        double d = -16.0;
958
        double I = -99.0;
959
960
        double vt = 30.0;

Gauthier Quesnel's avatar
Gauthier Quesnel committed
961
962
        constant.default_value = 1.0;
        constant2.default_value = c;
963
964
965
966
        constant3.default_value = I;

        cross.default_threshold = vt;
        cross2.default_threshold = vt;
967

Gauthier Quesnel's avatar
Gauthier Quesnel committed
968
969
        integrator_a.default_current_value = 0.0;

970
971
        quantifier_a.default_adapt_state =
          irt::quantifier::adapt_state::possible;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
972
973
974
975
976
977
        quantifier_a.default_zero_init_offset = true;
        quantifier_a.default_step_size = 0.01;
        quantifier_a.default_past_length = 3;

        integrator_b.default_current_value = 0.0;

978
979
        quantifier_b.default_adapt_state =
          irt::quantifier::adapt_state::possible;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
980
981
982
983
984
985
986
987
988
989
        quantifier_b.default_zero_init_offset = true;
        quantifier_b.default_step_size = 0.01;
        quantifier_b.default_past_length = 3;

        product.default_input_coeffs[0] = 1.0;
        product.default_input_coeffs[1] = 1.0;

        sum_a.default_input_coeffs[0] = 1.0;
        sum_a.default_input_coeffs[1] = -1.0;
        sum_b.default_input_coeffs[0] = -a;
990
        sum_b.default_input_coeffs[1] = a * b;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
991
992
993
994
995
996
997
        sum_c.default_input_coeffs[0] = 0.04;
        sum_c.default_input_coeffs[1] = 5.0;
        sum_c.default_input_coeffs[2] = 140.0;
        sum_c.default_input_coeffs[3] = 1.0;
        sum_d.default_input_coeffs[0] = 1.0;
        sum_d.default_input_coeffs[1] = d;

998
        expect(sim.models.can_alloc(14));
999
        !expect(irt::is_success(
1000
          sim.alloc(constant3, sim.constant_models.get_id(constant3), "tfun")));
1001
        !expect(irt::is_success(
1002
1003
1004
          sim.alloc(constant, sim.constant_models.get_id(constant), "1.0")));
        !expect(irt::is_success(sim.alloc(
          constant2, sim.constant_models.get_id(constant2), "-56.0")));
1005
1006
1007
1008
1009
1010
1011

        !expect(irt::is_success(
          sim.alloc(sum_a, sim.adder_2_models.get_id(sum_a), "sum_a")));
        !expect(irt::is_success(
          sim.alloc(sum_b, sim.adder_2_models.get_id(sum_b), "sum_b")));
        !expect(irt::is_success(
          sim.alloc(sum_c, sim.adder_4_models.get_id(sum_c), "sum_c")));
1012
1013
        !expect(irt::is_success(
          sim.alloc(sum_d, sim.adder_2_models.get_id(sum_d), "sum_d")));
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024

        !expect(irt::is_success(
          sim.alloc(product, sim.mult_2_models.get_id(product), "prod")));
        !expect(irt::is_success(sim.alloc(
          integrator_a, sim.integrator_models.get_id(integrator_a), "int_a")));
        !expect(irt::is_success(sim.alloc(
          integrator_b, sim.integrator_models.get_id(integrator_b), "int_b")));
        !expect(irt::is_success(sim.alloc(
          quantifier_a, sim.quantifier_models.get_id(quantifier_a), "qua_a")));
        !expect(irt::is_success(sim.alloc(
          quantifier_b, sim.quantifier_models.get_id(quantifier_b), "qua_b")));
1025
1026
        !expect(irt::is_success(
          sim.alloc(cross, sim.cross_models.get_id(cross), "cross")));
1027
1028
        !expect(irt::is_success(
          sim.alloc(cross2, sim.cross_models.get_id(cross2), "cross2")));
1029

1030
        !expect(sim.models.size() == 14_ul);
1031
1032
1033

        expect(sim.connect(integrator_a.y[0], cross.x[0]) ==
               irt::status::success);
1034
        expect(sim.connect(constant2.y[0], cross.x[1]) == irt::status::success);
1035
1036
1037
        expect(sim.connect(integrator_a.y[0], cross.x[2]) ==
               irt::status::success);

1038
        expect(sim.connect(cross.y[0], quantifier_a.x[0]) ==
1039
               irt::status::success);
1040
1041
1042
1043
1044
        expect(sim.connect(cross.y[0], product.x[0]) == irt::status::success);
        expect(sim.connect(cross.y[0], product.x[1]) == irt::status::success);
        expect(sim.connect(product.y[0], sum_c.x[0]) == irt::status::success);
        expect(sim.connect(cross.y[0], sum_c.x[1]) == irt::status::success);
        expect(sim.connect(cross.y[0], sum_b.x[1]) == irt::status::success);
1045

1046
        expect(sim.connect(constant.y[0], sum_c.x[2]) == irt::status::success);
1047
        expect(sim.connect(constant3.y[0], sum_c.x[3]) == irt::status::success);
1048

1049
1050
        expect(sim.connect(sum_c.y[0], sum_a.x[0]) == irt::status::success);
        expect(sim.connect(integrator_b.y[0], sum_a.x[1]) ==
1051
               irt::status::success);
1052
        expect(sim.connect(cross2.y[0], sum_a.x[1]) == irt::status::success);
1053
        expect(sim.connect(sum_a.y[0], integrator_a.x[1]) ==
1054
               irt::status::success);
1055
        expect(sim.connect(cross.y[0], integrator_a.x[2]) ==
1056
1057
1058
1059
               irt::status::success);
        expect(sim.connect(quantifier_a.y[0], integrator_a.x[0]) ==
               irt::status::success);

1060
        expect(sim.connect(cross2.y[0], quantifier_b.x[0]) ==
1061
               irt::status::success);
1062
        expect(sim.connect(cross2.y[0], sum_b.x[0]) == irt::status::success);
1063
        expect(sim.connect(quantifier_b.y[0], integrator_b.x[0]) ==
1064
               irt::status::success);
1065
        expect(sim.connect(sum_b.y[0], integrator_b.x[1]) ==
1066
1067
               irt::status::success);

1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
        expect(sim.connect(cross2.y[0], integrator_b.x[2]) ==
               irt::status::success);
        expect(sim.connect(integrator_a.y[0], cross2.x[0]) ==
               irt::status::success);
        expect(sim.connect(integrator_b.y[0], cross2.x[2]) ==
               irt::status::success);
        expect(sim.connect(sum_d.y[0], cross2.x[1]) == irt::status::success);
        expect(sim.connect(integrator_b.y[0], sum_d.x[0]) ==
               irt::status::success);
        expect(sim.connect(constant.y[0], sum_d.x[1]) == irt::status::success);

1079
        irt::dot_writer dw(std::cout);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1080
        dw(sim);
1081

1082
1083
        file_output fo_a("izhikevitch_a.csv");
        expect(fo_a.os != nullptr);
1084
1085
1086
1087
1088
1089
1090
1091
1092

        auto& obs_a = sim.observers.alloc(0.01,
                                          "A",
                                          static_cast<void*>(&fo_a),
                                          &file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        file_output fo_b("izhikevitch_b.csv");
1093
        expect(fo_b.os != nullptr);
1094
1095
1096
1097
1098
1099
1100
1101
1102
        auto& obs_b = sim.observers.alloc(0.01,
                                          "B",
                                          static_cast<void*>(&fo_b),
                                          &file_output_initialize,
                                          &file_output_observe,
                                          nullptr);

        sim.observe(sim.models.get(integrator_a.id), obs_a);
        sim.observe(sim.models.get(integrator_b.id), obs_b);
1103

1104
        irt::time t = 0.0;
1105
1106

        expect(irt::status::success == sim.initialize(t));
1107
        !expect(sim.sched.size() == 14_ul);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1108

1109
        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1110
            irt::status st = sim.run(t);
1111
            expect(st == irt::status::success);
1112
        } while (t < 120);
1113
    };
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1114
}