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