-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
64 lines (51 loc) · 1.12 KB
/
test.c
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
/**
* Tests for lambda.h
*
* Any copyright is dedicated to the Public Domain.
* https://creativecommons.org/publicdomain/zero/1.0/
*
* Compile with: `gcc test.c -o test.exe`
* And then run: `./test`
*
*/
#include <assert.h>
#include <stdio.h>
#include "lambda.h"
void test_default(void)
{
int (*max)(int, int) = lambda(int, (int a, int b),
{ return (a > b) ? a : b; });
int (*min)(int, int) = ʎ(int, (int a, int b),
{ return (a < b) ? a : b; });
printf("max(2, 5) = %d\n", max(2, 5));
assert(5 == max(2, 5));
printf("min(2, 5) = %d\n", min(2, 5));
assert(2 == min(2, 5));
}
void test_clousure(void)
{
typedef int (*adder_proc)(int);
adder_proc adder(int x)
{
// cast unnecessary, it just is here to improve readability
return (adder_proc)lambda(int, (int y), { return x + y; });
}
adder_proc adder5 = adder(5);
int res;
res = adder5(1);
printf("%d\n", res);
assert(6 == res);
res = adder5(1);
printf("%d\n", res);
assert(7 == res);
res = adder5(2);
printf("%d\n", res);
assert(9 == res);
}
int main(void)
{
test_default();
test_clousure();
test_generic_programming();
return 0;
}