blob: 061032ae0a3422409026a2308501b233d69d9bd9 [file] [log] [blame]
Jens Geyeraffea7b2020-05-22 17:28:30 +02001// Licensed to the Apache Software Foundation(ASF) under one
Jens Geyeraa0c8b32019-01-28 23:27:45 +01002// 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
18using System;
19using System.Collections.Generic;
Jens Geyeraffea7b2020-05-22 17:28:30 +020020using System.Linq;
21using System.Security.Cryptography.Xml;
Jens Geyeraa0c8b32019-01-28 23:27:45 +010022using System.Text;
23using Microsoft.VisualStudio.TestTools.UnitTesting;
24using Thrift.Collections;
25
26namespace 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 Geyeraffea7b2020-05-22 17:28:30 +020035 public void TCollection_List_Equals_Primitive_Test()
Jens Geyeraa0c8b32019-01-28 23:27:45 +010036 {
37 var collection1 = new List<int> {1,2,3};
38 var collection2 = new List<int> {1,2,3};
Jens Geyeraffea7b2020-05-22 17:28:30 +020039 Assert.IsTrue(TCollections.Equals(collection1, collection2));
40 Assert.IsTrue(collection1.SequenceEqual(collection2));
Jens Geyeraa0c8b32019-01-28 23:27:45 +010041 }
42
43 [TestMethod]
Jens Geyeraffea7b2020-05-22 17:28:30 +020044 public void TCollection_List_Equals_Primitive_Different_Test()
Jens Geyeraa0c8b32019-01-28 23:27:45 +010045 {
46 var collection1 = new List<int> { 1, 2, 3 };
47 var collection2 = new List<int> { 1, 2 };
Jens Geyeraffea7b2020-05-22 17:28:30 +020048 Assert.IsFalse(TCollections.Equals(collection1, collection2));
49 Assert.IsFalse(collection1.SequenceEqual(collection2));
Jens Geyeraa0c8b32019-01-28 23:27:45 +010050
Jens Geyeraffea7b2020-05-22 17:28:30 +020051 collection2.Add(4);
52 Assert.IsFalse(TCollections.Equals(collection1, collection2));
53 Assert.IsFalse(collection1.SequenceEqual(collection2));
Jens Geyeraa0c8b32019-01-28 23:27:45 +010054 }
55
56 [TestMethod]
Jens Geyeraffea7b2020-05-22 17:28:30 +020057 public void TCollection_List_Equals_Objects_Test()
Jens Geyeraa0c8b32019-01-28 23:27:45 +010058 {
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 Geyeraffea7b2020-05-22 17:28:30 +020061 Assert.IsTrue(TCollections.Equals(collection1, collection2));
62 Assert.IsTrue(collection1.SequenceEqual(collection2));
Jens Geyeraa0c8b32019-01-28 23:27:45 +010063 }
64
65 [TestMethod]
Jens Geyeraffea7b2020-05-22 17:28:30 +020066 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 Geyeraa0c8b32019-01-28 23:27:45 +010076 {
77 var collection1 = new List<ExampleClass> { new ExampleClass { X = 1 }, new ExampleClass { X = 2 } };
78 var collection2 = collection1;
Jens Geyeraffea7b2020-05-22 17:28:30 +020079 Assert.IsTrue(TCollections.Equals(collection1, collection2));
80 Assert.IsTrue(collection1.SequenceEqual(collection2));
Jens Geyeraa0c8b32019-01-28 23:27:45 +010081 }
82
Jens Geyeraffea7b2020-05-22 17:28:30 +020083 [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 Geyeraa0c8b32019-01-28 23:27:45 +0100214 private class ExampleClass
215 {
216 public int X { get; set; }
Jens Geyeraffea7b2020-05-22 17:28:30 +0200217
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 Geyeraa0c8b32019-01-28 23:27:45 +0100237 }
238 }
239}
Jens Geyeraffea7b2020-05-22 17:28:30 +0200240