-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.cpp
More file actions
83 lines (71 loc) · 1.94 KB
/
test.cpp
File metadata and controls
83 lines (71 loc) · 1.94 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
#include "Stack.hpp"
#include <chrono>
#include <cstddef>
#include <memory>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <thread>
Stack<int> stack;
Stack<int> stream;
static constexpr size_t THREADS = 10;
static constexpr size_t NUMBERS = 100;
static constexpr size_t TOTAL_NUMBERS = THREADS * NUMBERS;
static void execute(size_t start, size_t stop)
{
for (size_t index = start; index < stop; ++index)
{
stack.push(index);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
stream.push(*stack.pop());
}
}
static void lock_free()
{
std::shared_ptr<Stack<int>> shared_pointer;
std::cout << std::boolalpha << std::atomic_is_lock_free(&shared_pointer) << std::endl;
}
static void generate()
{
using std::cout;
std::thread threads[THREADS];
for (size_t index = 0; index < THREADS; ++index)
threads[index] = std::thread(execute, index * NUMBERS, (index + 1) * NUMBERS);
for (size_t index = 0; index < THREADS; ++index)
if (threads[index].joinable())
threads[index].join();
std::ofstream output("input.txt");
std::streambuf* std_cout = cout.rdbuf(output.rdbuf());
for (size_t index = 0; index < TOTAL_NUMBERS; ++index)
cout << *stream.pop() << ' ';
cout.rdbuf(std_cout);
}
static void validate()
{
using std::cin;
using std::cout;
std::ifstream input("input.txt");
std::streambuf* std_cin = cin.rdbuf(input.rdbuf());
std::ofstream output("output.txt");
std::streambuf* std_cout = cout.rdbuf(output.rdbuf());
bool result = true;
static size_t numbers[TOTAL_NUMBERS];
for (size_t index = 0; index < TOTAL_NUMBERS; ++index)
cin >> numbers[index];
std::sort(numbers, numbers + TOTAL_NUMBERS);
for (size_t index = 0; index < TOTAL_NUMBERS; ++index)
{
result = result && (index <= 0 || numbers[index] - numbers[index - 1] == 1);
cout << numbers[index] << ' ';
}
cin.rdbuf(std_cin);
cout.rdbuf(std_cout);
cout << std::boolalpha << result;
}
int main()
{
lock_free();
generate();
validate();
return 0;
}