Subversion Repositories gelsvn

Rev

Rev 56 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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