RAPPEL : Opération de maintenance > ForgeMIA indisponible le 20 Janvier entre 7h et 12h

public-api.cpp 90.7 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>
6
#include <irritator/examples.hpp>
7
#include <irritator/external_source.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
8
#include <irritator/io.hpp>
Gauthier Quesnel's avatar
Gauthier Quesnel committed
9
10
11

#include <fmt/format.h>

12
#include <iostream>
13
#include <random>
14
#include <sstream>
15

16
17
#include <cstdio>

18
19
#include <boost/ut.hpp>

20
21
struct file_output
{
22
23
24
25
26
27
28
29
    std::FILE* os = nullptr;
    std::string filename;

    file_output(const std::string_view name)
      : filename(name)
    {
        os = std::fopen(filename.c_str(), "w");
    }
30

31
    ~file_output()
32
33
34
35
36
    {
        if (os)
            std::fclose(os);
    }

37
    void operator()(const irt::observer& obs,
38
                    const irt::dynamics_type /*type*/,
39
                    const irt::time /*tl*/,
40
                    const irt::time t,
41
42
43
44
45
46
                    const irt::observer::status s) noexcept
    {
        switch (s) {
        case irt::observer::status::initialize:
            fmt::print(os, "t,{}\n", obs.name.c_str());
            break;
47

48
49
50
51
52
        case irt::observer::status::run:
            fmt::print(os, "{},{}\n", t, obs.msg.real[0]);
            break;

        case irt::observer::status::finalize:
53
            fmt::print(os, "{},{}\n", t, obs.msg.real[0]);
54
55
56
57
            break;
        }
    }
};
58

Gauthier Quesnel's avatar
Gauthier Quesnel committed
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
bool function_ref_called = false;

void
function_ref_f()
{
    function_ref_called = true;
}

struct function_ref_class
{
    bool baz_called = false;

    void baz()
    {
        baz_called = true;
    }

    bool qux_called = false;

    void qux()
    {
        qux_called = true;
    }
};

84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
struct function_ref_multiple_operator
{
    int i;

    void operator()(bool)
    {
        i = 1;
    }

    void operator()(double)
    {
        i++;
    }
};

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
static void empty_fun(irt::model_id /*id*/) noexcept
{}

static irt::status
run_simulation(irt::simulation& sim, const double duration)
{
    using namespace boost::ut;

    irt::time t = 0.0;

    expect(sim.initialize(t) == irt::status::success);

    do {
        auto status = sim.run(t);
        expect(status == irt::status::success);
    } while (t < duration);

    return irt::status::success;
}

119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
struct global_alloc
{
    size_t allocation_size = 0;
    int allocation_number = 0;

    void* operator()(size_t size)
    {
        allocation_size += size;
        allocation_number++;

        return std::malloc(size);
    }
};

struct global_free
{
    int free_number = 0;

137
    void operator()(void* ptr)
138
139
140
141
142
143
144
145
    {
        free_number++;

        if (ptr)
            std::free(ptr);
    }
};

146
static void* null_alloc(size_t /*sz*/)
147
148
149
150
{
    return nullptr;
}

151
152
153
static void
null_free(void*)
{}
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
inline int
make_input_node_id(const irt::model_id mdl, const int port) noexcept
{
    fmt::print("make_input_node_id({},{})\n", static_cast<irt::u64>(mdl), port);
    irt_assert(port >= 0 && port < 8);

    irt::u32 index = irt::get_index(mdl);
    irt_assert(index < 268435456u);

    fmt::print("{0:32b} <- index\n", index);
    fmt::print("{0:32b} <- port\n", port);

    irt::u32 port_index = static_cast<irt::u32>(port) << 28u;
    fmt::print("{0:32b} <- port_index\n", port_index);

    index |= port_index;
    fmt::print("{0:32b} <- index final\n", index);

    return static_cast<int>(index);
}

inline int
make_output_node_id(const irt::model_id mdl, const int port) noexcept
{
179
180
    fmt::print(
      "make_output_node_id({},{})\n", static_cast<irt::u64>(mdl), port);
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
    irt_assert(port >= 0 && port < 8);

    irt::u32 index = irt::get_index(mdl);
    irt_assert(index < 268435456u);

    fmt::print("{0:32b} <- index\n", index);
    fmt::print("{0:32b} <- port\n", port);
    fmt::print("{0:32b} <- port + 8u\n", 8u + port);

    irt::u32 port_index = static_cast<irt::u32>(8u + port) << 28u;
    fmt::print("{0:32b} <- port_index\n", port_index);

    index |= port_index;
    fmt::print("{0:32b} <- index final\n", index);

    return static_cast<int>(index);
}

