File: zone.cc

package info (click to toggle)
msgpack 0.5.7-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,172 kB
  • sloc: cpp: 18,899; sh: 11,012; ansic: 2,580; makefile: 140
file content (78 lines) | stat: -rw-r--r-- 1,242 bytes parent folder | download | duplicates (3)
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
#include <msgpack.hpp>
#include <gtest/gtest.h>

TEST(zone, malloc)
{
	msgpack::zone z;
	char* buf1 = (char*)z.malloc(4);
	memcpy(buf1, "test", 4);
	char* buf2 = (char*)z.malloc(4);
	memcpy(buf2, "test", 4);
}


class myclass {
public:
	myclass() : num(0), str("default") { }

	myclass(int num, const std::string& str) :
		num(num), str(str) { }

	~myclass() { }

	int num;
	std::string str;

private:
	myclass(const myclass&);
};


TEST(zone, allocate)
{
	msgpack::zone z;
	myclass* m = z.allocate<myclass>();
	EXPECT_EQ(m->num, 0);
	EXPECT_EQ(m->str, "default");
}


TEST(zone, allocate_constructor)
{
	msgpack::zone z;
	myclass* m = z.allocate<myclass>(7, "msgpack");
	EXPECT_EQ(m->num, 7);
	EXPECT_EQ(m->str, "msgpack");
}


static void custom_finalizer_func(void* user)
{
	myclass* m = (myclass*)user;
	delete m;
}

TEST(zone, push_finalizer)
{
	msgpack::zone z;
	myclass* m = new myclass();
	z.push_finalizer(custom_finalizer_func, (void*)m);
}


TEST(zone, push_finalizer_auto_ptr)
{
	msgpack::zone z;
	std::auto_ptr<myclass> am(new myclass());
	z.push_finalizer(am);
}


TEST(zone, malloc_no_align)
{
	msgpack::zone z;
	char* buf1 = (char*)z.malloc_no_align(4);
	char* buf2 = (char*)z.malloc_no_align(4);
	EXPECT_EQ(buf1+4, buf2);
}