This repository was archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollectionUtilTests.cs
161 lines (143 loc) · 6.52 KB
/
CollectionUtilTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* ====================
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of the Common Development
* and Distribution License("CDDL") (the "License"). You may not use this file
* except in compliance with the License.
*
* You can obtain a copy of the License at
* http://opensource.org/licenses/cddl1.php
* See the License for the specific language governing permissions and limitations
* under the License.
*
* When distributing the Covered Code, include this CDDL Header Notice in each file
* and include the License file at http://opensource.org/licenses/cddl1.php.
* If applicable, add the following below this CDDL Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
* ====================
*/
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Org.IdentityConnectors.Common;
namespace FrameworkTests
{
[TestFixture]
public class CollectionUtilTests
{
[Test]
public void TestEquals()
{
Assert.IsTrue(CollectionUtil.Equals(null, null));
Assert.IsFalse(CollectionUtil.Equals(null, "str"));
Assert.IsTrue(CollectionUtil.Equals("str", "str"));
byte[] arr1 = new byte[] { 1, 2, 3 };
byte[] arr2 = new byte[] { 1, 2, 3 };
byte[] arr3 = new byte[] { 1, 2, 4 };
byte[] arr4 = new byte[] { 1, 2 };
int[] arr5 = new int[] { 1, 2, 3 };
Assert.IsTrue(CollectionUtil.Equals(arr1, arr2));
Assert.IsFalse(CollectionUtil.Equals(arr2, arr3));
Assert.IsFalse(CollectionUtil.Equals(arr2, arr4));
Assert.IsFalse(CollectionUtil.Equals(arr2, arr5));
IList<byte[]> list1 = new List<byte[]>();
IList<byte[]> list2 = new List<byte[]>();
list1.Add(arr1);
list2.Add(arr2);
Assert.IsTrue(CollectionUtil.Equals(list1, list2));
list2.Add(arr2);
Assert.IsFalse(CollectionUtil.Equals(list1, list2));
list1.Add(arr1);
Assert.IsTrue(CollectionUtil.Equals(list1, list2));
list1.Add(arr1);
list2.Add(arr3);
Assert.IsFalse(CollectionUtil.Equals(list1, list2));
IDictionary<String, byte[]> map1 = new Dictionary<String, byte[]>();
IDictionary<String, byte[]> map2 = new Dictionary<String, byte[]>();
map1["key1"] = arr1;
map2["key1"] = arr2;
Assert.IsTrue(CollectionUtil.Equals(map1, map2));
map2["key2"] = arr2;
Assert.IsFalse(CollectionUtil.Equals(map1, map2));
map1["key2"] = arr1;
Assert.IsTrue(CollectionUtil.Equals(map1, map2));
map1["key2"] = arr3;
Assert.IsFalse(CollectionUtil.Equals(map1, map2));
ICollection<String> set1 = new HashSet<String>();
ICollection<String> set2 = new HashSet<String>();
set1.Add("val");
set2.Add("val");
Assert.IsTrue(CollectionUtil.Equals(set1, set2));
set2.Add("val2");
Assert.IsFalse(CollectionUtil.Equals(set1, set2));
set1.Add("val2");
Assert.IsTrue(CollectionUtil.Equals(set1, set2));
}
[Test]
public void TestHashCode()
{
Assert.AreEqual(0, CollectionUtil.GetHashCode(null));
Assert.AreEqual("str".GetHashCode(), CollectionUtil.GetHashCode("str"));
byte[] arr1 = new byte[] { 1, 2, 3 };
byte[] arr2 = new byte[] { 1, 2, 3 };
byte[] arr3 = new byte[] { 1, 2, 4 };
byte[] arr4 = new byte[] { 1, 2 };
int[] arr5 = new int[] { 1, 2, 3 };
Assert.AreEqual(CollectionUtil.GetHashCode(arr1),
CollectionUtil.GetHashCode(arr2));
Assert.IsFalse(CollectionUtil.GetHashCode(arr2) ==
CollectionUtil.GetHashCode(arr3));
Assert.IsFalse(CollectionUtil.GetHashCode(arr2) ==
CollectionUtil.GetHashCode(arr4));
Assert.IsTrue(CollectionUtil.GetHashCode(arr2) ==
CollectionUtil.GetHashCode(arr5));
List<byte[]> list1 = new List<byte[]>();
List<byte[]> list2 = new List<byte[]>();
list1.Add(arr1);
list2.Add(arr2);
Assert.IsTrue(CollectionUtil.GetHashCode(list1) ==
CollectionUtil.GetHashCode(list2));
list2.Add(arr2);
Assert.IsFalse(CollectionUtil.GetHashCode(list1) ==
CollectionUtil.GetHashCode(list2));
list1.Add(arr1);
Assert.IsTrue(CollectionUtil.GetHashCode(list1) ==
CollectionUtil.GetHashCode(list2));
list1.Add(arr1);
list2.Add(arr3);
Assert.IsFalse(CollectionUtil.GetHashCode(list1) ==
CollectionUtil.GetHashCode(list2));
Dictionary<String, byte[]> map1 = new Dictionary<String, byte[]>();
Dictionary<String, byte[]> map2 = new Dictionary<String, byte[]>();
map1["key1"] = arr1;
map2["key1"] = arr2;
Assert.IsTrue(CollectionUtil.GetHashCode(map1) ==
CollectionUtil.GetHashCode(map2));
map2["key2"] = arr2;
Assert.IsFalse(CollectionUtil.GetHashCode(map1) ==
CollectionUtil.GetHashCode(map2));
map1["key2"] = arr1;
Assert.IsTrue(CollectionUtil.GetHashCode(map1) ==
CollectionUtil.GetHashCode(map2));
map1["key2"] = arr3;
Assert.IsFalse(CollectionUtil.GetHashCode(map1) ==
CollectionUtil.GetHashCode(map2));
HashSet<String> set1 = new HashSet<String>();
HashSet<String> set2 = new HashSet<String>();
set1.Add("val");
set2.Add("val");
Assert.IsTrue(CollectionUtil.GetHashCode(set1) ==
CollectionUtil.GetHashCode(set2));
set2.Add("val2");
Assert.IsFalse(CollectionUtil.GetHashCode(set1) ==
CollectionUtil.GetHashCode(set2));
set1.Add("val2");
Assert.IsTrue(CollectionUtil.GetHashCode(set1) ==
CollectionUtil.GetHashCode(set2));
}
}
}