inline std::pair<irt::u32, irt::u32>
get_model_input_port(const int node_id) noexcept
{
    fmt::print("get_model_input_port {}\n", node_id);

    const irt::u32 real_node_id = static_cast<irt::u32>(node_id);
    fmt::print("{0:32b} <- real_node_id\n", real_node_id);

    irt::u32 port = real_node_id >> 28u;
    fmt::print("{0:32b} <- port\n", port);
    irt_assert(port < 8u);

    constexpr irt::u32 mask = ~(15u << 28u);
    fmt::print("{0:32b} <- mask\n", mask);
    irt::u32 index = real_node_id & mask;
    fmt::print("{0:32b} <- real_node_id & mask\n", index);

    fmt::print("index: {} port: {}\n", index, port);
    return std::make_pair(index, port);
}

inline std::pair<irt::u32, irt::u32>
get_model_output_port(const int node_id) noexcept
{
    fmt::print("get_model_output_port {}\n", node_id);

    const irt::u32 real_node_id = static_cast<irt::u32>(node_id);
    fmt::print("{0:32b} <- real_node_id\n", real_node_id);

    irt::u32 port = real_node_id >> 28u;
    fmt::print("{0:32b} <- port\n", port);

    irt_assert(port >= 8u && port < 16u);
    port -= 8u;
    fmt::print("{0:32b} <- port - 8u\n", port);
    irt_assert(port < 8u);

    constexpr irt::u32 mask = ~(15u << 28u);
    fmt::print("{0:32b} <- mask\n", mask);

    irt::u32 index = real_node_id & mask;
    fmt::print("{0:32b} <- real_node_id & mask\n", index);

    fmt::print("index: {} port: {}\n", index, port);
    return std::make_pair(index, port);
}

