[System] Fixes UdpClient.Receive with IPv6 endpoint
[mono.git] / mcs / class / corlib / Test / System / StringComparerTest.cs
1 //
2 // MonoTests.System.StringComparerTest
3 //
4 // Authors:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (C) 2007 Gert Driesen
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29
30 using System;
31 using System.Globalization;
32 using System.IO;
33 using System.Runtime.Serialization.Formatters.Binary;
34 using System.Threading;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System
39 {
40         [TestFixture]
41         public class StringComparerTest
42         {
43                 private CultureInfo old_culture;
44
45                 private BinaryFormatter CreateBinaryFormatter()
46                 {
47                         return new BinaryFormatter();
48                 }
49
50                 [SetUp]
51                 public void SetUp ()
52                 {
53                         old_culture = Thread.CurrentThread.CurrentCulture;
54                 }
55
56                 [TearDown]
57                 public void TearDown ()
58                 {
59                         Thread.CurrentThread.CurrentCulture = old_culture;
60                 }
61
62                 [Test]
63                 public void Serialize_CurrentCulture ()
64                 {
65                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
66                         BinaryFormatter bf = CreateBinaryFormatter();
67                         MemoryStream ms = new MemoryStream ();
68                         bf.Serialize (ms, StringComparer.CurrentCulture);
69
70                         // Assert.AreEqual (_serializedCurrentCulture, buffer);
71                 }
72
73                 [Test]
74                 public void Deserialize_CurrentCulture ()
75                 {
76                         MemoryStream ms = new MemoryStream ();
77                         ms.Write (_serializedCurrentCulture, 0, _serializedCurrentCulture.Length);
78                         ms.Position = 0;
79
80                         BinaryFormatter bf = CreateBinaryFormatter();
81                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
82                         Assert.IsNotNull (sc);
83                 }
84
85                 [Test]
86                 public void Serialize_CurrentCultureIgnoreCase ()
87                 {
88                         BinaryFormatter bf = CreateBinaryFormatter();
89                         MemoryStream ms = new MemoryStream ();
90                         bf.Serialize (ms, StringComparer.CurrentCultureIgnoreCase);
91
92                         // Assert.AreEqual (_serializedCurrentCultureIgnoreCase, buffer);
93                 }
94
95                 [Test]
96                 public void Deserialize_CurrentCultureIgnoreCase ()
97                 {
98                         MemoryStream ms = new MemoryStream ();
99                         ms.Write (_serializedCurrentCultureIgnoreCase, 0, _serializedCurrentCultureIgnoreCase.Length);
100                         ms.Position = 0;
101
102                         BinaryFormatter bf = CreateBinaryFormatter();
103                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
104                         Assert.IsNotNull (sc);
105                 }
106
107                 [Test]
108                 public void Serialize_InvariantCulture ()
109                 {
110                         BinaryFormatter bf = CreateBinaryFormatter();
111                         MemoryStream ms = new MemoryStream ();
112                         bf.Serialize (ms, StringComparer.InvariantCulture);
113
114                         byte [] buffer = new byte [ms.Length];
115                         ms.Position = 0;
116                         ms.Read (buffer, 0, buffer.Length);
117
118                         // Assert.AreEqual (_serializedInvariantCulture, buffer);
119                 }
120
121                 [Test]
122                 public void Deserialize_InvariantCulture ()
123                 {
124                         MemoryStream ms = new MemoryStream ();
125                         ms.Write (_serializedInvariantCulture, 0, _serializedInvariantCulture.Length);
126                         ms.Position = 0;
127
128                         BinaryFormatter bf = CreateBinaryFormatter();
129                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
130                         Assert.IsNotNull (sc);
131                 }
132
133                 [Test]
134                 public void Serialize_InvariantCultureIgnoreCase ()
135                 {
136                         BinaryFormatter bf = CreateBinaryFormatter();
137                         MemoryStream ms = new MemoryStream ();
138                         bf.Serialize (ms, StringComparer.InvariantCultureIgnoreCase);
139
140                         byte [] buffer = new byte [ms.Length];
141                         ms.Position = 0;
142                         ms.Read (buffer, 0, buffer.Length);
143
144                         // Assert.AreEqual (_serializedInvariantCultureIgnoreCase, buffer);
145                 }
146
147                 [Test]
148                 public void Deserialize_InvariantCultureIgnoreCase ()
149                 {
150                         MemoryStream ms = new MemoryStream ();
151                         ms.Write (_serializedInvariantCultureIgnoreCase, 0, _serializedInvariantCultureIgnoreCase.Length);
152                         ms.Position = 0;
153
154                         BinaryFormatter bf = CreateBinaryFormatter();
155                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
156                         Assert.IsNotNull (sc);
157                 }
158
159                 [Test]
160                 public void Serialize_Ordinal ()
161                 {
162                         BinaryFormatter bf = CreateBinaryFormatter();
163                         MemoryStream ms = new MemoryStream ();
164                         bf.Serialize (ms, StringComparer.Ordinal);
165
166                         byte [] buffer = new byte [ms.Length];
167                         ms.Position = 0;
168                         ms.Read (buffer, 0, buffer.Length);
169
170                         Assert.AreEqual (_serializedOrdinal, buffer);
171                 }
172
173                 [Test]
174                 public void Deserialize_Ordinal ()
175                 {
176                         MemoryStream ms = new MemoryStream ();
177                         ms.Write (_serializedOrdinal, 0, _serializedOrdinal.Length);
178                         ms.Position = 0;
179
180                         BinaryFormatter bf = CreateBinaryFormatter();
181                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
182                         Assert.IsNotNull (sc);
183                 }
184
185                 [Test]
186                 public void Serialize_OrdinalIgnoreCase ()
187                 {
188                         BinaryFormatter bf = CreateBinaryFormatter();
189                         MemoryStream ms = new MemoryStream ();
190                         bf.Serialize (ms, StringComparer.OrdinalIgnoreCase);
191
192                         byte [] buffer = new byte [ms.Length];
193                         ms.Position = 0;
194                         ms.Read (buffer, 0, buffer.Length);
195
196                         Assert.AreEqual (_serializedOrdinalIgnoreCase, buffer);
197                 }
198
199                 [Test]
200                 public void Deserialize_OrdinalIgnoreCase ()
201                 {
202                         MemoryStream ms = new MemoryStream ();
203                         ms.Write (_serializedOrdinalIgnoreCase, 0, _serializedOrdinalIgnoreCase.Length);
204                         ms.Position = 0;
205
206                         BinaryFormatter bf = CreateBinaryFormatter();
207                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
208                         Assert.IsNotNull (sc);
209                 }
210
211                 [Test]
212                 [ExpectedException (typeof (ArgumentNullException))]
213                 public void GetNullOrdinalHashCode ()
214                 {
215                         StringComparer.Ordinal.GetHashCode (null);
216                 }
217
218                 private static readonly byte [] _serializedCurrentCulture = new byte [] {
219                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
220                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
221                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
222                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
223                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
224                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
225                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
226                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
227                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
228                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
229                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00,
230                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
231                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
232                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
233                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
234                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
235                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
236                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x13, 0x08, 0x00, 0x00, 0x13,
237                         0x08, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x6e, 0x6c,
238                         0x2d, 0x42, 0x45, 0x0b };
239
240                 private static readonly byte [] _serializedCurrentCultureIgnoreCase = new byte [] {
241                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
242                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
243                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
244                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
245                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
246                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
247                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
248                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
249                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
250                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
251                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x01,
252                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
253                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
254                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
255                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
256                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
257                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
258                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x13, 0x08, 0x00, 0x00, 0x13,
259                         0x08, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x6e, 0x6c,
260                         0x2d, 0x42, 0x45, 0x0b };
261
262
263                 private static readonly byte [] _serializedInvariantCulture = new byte [] {
264                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
265                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
266                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
267                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
268                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
269                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
270                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
271                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
272                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
273                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
274                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00,
275                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
276                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
277                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
278                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
279                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
280                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
281                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x7f, 0x00, 0x00, 0x00, 0x7f,
282                         0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0b };
283
284                 private static readonly byte [] _serializedInvariantCultureIgnoreCase = new byte [] {
285                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
286                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
287                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
288                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
289                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
290                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
291                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
292                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
293                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
294                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
295                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x01,
296                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
297                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
298                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
299                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
300                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
301                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
302                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x7f, 0x00, 0x00, 0x00, 0x7f,
303                         0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0b };
304
305                 private static readonly byte [] _serializedOrdinal = new byte [] {
306                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
307                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
308                         0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4f, 0x72, 0x64,
309                         0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
310                         0x72, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f,
311                         0x72, 0x65, 0x43, 0x61, 0x73, 0x65, 0x00, 0x01, 0x00, 0x0b };
312
313                 private static readonly byte [] _serializedOrdinalIgnoreCase = new byte [] {
314                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
315                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
316                         0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4f, 0x72, 0x64,
317                         0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
318                         0x72, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f,
319                         0x72, 0x65, 0x43, 0x61, 0x73, 0x65, 0x00, 0x01, 0x01, 0x0b };
320         }
321 }
322