PageRenderTime 34ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. //#include <tut/tut.h>
  27. #include "linden_common.h"
  28. #include "lltut.h"
  29. #include "llscriptresource.h"
  30. #include "llscriptresourceconsumer.h"
  31. #include "llscriptresourcepool.h"
  32. class TestConsumer : public LLScriptResourceConsumer
  33. {
  34. public:
  35. TestConsumer()
  36. : mUsedURLs(0)
  37. { }
  38. // LLScriptResourceConsumer interface:
  39. S32 getUsedPublicURLs() const
  40. {
  41. return mUsedURLs;
  42. }
  43. // Test details:
  44. S32 mUsedURLs;
  45. };
  46. namespace tut
  47. {
  48. class LLScriptResourceTestData
  49. {
  50. };
  51. typedef test_group<LLScriptResourceTestData> LLScriptResourceTestGroup;
  52. typedef LLScriptResourceTestGroup::object LLScriptResourceTestObject;
  53. LLScriptResourceTestGroup scriptResourceTestGroup("scriptResource");
  54. template<> template<>
  55. void LLScriptResourceTestObject::test<1>()
  56. {
  57. LLScriptResource resource;
  58. U32 total = 42;
  59. resource.setTotal(total);
  60. ensure_equals("Verify set/get total", resource.getTotal(), total);
  61. ensure_equals("Verify all resources are initially available",resource.getAvailable(),total);
  62. // Requesting too many, releasing non-allocated
  63. ensure("Request total + 1 resources should fail",!resource.request(total + 1));
  64. ensure_equals("Verify all resources available after failed request",resource.getAvailable(),total);
  65. ensure("Releasing resources when none allocated should fail",!resource.release());
  66. ensure_equals("All resources should be available after failed release",resource.getAvailable(),total);
  67. ensure("Request one resource", resource.request());
  68. ensure_equals("Verify available resources after successful request",resource.getAvailable(),total - 1);
  69. // Is this right? Or should we release all used resources if we try to release more than are currently used?
  70. ensure("Release more resources than allocated",!resource.release(2));
  71. ensure_equals("Verify resource availability after failed release",resource.getAvailable(),total - 1);
  72. ensure("Release a resource",resource.release());
  73. ensure_equals("Verify all resources available after successful release",resource.getAvailable(),total);
  74. }
  75. template<> template<>
  76. void LLScriptResourceTestObject::test<2>()
  77. {
  78. LLScriptResource resource;
  79. U32 total = 42;
  80. resource.setTotal(total);
  81. S32 resources_to_request = 30;
  82. ensure("Get multiple resources resources",resource.request(resources_to_request));
  83. ensure_equals("Verify available resources is correct after request of multiple resources",resource.getAvailable(), total - resources_to_request);
  84. S32 resources_to_release = (resources_to_request / 2);
  85. ensure("Release some resources",resource.release(resources_to_release));
  86. S32 expected_available = (total - resources_to_request + resources_to_release);
  87. ensure_equals("Verify available resources after release of some resources",resource.getAvailable(), expected_available);
  88. resources_to_release = (resources_to_request - resources_to_release);
  89. ensure("Release remaining resources",resource.release(resources_to_release));
  90. ensure_equals("Verify available resources after release of remaining resources",resource.getAvailable(), total);
  91. }
  92. template<> template<>
  93. void LLScriptResourceTestObject::test<3>()
  94. {
  95. LLScriptResource resource;
  96. U32 total = 42;
  97. resource.setTotal(total);
  98. ensure("Request all resources",resource.request(total));
  99. U32 low_total = 10;
  100. ensure("Release all resources",resource.release(total));
  101. ensure_equals("Verify all resources available after releasing",resource.getAvailable(),total);
  102. resource.setTotal(low_total);
  103. ensure_equals("Verify low total resources are available after set",resource.getAvailable(),low_total);
  104. }
  105. template<> template<>
  106. void LLScriptResourceTestObject::test<4>()
  107. {
  108. S32 big_resource_total = 100;
  109. S32 small_resource_total = 10;
  110. LLScriptResourcePool big_pool;
  111. big_pool.getPublicURLResource().setTotal(big_resource_total);
  112. LLScriptResourcePool small_pool;
  113. small_pool.getPublicURLResource().setTotal(small_resource_total);
  114. TestConsumer consumer;
  115. LLScriptResourcePool& initial_pool = consumer.getScriptResourcePool();
  116. ensure("Initial resource pool is 'null'.", (&initial_pool == &LLScriptResourcePool::null));
  117. consumer.switchScriptResourcePools(big_pool);
  118. LLScriptResourcePool& get_pool = consumer.getScriptResourcePool();
  119. ensure("Get resource that was set.", (&big_pool == &get_pool));
  120. ensure_equals("No public urls in use yet.", consumer.getUsedPublicURLs(),0);
  121. S32 request_urls = 5;
  122. consumer.mUsedURLs = request_urls;
  123. consumer.getScriptResourcePool().getPublicURLResource().request(request_urls);
  124. ensure_equals("Available urls on big_pool is 5 less than total.",
  125. big_pool.getPublicURLResource().getAvailable(), big_resource_total - request_urls);
  126. ensure("Switching from big pool to small pool",
  127. consumer.switchScriptResourcePools(small_pool));
  128. ensure_equals("All resources available to big pool again",
  129. big_pool.getPublicURLResource().getAvailable(), big_resource_total);
  130. ensure_equals("Available urls on small pool is 5 less than total.",
  131. small_pool.getPublicURLResource().getAvailable(), small_resource_total - request_urls);
  132. ensure("Switching from small pool to big pool",
  133. consumer.switchScriptResourcePools(big_pool));
  134. consumer.getScriptResourcePool().getPublicURLResource().release(request_urls);
  135. request_urls = 50; // Too many for the small_pool
  136. consumer.mUsedURLs = request_urls;
  137. consumer.getScriptResourcePool().getPublicURLResource().request(request_urls);
  138. // Verify big pool has them
  139. ensure_equals("Available urls on big pool is 50 less than total.",
  140. big_pool.getPublicURLResource().getAvailable(), big_resource_total - request_urls);
  141. // Verify can't switch to small_pool
  142. ensure("Switching to small pool with too many resources",
  143. !consumer.switchScriptResourcePools(small_pool));
  144. // Verify big pool still accounting for used resources
  145. ensure_equals("Available urls on big_pool is still 50 less than total.",
  146. big_pool.getPublicURLResource().getAvailable(), big_resource_total - request_urls);
  147. // Verify small pool still has all resources available.
  148. ensure_equals("All resources in small pool are still available.",
  149. small_pool.getPublicURLResource().getAvailable(), small_resource_total);
  150. }
  151. }