Gauthier Quesnel's avatar
Gauthier Quesnel committed
246
247
248
249
250
int
main()
{
    using namespace boost::ut;

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
    "model-id-port-node-id"_test = [] {
        auto i = make_input_node_id(irt::model_id{ 50 }, 7);
        auto j = make_output_node_id(irt::model_id{ 50 }, 3);
        auto k1 = make_input_node_id(irt::model_id{ 268435455 }, 0);
        auto k2 = make_output_node_id(irt::model_id{ 268435455 }, 0);
        auto k3 = make_input_node_id(irt::model_id{ 268435455 }, 7);
        auto k4 = make_output_node_id(irt::model_id{ 268435455 }, 7);

        expect(i != j);

        auto ni = get_model_input_port(i);
        auto nj = get_model_output_port(j);
        auto nk1 = get_model_input_port(k1);
        auto nk2 = get_model_output_port(k2);
        auto nk3 = get_model_input_port(k3);
        auto nk4 = get_model_output_port(k4);

        expect(ni.first == 50u);
        expect(ni.second == 7u);
        expect(nj.first == 50u);
        expect(nj.second == 3u);
        expect(nk1.first == 268435455u);
        expect(nk1.second == 0u);
        expect(nk2.first == 268435455u);
        expect(nk2.second == 0u);
        expect(nk3.first == 268435455u);
        expect(nk3.second == 7u);
        expect(nk4.first == 268435455u);
        expect(nk4.second == 7u);
    };

    "sizeof"_test = [] {
        fmt::print("none {}\n", sizeof(irt::none));
        fmt::print("qss1_integrator {}\n", sizeof(irt::qss1_integrator));
        fmt::print("qss1_multiplier {}\n", sizeof(irt::qss1_multiplier));
        fmt::print("qss1_cross {}\n", sizeof(irt::qss1_cross));
        fmt::print("qss1_power {}\n", sizeof(irt::qss1_power));
        fmt::print("qss1_square {}\n", sizeof(irt::qss1_square));
        fmt::print("qss1_sum_2 {}\n", sizeof(irt::qss1_sum_2));
        fmt::print("qss1_sum_3 {}\n", sizeof(irt::qss1_sum_3));
        fmt::print("qss1_sum_4 {}\n", sizeof(irt::qss1_sum_4));
        fmt::print("qss1_wsum_2 {}\n", sizeof(irt::qss1_wsum_2));
        fmt::print("qss1_wsum_3 {}\n", sizeof(irt::qss1_wsum_3));
        fmt::print("qss1_wsum_4 {}\n", sizeof(irt::qss1_wsum_4));
        fmt::print("qss2_integrator {}\n", sizeof(irt::qss2_integrator));
        fmt::print("qss2_multiplier {}\n", sizeof(irt::qss2_multiplier));
        fmt::print("qss2_cross {}\n", sizeof(irt::qss2_cross));
        fmt::print("qss2_power {}\n", sizeof(irt::qss2_power));
        fmt::print("qss2_square {}\n", sizeof(irt::qss2_square));
        fmt::print("qss2_sum_2 {}\n", sizeof(irt::qss2_sum_2));
        fmt::print("qss2_sum_3 {}\n", sizeof(irt::qss2_sum_3));
        fmt::print("qss2_sum_4 {}\n", sizeof(irt::qss2_sum_4));
        fmt::print("qss2_wsum_2 {}\n", sizeof(irt::qss2_wsum_2));
        fmt::print("qss2_wsum_3 {}\n", sizeof(irt::qss2_wsum_3));
        fmt::print("qss2_wsum_4 {}\n", sizeof(irt::qss2_wsum_4));
        fmt::print("qss3_integrator {}\n", sizeof(irt::qss3_integrator));
        fmt::print("qss3_multiplier {}\n", sizeof(irt::qss3_multiplier));
        fmt::print("qss3_power {}\n", sizeof(irt::qss3_power));
        fmt::print("qss3_square {}\n", sizeof(irt::qss3_square));
        fmt::print("qss3_cross {}\n", sizeof(irt::qss3_cross));
        fmt::print("qss3_sum_2 {}\n", sizeof(irt::qss3_sum_2));
        fmt::print("qss3_sum_3 {}\n", sizeof(irt::qss3_sum_3));
        fmt::print("qss3_sum_4 {}\n", sizeof(irt::qss3_sum_4));
        fmt::print("qss3_wsum_2 {}\n", sizeof(irt::qss3_wsum_2));
        fmt::print("qss3_wsum_3 {}\n", sizeof(irt::qss3_wsum_3));
        fmt::print("qss3_wsum_4 {}\n", sizeof(irt::qss3_wsum_4));
        fmt::print("integrator {}\n", sizeof(irt::integrator));
        fmt::print("quantifier {}\n", sizeof(irt::quantifier));
        fmt::print("adder_2 {}\n", sizeof(irt::adder_2));
        fmt::print("adder_3 {}\n", sizeof(irt::adder_3));
        fmt::print("adder_4 {}\n", sizeof(irt::adder_4));
        fmt::print("mult_2 {}\n", sizeof(irt::mult_2));
        fmt::print("mult_3 {}\n", sizeof(irt::mult_3));
        fmt::print("mult_4 {}\n", sizeof(irt::mult_4));
        fmt::print("counter {}\n", sizeof(irt::counter));
326
327
328
        fmt::print("queue {}\n", sizeof(irt::queue));
        fmt::print("dynamic_queue {}\n", sizeof(irt::dynamic_queue));
        fmt::print("priority_queue {}\n", sizeof(irt::priority_queue));
329
330
331
332
333
334
        fmt::print("generator {}\n", sizeof(irt::generator));
        fmt::print("constant {}\n", sizeof(irt::constant));
        fmt::print("cross {}\n", sizeof(irt::cross));
        fmt::print("time_func {}\n", sizeof(irt::time_func));
        fmt::print("accumulator {}\n", sizeof(irt::accumulator_2));
        fmt::print("flow {}\n", sizeof(irt::flow));
335
        fmt::print("model {}\n", sizeof(irt::model));
336
        fmt::print("port {}\n", sizeof(irt::port));
337
338
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
339
340
341
342
343
344
345
    "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);
346
        expect(irt::is_detected_v<irt::has_input_port_t, irt::counter> == true);
347
348
        expect(irt::is_detected_v<irt::has_output_port_t, irt::counter> ==
               false);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
349

350
351
        expect(irt::is_detected_v<irt::initialize_function_t, irt::generator> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
352
353
        expect(irt::is_detected_v<irt::lambda_function_t, irt::generator> ==
               true);
354
        expect(irt::is_detected_v<irt::transition_function_t, irt::generator> ==
355
               true);
356
357
358
359
        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
360
361
362
363
364
365
366

        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);
367
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_2> == true);
368
369
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_2> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
370
371
372
373
374
375
376

        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);
