-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
204 lines (181 loc) · 4.4 KB
/
test.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "rubify.hpp"
using Rubify::vector;
using Rubify::string;
using Rubify::map;
using Rubify::continue_;
using Rubify::break_;
using Rubify::do_once;
using Rubify::do_at_interval;
using Rubify::do_a_few_times;
using Rubify::do_a_few_times_at_interval;
/* expect:
[ 10, 1, 1, 2, 2 ]
7
*/
void test_access() {
vector<int> v = std::vector<int>( 10, 0 );
v[6] = 1;
v[-5] = 1;
v[-8] = 9;
v.each([&] (int& e) {
e++;
});
puts( v.take(7).drop(2).to_s() );
v.sort_by_<int>([](const int& e)->int { return e; });
puts(v.locate( [](int& e)->bool { return e < 2; } ) );
puts(v.locate( [](int& e)->bool { return e <= 11; } ) );
}
/* expect:
[ 0, 0, 0, 6, 8, 10, 12, 14, 16, 0 ] */
void test_break_n_continue() {
vector<int> v = std::vector<int>(10, 0);
v.each( [&] (int i, int& e) {
if (i<3)
continue_();
if (i>8)
break_();
e = i*2;
});
puts(v.to_s());
}
/* expect:
{ 0 => 1, 1 => 3, 2 => 9, 3 => 27 }
[ [ 0, 1 ], [ 1, 3 ], [ 2, 9 ], [ 3, 27 ] ]
[ 0, 1, 1, 3, 2, 9, 3, 27 ] */
void test_conversion() {
map<int, int> m;
m[0] = 1;
m[1] = 3;
m[2] = 9;
m[3] = 27;
puts(m.to_s());
puts(m.to_a().to_s());
puts(m.to_a().flatten().to_s());
vector<int>mask(3,0);
mask[1] = 1;
mask[2] = 2;
puts(m.to_a().flatten().norm_zip(mask).to_s());
}
struct People {
string name;
int zip;
string email;
string to_s() {
return "(" + name + "," S_(zip) "," + email + ")";
}
};
/* expect:
(Alice,19026,[email protected])
(Bob,19026,[email protected])
(Cate,19026,[email protected])
(David,19026,[email protected])
(Anna,19104,[email protected])
(Bran,19104,[email protected])
(Chloe,19104,[email protected])
(Delphi,19104,[email protected])
(Abby,19520,[email protected])
(Black,19520,[email protected])
(Cyan,19520,[email protected])
(Doge,19520,[email protected])
(Emma,19520,[email protected])
[ (Bran,19104,[email protected]), (Anna,19104,[email protected]), (Chloe,19104,[email protected]), (Delphi,19104,[email protected]) ]*/
void test_group_n_sort() {
vector<People> name_list;
name_list.push_back( {"Bob", 19026, "[email protected]"} );
name_list.push_back( {"Bran", 19104, "[email protected]"} );
name_list.push_back( {"Emma", 19520, "[email protected]"} );
name_list.push_back( {"Cate", 19026, "[email protected]"} );
name_list.push_back( {"David", 19026, "[email protected]"} );
name_list.push_back( {"Anna", 19104, "[email protected]"} );
name_list.push_back( {"Black", 19520, "[email protected]"} );
name_list.push_back( {"Doge", 19520, "[email protected]"} );
name_list.push_back( {"Chloe", 19104, "[email protected]"} );
name_list.push_back( {"Delphi", 19104, "[email protected]"} );
name_list.push_back( {"Abby", 19520, "[email protected]"} );
name_list.push_back( {"Cyan", 19520, "[email protected]"} );
name_list.push_back( {"Alice", 19026, "[email protected]"} );
puts(
name_list
.sort_by<string>( [&](People p)->string {return p.name;})
.group_by<int>( [&](People p)->int {return p.zip;})
.map<string>( [&](vector<People> v)->string { return v.join("\n");} )
.join("\n\n")
);
puts( name_list.select([&](const People& p)->bool {return p.zip == 19104;}).to_s() );
}
/* expect:
123
5
*/
void test_stoi()
{
puts(string("123").to_i());
puts(string("101").to_i(2));
}
void test_debug()
{
for (int i=0; i<30; i++)
{
puts("[" S_(i) "]");
do_once( 1, [&]() { std::cout<<"| do once! |"; } );
do_a_few_times( 2, 5, [&]() { std::cout<<"| do 5 times! |"; } );
do_at_interval( 3, 3, [&]() { std::cout<<"| do at interval 3! |"; } );
do_a_few_times_at_interval( 4, 5, 5, [&]() { std::cout<<"| do 5 times at interval 5! |"; } );
puts(" ");
}
}
void need_int(int lvl)
{
if (lvl == 0)
{
int seven = require_(int, 7);
puts(seven);
}
else
{
provide_([&](int name)->int {
if (name == lvl)
return lvl;
else
return require_(int, name);
} )
need_int(lvl - 1);
}
}
string arabic_to_english[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven"};
void need_string(int lvl)
{
if (lvl == 0)
{
string eleven = require_from_(std::this_thread::get_id(), string, "11");
puts(eleven);
}
else
{
provide_([&](string name)->string {
if (name == _S_(lvl))
return arabic_to_english[lvl];
else
return require_(string, name);
} )
need_string(lvl - 1);
}
}
/* expected output:
7
eleven
*/
void test_algebraic_effect()
{
need_int(10);
need_string(20);
}
int main() {
test_access();
test_break_n_continue();
test_conversion();
test_group_n_sort();
test_stoi();
test_debug();
test_algebraic_effect();
}