Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 1 | // Licensed to the Apache Software Foundation(ASF) under one |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 2 | // or more contributor license agreements.See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership.The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | using System; |
| 19 | using System.Collections.Generic; |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 20 | using System.Linq; |
| 21 | using System.Security.Cryptography.Xml; |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 22 | using System.Text; |
| 23 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 24 | using Thrift.Collections; |
| 25 | |
| 26 | namespace Thrift.Tests.Collections |
| 27 | { |
| 28 | // ReSharper disable once InconsistentNaming |
| 29 | [TestClass] |
| 30 | public class TCollectionsTests |
| 31 | { |
| 32 | //TODO: Add tests for IEnumerable with objects and primitive values inside |
| 33 | |
| 34 | [TestMethod] |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 35 | public void TCollection_List_Equals_Primitive_Test() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 36 | { |
| 37 | var collection1 = new List<int> {1,2,3}; |
| 38 | var collection2 = new List<int> {1,2,3}; |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 39 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 40 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | [TestMethod] |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 44 | public void TCollection_List_Equals_Primitive_Different_Test() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 45 | { |
| 46 | var collection1 = new List<int> { 1, 2, 3 }; |
| 47 | var collection2 = new List<int> { 1, 2 }; |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 48 | Assert.IsFalse(TCollections.Equals(collection1, collection2)); |
| 49 | Assert.IsFalse(collection1.SequenceEqual(collection2)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 50 | |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 51 | collection2.Add(4); |
| 52 | Assert.IsFalse(TCollections.Equals(collection1, collection2)); |
| 53 | Assert.IsFalse(collection1.SequenceEqual(collection2)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | [TestMethod] |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 57 | public void TCollection_List_Equals_Objects_Test() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 58 | { |
| 59 | var collection1 = new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; |
| 60 | var collection2 = new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 61 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 62 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | [TestMethod] |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 66 | public void TCollection_List_List_Equals_Objects_Test() |
| 67 | { |
| 68 | var collection1 = new List<List<ExampleClass>> { new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } } }; |
| 69 | var collection2 = new List<List<ExampleClass>> { new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } } }; |
| 70 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 71 | Assert.IsFalse(collection1.SequenceEqual(collection2)); // SequenceEqual() calls Equals() of the inner list instead of SequenceEqual() |
| 72 | } |
| 73 | |
| 74 | [TestMethod] |
| 75 | public void TCollection_List_Equals_OneAndTheSameObject_Test() |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 76 | { |
| 77 | var collection1 = new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; |
| 78 | var collection2 = collection1; |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 79 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 80 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 83 | [TestMethod] |
| 84 | public void TCollection_Set_Equals_Primitive_Test() |
| 85 | { |
| 86 | var collection1 = new THashSet<int> {1,2,3}; |
| 87 | var collection2 = new THashSet<int> {1,2,3}; |
| 88 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 89 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
| 90 | } |
| 91 | |
| 92 | [TestMethod] |
| 93 | public void TCollection_Set_Equals_Primitive_Different_Test() |
| 94 | { |
| 95 | var collection1 = new THashSet<int> { 1, 2, 3 }; |
| 96 | var collection2 = new THashSet<int> { 1, 2 }; |
| 97 | Assert.IsFalse(TCollections.Equals(collection1, collection2)); |
| 98 | Assert.IsFalse(collection1.SequenceEqual(collection2)); |
| 99 | |
| 100 | collection2.Add(4); |
| 101 | Assert.IsFalse(TCollections.Equals(collection1, collection2)); |
| 102 | Assert.IsFalse(collection1.SequenceEqual(collection2)); |
| 103 | } |
| 104 | |
| 105 | [TestMethod] |
| 106 | public void TCollection_Set_Equals_Objects_Test() |
| 107 | { |
| 108 | var collection1 = new THashSet<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; |
| 109 | var collection2 = new THashSet<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; |
| 110 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 111 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
| 112 | } |
| 113 | |
| 114 | [TestMethod] |
| 115 | public void TCollection_Set_Set_Equals_Objects_Test() |
| 116 | { |
| 117 | var collection1 = new THashSet<THashSet<ExampleClass>> { new THashSet<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } } }; |
| 118 | var collection2 = new THashSet<THashSet<ExampleClass>> { new THashSet<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } } }; |
| 119 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 120 | Assert.IsFalse(collection1.SequenceEqual(collection2)); // SequenceEqual() calls Equals() of the inner list instead of SequenceEqual() |
| 121 | } |
| 122 | |
| 123 | [TestMethod] |
| 124 | public void TCollection_Set_Equals_OneAndTheSameObject_Test() |
| 125 | { |
| 126 | var collection1 = new THashSet<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } }; |
| 127 | var collection2 = collection1; // references to one and the same collection |
| 128 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 129 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | [TestMethod] |
| 134 | public void TCollection_Map_Equals_Primitive_Test() |
| 135 | { |
| 136 | var collection1 = new Dictionary<int, int> { [1] = 1, [2] = 2, [3] = 3 }; |
| 137 | var collection2 = new Dictionary<int, int> { [1] = 1, [2] = 2, [3] = 3 }; |
| 138 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 139 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
| 140 | } |
| 141 | |
| 142 | [TestMethod] |
| 143 | public void TCollection_Map_Equals_Primitive_Different_Test() |
| 144 | { |
| 145 | var collection1 = new Dictionary<int, int> { [1] = 1, [2] = 2, [3] = 3 }; |
| 146 | var collection2 = new Dictionary<int, int> { [1] = 1, [2] = 2 }; |
| 147 | Assert.IsFalse(TCollections.Equals(collection1, collection2)); |
| 148 | Assert.IsFalse(collection1.SequenceEqual(collection2)); |
| 149 | |
| 150 | collection2[3] = 3; |
| 151 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 152 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
| 153 | |
| 154 | collection2[3] = 4; |
| 155 | Assert.IsFalse(TCollections.Equals(collection1, collection2)); |
| 156 | } |
| 157 | |
| 158 | [TestMethod] |
| 159 | public void TCollection_Map_Equals_Objects_Test() |
| 160 | { |
| 161 | var collection1 = new Dictionary<int, ExampleClass> |
| 162 | { |
| 163 | [1] = new ExampleClass { X = 1 }, |
| 164 | [-1] = new ExampleClass { X = 2 } |
| 165 | }; |
| 166 | var collection2 = new Dictionary<int, ExampleClass> |
| 167 | { |
| 168 | [1] = new ExampleClass { X = 1 }, |
| 169 | [-1] = new ExampleClass { X = 2 } |
| 170 | }; |
| 171 | |
| 172 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 173 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
| 174 | } |
| 175 | |
| 176 | [TestMethod] |
| 177 | public void TCollection_Map_Map_Equals_Objects_Test() |
| 178 | { |
| 179 | var collection1 = new Dictionary<int, Dictionary<int, ExampleClass>> |
| 180 | { |
| 181 | [0] = new Dictionary<int, ExampleClass> |
| 182 | { |
| 183 | [1] = new ExampleClass { X = 1 }, |
| 184 | [-1] = new ExampleClass { X = 2 } |
| 185 | } |
| 186 | }; |
| 187 | var collection2 = new Dictionary<int, Dictionary<int, ExampleClass>> |
| 188 | { |
| 189 | [0] = new Dictionary<int, ExampleClass> |
| 190 | { |
| 191 | [1] = new ExampleClass { X = 1 }, |
| 192 | [-1] = new ExampleClass { X = 2 } |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 197 | Assert.IsFalse(collection1.SequenceEqual(collection2)); // SequenceEqual() calls Equals() of the inner list instead of SequenceEqual() |
| 198 | } |
| 199 | |
| 200 | [TestMethod] |
| 201 | public void TCollection_Map_Equals_OneAndTheSameObject_Test() |
| 202 | { |
| 203 | var collection1 = new Dictionary<int, ExampleClass> |
| 204 | { |
| 205 | [1] = new ExampleClass { X = 1 }, |
| 206 | [-1] = new ExampleClass { X = 2 } |
| 207 | }; |
| 208 | var collection2 = collection1; |
| 209 | Assert.IsTrue(TCollections.Equals(collection1, collection2)); |
| 210 | Assert.IsTrue(collection1.SequenceEqual(collection2)); |
| 211 | } |
| 212 | |
| 213 | |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 214 | private class ExampleClass |
| 215 | { |
| 216 | public int X { get; set; } |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 217 | |
| 218 | // all Thrift-generated classes override Equals(), we do just the same |
| 219 | public override bool Equals(object that) |
| 220 | { |
| 221 | if (!(that is ExampleClass other)) return false; |
| 222 | if (ReferenceEquals(this, other)) return true; |
| 223 | |
| 224 | return this.X == other.X; |
| 225 | } |
| 226 | |
| 227 | // overriding Equals() requires GetHashCode() as well |
| 228 | public override int GetHashCode() |
| 229 | { |
| 230 | int hashcode = 157; |
| 231 | unchecked |
| 232 | { |
| 233 | hashcode = (hashcode * 397) + X.GetHashCode(); |
| 234 | } |
| 235 | return hashcode; |
| 236 | } |
Jens Geyer | aa0c8b3 | 2019-01-28 23:27:45 +0100 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
Jens Geyer | affea7b | 2020-05-22 17:28:30 +0200 | [diff] [blame^] | 240 | |