1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System.Collections.Generic; 6 using System.Linq; 7 using Xunit; 8 9 namespace System.Collections.Tests 10 { 11 /// <summary> 12 /// Contains tests that ensure the correctness of the List class. 13 /// </summary> 14 public abstract partial class List_Generic_Tests<T> : IList_Generic_Tests<T> 15 { 16 [Theory] 17 [MemberData(nameof(ValidCollectionSizes))] Reverse(int listLength)18 public void Reverse(int listLength) 19 { 20 List<T> list = GenericListFactory(listLength); 21 List<T> listBefore = list.ToList(); 22 23 list.Reverse(); 24 25 for (int i = 0; i < listBefore.Count; i++) 26 { 27 Assert.Equal(list[i], listBefore[listBefore.Count - (i + 1)]); //"Expect them to be the same." 28 } 29 } 30 31 [Theory] 32 [InlineData(10, 0, 10)] 33 [InlineData(10, 3, 3)] 34 [InlineData(10, 0, 10)] 35 [InlineData(10, 10, 0)] 36 [InlineData(10, 5, 5)] 37 [InlineData(10, 0, 5)] 38 [InlineData(10, 1, 9)] 39 [InlineData(10, 9, 1)] 40 [InlineData(10, 2, 8)] 41 [InlineData(10, 8, 2)] Reverse_int_int(int listLength, int index, int count)42 public void Reverse_int_int(int listLength, int index, int count) 43 { 44 List<T> list = GenericListFactory(listLength); 45 List<T> listBefore = list.ToList(); 46 47 list.Reverse(index, count); 48 49 for (int i = 0; i < index; i++) 50 { 51 Assert.Equal(list[i], listBefore[i]); //"Expect them to be the same." 52 } 53 54 int j = 0; 55 for (int i = index; i < index + count; i++) 56 { 57 Assert.Equal(list[i], listBefore[index + count - (j + 1)]); //"Expect them to be the same." 58 j++; 59 } 60 61 for (int i = index + count; i < listBefore.Count; i++) 62 { 63 Assert.Equal(list[i], listBefore[i]); //"Expect them to be the same." 64 } 65 } 66 67 [Theory] 68 [InlineData(10, 3, 3)] 69 [InlineData(10, 0, 10)] 70 [InlineData(10, 10, 0)] 71 [InlineData(10, 5, 5)] 72 [InlineData(10, 0, 5)] 73 [InlineData(10, 1, 9)] 74 [InlineData(10, 9, 1)] 75 [InlineData(10, 2, 8)] 76 [InlineData(10, 8, 2)] Reverse_RepeatedValues(int listLength, int index, int count)77 public void Reverse_RepeatedValues(int listLength, int index, int count) 78 { 79 List<T> list = GenericListFactory(1); 80 for (int i = 1; i < listLength; i++) 81 list.Add(list[0]); 82 List<T> listBefore = list.ToList(); 83 84 list.Reverse(index, count); 85 86 for (int i = 0; i < index; i++) 87 { 88 Assert.Equal(list[i], listBefore[i]); //"Expect them to be the same." 89 } 90 91 int j = 0; 92 for (int i = index; i < index + count; i++) 93 { 94 Assert.Equal(list[i], listBefore[index + count - (j + 1)]); //"Expect them to be the same." 95 j++; 96 } 97 98 for (int i = index + count; i < listBefore.Count; i++) 99 { 100 Assert.Equal(list[i], listBefore[i]); //"Expect them to be the same." 101 } 102 } 103 104 [Theory] 105 [MemberData(nameof(ValidCollectionSizes))] Reverse_InvalidParameters(int listLength)106 public void Reverse_InvalidParameters(int listLength) 107 { 108 if (listLength % 2 != 0) 109 listLength++; 110 List<T> list = GenericListFactory(listLength); 111 Tuple<int, int>[] InvalidParameters = new Tuple<int, int>[] 112 { 113 Tuple.Create(listLength ,1 ), 114 Tuple.Create(listLength+1 ,0 ), 115 Tuple.Create(listLength+1 ,1 ), 116 Tuple.Create(listLength ,2 ), 117 Tuple.Create(listLength/2 ,listLength/2+1), 118 Tuple.Create(listLength-1 ,2 ), 119 Tuple.Create(listLength-2 ,3 ), 120 Tuple.Create(1 ,listLength ), 121 Tuple.Create(0 ,listLength+1 ), 122 Tuple.Create(1 ,listLength+1 ), 123 Tuple.Create(2 ,listLength ), 124 Tuple.Create(listLength/2+1 ,listLength/2 ), 125 Tuple.Create(2 ,listLength-1 ), 126 Tuple.Create(3 ,listLength-2 ), 127 }; 128 129 Assert.All(InvalidParameters, invalidSet => 130 { 131 if (invalidSet.Item1 >= 0 && invalidSet.Item2 >= 0) 132 AssertExtensions.Throws<ArgumentException>(null, () => list.Reverse(invalidSet.Item1, invalidSet.Item2)); 133 }); 134 } 135 136 [Theory] 137 [MemberData(nameof(ValidCollectionSizes))] Reverse_NegativeParameters(int listLength)138 public void Reverse_NegativeParameters(int listLength) 139 { 140 if (listLength % 2 != 0) 141 listLength++; 142 List<T> list = GenericListFactory(listLength); 143 Tuple<int, int>[] InvalidParameters = new Tuple<int, int>[] 144 { 145 Tuple.Create(-1,-1), 146 Tuple.Create(-1, 0), 147 Tuple.Create(-1, 1), 148 Tuple.Create(-1, 2), 149 Tuple.Create(0 ,-1), 150 Tuple.Create(1 ,-1), 151 Tuple.Create(2 ,-1), 152 }; 153 154 Assert.All(InvalidParameters, invalidSet => 155 { 156 Assert.Throws<ArgumentOutOfRangeException>(() => list.Reverse(invalidSet.Item1, invalidSet.Item2)); 157 }); 158 } 159 } 160 } 161