Skip to content

Commit eeb9f0d

Browse files
committed
Update tests
1 parent 1ce3815 commit eeb9f0d

58 files changed

Lines changed: 3325 additions & 123 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎tests/unit/lambda_as_field.cpp‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
2+
#include <assert.h>
3+
4+
template <typename F> struct Guard {
5+
F f;
6+
~Guard() { f(); }
7+
};
8+
9+
template <typename F> struct Holder {
10+
F f;
11+
int calls;
12+
int call(int x) {
13+
calls++;
14+
return f(x);
15+
}
16+
};
17+
18+
template <typename T> auto wrap(T fn) {
19+
return [fn](int x) { return fn(x) + 1; };
20+
}
21+
22+
int main() {
23+
int cleaned = 0;
24+
{
25+
Guard g{[&cleaned]() { cleaned++; }};
26+
assert(cleaned == 0);
27+
}
28+
assert(cleaned == 1);
29+
30+
int factor = 3;
31+
Holder h{[factor](int x) { return x * factor; }, 0};
32+
factor = 100;
33+
assert(h.call(2) == 6);
34+
assert(h.call(5) == 15);
35+
assert(h.calls == 2);
36+
37+
auto w = wrap([factor](int x) { return x * factor; });
38+
factor = 7;
39+
assert(w(2) == 201);
40+
41+
auto ww = wrap(w);
42+
assert(ww(2) == 202);
43+
44+
return 0;
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <assert.h>
2+
3+
template <typename F> int apply_int(F fn, int x) { return fn(x); }
4+
5+
template <typename F> double apply_double(F fn, double x) { return fn(x); }
6+
7+
template <typename F> int apply_twice(F fn, int x) { return fn(fn(x)); }
8+
9+
int main() {
10+
int factor = 3;
11+
auto scale = [factor](int x) { return x * factor; };
12+
assert(apply_twice(scale, 4) == 36);
13+
14+
assert(apply_int([](int x) { return -x; }, 9) == -9);
15+
16+
auto generic_scale = [factor](auto x) { return x * factor; };
17+
assert(apply_int(generic_scale, 4) == 12);
18+
assert(apply_double(generic_scale, 1.5) == 4.5);
19+
20+
assert(apply_int([](auto x) { return -x; }, 9) == -9);
21+
22+
auto offset = [factor]<typename T>(T x) { return x + factor; };
23+
assert(apply_int(offset, 4) == 7);
24+
assert(apply_double(offset, 1.5) == 4.5);
25+
26+
return 0;
27+
}

‎tests/unit/lambda_basic.cpp‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <assert.h>
2+
3+
int main() {
4+
auto zero = []() { return 42; };
5+
assert(zero() == 42);
6+
7+
auto one = [](int x) { return x + 1; };
8+
assert(one(1) == 2);
9+
10+
auto three = [](int x, int y, int z) { return x * 100 + y * 10 + z; };
11+
assert(three(1, 2, 3) == 123);
12+
13+
return 0;
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <assert.h>
2+
#include <utility>
3+
4+
struct Counted {
5+
int copies;
6+
int moves;
7+
Counted() : copies(0), moves(0) {}
8+
Counted(const Counted &o) : copies(o.copies + 1), moves(o.moves) {}
9+
Counted(Counted &&o) : copies(o.copies), moves(o.moves + 1) {}
10+
};
11+
12+
int main() {
13+
Counted c;
14+
auto f = [c]() { return c.copies * 10 + c.moves; };
15+
assert(f() == 10);
16+
17+
auto g = f;
18+
assert(g() == 20);
19+
assert(f() == 10);
20+
21+
auto h = std::move(f);
22+
assert(h() == 11);
23+
24+
int returned = [c]() { return c; }().copies;
25+
assert(returned == 2);
26+
27+
Counted arr[2];
28+
auto a = [arr]() { return arr[0].copies + arr[1].copies; };
29+
assert(a() == 2);
30+
31+
auto a2 = a;
32+
assert(a2() == 4);
33+
34+
return 0;
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <assert.h>
2+
3+
int main() {
4+
int a = 1;
5+
int b = 2;
6+
int c = 3;
7+
8+
auto by_value = [=](int x) { return a + b + c + x; };
9+
assert(by_value(10) == 16);
10+
a = 100;
11+
assert(by_value(10) == 16);
12+
13+
auto by_ref = [&](int x) { return a + b + c + x; };
14+
assert(by_ref(10) == 115);
15+
b = 200;
16+
assert(by_ref(10) == 313);
17+
18+
auto mixed = [=, &c](int x) {
19+
c += x;
20+
return a + b + c;
21+
};
22+
assert(mixed(1) == 100 + 200 + 4);
23+
assert(c == 4);
24+
25+
return 0;
26+
}

‎tests/unit/lambda_capture_pass.cpp‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎tests/unit/lambda_capture_ref.cpp‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <assert.h>
2+
#include <stddef.h>
3+
#include <stdint.h>
4+
5+
struct S {
6+
int x;
7+
int y;
8+
};
9+
10+
int main() {
11+
int base = 10;
12+
auto add_base = [&base](int x) { return x + base; };
13+
assert(add_base(5) == 15);
14+
base = 100;
15+
assert(add_base(5) == 105);
16+
17+
S s = {1, 2};
18+
auto sum = [&s]() { return s.x + s.y; };
19+
assert(sum() == 3);
20+
s.x = 50;
21+
assert(sum() == 52);
22+
23+
int counter = 0;
24+
auto bump = [&counter]() { counter++; };
25+
bump();
26+
bump();
27+
assert(counter == 2);
28+
29+
uint16_t arr[4] = {3, 1, 2, 0};
30+
auto swap = [&arr](size_t i, size_t j) {
31+
uint16_t t = arr[j];
32+
arr[j] = arr[i];
33+
arr[i] = t;
34+
};
35+
swap(0, 3);
36+
assert(arr[0] == 0);
37+
assert(arr[3] == 3);
38+
39+
return 0;
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <assert.h>
2+
3+
struct S {
4+
int n;
5+
int twice() const { return n * 2; }
6+
7+
int modify_copy() {
8+
auto f = [*this]() mutable {
9+
n += 10;
10+
return n;
11+
};
12+
int r = f();
13+
return r * 100 + n;
14+
}
15+
16+
int snapshot() {
17+
auto f = [*this]() { return twice(); };
18+
n = 99;
19+
return f();
20+
}
21+
22+
int mixed(int k) {
23+
auto f = [=, *this]() { return n + k; };
24+
n = 0;
25+
return f();
26+
}
27+
};
28+
29+
int main() {
30+
S s = {1};
31+
assert(s.modify_copy() == 1101);
32+
assert(s.n == 1);
33+
34+
assert(s.snapshot() == 2);
35+
assert(s.n == 99);
36+
37+
assert(s.mixed(1) == 100);
38+
assert(s.n == 0);
39+
40+
return 0;
41+
}

‎tests/unit/lambda_capture_this.cpp‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <assert.h>
2+
3+
struct S {
4+
int n;
5+
int step;
6+
void add(int k) { n += k; }
7+
int scaled() const { return n * step; }
8+
void bump(int by) {
9+
auto inc = [this](int k) { n += k; };
10+
inc(by);
11+
inc(by);
12+
}
13+
void bump_via_method(int by) {
14+
auto inc = [this](int k) { add(k); };
15+
inc(by);
16+
}
17+
int read_scaled() const {
18+
auto get = [this]() { return scaled(); };
19+
return get();
20+
}
21+
};
22+
23+
int main() {
24+
S s = {0, 2};
25+
s.bump(3);
26+
assert(s.n == 6);
27+
s.bump_via_method(4);
28+
assert(s.n == 10);
29+
assert(s.read_scaled() == 20);
30+
return 0;
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <assert.h>
2+
3+
struct S {
4+
int x;
5+
int y;
6+
};
7+
8+
int main() {
9+
int factor = 3;
10+
auto scale = [factor](int x) { return x * factor; };
11+
assert(scale(4) == 12);
12+
factor = 100;
13+
assert(scale(4) == 12);
14+
15+
int slot = 7;
16+
int *p = &slot;
17+
auto read_ptr = [p]() { return *p; };
18+
slot = 8;
19+
assert(read_ptr() == 8);
20+
21+
S s = {1, 2};
22+
auto sum = [s]() { return s.x + s.y; };
23+
s.x = 50;
24+
assert(sum() == 3);
25+
26+
int base = 10;
27+
auto shifted = [y = base + 1](int x) { return x + y; };
28+
assert(shifted(5) == 16);
29+
30+
return 0;
31+
}

0 commit comments

Comments
 (0)