base_tests.cpp 2.13 KB
Newer Older
1
#include <catch.hpp>
2 3 4
#include "pls/internal/base/thread.h"
#include "pls/internal/base/spin_lock.h"
#include "pls/internal/base/system_details.h"
5 6

#include <vector>
7
#include <mutex>
8 9 10 11 12 13 14 15

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;

16 17
TEST_CASE("thread creation and joining", "[internal/data_structures/thread.h]") {
  base_tests_visited = false;
18
  thread t1{[]() { base_tests_visited = true; }};
19
  t1.join();
20

21
  REQUIRE(base_tests_visited);
22 23
}

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

28 29
  thread t1{[]() { base_tests_local_value_one = *this_thread::state<int>(); }, &state_one};
  thread t2{[]() { base_tests_local_value_two = *this_thread::state<vector<int>>(); }, &state_two};
30 31
  t1.join();
  t2.join();
32

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

37 38
int base_tests_shared_counter;

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

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

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

    REQUIRE(base_tests_shared_counter == 0);
  }

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

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

    REQUIRE(base_tests_shared_counter == 0);
  }
85
}