|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: CommonExtendedObjectsTests |
| 4 | + * FILE: UnmanagedListTests.cs |
| 5 | + * PURPOSE: Tests for our UnmanagedList class, ensuring correct behavior of all operations including edge cases. |
| 6 | + * PROGRAMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +using System; |
| 10 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 11 | +using ExtendedSystemObjects; |
| 12 | + |
| 13 | +namespace CommonExtendedObjectsTests |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// General tests for the UnmanagedList class, covering construction, basic list operations (add, remove, insert), |
| 17 | + /// stack operations (push, pop, peek), and edge cases such as out-of-range access and disposal behavior. |
| 18 | + /// </summary> |
| 19 | + [TestClass] |
| 20 | + public class UnmanagedListTests |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// A simple unmanaged struct to test generic capabilities |
| 24 | + /// </summary> |
| 25 | + private struct PointD |
| 26 | + { |
| 27 | + public double X; |
| 28 | + public double Y; |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Constructors the initializes correctly. |
| 33 | + /// </summary> |
| 34 | + [TestMethod] |
| 35 | + public void Constructor_InitializesCorrectly() |
| 36 | + { |
| 37 | + // Arrange & Act |
| 38 | + using var list = new UnmanagedList<int>(10); |
| 39 | + |
| 40 | + // Assert |
| 41 | + Assert.AreEqual(0, list.Length); |
| 42 | + Assert.AreEqual(10, list.Capacity); |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary> |
| 46 | + /// Adds the increases length and expands capacity. |
| 47 | + /// </summary> |
| 48 | + [TestMethod] |
| 49 | + public void Add_IncreasesLengthAndExpandsCapacity() |
| 50 | + { |
| 51 | + // Arrange |
| 52 | + using var list = new UnmanagedList<int>(2); |
| 53 | + |
| 54 | + // Act |
| 55 | + list.Add(10); |
| 56 | + list.Add(20); |
| 57 | + list.Add(30); // Should trigger EnsureCapacity |
| 58 | + |
| 59 | + // Assert |
| 60 | + Assert.AreEqual(3, list.Length); |
| 61 | + Assert.IsTrue(list.Capacity >= 3); |
| 62 | + Assert.AreEqual(10, list[0]); |
| 63 | + Assert.AreEqual(20, list[1]); |
| 64 | + Assert.AreEqual(30, list[2]); |
| 65 | + } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Removes at shifts elements correctly. |
| 69 | + /// </summary> |
| 70 | + [TestMethod] |
| 71 | + public void RemoveAt_ShiftsElementsCorrectly() |
| 72 | + { |
| 73 | + // Arrange |
| 74 | + using var list = new UnmanagedList<int>(5); |
| 75 | + list.Add(1); |
| 76 | + list.Add(2); |
| 77 | + list.Add(3); |
| 78 | + list.Add(4); |
| 79 | + |
| 80 | + // Act |
| 81 | + list.RemoveAt(1, 2); // Remove '2' and '3' |
| 82 | + |
| 83 | + // Assert |
| 84 | + Assert.AreEqual(2, list.Length); |
| 85 | + Assert.AreEqual(1, list[0]); |
| 86 | + Assert.AreEqual(4, list[1]); |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Inserts at shifts elements and fills correctly. |
| 91 | + /// </summary> |
| 92 | + [TestMethod] |
| 93 | + public void InsertAt_ShiftsElementsAndFillsCorrectly() |
| 94 | + { |
| 95 | + // Arrange |
| 96 | + using var list = new UnmanagedList<int>(5); |
| 97 | + list.Add(10); |
| 98 | + list.Add(40); |
| 99 | + |
| 100 | + // Act |
| 101 | + list.InsertAt(1, 20, 2); // Insert two '20's at index 1 |
| 102 | + |
| 103 | + // Assert |
| 104 | + Assert.AreEqual(4, list.Length); |
| 105 | + Assert.AreEqual(10, list[0]); |
| 106 | + Assert.AreEqual(20, list[1]); |
| 107 | + Assert.AreEqual(20, list[2]); |
| 108 | + Assert.AreEqual(40, list[3]); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Stacks the operations push pop peek work as expected. |
| 113 | + /// </summary> |
| 114 | + [TestMethod] |
| 115 | + public void StackOperations_PushPopPeek_WorkAsExpected() |
| 116 | + { |
| 117 | + // Arrange |
| 118 | + using var list = new UnmanagedList<int>(5); |
| 119 | + |
| 120 | + // Act & Assert |
| 121 | + list.Push(100); |
| 122 | + list.Push(200); |
| 123 | + |
| 124 | + Assert.AreEqual(200, list.Peek()); |
| 125 | + Assert.AreEqual(2, list.Length); |
| 126 | + |
| 127 | + var popped = list.Pop(); |
| 128 | + Assert.AreEqual(200, popped); |
| 129 | + Assert.AreEqual(1, list.Length); |
| 130 | + Assert.AreEqual(100, list.Peek()); |
| 131 | + } |
| 132 | + |
| 133 | + /// <summary> |
| 134 | + /// Pops the on empty list throws invalid operation exception. |
| 135 | + /// </summary> |
| 136 | + [TestMethod] |
| 137 | + public void Pop_OnEmptyList_ThrowsInvalidOperationException() |
| 138 | + { |
| 139 | + using var list = new UnmanagedList<int>(); |
| 140 | + Assert.ThrowsException<InvalidOperationException>(() => list.Pop()); |
| 141 | + } |
| 142 | + |
| 143 | + /// <summary> |
| 144 | + /// Structures the support works with custom unmanaged types. |
| 145 | + /// </summary> |
| 146 | + [TestMethod] |
| 147 | + public void StructSupport_WorksWithCustomUnmanagedTypes() |
| 148 | + { |
| 149 | + // Arrange |
| 150 | + using var list = new UnmanagedList<PointD>(2); |
| 151 | + var p1 = new PointD { X = 1.5, Y = 2.5 }; |
| 152 | + var p2 = new PointD { X = 3.0, Y = 4.0 }; |
| 153 | + |
| 154 | + // Act |
| 155 | + list.Add(p1); |
| 156 | + list.Add(p2); |
| 157 | + |
| 158 | + // Assert |
| 159 | + Assert.AreEqual(2, list.Length); |
| 160 | + Assert.AreEqual(1.5, list[0].X); |
| 161 | + Assert.AreEqual(4.0, list[1].Y); |
| 162 | + } |
| 163 | + |
| 164 | + /// <summary> |
| 165 | + /// Tries the get returns true for valid index false for invalid. |
| 166 | + /// </summary> |
| 167 | + [TestMethod] |
| 168 | + public void TryGet_ReturnsTrueForValidIndex_FalseForInvalid() |
| 169 | + { |
| 170 | + using var list = new UnmanagedList<int>(5); |
| 171 | + list.Add(42); |
| 172 | + |
| 173 | + Assert.IsTrue(list.TryGet(0, out var val)); |
| 174 | + Assert.AreEqual(42, val); |
| 175 | + |
| 176 | + Assert.IsFalse(list.TryGet(1, out _)); |
| 177 | + Assert.IsFalse(list.TryGet(-1, out _)); |
| 178 | + } |
| 179 | + |
| 180 | + /// <summary> |
| 181 | + /// Ranges the indexer returns correct span. |
| 182 | + /// </summary> |
| 183 | + [TestMethod] |
| 184 | + public void RangeIndexer_ReturnsCorrectSpan() |
| 185 | + { |
| 186 | + // Arrange |
| 187 | + using var list = new UnmanagedList<int>(5); |
| 188 | + for (var i = 0; i < 5; i++) list.Add(i * 10); // 0, 10, 20, 30, 40 |
| 189 | + |
| 190 | + // Act |
| 191 | + var span = list[1..4]; // Should be 10, 20, 30 |
| 192 | + |
| 193 | + // Assert |
| 194 | + Assert.AreEqual(3, span.Length); |
| 195 | + Assert.AreEqual(10, span[0]); |
| 196 | + Assert.AreEqual(30, span[2]); |
| 197 | + } |
| 198 | + |
| 199 | + /// <summary> |
| 200 | + /// Disposes the prevents further access. |
| 201 | + /// </summary> |
| 202 | + [TestMethod] |
| 203 | + public void Dispose_PreventsFurtherAccess() |
| 204 | + { |
| 205 | + // Arrange |
| 206 | + var list = new UnmanagedList<int>(5); |
| 207 | + list.Add(1); |
| 208 | + |
| 209 | + // Act |
| 210 | + list.Dispose(); |
| 211 | + |
| 212 | + // Assert |
| 213 | + Assert.ThrowsException<ObjectDisposedException>(() => list.Add(2)); |
| 214 | + Assert.ThrowsException<ObjectDisposedException>(() => { var x = list[0]; }); |
| 215 | + Assert.ThrowsException<ObjectDisposedException>(() => list.Clear()); |
| 216 | + } |
| 217 | + |
| 218 | + /// <summary> |
| 219 | + /// Clears the resets length but keeps capacity. |
| 220 | + /// </summary> |
| 221 | + [TestMethod] |
| 222 | + public void Clear_ResetsLengthButKeepsCapacity() |
| 223 | + { |
| 224 | + // Arrange |
| 225 | + using var list = new UnmanagedList<int>(10); |
| 226 | + list.Add(1); |
| 227 | + list.Add(2); |
| 228 | + var initialCapacity = list.Capacity; |
| 229 | + |
| 230 | + // Act |
| 231 | + list.Clear(); |
| 232 | + |
| 233 | + // Assert |
| 234 | + Assert.AreEqual(0, list.Length); |
| 235 | + Assert.AreEqual(initialCapacity, list.Capacity); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments