-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmacrotest.nt
81 lines (62 loc) · 1.88 KB
/
macrotest.nt
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
module macrotest;
macro import std.macro.assert;
macro import std.macro.the;
macro import std.macro.listcomprehension;
macro import std.macro.once;
extern(C) void print(char[]);
int square(int) { return (the int) * the int; }
class A {
this() { }
}
struct S {
int i;
void inc() { i++; }
}
void listcotest() {
int[] arr = [2, 3, 4];
assert([a * 2 for a in arr] == [4, 6, 8]);
mut int i;
void foo() { i += 1; }
[foo() for x in arr];
assert(i == 3);
assert([any a > 2 for a in arr]);
assert(![any a > 4 for a in arr]);
// equivalent
assert([first true for a in arr where a > 2 else false]);
assert(![first true for a in arr where a > 4 else false]);
assert([first i for i, a in arr where a > 3 else -1] == 2);
auto list = [[1, 2], [3, 4], [5, 6]];
assert([first a[1] for a in list where a[0] > 1 else -1] == 4);
assert([all a > 1 for a in arr]);
assert(![all a > 2 for a in arr]);
assert([count a in arr where a > 2] == 2);
assert([sum a for a in arr where a > 2] == 7);
assert([min x for x in [2, 3, 4, 5] where x > 3] == 4);
assert([min x for x in [2, 3] where x > 3 base 0] == 0);
assert([max x for x in [2, 3, 4, 5] where x < 4] == 3);
assert([max x for x in [4, 5] where x < 4 base 0] == 0);
import std.math : abs;
assert([argmax(abs(x)) x for x in [2, -3]] == -3);
S[] arr = [S(0)];
[s.inc for s in arr];
// TODO
// assert(arr[0].i == 1);
}
class Object {
int i;
this(this.i) { }
}
void oncetest() {
Object make(int i) { return once new Object(i); }
Object o1 = make(1), o2 = make(2);
assert(o1.i == 1 && o1 is o2);
}
void main(string[] args) {
print("macrotest");
A a = new A;
assert(a); // this would not have worked before, because objects are not bools
listcotest();
oncetest();
print("- success");
assert(square(2) == 4);
}