File: safe-list-test.cpp

package info (click to toggle)
wayfire 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,764 kB
  • sloc: cpp: 52,464; xml: 2,987; ansic: 699; makefile: 161
file content (96 lines) | stat: -rw-r--r-- 1,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include <wayfire/nonstd/safe-list.hpp>

TEST_CASE("Safe-list basics")
{
    wf::safe_list_t<int> list;

    list.push_back(5);
    list.push_back(6);
    REQUIRE(list.size() == 2);

    list.for_each([x = 0] (int i) mutable
    {
        if (x == 0)
        {
            REQUIRE(i == 5);
        }

        if (x == 1)
        {
            REQUIRE(i == 6);
        }

        REQUIRE(x <= 1);
        x++;
    });

    list.remove_if([] (int i) { return i == 5; });
    REQUIRE(list.size() == 1);

    list.for_each([x = 0] (int i) mutable
    {
        if (x == 0)
        {
            REQUIRE(i == 6);
        }

        REQUIRE(x <= 0);
        x++;
    });
}

TEST_CASE("safe-list remove self")
{
    using cb = std::function<void ()>;
    wf::safe_list_t<cb*> list;

    cb self;
    list.push_back(&self);
    list.for_each_reverse([&] (cb *c)
    {
        REQUIRE(c == &self);
        list.remove_all(c);
    });
}

TEST_CASE("safe-list remove next")
{
    using cb = std::function<void ()>;
    wf::safe_list_t<cb*> list;

    cb self, next;
    list.push_back(&self);
    list.push_back(&next);

    int calls = 0;
    list.for_each([&] (cb *c)
    {
        calls++;
        REQUIRE(calls <= 1);
        REQUIRE(c == &self);
        list.remove_all(&next);
        REQUIRE(list.back() == &self);
    });
}

TEST_CASE("safe-list push next")
{
    using cb = std::function<void ()>;
    wf::safe_list_t<cb*> list;

    cb self, next;
    list.push_back(&self);

    int calls = 0;
    list.for_each([&] (cb *c)
    {
        calls++;
        REQUIRE(calls <= 1);
        list.push_back(&next);
    });

    REQUIRE(list.size() == 2);
}