377
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_3> == true);
378
379
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_3> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
380
381
382
383
384
385
386

        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);
387
        expect(irt::is_detected_v<irt::has_input_port_t, irt::adder_4> == true);
388
389
        expect(irt::is_detected_v<irt::has_output_port_t, irt::adder_4> ==
               true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
390
391
392

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_2> ==
               true);
393
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_2> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
394
395
396
        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);
397
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_2> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
398
399
400

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_3> ==
               true);
401
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_3> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
402
403
404
        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);
405
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_3> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
406
407
408

        expect(irt::is_detected_v<irt::initialize_function_t, irt::mult_4> ==
               true);
409
        expect(irt::is_detected_v<irt::lambda_function_t, irt::mult_4> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
410
411
412
        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);
413
        expect(irt::is_detected_v<irt::has_output_port_t, irt::mult_4> == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
414
415
416
417
418
419
420
421
422

        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);
423
424
425
426
        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
427
428
429
430
431
432
433
434
435

        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);
436
437
438
439
        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
440
441
442
443
444
445
446
447
448
449
450
451
    };

    "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);
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    "function_ref"_test = [] {
        {
            irt::function_ref<void(void)> fr = function_ref_f;
            fr();
            expect(function_ref_called == true);
        }

        {
            function_ref_class o;
            auto x = &function_ref_class::baz;
            irt::function_ref<void(function_ref_class&)> fr = x;
            fr(o);
            expect(o.baz_called);
            x = &function_ref_class::qux;
            fr = x;
            fr(o);
            expect(o.qux_called);
        }

        {
            auto x = [] { return 42; };
            irt::function_ref<int()> fr = x;
            expect(fr() == 42);
        }

        {
            int i = 0;
            auto x = [&i] { i = 42; };
            irt::function_ref<void()> fr = x;
            fr();
            expect(i == 42);
        }
484
485
486
487
488
489
490
491
492
493
494
495

        {
            function_ref_multiple_operator ops;
            ops.i = 0;
            irt::function_ref<void(bool)> b1(ops);
            irt::function_ref<void(double)> b2(ops);

            b1(true);
            b2(0.0);

            expect(ops.i == 2);
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
496
497
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
498
499
500
501
502
503
504
505
506
507
508
509
510
    "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);

511
        f1 = "ok";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
512
513
514
        expect(f1 == "ok");
        expect(f1.size() == 2_ul);

515
        f1 = "okok";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
516
517
518
        expect(f1 == "okok");
        expect(f1.size() == 4_ul);

519
        f1 = "okok123456";
Gauthier Quesnel's avatar
Gauthier Quesnel committed
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
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
        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);
        }
630
631
632
633
634
635
636
637
638
639
640
641

        lst.emplace(lst.begin(), 10);
        expect(*lst.begin() == 10);

        {
            auto it = lst.begin();
            ++it;

            it = lst.emplace(it, 11);
            expect(*it == 11);
            expect(*lst.begin() == 10);
        }
Gauthier Quesnel's avatar
Gauthier Quesnel committed
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
    };

    "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);
711
        expect(array.capacity() == 3);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
        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 = [] {
        {
769
            irt::message vdouble;
770
771
772
773
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 0.0);
            expect(vdouble[3] == 0.0);
774
            expect(vdouble.size() == 0_ul);
775
            expect(vdouble.ssize() == 0);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
776
        }
777

Gauthier Quesnel's avatar
Gauthier Quesnel committed
778
        {
779
780
            irt::message vdouble(1.0);
            expect(vdouble[0] == 1.0);
781
782
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 0.0);
783
            expect(vdouble[3] == 0.0);
784
            expect(vdouble.size() == 1_ul);
785
            expect(vdouble.ssize() == 1);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
786
        }
787

Gauthier Quesnel's avatar
Gauthier Quesnel committed
788
        {
789
790
791
            irt::message vdouble(0.0, 1.0);
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 1.0);
792
            expect(vdouble[2] == 0.0);
793
            expect(vdouble[3] == 0.0);
794
            expect(vdouble.size() == 2_ul);
795
            expect(vdouble.ssize() == 2);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
796
        }
797

Gauthier Quesnel's avatar
Gauthier Quesnel committed
798
        {
799
800
801
802
803
            irt::message vdouble(0.0, 0.0, 1.0);
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 1.0);
            expect(vdouble[3] == 0.0);
804
            expect(vdouble.size() == 3_ul);
