base_tests.cpp 6.76 KB
Newer Older
1 2 3
#include <catch.hpp>
#include <pls/internal/base/thread.h>
#include <pls/internal/base/spin_lock.h>
FritzFlorian committed
4 5
#include <pls/internal/base/aligned_stack.h>
#include <pls/internal/base/system_details.h>
6 7

#include <vector>
8
#include <mutex>
9
#include <pls/internal/base/deque.h>
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

using namespace pls::internal::base;
using namespace std;

static bool base_tests_visited;
static int base_tests_local_value_one;
static vector<int> base_tests_local_value_two;

TEST_CASE( "thread creation and joining", "[internal/base/thread.h]") {
    base_tests_visited = false;
    auto t1 = start_thread([]() { base_tests_visited = true; });
    t1.join();

    REQUIRE(base_tests_visited);
}

TEST_CASE( "thread state", "[internal/base/thread.h]") {
    int state_one = 1;
    vector<int> state_two{1, 2};

    auto t1 = start_thread([]() { base_tests_local_value_one = *this_thread::state<int>(); }, &state_one);
    auto t2 = start_thread([]() { base_tests_local_value_two = *this_thread::state<vector<int>>(); }, &state_two);
    t1.join();
    t2.join();

    REQUIRE(base_tests_local_value_one == 1);
    REQUIRE(base_tests_local_value_two == vector<int>{1, 2});
}

39 40
int base_tests_shared_counter;

41
TEST_CASE( "spinlock protects concurrent counter", "[internal/base/spinlock.h]") {
42 43
    constexpr int num_iterations = 1000000;
    base_tests_shared_counter = 0;
44 45
    spin_lock lock{};

46 47 48 49
    SECTION( "lock can be used by itself" ) {
        auto t1 = start_thread([&]() {
            for (int i = 0; i < num_iterations; i++) {
                lock.lock();
50
                base_tests_shared_counter++;
51 52 53 54 55 56
                lock.unlock();
            }
        });
        auto t2 = start_thread([&]() {
            for (int i = 0; i < num_iterations; i++) {
                lock.lock();
57
                base_tests_shared_counter--;
58 59 60
                lock.unlock();
            }
        });
61

62 63 64
        t1.join();
        t2.join();

65
        REQUIRE(base_tests_shared_counter == 0);
66 67 68 69 70 71
    }

    SECTION( "lock can be used with std::lock_guard" ) {
        auto t1 = start_thread([&]() {
            for (int i = 0; i < num_iterations; i++) {
                std::lock_guard<spin_lock> my_lock{lock};
72
                base_tests_shared_counter++;
73 74 75 76 77
            }
        });
        auto t2 = start_thread([&]() {
            for (int i = 0; i < num_iterations; i++) {
                std::lock_guard<spin_lock> my_lock{lock};
78
                base_tests_shared_counter--;
79 80 81 82 83
            }
        });

        t1.join();
        t2.join();
84

85
        REQUIRE(base_tests_shared_counter == 0);
86
    }
87
}
FritzFlorian committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150

