[Cleanup] Removed TARGET_JVM
[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 #if NET_2_0
30
31 using System;
32 using System.Globalization;
33 using System.IO;
34 using System.Runtime.Serialization.Formatters.Binary;
35 using System.Threading;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System
40 {
41         [TestFixture]
42         public class StringComparerTest
43         {
44                 private CultureInfo old_culture;
45
46                 private BinaryFormatter CreateBinaryFormatter()
47                 {
48                         return new BinaryFormatter();
49                 }
50
51                 [SetUp]
52                 public void SetUp ()
53                 {
54                         old_culture = Thread.CurrentThread.CurrentCulture;
55                 }
56
57                 [TearDown]
58                 public void TearDown ()
59                 {
60                         Thread.CurrentThread.CurrentCulture = old_culture;
61                 }
62
63                 [Test]
64                 public void Serialize_CurrentCulture ()
65                 {
66                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
67                         BinaryFormatter bf = CreateBinaryFormatter();
68                         MemoryStream ms = new MemoryStream ();
69                         bf.Serialize (ms, StringComparer.CurrentCulture);
70
71                         // Assert.AreEqual (_serializedCurrentCulture, buffer);
72                 }
73
74                 [Test]
75                 public void Deserialize_CurrentCulture ()
76                 {
77                         MemoryStream ms = new MemoryStream ();
78                         ms.Write (_serializedCurrentCulture, 0, _serializedCurrentCulture.Length);
79                         ms.Position = 0;
80
81                         BinaryFormatter bf = CreateBinaryFormatter();
82                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
83                         Assert.IsNotNull (sc);
84                 }
85
86                 [Test]
87                 public void Serialize_CurrentCultureIgnoreCase ()
88                 {
89                         BinaryFormatter bf = CreateBinaryFormatter();
90                         MemoryStream ms = new MemoryStream ();
91                         bf.Serialize (ms, StringComparer.CurrentCultureIgnoreCase);
92
93                         // Assert.AreEqual (_serializedCurrentCultureIgnoreCase, buffer);
94                 }
95
96                 [Test]
97                 public void Deserialize_CurrentCultureIgnoreCase ()
98                 {
99                         MemoryStream ms = new MemoryStream ();
100                         ms.Write (_serializedCurrentCultureIgnoreCase, 0, _serializedCurrentCultureIgnoreCase.Length);
101                         ms.Position = 0;
102
103                         BinaryFormatter bf = CreateBinaryFormatter();
104                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
105                         Assert.IsNotNull (sc);
106                 }
107
108                 [Test]
109                 public void Serialize_InvariantCulture ()
110                 {
111                         BinaryFormatter bf = CreateBinaryFormatter();
112                         MemoryStream ms = new MemoryStream ();
113                         bf.Serialize (ms, StringComparer.InvariantCulture);
114
115                         byte [] buffer = new byte [ms.Length];
116                         ms.Position = 0;
117                         ms.Read (buffer, 0, buffer.Length);
118
119                         // Assert.AreEqual (_serializedInvariantCulture, buffer);
120                 }
121
122                 [Test]
123                 public void Deserialize_InvariantCulture ()
124                 {
125                         MemoryStream ms = new MemoryStream ();
126                         ms.Write (_serializedInvariantCulture, 0, _serializedInvariantCulture.Length);
127                         ms.Position = 0;
128
129                         BinaryFormatter bf = CreateBinaryFormatter();
130                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
131                         Assert.IsNotNull (sc);
132                 }
133
134                 [Test]
135                 public void Serialize_InvariantCultureIgnoreCase ()
136                 {
137                         BinaryFormatter bf = CreateBinaryFormatter();
138                         MemoryStream ms = new MemoryStream ();
139                         bf.Serialize (ms, StringComparer.InvariantCultureIgnoreCase);
140
141                         byte [] buffer = new byte [ms.Length];
142                         ms.Position = 0;
143                         ms.Read (buffer, 0, buffer.Length);
144
145                         // Assert.AreEqual (_serializedInvariantCultureIgnoreCase, buffer);
146                 }
147
148                 [Test]
149                 public void Deserialize_InvariantCultureIgnoreCase ()
150                 {
151                         MemoryStream ms = new MemoryStream ();
152                         ms.Write (_serializedInvariantCultureIgnoreCase, 0, _serializedInvariantCultureIgnoreCase.Length);
153                         ms.Position = 0;
154
155                         BinaryFormatter bf = CreateBinaryFormatter();
156                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
157                         Assert.IsNotNull (sc);
158                 }
159
160                 [Test]
161                 public void Serialize_Ordinal ()
162                 {
163                         BinaryFormatter bf = CreateBinaryFormatter();
164                         MemoryStream ms = new MemoryStream ();
165                         bf.Serialize (ms, StringComparer.Ordinal);
166
167                         byte [] buffer = new byte [ms.Length];
168                         ms.Position = 0;
169                         ms.Read (buffer, 0, buffer.Length);
170
171                         Assert.AreEqual (_serializedOrdinal, buffer);
172                 }
173
174                 [Test]
175                 public void Deserialize_Ordinal ()
176                 {
177                         MemoryStream ms = new MemoryStream ();
178                         ms.Write (_serializedOrdinal, 0, _serializedOrdinal.Length);
179                         ms.Position = 0;
180
181                         BinaryFormatter bf = CreateBinaryFormatter();
182                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
183                         Assert.IsNotNull (sc);
184                 }
185
186                 [Test]
187                 public void Serialize_OrdinalIgnoreCase ()
188                 {
189                         BinaryFormatter bf = CreateBinaryFormatter();
190                         MemoryStream ms = new MemoryStream ();
191                         bf.Serialize (ms, StringComparer.OrdinalIgnoreCase);
192
193                         byte [] buffer = new byte [ms.Length];
194                         ms.Position = 0;
195                         ms.Read (buffer, 0, buffer.Length);
196
197                         Assert.AreEqual (_serializedOrdinalIgnoreCase, buffer);
198                 }
199
200                 [Test]
201                 public void Deserialize_OrdinalIgnoreCase ()
202                 {
203                         MemoryStream ms = new MemoryStream ();
204                         ms.Write (_serializedOrdinalIgnoreCase, 0, _serializedOrdinalIgnoreCase.Length);
205                         ms.Position = 0;
206
207                         BinaryFormatter bf = CreateBinaryFormatter();
208                         StringComparer sc = (StringComparer) bf.Deserialize (ms);
209                         Assert.IsNotNull (sc);
210                 }
211
212                 [Test]
213                 [ExpectedException (typeof (ArgumentNullException))]
214                 public void GetNullOrdinalHashCode ()
215                 {
216                         StringComparer.Ordinal.GetHashCode (null);
217                 }
218
219                 private static readonly byte [] _serializedCurrentCulture = new byte [] {
220                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
221                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
222                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
223                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
224                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
225                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
226                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
227                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
228                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
229                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
230                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00,
231                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
232                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
233                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
234                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
235                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
236                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
237                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x13, 0x08, 0x00, 0x00, 0x13,
238                         0x08, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x6e, 0x6c,
239                         0x2d, 0x42, 0x45, 0x0b };
240
241                 private static readonly byte [] _serializedCurrentCultureIgnoreCase = new byte [] {
242                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
243                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
244                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
245                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
246                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
247                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
248                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
249                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
250                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
251                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
252                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x01,
253                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
254                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
255                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
256                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
257                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
258                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
259                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x13, 0x08, 0x00, 0x00, 0x13,
260                         0x08, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x05, 0x6e, 0x6c,
261                         0x2d, 0x42, 0x45, 0x0b };
262
263
264                 private static readonly byte [] _serializedInvariantCulture = new byte [] {
265                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
266                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
267                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
268                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
269                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
270                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
271                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
272                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
273                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
274                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
275                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x00,
276                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
277                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
278                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
279                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
280                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
281                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
282                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x7f, 0x00, 0x00, 0x00, 0x7f,
283                         0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0b };
284
285                 private static readonly byte [] _serializedInvariantCultureIgnoreCase = new byte [] {
286                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
287                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
288                         0x1b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x75, 0x6c,
289                         0x74, 0x75, 0x72, 0x65, 0x41, 0x77, 0x61, 0x72, 0x65, 0x43, 0x6f,
290                         0x6d, 0x70, 0x61, 0x72, 0x65, 0x72, 0x02, 0x00, 0x00, 0x00, 0x0c,
291                         0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65, 0x49, 0x6e, 0x66,
292                         0x6f, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f, 0x72, 0x65, 0x43, 0x61,
293                         0x73, 0x65, 0x03, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d,
294                         0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61, 0x74,
295                         0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
296                         0x49, 0x6e, 0x66, 0x6f, 0x01, 0x09, 0x02, 0x00, 0x00, 0x00, 0x01,
297                         0x04, 0x02, 0x00, 0x00, 0x00, 0x20, 0x53, 0x79, 0x73, 0x74, 0x65,
298                         0x6d, 0x2e, 0x47, 0x6c, 0x6f, 0x62, 0x61, 0x6c, 0x69, 0x7a, 0x61,
299                         0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72,
300                         0x65, 0x49, 0x6e, 0x66, 0x6f, 0x03, 0x00, 0x00, 0x00, 0x09, 0x77,
301                         0x69, 0x6e, 0x33, 0x32, 0x4c, 0x43, 0x49, 0x44, 0x07, 0x63, 0x75,
302                         0x6c, 0x74, 0x75, 0x72, 0x65, 0x06, 0x6d, 0x5f, 0x6e, 0x61, 0x6d,
303                         0x65, 0x00, 0x00, 0x01, 0x08, 0x08, 0x7f, 0x00, 0x00, 0x00, 0x7f,
304                         0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x0b };
305
306                 private static readonly byte [] _serializedOrdinal = new byte [] {
307                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
308                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
309                         0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4f, 0x72, 0x64,
310                         0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
311                         0x72, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f,
312                         0x72, 0x65, 0x43, 0x61, 0x73, 0x65, 0x00, 0x01, 0x00, 0x0b };
313
314                 private static readonly byte [] _serializedOrdinalIgnoreCase = new byte [] {
315                         0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
316                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
317                         0x16, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x4f, 0x72, 0x64,
318                         0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6d, 0x70, 0x61, 0x72, 0x65,
319                         0x72, 0x01, 0x00, 0x00, 0x00, 0x0b, 0x5f, 0x69, 0x67, 0x6e, 0x6f,
320                         0x72, 0x65, 0x43, 0x61, 0x73, 0x65, 0x00, 0x01, 0x01, 0x0b };
321         }
322 }
323
324 #endif