-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathxformed.cpp
459 lines (387 loc) · 11.6 KB
/
xformed.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//
#include <catch2/catch.hpp>
#include <lager/constant.hpp>
#include <lager/state.hpp>
#include <lager/with.hpp>
#include <lager/lenses/at.hpp>
#include <lager/lenses/attr.hpp>
#include <lager/lenses/optional.hpp>
#include <zug/transducer/filter.hpp>
#include <array>
#include <map>
#include <optional>
using namespace zug;
using namespace lager;
namespace detail {
template <typename T>
struct is_optional : std::false_type
{};
template <typename T>
struct is_optional<std::optional<T>> : std::true_type
{};
template <typename T>
[[maybe_unused]] constexpr bool is_optional_v =
is_optional<std::decay_t<T>>::value;
} // namespace detail
template <typename AttrT, typename... CursorTs>
auto attred(AttrT&& member, CursorTs&&... cs)
{
if constexpr (std::disjunction_v<::detail::is_optional<
typename std::decay_t<CursorTs>::value_type>...>) {
return with(std::forward<CursorTs>(cs)...)
.zoom(lenses::with_opt(lenses::attr(std::forward<AttrT>(member))))
.make();
} else {
return with(std::forward<CursorTs>(cs)...)
.zoom(lenses::attr(std::forward<AttrT>(member)))
.make();
}
}
template <typename KeyT, typename... CursorTs>
auto atted(KeyT&& key, CursorTs&&... cs)
{
if constexpr (std::disjunction_v<::detail::is_optional<
typename std::decay_t<CursorTs>::value_type>...>) {
return with(std::forward<CursorTs>(cs)...)
.zoom(lenses::with_opt(lenses::at(std::forward<KeyT>(key))));
} else {
return with(std::forward<CursorTs>(cs)...)
.zoom(lenses::at(std::forward<KeyT>(key)))
.make();
}
}
TEST_CASE("xformed, to_in")
{
reader<int> i = make_state(0).xform(zug::identity);
}
TEST_CASE("xformed, identity")
{
auto s = state<int>{42};
auto x = s.xform(zug::identity).make();
CHECK(x.get() == 42);
}
TEST_CASE("merging nodes")
{
auto s1 = state<int>{42};
auto s2 = state<int>{13};
auto x = with(s1, s2).make();
CHECK(x.get() == std::make_tuple(42, 13));
}
TEST_CASE("xformed, identity two args is zipping")
{
auto s1 = state<int>{42};
auto s2 = state<int>{13};
auto x = with(s1, s2).xform(zug::identity).make();
CHECK(x.get() == std::make_tuple(42, 13));
}
TEST_CASE("xformed, one arg mapping, direct")
{
auto s = state<int>{42};
auto x = s.map([](int a) { return a + 1; }).make();
CHECK(x.get() == 43);
}
TEST_CASE("xformed, directo composition")
{
auto s = state<int>{42};
auto x = s.map([](int a) { return a + 1; })
.map([](int a) { return a * 2; })
.make();
CHECK(x.get() == 86);
}
TEST_CASE("xformed, two arg mapping")
{
auto s1 = state<int>{42};
auto s2 = state<int>{10};
auto x = with(s1, s2).xform(map(std::plus<int>{})).make();
CHECK(x.get() == 52);
}
TEST_CASE("xformed, two arg mapping direct")
{
auto s1 = state<int>{42};
auto s2 = state<int>{10};
auto x = with(s1, s2).map(std::plus<int>{}).make();
CHECK(x.get() == 52);
}
TEST_CASE("xformed, one arg filter with value")
{
auto s = state<int>{42};
auto x = s.xform(filter([](int a) { return a % 2 == 0; })).make();
CHECK(x.get() == 42);
}
TEST_CASE("xformed, one arg filter with value, direct")
{
auto s = state<int>{42};
auto x = s.filter([](int a) { return a % 2 == 0; }).make();
CHECK(x.get() == 42);
}
TEST_CASE("xformed, one arg filter without value")
{
auto s = state<int>{43};
auto x = s.xform(filter([](int a) { return a % 2 == 0; })).make();
CHECK(x.get() == 0);
}
struct non_default
{
int v = 0;
non_default() = delete;
non_default(int v_)
: v(v_)
{}
bool operator==(non_default x) const { return v == x.v; }
bool operator!=(non_default x) const { return v != x.v; }
};
TEST_CASE("xformed, one arg filter without value non default ctr")
{
auto s = state<non_default>{non_default{43}};
CHECK_THROWS_AS(
s.xform(filter([](non_default x) { return x.v % 2 == 0; })).make(),
no_value_error);
}
TEST_CASE(
"xformed",
"one arg filter without value non default ctr ok if first value passes")
{
auto s = state<non_default>{42};
auto x = s.xform(filter([](non_default a) { return a.v % 2 == 0; })).make();
s.set(non_default{43});
commit(s);
CHECK(x.get().v == 42); // old value still visible
s.set(non_default{44});
commit(s);
CHECK(x.get().v == 44); // new value passes
}
TEST_CASE("xformed, identity setter")
{
auto s = state<int>{42};
auto x = s.xform(zug::identity, zug::identity).make();
CHECK(x.get() == 42);
x.set(5);
CHECK(x.get() == 42);
CHECK(s.get() == 42);
commit(s);
CHECK(x.get() == 5);
CHECK(s.get() == 5);
}
TEST_CASE("xformed, identity setter two parents")
{
auto s1 = state<int>{42};
auto s2 = state<int>{12};
auto x = with(s1, s2).xform(zug::identity, zug::identity).make();
CHECK(x.get() == std::make_tuple(42, 12));
x.set(std::make_tuple(5, 12));
CHECK(x.get() == std::make_tuple(42, 12));
CHECK(s1.get() == 42);
CHECK(s2.get() == 12);
commit(s1, s2);
CHECK(x.get() == std::make_tuple(5, 12));
CHECK(s1.get() == 5);
CHECK(s2.get() == 12);
}
TEST_CASE("xformed, mapping")
{
auto st = make_state(0);
auto x = st.xform(map([](int a) { return a + 2; })).make();
CHECK(2 == x.get());
st.set(42);
commit(st);
CHECK(44 == x.get());
}
TEST_CASE("xformed, bidirectional")
{
auto st = make_state(0);
auto x = st.xform(map([](int a) { return a + 2; }),
map([](int a) { return a - 2; }))
.make();
CHECK(2 == x.get());
x.set(42);
commit(st);
CHECK(42 == x.get());
CHECK(40 == st.get());
}
TEST_CASE("xformed, bidirectional, direct")
{
auto st = make_state(0);
auto x =
st.map([](int a) { return a + 2; }, [](int a) { return a - 2; }).make();
CHECK(2 == x.get());
x.set(42);
commit(st);
CHECK(42 == x.get());
CHECK(40 == st.get());
}
TEST_CASE("atted, accessing keys of a container")
{
using map_t = std::map<std::string, int>;
auto st = make_state(map_t{});
auto x = atted("john", st);
CHECK(std::nullopt == x.get()); // not found => nullopt
x.set(12);
commit(st);
CHECK(std::nullopt == x.get()); // not magically created
CHECK(map_t{} == st.get());
st.set(map_t{{"john", 42}});
commit(st);
CHECK(42 == x.get()); // at last, I found it!
x.set(43);
commit(st);
CHECK(43 == x.get()); // happy birthday, john!
CHECK((map_t{{"john", 43}}) == st.get());
st.set(map_t{{}});
commit(st);
// Note: In previous versions, before implementing atted using lenses, we
// would have this assertion:
//
// CHECK(43 == x.get());
//
// This is, after removing the element, the cursor zooming on it would not
// update to show nullopt. This was considered useful to animate element
// removals (the view of the element still has a reference to the data).
// There are other ways to achieve this behavior though. We can, for
// example, have an at() lense that returns an optional, and then filter and
// dereference, to achieve, exactly the same behavior.
CHECK(std::nullopt == x.get());
}
TEST_CASE("atted, accessing keys of acontainer in version")
{
using map_t = std::map<std::string, int>;
auto st = make_state(map_t{});
auto x = atted("john", st);
CHECK(std::nullopt == x.get()); // not found => nullopt
st.set(map_t{{"john", 42}});
commit(st);
CHECK(42 == x.get()); // at last, I found it!
x.set(43); // Should not compile!
}
TEST_CASE("atted, atted from erased cursor")
{
using map_t = std::map<std::string, int>;
auto st = make_state(map_t{});
auto x = atted("john", cursor<map_t>{st});
st.set(map_t{{"john", 42}});
commit(st);
CHECK(42 == x.get());
}
namespace {
struct person
{
std::string name;
std::size_t age;
bool operator==(const person& x) const
{
return name == x.name && age == x.age;
}
bool operator!=(const person& x) const { return !(*this == x); }
};
} // namespace
TEST_CASE("atted, updates dont overwrite new data in complex scenario")
{
auto st = make_state(std::array<person, 2>{{{"john", 42}, {"emil", 2}}});
auto x1 = atted(0u, st);
auto x2 = atted(1u, st.xform(zug::identity, zug::identity).make());
auto x3 = atted(1u, st);
auto x4 = attred(&person::name, x2);
auto x5 = attred(&person::age, x3);
auto x6 = attred(&person::age, x1);
x6.set(43u);
x5.set(3u);
x4.set("emily");
commit(st);
CHECK(st.get() == (std::array<person, 2>{{{"john", 43}, {"emily", 3}}}));
CHECK(x6.get() == 43);
CHECK(x4.get() == "emily");
CHECK(x5.get() == 3);
}
TEST_CASE("atted, accesing attributes")
{
auto st = make_state(person{"john", 42});
auto x = attred(&person::age, st);
CHECK(42 == x.get());
x.set(43u);
commit(st);
CHECK(43 == x.get()); // Happy birthday John!
CHECK((person{"john", 43}) == st.get());
}
TEST_CASE("atted, accesing attributes in version")
{
auto st = make_state(person{"john", 42});
auto x = attred(&person::age, st);
CHECK(42 == x.get());
// Should not compile!
// x.set(43);
}
struct machine
{
std::string name;
std::size_t wheels;
};
bool operator==(const machine& a, const machine& b)
{
return std::tie(a.name, a.wheels) == std::tie(b.name, b.wheels);
}
bool operator!=(const machine& a, const machine& b)
{
return !operator==(a, b);
}
TEST_CASE("atted, modifying attributes of immutable")
{
auto st = make_state(machine{"car", 4});
auto x = attred(&machine::name, st);
auto y = attred(&machine::wheels, st);
y.set(3u);
commit(st);
CHECK(st.get() == (machine{"car", 3}));
CHECK(x.get() == "car");
CHECK(y.get() == 3);
x.set("tricar");
commit(st);
CHECK(st.get() == (machine{"tricar", 3}));
CHECK(x.get() == "tricar");
CHECK(y.get() == 3);
}
TEST_CASE("access member with square brackets")
{
auto st = make_state(machine{"car", 4});
auto x = st[&machine::name].make();
auto y = reader<machine>{st}[&machine::name].make();
x.set("tricar");
commit(st);
CHECK(st.get() == (machine{"tricar", 4}));
CHECK(x.get() == "tricar");
CHECK(y.get() == "tricar");
}
TEST_CASE("accessing keys with square brackets")
{
using map_t = std::map<std::string, int>;
auto st = make_state(map_t{{"john", 12}});
auto x = st["john"].make();
auto y = reader<map_t>{st}["john"].make();
x.set(42);
commit(st);
CHECK(42 == x.get());
CHECK(42 == y.get());
}
TEST_CASE("mixing keys from expressions")
{
using map_t = std::map<std::string, int>;
auto st = make_state(map_t{{"john", 12}, {"peter", 42}});
auto x = st["john"];
auto y = st["peter"];
auto z = with(std::move(x), std::move(y)).make();
CHECK(z.get() == std::make_tuple(12, 42));
}
TEST_CASE("mix cursor and reader")
{
auto c = make_constant(42);
auto i = make_state(std::string{"john"});
reader<std::tuple<int, std::string>> r = with(c, i);
}