|
| 1 | +// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp) |
| 2 | +// Distributed under the MIT license. See the LICENSE.md file in the project root for more information. |
| 3 | + |
| 4 | +using Xunit; |
| 5 | +using Stride.Core.Collections; |
| 6 | + |
| 7 | +namespace Stride.Core.Tests.Collections; |
| 8 | + |
| 9 | +public class ConstrainedListTests |
| 10 | +{ |
| 11 | + [Fact] |
| 12 | + public void Constructor_WithoutParameters_CreatesEmptyList() |
| 13 | + { |
| 14 | + var list = new ConstrainedList<int>(); |
| 15 | + |
| 16 | + Assert.Empty(list); |
| 17 | + Assert.True(list.ThrowException); |
| 18 | + } |
| 19 | + |
| 20 | + [Fact] |
| 21 | + public void Constructor_WithConstraint_SetsProperties() |
| 22 | + { |
| 23 | + var list = new ConstrainedList<int>((l, item) => item > 0, false, "Must be positive"); |
| 24 | + |
| 25 | + Assert.NotNull(list.Constraint); |
| 26 | + Assert.False(list.ThrowException); |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void Add_WithoutConstraint_AddsItem() |
| 31 | + { |
| 32 | + var list = new ConstrainedList<int>(); |
| 33 | + |
| 34 | + list.Add(1); |
| 35 | + list.Add(2); |
| 36 | + |
| 37 | + Assert.Equal(2, list.Count); |
| 38 | + Assert.Contains(1, list); |
| 39 | + Assert.Contains(2, list); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void Add_WithPassingConstraint_AddsItem() |
| 44 | + { |
| 45 | + var list = new ConstrainedList<int>((l, item) => item > 0); |
| 46 | + |
| 47 | + list.Add(5); |
| 48 | + list.Add(10); |
| 49 | + |
| 50 | + Assert.Equal(2, list.Count); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void Add_WithFailingConstraintAndThrowFalse_DoesNotAddItem() |
| 55 | + { |
| 56 | + var list = new ConstrainedList<int>((l, item) => item > 0, false); |
| 57 | + |
| 58 | + list.Add(-5); |
| 59 | + |
| 60 | + Assert.Empty(list); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void Add_WithFailingConstraintAndThrowTrue_ThrowsException() |
| 65 | + { |
| 66 | + var list = new ConstrainedList<int>((l, item) => item > 0, true, "Must be positive"); |
| 67 | + |
| 68 | + var ex = Assert.Throws<ArgumentException>(() => list.Add(-5)); |
| 69 | + Assert.Contains("Must be positive", ex.Message); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void Insert_WithPassingConstraint_InsertsItem() |
| 74 | + { |
| 75 | + var list = new ConstrainedList<int>((l, item) => item > 0); |
| 76 | + list.Add(10); |
| 77 | + list.Add(30); |
| 78 | + |
| 79 | + list.Insert(1, 20); |
| 80 | + |
| 81 | + Assert.Equal(3, list.Count); |
| 82 | + Assert.Equal(20, list[1]); |
| 83 | + } |
| 84 | + |
| 85 | + [Fact] |
| 86 | + public void Insert_WithFailingConstraint_DoesNotInsert() |
| 87 | + { |
| 88 | + var list = new ConstrainedList<int>((l, item) => item > 0, false); |
| 89 | + list.Add(10); |
| 90 | + |
| 91 | + list.Insert(0, -5); |
| 92 | + |
| 93 | + Assert.Single(list); |
| 94 | + } |
| 95 | + |
| 96 | + [Fact] |
| 97 | + public void Indexer_Set_WithPassingConstraint_UpdatesValue() |
| 98 | + { |
| 99 | + var list = new ConstrainedList<int>((l, item) => item > 0); |
| 100 | + list.Add(10); |
| 101 | + |
| 102 | + list[0] = 20; |
| 103 | + |
| 104 | + Assert.Equal(20, list[0]); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void Indexer_Set_WithFailingConstraint_DoesNotUpdate() |
| 109 | + { |
| 110 | + var list = new ConstrainedList<int>((l, item) => item > 0, false); |
| 111 | + list.Add(10); |
| 112 | + |
| 113 | + list[0] = -5; |
| 114 | + |
| 115 | + Assert.Equal(10, list[0]); |
| 116 | + } |
| 117 | + |
| 118 | + [Fact] |
| 119 | + public void Remove_RemovesItem() |
| 120 | + { |
| 121 | + var list = new ConstrainedList<int> { 1, 2, 3 }; |
| 122 | + |
| 123 | + var removed = list.Remove(2); |
| 124 | + |
| 125 | + Assert.True(removed); |
| 126 | + Assert.Equal(2, list.Count); |
| 127 | + Assert.DoesNotContain(2, list); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public void RemoveAt_RemovesItemAtIndex() |
| 132 | + { |
| 133 | + var list = new ConstrainedList<string> { "a", "b", "c" }; |
| 134 | + |
| 135 | + list.RemoveAt(1); |
| 136 | + |
| 137 | + Assert.Equal(2, list.Count); |
| 138 | + Assert.Equal("a", list[0]); |
| 139 | + Assert.Equal("c", list[1]); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void Clear_RemovesAllItems() |
| 144 | + { |
| 145 | + var list = new ConstrainedList<int> { 1, 2, 3, 4 }; |
| 146 | + |
| 147 | + list.Clear(); |
| 148 | + |
| 149 | + Assert.Empty(list); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public void Contains_ReturnsTrueForExistingItem() |
| 154 | + { |
| 155 | + var list = new ConstrainedList<string> { "a", "b", "c" }; |
| 156 | + |
| 157 | + Assert.Contains("b", list); |
| 158 | + Assert.DoesNotContain("z", list); |
| 159 | + } |
| 160 | + |
| 161 | + [Fact] |
| 162 | + public void IndexOf_ReturnsCorrectIndex() |
| 163 | + { |
| 164 | + var list = new ConstrainedList<int> { 10, 20, 30 }; |
| 165 | + |
| 166 | + Assert.Equal(1, list.IndexOf(20)); |
| 167 | + Assert.Equal(-1, list.IndexOf(99)); |
| 168 | + } |
| 169 | + |
| 170 | + [Fact] |
| 171 | + public void CopyTo_CopiesItemsToArray() |
| 172 | + { |
| 173 | + var list = new ConstrainedList<int> { 1, 2, 3 }; |
| 174 | + var array = new int[5]; |
| 175 | + |
| 176 | + list.CopyTo(array, 1); |
| 177 | + |
| 178 | + Assert.Equal(0, array[0]); |
| 179 | + Assert.Equal(1, array[1]); |
| 180 | + Assert.Equal(2, array[2]); |
| 181 | + Assert.Equal(3, array[3]); |
| 182 | + Assert.Equal(0, array[4]); |
| 183 | + } |
| 184 | + |
| 185 | + [Fact] |
| 186 | + public void GetEnumerator_IteratesAllItems() |
| 187 | + { |
| 188 | + var list = new ConstrainedList<int> { 1, 2, 3 }; |
| 189 | + var sum = 0; |
| 190 | + |
| 191 | + foreach (var item in list) |
| 192 | + { |
| 193 | + sum += item; |
| 194 | + } |
| 195 | + |
| 196 | + Assert.Equal(6, sum); |
| 197 | + } |
| 198 | + |
| 199 | + [Fact] |
| 200 | + public void IsReadOnly_ReturnsFalse() |
| 201 | + { |
| 202 | + var list = new ConstrainedList<int>(); |
| 203 | + |
| 204 | + Assert.False(list.IsReadOnly); |
| 205 | + } |
| 206 | + |
| 207 | + [Fact] |
| 208 | + public void Constraint_CanAccessListInPredicate() |
| 209 | + { |
| 210 | + var list = new ConstrainedList<int>((l, item) => l.Count < 3, false); |
| 211 | + |
| 212 | + list.Add(1); |
| 213 | + list.Add(2); |
| 214 | + list.Add(3); |
| 215 | + list.Add(4); // This should fail constraint (count already 3) |
| 216 | + |
| 217 | + Assert.Equal(3, list.Count); |
| 218 | + } |
| 219 | +} |
0 commit comments