-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimal_smoke_test.cpp
More file actions
98 lines (80 loc) · 3.16 KB
/
minimal_smoke_test.cpp
File metadata and controls
98 lines (80 loc) · 3.16 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
#include "linq.h"
#include <math.h>
#include <assert.h>
#include <vector>
#include <sstream>
#include <iterator>
void example1() {
int xs[] = { 1, 2, 3, 4, 5 };
std::vector<int> res =
from(xs, xs + 5) // Взять элементы xs
.select([](int x) { return x * x; }) // Возвести в квадрат
.where_neq(25) // Оставить только значения != 25
.where([](int x) { return x > 3; }) // Оставить только значения > 3
.drop(2) // Убрать два элемента из начала
.to_vector(); // Преобразовать результат в вектор
std::vector<int> expected = { 16 };
assert(res == expected);
}
void example2() {
std::stringstream ss("1 2 3 -1 4");
std::istream_iterator<int> in(ss), eof;
std::vector<int> res =
from(in, eof) // Взять числа из входного потока
.take(4) // Не более четырёх чисел
.until_eq(-1) // Перестать читать после прочтения -1
.to_vector(); // Получить список считанных чисел
std::vector<int> expected = { 1, 2, 3 };
assert(expected == res);
int remaining;
assert(ss >> remaining);
assert(remaining == 4);
}
void example3() {
int xs[] = { 1, 2, 3, 4, 5 };
std::vector<double> res =
from(xs, xs + 5) // Взять элементы xs
.select<double>([](int x) { return sqrt(x); }) // Извлечь корень
.to_vector(); // Преобразовать результат в вектор
assert(res.size() == 5);
for (std::size_t i = 0; i < res.size(); i++) {
assert(fabs(res[i] - sqrt(xs[i])) < 1e-9);
}
}
void example4() {
std::stringstream iss("4 16");
std::stringstream oss;
std::istream_iterator<int> in(iss), eof;
std::ostream_iterator<double> out(oss, "\n");
from(in, eof) // Взять числа из входного потока
.select([](int x) { return static_cast<int>(sqrt(x) + 1e-6); }) // Извлечь из каждого корень
.copy_to(out); // Вывести на экран
assert(oss.str() == "2\n4\n");
}
void from_to_vector() {
std::vector<int> xs = { 1, 2, 3 };
std::vector<int> res = from(xs.begin(), xs.end()).to_vector();
assert(res == xs);
}
void from_select() {
const int xs[] = { 1, 2, 3 };
std::vector<int> res = from(xs, xs + 3).select([](int x) { return x + 5; }).to_vector();
std::vector<int> expected = { 6, 7, 8 };
assert(res == expected);
}
void from_drop_select() {
const int xs[] = {1, 2, 3};
std::vector<int> res = from(xs, xs + 3).drop(1).select([](int x) { return x + 5; }).to_vector();
std::vector<int> expected = {7, 8};
assert(res == expected);
}
int main_test() {
from_to_vector();
from_select();
from_drop_select();
example1();
example2();
example3();
example4();
return 0;
}