Merge pull request #2338 from BogdanovKirill/httpwritefix3
[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                 [Test]
219                 [SetCulture("en-us")]
220                 public void OrdinarCultureSwitch ()
221                 {
222                         var cmp1 = StringComparer.OrdinalIgnoreCase;
223                         var h1 = cmp1.GetHashCode ("w");
224
225                         global::System.Threading.Thread.CurrentThread.CurrentCulture = new global::System.Globalization.CultureInfo ("fi");
226
227                         var cmp2 = StringComparer.OrdinalIgnoreCase;
228                         var h2 = cmp2.GetHashCode ("w");
229                         Assert.AreEqual (h1, h2);
230                 }
231
232                 private static readonly byte [] _serializedCurrentCulture = new byte [] {
233                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
234                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
235                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
236                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
237                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
238                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
239                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
240                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
241                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
242                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
243                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00,
244                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
245                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
246                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
247                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
248                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
249                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
250                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x13, 0x08, 0x00, 0x00, 0x13,
251                         0x08, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x6e, 0x6c,
252                         0x2d, 0x42, 0x45, 0x0b };
253
254                 private static readonly byte [] _serializedCurrentCultureIgnoreCase = new byte [] {
255                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
256                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
257                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
258                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
259                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
260                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
261                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
262                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
263                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
264                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
265                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x01,
266                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
267                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
268                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
269                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
270                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
271                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
272                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x13, 0x08, 0x00, 0x00, 0x13,
273                         0x08, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x6e, 0x6c,
274                         0x2d, 0x42, 0x45, 0x0b };
275
276
277                 private static readonly byte [] _serializedInvariantCulture = new byte [] {
278                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
279                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
280                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
281                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
282                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
283                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
284                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
285                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
286                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
287                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
288                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00,
289                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
290                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
291                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
292                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
293                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
294                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
295                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x7f, 0x00, 0x00, 0x00, 0x7f,
296                         0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0b };
297
298                 private static readonly byte [] _serializedInvariantCultureIgnoreCase = new byte [] {
299                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
300                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
301                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
302                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
303                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
304                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
305                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
306                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
307                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
308                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
309                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x01,
310                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
311                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
312                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
313                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
314                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
315                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
316                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x7f, 0x00, 0x00, 0x00, 0x7f,
317                         0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0b };
318
319                 private static readonly byte [] _serializedOrdinal = new byte [] {
320                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
321                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
322                         0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4f, 0x72, 0x64,
323                         0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
324                         0x72, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f,
325                         0x72, 0x65, 0x43, 0x61, 0x73, 0x65, 0x00, 0x01, 0x00, 0x0b };
326
327                 private static readonly byte [] _serializedOrdinalIgnoreCase = new byte [] {
328                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
329                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
330                         0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4f, 0x72, 0x64,
331                         0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
332                         0x72, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f,
333                         0x72, 0x65, 0x43, 0x61, 0x73, 0x65, 0x00, 0x01, 0x01, 0x0b };
334         }
335 }
336