-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest.h
More file actions
116 lines (96 loc) · 2.84 KB
/
test.h
File metadata and controls
116 lines (96 loc) · 2.84 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#ifndef SYSTEMATIC_TESTING_TEST_H
#define SYSTEMATIC_TESTING_TEST_H
#include <chrono>
#include <iostream>
#include "systematic_testing.h"
using namespace SystematicTesting;
void assert(bool predicate, std::string error)
{
if (!predicate)
{
throw error;
}
}
size_t total_time(std::chrono::steady_clock::time_point start_time)
{
auto end_time = std::chrono::steady_clock::now();
return (size_t)std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
}
Settings CreateDefaultSettings()
{
auto settings = Settings();
settings.with_verbosity_level(VerbosityLevel::Exhaustive);
settings.with_random_generator_seed(0);
settings.with_random_strategy();
settings.with_partially_controlled_concurrency_allowed(false);
return settings;
}
class SystematicTestIterationContext
{
public:
const uint32_t id;
SystematicTestIterationContext(uint32_t iteration, bool start_controlled) :
id(iteration)
{
std::cout << "[test] iteration " << id << std::endl;
GetTestEngine()->prepare();
if (start_controlled)
{
TakeControl();
}
}
virtual ~SystematicTestIterationContext()
{
GetTestEngine()->detach();
}
void TakeControl()
{
assert(!m_is_attached, "The test engine is already attached.");
m_is_attached = true;
GetTestEngine()->attach();
}
private:
bool m_is_attached = false;
};
class SystematicTestEngineContext
{
public:
SystematicTestEngineContext(Settings settings, uint32_t iterations) :
m_iterations(0),
m_total_iterations(iterations)
{
// Set the global test engine.
InitTestEngine(settings);
}
std::unique_ptr<SystematicTestIterationContext> next_iteration(bool start_controlled = true)
{
return m_iterations < m_total_iterations ?
std::make_unique<SystematicTestIterationContext>(m_iterations++, start_controlled) :
std::unique_ptr<SystematicTestIterationContext>();
}
std::unique_ptr<SystematicTestIterationContext> next_iteration_if(bool condition, bool start_controlled = true)
{
return m_iterations < m_total_iterations && condition ?
std::make_unique<SystematicTestIterationContext>(m_iterations++, start_controlled) :
std::unique_ptr<SystematicTestIterationContext>();
}
TestReport report()
{
return GetTestEngine()->report();
}
uint32_t total_iterations() const
{
return m_total_iterations;
}
~SystematicTestEngineContext()
{
// Remove the global test engine.
RemoveTestEngine();
}
private:
uint32_t m_iterations;
const uint32_t m_total_iterations;
};
#endif // SYSTEMATIC_TESTING_TEST_H