805
806
807
808
809
810
811
812
813
814
815
            expect(vdouble.ssize() == 3);
        }

        {
            irt::message vdouble(0.0, 0.0, 0.0, 1.0);
            expect(vdouble[0] == 0.0);
            expect(vdouble[1] == 0.0);
            expect(vdouble[2] == 0.0);
            expect(vdouble[3] == 1.0);
            expect(vdouble.size() == 4_ul);
            expect(vdouble.ssize() == 4);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
        }
    };

    "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();
        }
    };

925
926
927
928
    "simulation-dispatch"_test = [] {
        irt::simulation sim;
        sim.init(64u, 256u);
        auto& dyn1 = sim.alloc<irt::none>();
929
930
        (void)sim.alloc<irt::qss1_integrator>();
        (void)sim.alloc<irt::qss1_multiplier>();
931

932
        auto& mdl = irt::get_model(dyn1);
933

934
935
        sim.dispatch(mdl,
                     []([[maybe_unused]] auto& dyns) { std::cout << "ok"; });
936

937
938
939
940
941
        auto ret =
          sim.dispatch(mdl, []([[maybe_unused]] const auto& dyns) -> int {
              std::cout << "ok";
              return 1;
          });
942
943
944

        expect(ret == 1);

945
        auto ret_2 = sim.dispatch(
946
947
          mdl,
          []([[maybe_unused]] const auto& dyns, int v1, double v2) {
948
949
950
951
952
953
954
955
956
              std::cout << "ok" << v1 << ' ' << v2;
              return v2 + v1;
          },
          123,
          456.0);

        expect(ret_2 == 579.0);
    };

