PageRenderTime 23ms CodeModel.GetById 9ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/test/llscriptresource_tut.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 198 lines | 117 code | 47 blank | 34 comment | 2 complexity | 27cc657cdad64b9e78daab07da4d6f45 MD5 | raw file
  1/** 
  2 * @file llscriptresource_tut.cpp
  3 * @brief Test LLScriptResource
  4 *
  5 * $LicenseInfo:firstyear=2008&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27//#include <tut/tut.h>
 28#include "linden_common.h"
 29
 30#include "lltut.h"
 31
 32#include "llscriptresource.h"
 33#include "llscriptresourceconsumer.h"
 34#include "llscriptresourcepool.h"
 35
 36class TestConsumer : public LLScriptResourceConsumer
 37{
 38public:
 39	TestConsumer()
 40		: mUsedURLs(0)
 41	{ }
 42
 43	// LLScriptResourceConsumer interface:
 44	S32 getUsedPublicURLs() const
 45	{
 46		return mUsedURLs;
 47	}
 48
 49	// Test details:
 50	S32 mUsedURLs;
 51};
 52
 53
 54namespace tut
 55{
 56	class LLScriptResourceTestData
 57	{
 58	};
 59	
 60	typedef test_group<LLScriptResourceTestData> LLScriptResourceTestGroup;
 61	typedef LLScriptResourceTestGroup::object		LLScriptResourceTestObject;
 62	LLScriptResourceTestGroup scriptResourceTestGroup("scriptResource");
 63
 64	template<> template<>
 65	void LLScriptResourceTestObject::test<1>()
 66	{
 67		LLScriptResource resource;
 68		U32 total = 42;
 69		
 70		resource.setTotal(total);
 71		ensure_equals("Verify set/get total", resource.getTotal(), total);
 72		ensure_equals("Verify all resources are initially available",resource.getAvailable(),total);
 73
 74		// Requesting too many, releasing non-allocated
 75		ensure("Request total + 1 resources should fail",!resource.request(total + 1));
 76		ensure_equals("Verify all resources available after failed request",resource.getAvailable(),total);
 77
 78		ensure("Releasing resources when none allocated should fail",!resource.release());
 79		ensure_equals("All resources should be available after failed release",resource.getAvailable(),total);
 80
 81		ensure("Request one resource", resource.request());
 82		ensure_equals("Verify available resources after successful request",resource.getAvailable(),total - 1);
 83
 84		// Is this right?  Or should we release all used resources if we try to release more than are currently used?
 85		ensure("Release more resources than allocated",!resource.release(2));
 86		ensure_equals("Verify resource availability after failed release",resource.getAvailable(),total - 1);
 87
 88		ensure("Release a resource",resource.release());
 89		ensure_equals("Verify all resources available after successful release",resource.getAvailable(),total);
 90	}
 91
 92
 93	template<> template<>
 94	void LLScriptResourceTestObject::test<2>()
 95	{
 96		LLScriptResource resource;
 97		U32 total = 42;
 98
 99		resource.setTotal(total);
100
101		S32 resources_to_request = 30;
102		ensure("Get multiple resources resources",resource.request(resources_to_request));
103		ensure_equals("Verify available resources is correct after request of multiple resources",resource.getAvailable(), total - resources_to_request);
104
105		S32 resources_to_release = (resources_to_request / 2);
106		ensure("Release some resources",resource.release(resources_to_release));
107
108		S32 expected_available = (total - resources_to_request + resources_to_release);
109		ensure_equals("Verify available resources after release of some resources",resource.getAvailable(), expected_available);
110
111		resources_to_release = (resources_to_request - resources_to_release);
112		ensure("Release remaining resources",resource.release(resources_to_release));
113
114		ensure_equals("Verify available resources after release of remaining resources",resource.getAvailable(), total);
115	}
116
117	template<> template<>
118	void LLScriptResourceTestObject::test<3>()
119	{
120		LLScriptResource resource;
121
122		U32 total = 42;
123		resource.setTotal(total);
124
125		ensure("Request all resources",resource.request(total));
126
127		U32 low_total = 10;
128		ensure("Release all resources",resource.release(total));
129		ensure_equals("Verify all resources available after releasing",resource.getAvailable(),total);
130
131		resource.setTotal(low_total);
132		ensure_equals("Verify low total resources are available after set",resource.getAvailable(),low_total);
133	}
134	
135
136	template<> template<>
137	void LLScriptResourceTestObject::test<4>()
138	{
139		S32 big_resource_total = 100;
140		S32 small_resource_total = 10;
141		LLScriptResourcePool big_pool;
142		big_pool.getPublicURLResource().setTotal(big_resource_total);
143		LLScriptResourcePool small_pool;
144		small_pool.getPublicURLResource().setTotal(small_resource_total);
145
146		TestConsumer consumer;
147		LLScriptResourcePool& initial_pool = consumer.getScriptResourcePool();
148		ensure("Initial resource pool is 'null'.", (&initial_pool == &LLScriptResourcePool::null));
149
150		consumer.switchScriptResourcePools(big_pool);
151		LLScriptResourcePool& get_pool = consumer.getScriptResourcePool();
152		ensure("Get resource that was set.", (&big_pool == &get_pool));
153
154		ensure_equals("No public urls in use yet.", consumer.getUsedPublicURLs(),0);
155
156		S32 request_urls = 5;
157		consumer.mUsedURLs = request_urls;
158		consumer.getScriptResourcePool().getPublicURLResource().request(request_urls);
159
160		ensure_equals("Available urls on big_pool is 5 less than total.",
161			big_pool.getPublicURLResource().getAvailable(), big_resource_total - request_urls);
162
163		ensure("Switching from big pool to small pool",
164			   consumer.switchScriptResourcePools(small_pool));
165
166		ensure_equals("All resources available to big pool again",
167			big_pool.getPublicURLResource().getAvailable(), big_resource_total);
168
169		ensure_equals("Available urls on small pool is 5 less than total.",
170			small_pool.getPublicURLResource().getAvailable(), small_resource_total - request_urls);
171
172		ensure("Switching from small pool to big pool",
173			   consumer.switchScriptResourcePools(big_pool));
174
175		consumer.getScriptResourcePool().getPublicURLResource().release(request_urls);
176
177		request_urls = 50; // Too many for the small_pool
178
179		consumer.mUsedURLs = request_urls;
180		consumer.getScriptResourcePool().getPublicURLResource().request(request_urls);
181
182		// Verify big pool has them
183		ensure_equals("Available urls on big pool is 50 less than total.",
184			big_pool.getPublicURLResource().getAvailable(), big_resource_total - request_urls);
185
186		// Verify can't switch to small_pool
187		ensure("Switching to small pool with too many resources",
188			   !consumer.switchScriptResourcePools(small_pool));
189
190		// Verify big pool still accounting for used resources
191		ensure_equals("Available urls on big_pool is still 50 less than total.",
192			big_pool.getPublicURLResource().getAvailable(), big_resource_total - request_urls);
193		
194		// Verify small pool still has all resources available.
195		ensure_equals("All resources in small pool are still available.",
196			small_pool.getPublicURLResource().getAvailable(), small_resource_total);
197	}
198}