Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContextContainerTests.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using NUnit.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine.TestTools.Constraints;
  5. using Is = UnityEngine.TestTools.Constraints.Is;
  6. namespace UnityEngine.Rendering.Tests
  7. {
  8. class TestData : ContextItem
  9. {
  10. public int x;
  11. public float y;
  12. public bool z;
  13. public override void Reset()
  14. {
  15. x = 0;
  16. y = 0f;
  17. // Reuse z without clearing.
  18. }
  19. }
  20. class OtherTestData : ContextItem
  21. {
  22. public List<int> list = new();
  23. public override void Reset()
  24. {
  25. for (int i = 0; i < list.Count; i++)
  26. {
  27. list[i] = 0;
  28. }
  29. }
  30. }
  31. class ContextContainerTests
  32. {
  33. ContextContainer m_container = new();
  34. [OneTimeSetUp]
  35. public void FirstCreationOfData()
  36. {
  37. CreateMemoryAlloc();
  38. m_container.Dispose();
  39. }
  40. [SetUp]
  41. public void SetUp()
  42. {
  43. m_container.Dispose();
  44. TestData data = m_container.Create<TestData>();
  45. data.x = 0;
  46. data.y = 0f;
  47. data.z = false;
  48. OtherTestData other = m_container.Create<OtherTestData>();
  49. other.list.Clear();
  50. m_container.Dispose();
  51. }
  52. [Test]
  53. public void ReuseData()
  54. {
  55. {
  56. TestData data = CreateTestData();
  57. Assert.That(data.x, Is.EqualTo(0));
  58. Assert.That(data.y, Is.EqualTo(0f));
  59. Assert.That(data.z, Is.EqualTo(false));
  60. data.x = -23;
  61. data.y = 8.4f;
  62. data.z = true;
  63. m_container.Dispose();
  64. }
  65. {
  66. TestData data = CreateTestData();
  67. Assert.That(data.x, Is.EqualTo(0));
  68. Assert.That(data.y, Is.EqualTo(0f));
  69. Assert.That(data.z, Is.EqualTo(true));
  70. m_container.Dispose();
  71. }
  72. }
  73. [Test]
  74. public void GetData()
  75. {
  76. Assert.Throws<InvalidOperationException>(() => m_container.Get<TestData>());
  77. {
  78. TestData data = CreateTestData();
  79. Assert.That(data.x, Is.EqualTo(0));
  80. Assert.That(data.y, Is.EqualTo(0f));
  81. Assert.That(data.z, Is.EqualTo(false));
  82. data.x = -23;
  83. data.y = 8.4f;
  84. data.z = true;
  85. }
  86. {
  87. TestData data = m_container.Get<TestData>();
  88. Assert.That(data.x, Is.EqualTo(-23));
  89. Assert.That(data.y, Is.EqualTo(8.4f));
  90. Assert.That(data.z, Is.EqualTo(true));
  91. m_container.Dispose();
  92. }
  93. Assert.Throws<InvalidOperationException>(() => m_container.Get<TestData>());
  94. }
  95. [Test]
  96. public void ContainsData()
  97. {
  98. // Initially does not container TestData:
  99. Assert.False(m_container.Contains<TestData>());
  100. Assert.Throws<InvalidOperationException>(() => m_container.Get<TestData>());
  101. // Create TestData without error and the container should now contain TestData:
  102. Assert.DoesNotThrow(() => CreateTestData());
  103. Assert.True(m_container.Contains<TestData>());
  104. // Create again should throw error:
  105. Assert.Throws<InvalidOperationException>(() => CreateTestData());
  106. Assert.True(m_container.Contains<TestData>());
  107. Assert.DoesNotThrow(() => m_container.Get<TestData>());
  108. // Clear the container from created items, TestData should no longer be contained:
  109. m_container.Dispose();
  110. Assert.False(m_container.Contains<TestData>());
  111. Assert.Throws<InvalidOperationException>(() => m_container.Get<TestData>());
  112. // GetOrCreate should create should set contains to true without error:
  113. Assert.DoesNotThrow(() => GetOrCreateTestData());
  114. Assert.True(m_container.Contains<TestData>());
  115. Assert.DoesNotThrow(() => m_container.Get<TestData>());
  116. // GetOrCreate should get without error and contains should still be true:
  117. Assert.DoesNotThrow(() => GetOrCreateTestData());
  118. Assert.True(m_container.Contains<TestData>());
  119. Assert.DoesNotThrow(() => m_container.Get<TestData>());
  120. }
  121. [Test]
  122. public void ReuseList()
  123. {
  124. {
  125. OtherTestData other = CreateOtherTestData();
  126. Assert.That(other.list, Is.EqualTo(new List<int>()));
  127. other.list.Add(4);
  128. other.list.Add(8);
  129. other.list.Add(3);
  130. other.list.Add(-7);
  131. }
  132. {
  133. OtherTestData other = m_container.Get<OtherTestData>();
  134. Assert.That(other.list, Is.EqualTo(new List<int>() { 4, 8, 3, -7 }));
  135. m_container.Dispose();
  136. }
  137. {
  138. OtherTestData other = CreateOtherTestData();
  139. Assert.That(other.list, Is.EqualTo(new List<int>() { 0, 0, 0, 0 }));
  140. }
  141. }
  142. // Need to have one create location to avoid debug allocations.
  143. TestData CreateTestData()
  144. {
  145. return m_container.Create<TestData>();
  146. }
  147. // Need to have one create location to avoid debug allocations.
  148. TestData GetOrCreateTestData()
  149. {
  150. return m_container.GetOrCreate<TestData>();
  151. }
  152. // Need to have one create location to avoid debug allocations.
  153. OtherTestData CreateOtherTestData()
  154. {
  155. return m_container.Create<OtherTestData>();
  156. }
  157. #if CONTEXT_CONTAINER_ALLOCATOR_DEBUG
  158. public void CreateMemoryAlloc()
  159. {
  160. //Alloc for Create TestData
  161. Assert.That(() => { CreateTestData(); }, Is.AllocatingGCMemory());
  162. m_container.Dispose();
  163. Assert.That(() => { CreateTestData(); }, Is.Not.AllocatingGCMemory());
  164. m_container.Dispose();
  165. Assert.That(() => { GetOrCreateTestData(); }, Is.AllocatingGCMemory());
  166. m_container.Dispose();
  167. Assert.That(() => { GetOrCreateTestData(); }, Is.Not.AllocatingGCMemory());
  168. //Alloc for Create OtherTestData
  169. Assert.That(() => { CreateOtherTestData(); }, Is.AllocatingGCMemory());
  170. m_container.Dispose();
  171. Assert.That(() => { CreateOtherTestData(); }, Is.Not.AllocatingGCMemory());
  172. }
  173. #else
  174. public void CreateMemoryAlloc()
  175. {
  176. //TestData
  177. Assert.That(() => { CreateTestData(); }, Is.AllocatingGCMemory());
  178. m_container.Dispose();
  179. Assert.That(() => { GetOrCreateTestData(); }, Is.Not.AllocatingGCMemory());
  180. m_container.Dispose();
  181. Assert.That(() => { m_container.Create<TestData>(); }, Is.Not.AllocatingGCMemory());
  182. //OtherTestData
  183. Assert.That(() => { CreateOtherTestData(); }, Is.AllocatingGCMemory());
  184. m_container.Dispose();
  185. Assert.That(() => { m_container.Create<OtherTestData>(); }, Is.Not.AllocatingGCMemory());
  186. }
  187. #endif
  188. }
  189. }