957
    "input-output"_test = [] {
958
959
960
961
962
        std::string str;
        str.reserve(4096u);

        {
            irt::simulation sim;
963
            irt::external_source srcs;
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
            expect(irt::is_success(sim.init(64lu, 4096lu)));

            sim.alloc<irt::none>();
            sim.alloc<irt::qss1_integrator>();
            sim.alloc<irt::qss1_multiplier>();
            sim.alloc<irt::qss1_cross>();
            sim.alloc<irt::qss1_power>();
            sim.alloc<irt::qss1_square>();
            sim.alloc<irt::qss1_sum_2>();
            sim.alloc<irt::qss1_sum_3>();
            sim.alloc<irt::qss1_sum_4>();
            sim.alloc<irt::qss1_wsum_2>();
            sim.alloc<irt::qss1_wsum_3>();
            sim.alloc<irt::qss1_wsum_4>();
            sim.alloc<irt::qss2_integrator>();
            sim.alloc<irt::qss2_multiplier>();
            sim.alloc<irt::qss2_cross>();
            sim.alloc<irt::qss2_power>();
            sim.alloc<irt::qss2_square>();
            sim.alloc<irt::qss2_sum_2>();
            sim.alloc<irt::qss2_sum_3>();
            sim.alloc<irt::qss2_sum_4>();
            sim.alloc<irt::qss2_wsum_2>();
            sim.alloc<irt::qss2_wsum_3>();
            sim.alloc<irt::qss2_wsum_4>();
            sim.alloc<irt::qss3_integrator>();
            sim.alloc<irt::qss3_multiplier>();
            sim.alloc<irt::qss3_power>();
            sim.alloc<irt::qss3_square>();
            sim.alloc<irt::qss3_cross>();
            sim.alloc<irt::qss3_sum_2>();
            sim.alloc<irt::qss3_sum_3>();
            sim.alloc<irt::qss3_sum_4>();
            sim.alloc<irt::qss3_wsum_2>();
            sim.alloc<irt::qss3_wsum_3>();
            sim.alloc<irt::qss3_wsum_4>();
1000
1001
            sim.alloc<irt::integrator>();
            sim.alloc<irt::quantifier>();
1002
1003
1004
1005
1006
1007
1008
            sim.alloc<irt::adder_2>();
            sim.alloc<irt::adder_3>();
            sim.alloc<irt::adder_4>();
            sim.alloc<irt::mult_2>();
            sim.alloc<irt::mult_3>();
            sim.alloc<irt::mult_4>();
            sim.alloc<irt::counter>();
1009
1010
1011
            sim.alloc<irt::queue>();
            sim.alloc<irt::dynamic_queue>();
            sim.alloc<irt::priority_queue>();
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
            sim.alloc<irt::generator>();
            sim.alloc<irt::constant>();
            sim.alloc<irt::cross>();
            sim.alloc<irt::time_func>();
            sim.alloc<irt::accumulator_2>();
            sim.alloc<irt::flow>();

            std::ostringstream os;
            irt::writer w(os);

1022
            expect(irt::is_success(w(sim, srcs)));
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
            str = os.str();
        }

        expect(!str.empty());
        fmt::print(str);

        {
            std::istringstream is(str);

            irt::simulation sim;
1033
            irt::external_source srcs;
1034
            expect(irt::is_success(sim.init(64lu, 32lu)));
1035
            expect(irt::is_success(srcs.init(64u)));
1036
1037

            irt::reader r(is);
1038
            expect(irt::is_success(r(sim, srcs)));
1039

1040
            expect(sim.models.size() == 52);
1041
        }
1042
1043
1044
1045
1046
1047

        {
            std::istringstream is(str);
            int i = 0;

            irt::simulation sim;
1048
            irt::external_source srcs;
1049
1050
1051
            expect(irt::is_success(sim.init(64lu, 32lu)));

            irt::reader r(is);
1052
1053
1054
            expect(irt::is_success(
              r(sim, srcs, [&i](irt::model_id /*id*/) { ++i; })));
            expect(i == 52);
1055

1056
            expect(sim.models.size() == 52);
1057
        }
1058
1059

        {
1060
            std::string string_error{ "0 0 0 0\n1\n0 5 6 qss1_integrator A B C\n" };
1061
1062
            std::istringstream is{ string_error };
            irt::simulation sim;
1063
1064
            irt::external_source srcs;

1065
1066
1067
1068
1069
            expect(irt::is_success(sim.init(64lu, 32lu)));

            irt::is_fatal_breakpoint = false;

            irt::reader r(is);
1070
1071
            expect(irt::is_bad(r(sim, srcs)));
            expect(r.line_error() == 3);
1072
            expect(r.column_error() <= 23); /* linux/win: 22 macos: 23 */
1073
1074
1075
            expect(r.model_error == 0);
            expect(r.connection_error == 0);

1076
1077
1078
            expect(r.get_position(0).x == 5.f);
            expect(r.get_position(0).y == 6.f);

1079
1080
            irt::is_fatal_breakpoint = true;
        }
1081
1082
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1083
1084
1085
1086
    "constant_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
1087
        expect(sim.can_alloc(3));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1088

1089
1090
1091
        auto& cnt = sim.alloc<irt::counter>();
        auto& c1 = sim.alloc<irt::constant>();
        auto& c2 = sim.alloc<irt::constant>();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1092

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1093
1094
        c1.default_value = 0.0;
        c2.default_value = 0.0;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1095

1096
1097
        expect(sim.connect(c1, 0, cnt, 0) == irt::status::success);
        expect(sim.connect(c2, 0, cnt, 0) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1098
1099

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

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1102
1103
1104
1105
1106
1107
1108
        irt::status st;

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

1109
        expect(cnt.number == static_cast<irt::i64>(2));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1110
1111
    };

1112
1113
1114
1115
    "cross_simulation"_test = [] {
        irt::simulation sim;

        expect(irt::is_success(sim.init(16lu, 256lu)));
1116
        expect(sim.can_alloc(3));
1117

1118
1119
1120
        auto& cnt = sim.alloc<irt::counter>();
        auto& cross1 = sim.alloc<irt::cross>();
        auto& c1 = sim.alloc<irt::constant>();
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1121
1122
1123

        c1.default_value = 3.0;
        cross1.default_threshold = 0.0;
1124

1125
1126
1127
1128
        expect(sim.connect(c1, 0, cross1, 0) == irt::status::success);
        expect(sim.connect(c1, 0, cross1, 1) == irt::status::success);
        expect(sim.connect(c1, 0, cross1, 2) == irt::status::success);
        expect(sim.connect(cross1, 0, cnt, 0) == irt::status::success);
1129
1130

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

1133
1134
        irt::status st;

1135
        do {
1136
1137
1138
1139
            st = sim.run(t);
            expect(irt::is_success(st));
        } while (t < sim.end);

1140
        expect(cnt.number == static_cast<irt::i64>(2));
1141
1142
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1143
    "generator_counter_simluation"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1144
        fmt::print("generator_counter_simluation\n");
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1145
        irt::simulation sim;
1146
1147
        irt::external_source srcs;
        sim.source_dispatch = srcs;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1148
1149

        expect(irt::is_success(sim.init(16lu, 256lu)));
1150
        expect(irt::is_success(srcs.init(4lu)));
1151
        expect(sim.can_alloc(2));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1152

1153
1154
1155
1156
        expect(srcs.constant_sources.can_alloc(2u));
        auto& cst_value = srcs.constant_sources.alloc();
        expect(irt::is_success(cst_value.init(32)));
        cst_value.buffer = { 1., 2., 3., 4., 5., 6., 7., 8., 9., 10. };
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1157

1158
1159
1160
        auto& cst_ta = srcs.constant_sources.alloc();
        expect(irt::is_success(cst_ta.init(32)));
        cst_ta.buffer = { 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1. };
1161

1162
1163
        auto& gen = sim.alloc<irt::generator>();
        auto& cnt = sim.alloc<irt::counter>();
1164

1165
1166
1167
1168
1169
1170
1171
1172
        gen.default_source_value.id =
          irt::ordinal(srcs.constant_sources.get_id(cst_value));
        gen.default_source_value.type =
          irt::ordinal(irt::external_source_type::constant);
        gen.default_source_ta.id =
          irt::ordinal(srcs.constant_sources.get_id(cst_ta));
        gen.default_source_ta.type =
          irt::ordinal(irt::external_source_type::constant);
1173

1174
        expect(sim.connect(gen, 0, cnt, 0) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1175

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

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1179
1180
1181
1182
1183
        irt::status st;

        do {
            st = sim.run(t);
            expect(irt::is_success(st));
1184
        } while (t < 10.0);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1185

1186
        expect(cnt.number == static_cast<irt::i64>(10));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1187
    };
1188

K-H-Ismail's avatar
K-H-Ismail committed
1189
    "time_func"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1190
        fmt::print("time_func\n");
K-H-Ismail's avatar
K-H-Ismail committed
1191
        irt::simulation sim;
1192
        const double duration = 30;
K-H-Ismail's avatar
K-H-Ismail committed
1193
1194

        expect(irt::is_success(sim.init(16lu, 256lu)));
1195
        expect(sim.can_alloc(2));
1196

1197
1198
        auto& time_fun = sim.alloc<irt::time_func>();
        auto& cnt = sim.alloc<irt::counter>();
K-H-Ismail's avatar
K-H-Ismail committed
1199

1200
        time_fun.default_f = &irt::square_time_function;
1201
        time_fun.default_sigma = 0.1;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1202

1203
        expect(sim.connect(time_fun, 0, cnt, 0) == irt::status::success);
K-H-Ismail's avatar
K-H-Ismail committed
1204

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1205
        irt::time t{ 0 };
1206
        expect(sim.initialize(t) == irt::status::success);
1207
        double c = 0.0;
K-H-Ismail's avatar
K-H-Ismail committed
1208
        do {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1209
            auto st = sim.run(t);
1210
            expect((irt::is_success(st)) == true);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1211
            expect(time_fun.value == t * t);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1212
            c++;
1213
        } while (t < duration);
1214
1215
1216

        const auto value = (2.0 * duration / time_fun.default_sigma - 1.0);
        expect(c == value);
1217
1218
1219
    };

    "time_func_sin"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1220
        fmt::print("time_func_sin\n");
1221
1222
1223
        const double pi = std::acos(-1);
        const double f0 = 0.1;
        const double duration = 30;
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1224
        irt::simulation sim;
1225
1226

        expect(irt::is_success(sim.init(16lu, 256lu)));
1227
        expect(sim.can_alloc(2));
1228

1229
1230
        auto& time_fun = sim.alloc<irt::time_func>();
        auto& cnt = sim.alloc<irt::counter>();
1231
1232
1233
1234

        time_fun.default_f = &irt::sin_time_function;
        time_fun.default_sigma = 0.1;

1235
        expect(sim.connect(time_fun, 0, cnt, 0) == irt::status::success);
1236
1237

        irt::time t{ 0 };
1238
        expect(sim.initialize(t) == irt::status::success);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1239
        double c = 0;
1240
1241
1242
        do {

            auto st = sim.run(t);
1243
            expect((irt::is_success(st)) >> fatal);
1244
            expect(time_fun.value == std::sin(2 * pi * f0 * t));
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1245
            c++;
1246
        } while (t < duration);
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1247
        expect(c == 2.0 * duration / time_fun.default_sigma - 1.0);
1248
1249
    };

