Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
671 khor 1
#include <iostream>
680 janba 2
#include <GEL/Util/ResourceManager.h>
671 khor 3
 
4
using namespace std;
5
using namespace Util;
6
 
7
struct Blob
8
{
9
	double x;
10
 
11
	int step(int iter)
12
	{
13
		x += .5 / ( (iter-.75)*(iter-.25) );
14
		return 0;
15
	}
16
 
17
	~Blob() {cout << "Blob destroyed" << endl;}
18
};
19
 
20
 
21
double x=0;
22
int step(int iter)
23
{
24
	x += .5 / ( (iter-.75)*(iter-.25) );				
25
	return 0;
26
}
27
 
28
int (*step_fun)(int) = step;
29
 
30
 
31
ResourcePtr<Blob> g_ptr;
32
int main()
33
{
34
 	Blob b;
35
 	b.x = 312312;
36
 
37
	// Test 1: Register resource and verify that there is 1 resource
38
	register_static_resource<Blob>("myblob", &b);
39
	cout << "no resources (1) " << get_no_resources<Blob>() << endl;
40
 
41
	// Test 1.1: In debug mode, uncommenting the line below will make the 
42
	// program fail since a static resource cannot be removed by calling 
43
	// delete. In release mode, calling this function on a static resource
44
	// has no effect.
45
	// get_resource_ptr<Blob>("myblob").remove_when_unused();
46
 
47
 
48
	// Test 2:
49
	// Create a new resources and mark it as "remove when unused".
50
	// it goes away immediately since nothing points to it.
51
	cout << "Should print \"Blob destroyed\" below " << endl;
52
	register_dynamic_resource<Blob>("myblob2", new Blob).remove_when_unused();
53
	cout << "no resources (still 1) " << get_no_resources<Blob>() << endl;
54
 
55
	// Test 3:
56
	// Create a new resource and get a ResourcePointer. Mark as remove
57
	// when unused and verify that it goes away when ResourcePtr 
58
	// overwritten by a null resource pointer.
680 janba 59
	ResourcePtr<Blob> b_ptr_x = register_dynamic_resource<Blob>("myblob3", new Blob);
671 khor 60
	b_ptr_x.remove_when_unused();
61
	{
62
		ResourcePtr<Blob> gni;
63
		cout << "Should print \"Blob destroyed\" below " << endl;
64
		b_ptr_x = gni;
65
	}
66
	cout << "no resources (still 1) " << get_no_resources<Blob>() << endl;
67
 
68
	// Test 4:
69
	// Print value to ascertain the pointer is valid
70
	ResourcePtr<Blob> b_ptr = get_resource_ptr<Blob>("myblob");
71
	cout << "Value should be " << b.x << " : " << (b_ptr->x) << endl;
72
 
73
	// Test 5:
74
	// Print usage count
75
	cout << "Usage count (should be 1) " << b_ptr.usage() << endl;
76
 
77
	// Test 6:
78
	// Test that assigning to a new ResourcePtr increases usage count
79
	ResourcePtr<Blob> b_ptr2;
80
	b_ptr2 = get_resource_ptr<Blob>("myblob");
81
	cout << "Usage count (should be 2) : " << b_ptr.usage() << endl;
82
 
83
 
84
	// Test 7:
85
	// Create a local ResourcePtr
86
	{
87
		ResourcePtr<Blob> b_ptr3 = b_ptr;
88
		cout << "Usage count (should be 3) : " << b_ptr.usage() << endl;
89
	}
90
 
91
	// Test 8:
92
	// Test copy constructor
93
 	ResourcePtr<Blob> b_ptr4(b_ptr);
94
	cout << "Usage count (should be 3) : " << b_ptr.usage() << endl;
95
 
96
	// Test 9:
97
	// Get and throw away a resource ptr to test that usage is not increased.
98
	get_resource_ptr<Blob>("myblob");
99
	cout << "Usage count (should be 3) : " << b_ptr.usage() << endl;
100
 
101
	// Test 10:
102
	// Get raw pointer
103
	Blob* blob_raw_ptr = get_resource_ptr<Blob>("myblob").get_raw_ptr();
104
	cout << "Access with raw pointer " << blob_raw_ptr->x << endl;
105
	cout << "Usage count (should be 3) : " << b_ptr.usage() << endl;
106
 
107
	// --------------
108
 
109
	Blob b2;
110
	b2.x = 666;
111
 	register_static_resource<Blob>("my other blob", &b2);
112
 
113
	// Test 10:
114
	// Assign another resource to b_ptr4, check that usage count decreases
115
 	b_ptr4 = get_resource_ptr<Blob>("my other blob");
116
	cout << "Usage count (should be 2) : " << b_ptr.usage() << endl;
117
	cout << "Usage count (should be 1) : " << b_ptr4.usage() << endl;
118
 
119
	// Test 11:
120
	// Check that self assignment does not do anything
121
	b_ptr4 = b_ptr4;
122
	cout << "Usage count (should be 1) : " << b_ptr4.usage() << endl;
123
 
124
	// Test 12:
125
	// Assign the null resource ptr
126
 	ResourcePtr<Blob> gni;
127
	cout << "Usage count for 0 resource (should be -1) : " << gni.usage() 
128
			 << endl;
129
 
130
	// Test 13:
131
	// Test that overwriting a resource ptr with a new resource ptr to the
132
	// same resource does not increase usage count.
133
 	b_ptr4 = get_resource_ptr<Blob>("my other blob");
134
	cout << "Usage count (should be 1) : " << b_ptr4.usage() << endl;
135
 
136
	// Test 14:
137
	// Efficiency test. This test just computes an approximation of pi
138
	// using a function in a resource.
139
	b_ptr4->x = 0;
140
	for(int i=1;i<5000000;++i)
141
		{
142
			b_ptr4->step(i);
143
		}
144
	cout << " PI " << b_ptr4->x << endl;
145
 
146
// 	x = 0;
147
// 	for(int i=1;i<500000000;++i)
148
// 		{
149
// 			step(i);
150
// 		}
151
// 	cout << " PI " << x << endl;
152
 
153
	// Test 15: 
154
	// Global resource ptr test. If the relinquish line is commented out,
155
	// the program will give a warning upon termination and fail in debug
156
	// mode.
157
	g_ptr = b_ptr4;
158
	g_ptr.relinquish_resource();
159
 
160
	// At the end of the program, the two local blobs are destroyed.
161
	cout << "Two Blobs are now destroyed " << endl;
162
}
163