File: thread_pool_test.cpp

package info (click to toggle)
libthread-pool 4.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 132 kB
  • sloc: cpp: 180; sh: 14; makefile: 10
file content (48 lines) | stat: -rw-r--r-- 1,110 bytes parent folder | download | duplicates (3)
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
// Copyright (c) 2020 Robert Vaser

#include "thread_pool/thread_pool.hpp"

#include "gtest/gtest.h"

namespace thread_pool {
namespace test {

TEST(ThreadPoolThreadPoolTest, Submit) {
  std::function<std::size_t(std::size_t)> fibonacci =
      [&fibonacci] (std::size_t i) -> std::size_t {
    if (i == 1 || i == 2) {
      return 1;
    }
    return fibonacci(i - 1) + fibonacci(i - 2);
  };

  ThreadPool tp{};

  std::vector<std::future<std::size_t>> f;
  for (std::size_t i = 0; i < tp.num_threads(); ++i) {
    f.emplace_back(tp.Submit(fibonacci, 42));
  }
  for (auto& it : f) {
    EXPECT_EQ(267914296, it.get());
  }
}

TEST(ThreadPoolThreadPoolTest, ThreadIds) {
  ThreadPool tp{};
  EXPECT_EQ(tp.num_threads(), tp.thread_map().size());

  auto thread_id = [&] () -> std::size_t {
    return tp.thread_map().count(std::this_thread::get_id());
  };

  std::vector<std::future<size_t>> f;
  for (std::size_t i = 0; i < tp.num_threads() * 42; ++i) {
    f.emplace_back(tp.Submit(thread_id));
  }
  for (auto& it : f) {
    EXPECT_EQ(1, it.get());
  }
}

}  // namespace test
}  // namespace thread_pool