Gauthier Quesnel's avatar
Gauthier Quesnel committed
1250
1251
    "lotka_volterra_simulation"_test = [] {
        fmt::print("lotka_volterra_simulation\n");
1252
1253
1254
        irt::simulation sim;

        expect(irt::is_success(sim.init(32lu, 512lu)));
1255
1256
1257
1258
1259
1260
1261
1262
1263
        expect(sim.can_alloc(8));

        auto& sum_a = sim.alloc<irt::adder_2>();
        auto& sum_b = sim.alloc<irt::adder_2>();
        auto& product = sim.alloc<irt::mult_2>();
        auto& integrator_a = sim.alloc<irt::integrator>();
        auto& integrator_b = sim.alloc<irt::integrator>();
        auto& quantifier_a = sim.alloc<irt::quantifier>();
        auto& quantifier_b = sim.alloc<irt::quantifier>();
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287

        integrator_a.default_current_value = 18.0;

        quantifier_a.default_adapt_state =
          irt::quantifier::adapt_state::possible;
        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 = 7.0;

        quantifier_b.default_adapt_state =
          irt::quantifier::adapt_state::possible;
        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] = 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;

1288
        expect((sim.models.size() == 7_ul) >> fatal);
1289

1290
1291
        expect(sim.connect(sum_a, 0, integrator_a, 1) == irt::status::success);
        expect(sim.connect(sum_b, 0, integrator_b, 1) == irt::status::success);
