Skip to content

Commit 91b5fb9

Browse files
committed
Added test
1 parent 6ac64f5 commit 91b5fb9

File tree

7 files changed

+239
-0
lines changed

7 files changed

+239
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build
2+
*.o

test/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
3+
project("DelegateTest")
4+
5+
file(GLOB SOURCE *.cpp)
6+
7+
add_executable("delegate_test" ${SOURCE})
8+
9+
target_include_directories("delegate_test" PUBLIC "../")
10+
target_compile_options("delegate_test" PUBLIC -O3 -std=c++1y -Wall -Wextra -pedantic -fno-rtti -fno-exceptions -fstack-protector-all)
11+
target_link_libraries("delegate_test" PUBLIC -nodefaultlibs -lc)

test/main.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "routines.hpp"
2+
3+
#include <stdio.h>
4+
5+
#define UNUSED(x) ((void) x)
6+
7+
8+
int main(int argc, char **argv)
9+
{
10+
UNUSED(argc);
11+
UNUSED(argv);
12+
13+
{
14+
C a1 = create_closure(3, "a1");
15+
printf("got a1\n\n");
16+
17+
C a2(create_closure(5, "a2"));
18+
printf("got a2\n\n");
19+
20+
C1 a3 = create_closure2(7, "a3");
21+
printf("got a3\n\n");
22+
23+
C a4(a1);
24+
printf("got a4\n\n");
25+
26+
C1 a5(a2);
27+
printf("got a5\n\n");
28+
29+
C1 a6;
30+
a6 = a5;
31+
printf("got a6\n\n");
32+
33+
C1 a7(create_closure2(7, "a7"));
34+
a7 = static_cast<C1&&>(a6);
35+
printf("got a7, killed a6\n\n");
36+
37+
printf("%d\n", a1(3)); // 3 + 3
38+
printf("%d\n", a2(4)); // 5 + 4
39+
printf("%d\n", a3(5)); // 7 + 5
40+
41+
printf("%d\n", a4(3)); // 3 + 3
42+
printf("%d\n", a5(4)); // 5 + 4
43+
44+
printf("%d\n", a7(7)); // 5 + 7
45+
46+
printf("\n\n");
47+
}
48+
49+
{
50+
printf("\n");
51+
Value vb1 {3, "b1"};
52+
Value vb2 {3, "b2"};
53+
Value vb3 {3, "b3"};
54+
D b1 = create_delegate(vb1);
55+
D b2 = create_delegate(vb2);
56+
D b3 = create_delegate(vb3);
57+
58+
printf("%d\n", b1(3));
59+
printf("%d\n", b2(4));
60+
printf("%d\n", b3(5));
61+
62+
}
63+
64+
return 0;
65+
}
66+
67+

test/object.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include "object.hpp"
2+
3+
#include <stdio.h>
4+
5+
static char* _print_obj(char *buf, const Object& obj)
6+
{
7+
sprintf(buf, "(name=%s, epoch=%d, alive=%i)", obj.name, obj.epoch, obj.alive);
8+
return buf;
9+
}
10+
11+
static void _print(const char *action, const Object& from, const Object& to)
12+
{
13+
char b1[512];
14+
char b2[512];
15+
printf("%s => %s -> %s\n", action, _print_obj(b1, from), _print_obj(b2, to));
16+
}
17+
18+
static void _print(const char *action, const Object& obj)
19+
{
20+
char b1[512];
21+
printf("%s => %s\n", action, _print_obj(b1, obj));
22+
}
23+
24+
25+
Object::Object(const char *name)
26+
: name(name), epoch(0), alive(ALIVE)
27+
{
28+
_print("Object", *this);
29+
}
30+
31+
Object::Object(const Object& other)
32+
: name(other.name), epoch(other.epoch + 1), alive(other.alive)
33+
{
34+
_print("Copy", other, *this);
35+
}
36+
37+
Object& Object::operator=(const Object& other)
38+
{
39+
if (this == &other) {
40+
return *this;
41+
}
42+
43+
name = other.name;
44+
epoch = other.epoch + 1;
45+
alive = other.alive;
46+
47+
_print("CopyOp", other, *this);
48+
49+
return *this;
50+
}
51+
52+
Object::Object(Object&& other)
53+
: name(other.name), epoch(other.epoch + 1), alive(true)
54+
{
55+
_print("Move", other, *this);
56+
other.alive = MOVED;
57+
}
58+
59+
Object& Object::operator=(Object&& other)
60+
{
61+
if (this == &other) {
62+
return *this;
63+
}
64+
65+
name = other.name;
66+
epoch = other.epoch + 1;
67+
alive = other.alive;
68+
69+
_print("MoveOp", other, *this);
70+
other.alive = MOVED;
71+
72+
return *this;
73+
}
74+
75+
Object::~Object()
76+
{
77+
_print("Destroy", *this);
78+
alive = DESTROYED;
79+
}
80+
81+
void Object::doit()
82+
{
83+
_print("DO", *this);
84+
}

test/object.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef OBJECT_HPP_
2+
#define OBJECT_HPP_
3+
4+
class Object
5+
{
6+
public:
7+
8+
enum state {
9+
DESTROYED = -1,
10+
MOVED = 0,
11+
ALIVE = 1
12+
};
13+
14+
Object(const char *name);
15+
16+
Object(const Object& other);
17+
Object& operator=(const Object& other);
18+
19+
Object(Object&& other);
20+
Object& operator=(Object&& other);
21+
22+
void doit();
23+
24+
~Object();
25+
26+
const char *name;
27+
unsigned epoch;
28+
int alive;
29+
30+
};
31+
32+
#endif

test/routines.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "routines.hpp"
2+
3+
4+
C create_closure(int init, const char *text)
5+
{
6+
Value _v {init, text};
7+
return C([v = static_cast<Value&&>(_v)](int item){return v.value + item;});
8+
}
9+
10+
C1 create_closure2(int init, const char *text)
11+
{
12+
Value _v {init, text};
13+
return C1([v = static_cast<Value&&>(_v)](int item){return v.value + item;});
14+
}
15+
16+
D create_delegate(Value &v)
17+
{
18+
D d = CB_DELEGATE_INIT(&v, &Value::apply);
19+
return d;
20+
}
21+

test/routines.hpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <closure.hpp>
2+
#include <delegate.hpp>
3+
#include "object.hpp"
4+
5+
using C = CB::StaticClosure<int (int), 64>;
6+
using C1 = CB::StaticClosure<int (int), 76>;
7+
using D = CB::Delegate<int (int)>;
8+
9+
struct Value {
10+
11+
int value;
12+
Object _garbage;
13+
14+
int apply(int other) const {
15+
return value + other;
16+
}
17+
18+
};
19+
20+
C create_closure(int init, const char *text);
21+
C1 create_closure2(int init, const char *text);
22+
D create_delegate(Value &v);

0 commit comments

Comments
 (0)