patterns_test.cpp 1.06 KB
Newer Older
1 2 3 4 5 6 7 8 9
#include <catch.hpp>

#include <atomic>

#include "pls/pls.h"

constexpr int MAX_NUM_TASKS = 32;
constexpr int MAX_STACK_SIZE = 1024 * 8;

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
TEST_CASE("spawn/sync invoke calls correctly", "[algorithms/invoke.h]") {
  pls::scheduler scheduler{3, MAX_NUM_TASKS, MAX_STACK_SIZE};

  std::atomic<int> num_run{0};
  scheduler.perform_work([&] {
    pls::spawn([&] {
      num_run++;
      while (num_run < 3);
    });
    pls::spawn([&] {
      while (num_run < 1);
      num_run++;
      while (num_run < 3);
    });
    pls::spawn([&] {
      while (num_run < 2);
      num_run++;
    });
    pls::sync();
    REQUIRE(num_run == 3);
  });
}

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
TEST_CASE("parallel invoke calls correctly", "[algorithms/invoke.h]") {
  pls::scheduler scheduler{3, MAX_NUM_TASKS, MAX_STACK_SIZE};

  std::atomic<int> num_run{0};
  scheduler.perform_work([&] {
    pls::invoke([&] {
      num_run++;
      while (num_run < 3);
    }, [&] {
      while (num_run < 1);
      num_run++;
      while (num_run < 3);
    }, [&] {
      while (num_run < 2);
      num_run++;
    });
    REQUIRE(num_run == 3);
  });
}