1292

1293
1294
        expect(sim.connect(integrator_a, 0, sum_a, 0) == irt::status::success);
        expect(sim.connect(integrator_b, 0, sum_b, 0) == irt::status::success);
1295

1296
        expect(sim.connect(integrator_a, 0, product, 0) ==
1297
               irt::status::success);
1298
        expect(sim.connect(integrator_b, 0, product, 1) ==
1299
1300
               irt::status::success);

1301
1302
        expect(sim.connect(product, 0, sum_a, 1) == irt::status::success);
        expect(sim.connect(product, 0, sum_b, 1) == irt::status::success);
1303

1304
        expect(sim.connect(quantifier_a, 0, integrator_a, 0) ==
1305
               irt::status::success);
1306
        expect(sim.connect(quantifier_b, 0, integrator_b, 0) ==
1307
               irt::status::success);
1308
        expect(sim.connect(integrator_a, 0, quantifier_a, 0) ==
1309
               irt::status::success);
1310
        expect(sim.connect(integrator_b, 0, quantifier_b, 0) ==
1311
1312
1313
1314
1315
1316
1317
               irt::status::success);

        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);

1318
1319
        auto& obs_a = sim.observers.alloc("A", fo_a);
        auto& obs_b = sim.observers.alloc("B", fo_b);
1320

1321
1322
        sim.observe(irt::get_model(integrator_a), obs_a);
        sim.observe(irt::get_model(integrator_b), obs_b);
1323
1324
1325
1326

        irt::time t = 0.0;

        expect(sim.initialize(t) == irt::status::success);
1327
        expect((sim.sched.size() == 7_ul) >> fatal);
1328
1329
1330
1331
1332

        do {
            auto st = sim.run(t);
            expect(st == irt::status::success);
        } while (t < 15.0);
K-H-Ismail's avatar
K-H-Ismail committed
1333
1334
    };

1335
    "izhikevitch_simulation"_test = [] {
Gauthier Quesnel's avatar
Gauthier Quesnel committed
1336
        fmt::print("izhikevitch_simulation\n");
1337
1338
1339
        irt::simulation sim;

        expect(irt::is_success(sim.init(64lu, 256lu)));
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
        expect(sim.models.can_alloc(14));

        auto& constant = sim.alloc<irt::constant>();
        auto& constant2 = sim.alloc<irt::constant>();
        auto& constant3 = sim.alloc<irt::constant>();
        auto& sum_a = sim.alloc<irt::adder_2>();
        auto& sum_b = sim.alloc<irt::adder_2>();
        auto& sum_c = sim.alloc<irt::adder_4>();
        auto& sum_d = sim.alloc<irt::adder_2>();
        auto& product = sim.alloc<irt::mult_2>();
        auto& integrator_a = sim.alloc<irt::integrator>();
        auto& integrator_b = sim.alloc<irt::integrator>();
        auto& quantifier_a = sim.alloc<irt::quantifier>();
        auto& quantifier_b = sim.alloc<irt::quantifier>();
        auto& cross = sim.alloc<irt::cross>();
        auto& cross2 = sim.alloc<irt::cross>();