|
| 1 | +// Some tests adapted from Apache Harmony: |
| 2 | +// https://github.com/apache/harmony/blob/02970cb7227a335edd2c8457ebdde0195a735733/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/CollectionsTest.java |
| 3 | + |
| 4 | +using Lucene.Net.Attributes; |
| 5 | +using Lucene.Net.Util; |
| 6 | +using NUnit.Framework; |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | + |
| 10 | +#nullable enable |
| 11 | + |
| 12 | +namespace Lucene.Net.Support |
| 13 | +{ |
| 14 | + /* |
| 15 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 16 | + * contributor license agreements. See the NOTICE file distributed with |
| 17 | + * this work for additional information regarding copyright ownership. |
| 18 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 19 | + * (the "License"); you may not use this file except in compliance with |
| 20 | + * the License. You may obtain a copy of the License at |
| 21 | + * |
| 22 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | + * |
| 24 | + * Unless required by applicable law or agreed to in writing, software |
| 25 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 26 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | + * See the License for the specific language governing permissions and |
| 28 | + * limitations under the License. |
| 29 | + */ |
| 30 | + |
| 31 | + [TestFixture] |
| 32 | + public class TestCollections : LuceneTestCase |
| 33 | + { |
| 34 | + private List<object> ll = null!; // LUCENENET specific: was LinkedList in Harmony tests, !: will be initialized in SetUp |
| 35 | + |
| 36 | + // LUCENENET - omitting unused fields |
| 37 | + |
| 38 | + private static object[] objArray = LoadObjArray(); // LUCENENET - use static loader method instead of static ctor |
| 39 | + |
| 40 | + private static object[] LoadObjArray() |
| 41 | + { |
| 42 | + object[] objArray = new object[1000]; |
| 43 | + for (int i = 0; i < objArray.Length; i++) |
| 44 | + { |
| 45 | + objArray[i] = i; |
| 46 | + } |
| 47 | + |
| 48 | + return objArray; |
| 49 | + } |
| 50 | + |
| 51 | + [Test, LuceneNetSpecific] |
| 52 | + public void TestEmptyList() |
| 53 | + { |
| 54 | + IList<object> list = Collections.EmptyList<object>(); |
| 55 | + |
| 56 | + Assert.AreEqual(0, list.Count); |
| 57 | + Assert.IsTrue(list.IsReadOnly); |
| 58 | + Assert.Throws<NotSupportedException>(() => list.Add(new object())); |
| 59 | + |
| 60 | + IList<object> list2 = Collections.EmptyList<object>(); |
| 61 | + |
| 62 | + Assert.AreSame(list, list2); // ensure it does not allocate |
| 63 | + } |
| 64 | + |
| 65 | + [Test, LuceneNetSpecific] |
| 66 | + public void TestEmptyMap() |
| 67 | + { |
| 68 | + IDictionary<object, object> map = Collections.EmptyMap<object, object>(); |
| 69 | + |
| 70 | + Assert.AreEqual(0, map.Count); |
| 71 | + Assert.IsTrue(map.IsReadOnly); |
| 72 | + Assert.Throws<NotSupportedException>(() => map.Add(new object(), new object())); |
| 73 | + |
| 74 | + IDictionary<object, object> map2 = Collections.EmptyMap<object, object>(); |
| 75 | + |
| 76 | + Assert.AreSame(map, map2); // ensure it does not allocate |
| 77 | + } |
| 78 | + |
| 79 | + [Test, LuceneNetSpecific] |
| 80 | + public void TestEmptySet() |
| 81 | + { |
| 82 | + ISet<object> set = Collections.EmptySet<object>(); |
| 83 | + |
| 84 | + Assert.AreEqual(0, set.Count); |
| 85 | + Assert.IsTrue(set.IsReadOnly); |
| 86 | + Assert.Throws<NotSupportedException>(() => set.Add(new object())); |
| 87 | + |
| 88 | + ISet<object> set2 = Collections.EmptySet<object>(); |
| 89 | + |
| 90 | + Assert.AreSame(set, set2); // ensure it does not allocate |
| 91 | + } |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Adapted from Harmony test_reverseLjava_util_List() |
| 95 | + /// </summary> |
| 96 | + [Test] |
| 97 | + public void TestReverse() |
| 98 | + { |
| 99 | + // Test for method void java.util.Collections.reverse(java.util.List) |
| 100 | + try |
| 101 | + { |
| 102 | + Collections.Reverse<object>(null!); |
| 103 | + fail("Expected NullPointerException for null list parameter"); |
| 104 | + } |
| 105 | + catch (Exception e) when (e.IsNullPointerException()) |
| 106 | + { |
| 107 | + //Expected |
| 108 | + } |
| 109 | + |
| 110 | + Collections.Reverse(ll); |
| 111 | + using var i = ll.GetEnumerator(); |
| 112 | + int count = objArray.Length - 1; |
| 113 | + while (i.MoveNext()) |
| 114 | + { |
| 115 | + assertEquals("Failed to reverse collection", objArray[count], i.Current); |
| 116 | + --count; |
| 117 | + } |
| 118 | + |
| 119 | + var myList = new List<object?> |
| 120 | + { |
| 121 | + null, |
| 122 | + 20, |
| 123 | + }; |
| 124 | + Collections.Reverse(myList); |
| 125 | + assertEquals($"Did not reverse correctly--first element is: {myList[0]}", 20, myList[0]); |
| 126 | + assertNull($"Did not reverse correctly--second element is: {myList[1]}", myList[1]); |
| 127 | + } |
| 128 | + |
| 129 | + /// <summary> |
| 130 | + /// Adapted from Harmony test_reverseOrder() |
| 131 | + /// </summary> |
| 132 | + [Test] |
| 133 | + public void TestReverseOrder() { |
| 134 | + // Test for method IComparer<T> |
| 135 | + // Collections.ReverseOrder() |
| 136 | + // assumes no duplicates in ll |
| 137 | + IComparer<object> comp = Collections.ReverseOrder<object>(); |
| 138 | + var list2 = new List<object>(ll); // LUCENENET - was LinkedList in Harmony |
| 139 | + list2.Sort(comp); |
| 140 | + int llSize = ll.Count; |
| 141 | + for (int counter = 0; counter < llSize; counter++) |
| 142 | + { |
| 143 | + assertEquals("New comparator does not reverse sorting order", list2[llSize - counter - 1], ll[counter]); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + [Test, LuceneNetSpecific] |
| 148 | + public void TestReverseOrder_WithComparer() |
| 149 | + { |
| 150 | + IComparer<string> comp = Collections.ReverseOrder<string>(StringComparer.OrdinalIgnoreCase); |
| 151 | + var list = new List<string> { "B", "c", "a", "D" }; |
| 152 | + list.Sort(comp); |
| 153 | + Assert.AreEqual(4, list.Count); |
| 154 | + Assert.AreEqual("D", list[0]); |
| 155 | + Assert.AreEqual("c", list[1]); |
| 156 | + Assert.AreEqual("B", list[2]); |
| 157 | + Assert.AreEqual("a", list[3]); |
| 158 | + } |
| 159 | + |
| 160 | + [Test, LuceneNetSpecific] |
| 161 | + public void TestSingletonMap() |
| 162 | + { |
| 163 | + IDictionary<string, string> map = Collections.SingletonMap("key", "value"); |
| 164 | + |
| 165 | + Assert.AreEqual(1, map.Count); |
| 166 | + Assert.IsTrue(map.IsReadOnly); |
| 167 | + Assert.Throws<NotSupportedException>(() => map.Add("key2", "value2")); |
| 168 | + Assert.Throws<NotSupportedException>(() => map["key"] = "value2"); |
| 169 | + |
| 170 | + Assert.AreEqual("value", map["key"]); |
| 171 | + } |
| 172 | + |
| 173 | + [Test, LuceneNetSpecific] |
| 174 | + public void TestToString_Collection_Null() |
| 175 | + { |
| 176 | + Assert.AreEqual("null", Collections.ToString<object>(null!)); |
| 177 | + } |
| 178 | + |
| 179 | + [Test, LuceneNetSpecific] |
| 180 | + public void TestToString_Collection_Empty() |
| 181 | + { |
| 182 | + Assert.AreEqual("[]", Collections.ToString(new List<object>())); |
| 183 | + } |
| 184 | + |
| 185 | + [Test, LuceneNetSpecific] |
| 186 | + public void TestToString_Collection() |
| 187 | + { |
| 188 | + var list = new List<object?>(); |
| 189 | + list.Add(list); |
| 190 | + list.Add(1); |
| 191 | + list.Add('a'); |
| 192 | + list.Add(2.1); |
| 193 | + list.Add("xyz"); |
| 194 | + list.Add(new List<int> { 1, 2, 3 }); |
| 195 | + list.Add(null); |
| 196 | + |
| 197 | + Assert.AreEqual("[(this Collection), 1, a, 2.1, xyz, [1, 2, 3], null]", Collections.ToString(list)); |
| 198 | + } |
| 199 | + |
| 200 | + [Test, LuceneNetSpecific] |
| 201 | + public void TestToString_Dictionary() |
| 202 | + { |
| 203 | + var dict = new Dictionary<object, object?>() |
| 204 | + { |
| 205 | + { "key1", "value1" }, |
| 206 | + { "key2", 2 }, |
| 207 | + { "key3", 'a' }, |
| 208 | + { "key4", 3.1 }, |
| 209 | + { "key5", new List<int> { 1, 2, 3 } }, |
| 210 | + { "key6", null } |
| 211 | + }; |
| 212 | + |
| 213 | + Assert.AreEqual("{key1=value1, key2=2, key3=a, key4=3.1, key5=[1, 2, 3], key6=null}", Collections.ToString(dict)); |
| 214 | + } |
| 215 | + |
| 216 | + [Test, LuceneNetSpecific] |
| 217 | + public void TestToString_Object_Null() |
| 218 | + { |
| 219 | + Assert.AreEqual("null", Collections.ToString(null)); |
| 220 | + } |
| 221 | + |
| 222 | + [Test, LuceneNetSpecific] |
| 223 | + public void TestToString_Object() |
| 224 | + { |
| 225 | + Assert.AreEqual("1", Collections.ToString(1)); |
| 226 | + Assert.AreEqual("a", Collections.ToString('a')); |
| 227 | + Assert.AreEqual("2.1", Collections.ToString(2.1)); |
| 228 | + Assert.AreEqual("xyz", Collections.ToString("xyz")); |
| 229 | + Assert.AreEqual("[1, 2, 3]", Collections.ToString(new List<int> { 1, 2, 3 })); |
| 230 | + } |
| 231 | + |
| 232 | + [Test, LuceneNetSpecific] |
| 233 | + public void TestAsReadOnly_List() |
| 234 | + { |
| 235 | + var list = new List<object> { 1, 2, 3 }; |
| 236 | + IList<object> readOnlyList = Collections.AsReadOnly(list); |
| 237 | + |
| 238 | + Assert.AreEqual(3, readOnlyList.Count); |
| 239 | + Assert.IsTrue(readOnlyList.IsReadOnly); |
| 240 | + Assert.Throws<NotSupportedException>(() => readOnlyList.Add(4)); |
| 241 | + Assert.Throws<NotSupportedException>(() => readOnlyList[0] = 5); |
| 242 | + } |
| 243 | + |
| 244 | + [Test, LuceneNetSpecific] |
| 245 | + public void TestAsReadOnly_Dictionary() |
| 246 | + { |
| 247 | + var dict = new Dictionary<object, object> |
| 248 | + { |
| 249 | + { "key1", "value1" }, |
| 250 | + { "key2", 2 }, |
| 251 | + { "key3", 'a' } |
| 252 | + }; |
| 253 | + IDictionary<object, object> readOnlyDict = Collections.AsReadOnly(dict); |
| 254 | + |
| 255 | + Assert.AreEqual(3, readOnlyDict.Count); |
| 256 | + Assert.IsTrue(readOnlyDict.IsReadOnly); |
| 257 | + Assert.Throws<NotSupportedException>(() => readOnlyDict.Add("key4", 4)); |
| 258 | + Assert.Throws<NotSupportedException>(() => readOnlyDict["key1"] = "value2"); |
| 259 | + } |
| 260 | + |
| 261 | + public override void SetUp() |
| 262 | + { |
| 263 | + base.SetUp(); |
| 264 | + |
| 265 | + ll = new List<object>(); |
| 266 | + // LUCENENET - omitting unused fields |
| 267 | + |
| 268 | + for (int i = 0; i < objArray.Length; i++) |
| 269 | + { |
| 270 | + ll.Add(objArray[i]); |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | +} |
0 commit comments