Welcome to mirror list, hosted at ThFree Co, Russian Federation.

threads_test.cpp « base_tests « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 98933c76826b510531eaa2292740eb4968718327 (plain)
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
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
#include "testing/testing.hpp"

#include "base/thread.hpp"
#include "base/stl_add.hpp"

#include <vector>


typedef std::vector<int> Vector;

static size_t summ = 0;
static size_t checkSumm = 0;
static size_t const MAX_COUNT = 1000000;

struct GeneratorThread : public threads::IRoutine
{
  GeneratorThread(Vector & vec) : m_vec(vec) {}

  virtual void Do()
  {
    for (size_t i = 0; i < MAX_COUNT; ++i)
    {
      m_vec.push_back(static_cast<int>(i));
      summ += i;
    }
  }
  Vector & m_vec;
};

struct ReaderThread : public threads::IRoutine
{
  ReaderThread(Vector & vec) : m_vec(vec) {}

  virtual void Do()
  {
    for (size_t i = 0; i < m_vec.size(); ++i)
      checkSumm += m_vec[i];
  }
  Vector & m_vec;
};


UNIT_TEST(Simple_Threads)
{
  Vector vec;

  threads::Thread reader;
  bool ok = reader.Create(my::make_unique<GeneratorThread>(vec));
  TEST( ok, ("Create Generator thread") );

  reader.Join();

  threads::Thread writer;
  ok = writer.Create(my::make_unique<ReaderThread>(vec));
  TEST( ok, ("Create Reader thread") );

  writer.Join();

  TEST_EQUAL(vec.size(), MAX_COUNT, ("vector size"));
  TEST_EQUAL(summ, checkSumm, ("check summ"));
}

class SomeClass
{
  DISALLOW_COPY(SomeClass);

public:
  SomeClass() {}
  void Increment(int * a, int b)
  {
    *a = *a + b;
  }
};

static void Increment(int * a, int b)
{
  *a = *a + b;
}

UNIT_TEST(SimpleThreadTest1)
{
  int a = 0;

  auto fn = [&a](){ a = 1; };

  threads::SimpleThread t(fn);
  t.join();

  TEST_EQUAL(a, 1, ("test a"));
}

UNIT_TEST(SimpleThreadTest2)
{
  int a = 0;

  threads::SimpleThread t([&a](){ a = 1; });
  t.join();

  TEST_EQUAL(a, 1, ("test a"));
}

UNIT_TEST(SimpleThreadTest3)
{
  int a = 0;

  SomeClass instance;
  threads::SimpleThread t(&SomeClass::Increment, &instance, &a, 1);
  t.join();

  TEST_EQUAL(a, 1, ("test a"));
}

UNIT_TEST(SimpleThreadTest4)
{
  int a = 0;

  threads::SimpleThread t(&Increment, &a, 1);
  t.join();

  TEST_EQUAL(a, 1, ("test a"));
}