TEST_CASE( "aligned stack stores objects correctly", "[internal/base/aligned_stack.h]") {
    constexpr long data_size = 1024;
    char data[data_size];
    aligned_stack stack{data, data_size};

    SECTION( "stack correctly pushes sub linesize objects" ) {
        std::array<char, 5> small_data_one{'a', 'b', 'c', 'd', 'e'};
        std::array<char, 64> small_data_two{};
        std::array<char, 1> small_data_three{'A'};

        auto pointer_one = stack.push(small_data_one);
        auto pointer_two = stack.push(small_data_two);
        auto pointer_three = stack.push(small_data_three);

        REQUIRE(reinterpret_cast<std::uintptr_t>(pointer_one) % CACHE_LINE_SIZE == 0);
        REQUIRE(reinterpret_cast<std::uintptr_t>(pointer_two) % CACHE_LINE_SIZE == 0);
        REQUIRE(reinterpret_cast<std::uintptr_t>(pointer_three) % CACHE_LINE_SIZE == 0);
    }

    SECTION( "stack correctly pushes above linesize objects" ) {
        std::array<char, 5> small_data_one{'a', 'b', 'c', 'd', 'e'};
        std::array<char, CACHE_LINE_SIZE + 10> big_data_one{};

        auto big_pointer_one = stack.push(big_data_one);
        auto small_pointer_one = stack.push(small_data_one);

        REQUIRE(reinterpret_cast<std::uintptr_t>(big_pointer_one) % CACHE_LINE_SIZE == 0);
        REQUIRE(reinterpret_cast<std::uintptr_t>(small_pointer_one) % CACHE_LINE_SIZE == 0);
    }

    SECTION( "stack correctly stores and retrieves objects" ) {
        std::array<char, 5> data_one{'a', 'b', 'c', 'd', 'e'};

        stack.push(data_one);
        auto retrieved_data = stack.pop<std::array<char, 5>>();

        REQUIRE(retrieved_data == std::array<char, 5>{'a', 'b', 'c', 'd', 'e'});
    }

    SECTION( "stack can push and pop multiple times with correct alignment" ) {
        std::array<char, 5> small_data_one{'a', 'b', 'c', 'd', 'e'};
        std::array<char, 64> small_data_two{};
        std::array<char, 1> small_data_three{'A'};

        auto pointer_one = stack.push(small_data_one);
        auto pointer_two = stack.push(small_data_two);
        auto pointer_three = stack.push(small_data_three);
        stack.pop<typeof(small_data_three)>();
        stack.pop<typeof(small_data_two)>();
        auto pointer_four = stack.push(small_data_two);
        auto pointer_five = stack.push(small_data_three);

        REQUIRE(reinterpret_cast<std::uintptr_t>(pointer_one) % CACHE_LINE_SIZE == 0);
        REQUIRE(reinterpret_cast<std::uintptr_t>(pointer_two) % CACHE_LINE_SIZE == 0);
        REQUIRE(reinterpret_cast<std::uintptr_t>(pointer_three) % CACHE_LINE_SIZE == 0);
        REQUIRE(reinterpret_cast<std::uintptr_t>(pointer_four) % CACHE_LINE_SIZE == 0);
        REQUIRE(reinterpret_cast<std::uintptr_t>(pointer_five) % CACHE_LINE_SIZE == 0);

        REQUIRE(pointer_four == pointer_two);
        REQUIRE(pointer_five == pointer_three);
    }
}
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

TEST_CASE( "deque stores objects correctly", "[internal/base/deque.h]") {
    class my_item: public deque_item {

    };

    deque<my_item> deque;
    my_item one, two, three;

    SECTION( "add and remove items form the tail" ) {
        deque.push_tail(&one);
        deque.push_tail(&two);
        deque.push_tail(&three);

        REQUIRE(deque.pop_tail() == &three);
        REQUIRE(deque.pop_tail() == &two);
        REQUIRE(deque.pop_tail() == &one);
    }

    SECTION( "handles getting empty by popping the tail correctly" ) {
        deque.push_tail(&one);
        REQUIRE(deque.pop_tail() == &one);

        deque.push_tail(&two);
        REQUIRE(deque.pop_tail() == &two);
    }

    SECTION( "remove items form the head" ) {
        deque.push_tail(&one);
        deque.push_tail(&two);
        deque.push_tail(&three);

        REQUIRE(deque.pop_head() == &one);
        REQUIRE(deque.pop_head() == &two);
        REQUIRE(deque.pop_head() == &three);
    }

    SECTION( "handles getting empty by popping the head correctly" ) {
        deque.push_tail(&one);
        REQUIRE(deque.pop_head() == &one);

        deque.push_tail(&two);
        REQUIRE(deque.pop_head() == &two);
    }

    SECTION( "handles getting empty by popping the head and tail correctly" ) {
        deque.push_tail(&one);
        REQUIRE(deque.pop_tail() == &one);

        deque.push_tail(&two);
        REQUIRE(deque.pop_head() == &two);

        deque.push_tail(&three);
        REQUIRE(deque.pop_tail() == &three);
    }
}