Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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