Subversion Repositories gelsvn

Rev

Rev 67 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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