1 // SqlStringTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlString
4 // Ville Palo (vi64pa@koti.soon.fi)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
8 // (C) 2003 Martin Willemoes Hansen
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using NUnit.Framework;
36 using System.Data.SqlTypes;
37 using System.Globalization;
38 using System.Threading;
40 namespace MonoTests.System.Data.SqlTypes
43 public class SqlStringTest : Assertion {
45 private SqlString Test1 = null;
46 private SqlString Test2 = null;
47 private SqlString Test3 = null;
50 public void GetReady()
52 Test1 = new SqlString ("First TestString");
53 Test2 = new SqlString ("This is just a test SqlString");
54 Test3 = new SqlString ("This is just a test SqlString");
55 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-AU");
64 SqlString TestString = new SqlString ("Test");
65 AssertEquals ("#A01", "Test", TestString.Value);
67 // SqlString (String, int)
68 TestString = new SqlString ("Test", 2057);
69 AssertEquals ("#A02", 2057, TestString.LCID);
71 // SqlString (int, SqlCompareOptions, byte[])
72 TestString = new SqlString (2057,
73 SqlCompareOptions.BinarySort|SqlCompareOptions.IgnoreCase,
74 new byte [2] {123, 221});
75 AssertEquals ("#A03", 2057, TestString.CompareInfo.LCID);
77 // SqlString(string, int, SqlCompareOptions)
78 TestString = new SqlString ("Test", 2057, SqlCompareOptions.IgnoreNonSpace);
79 Assert ("#A04", !TestString.IsNull);
81 // SqlString (int, SqlCompareOptions, byte[], bool)
82 TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [4] {100, 100, 200, 45}, true);
83 AssertEquals ("#A05", (byte)63, TestString.GetNonUnicodeBytes () [0]);
84 TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, false);
85 AssertEquals ("#A06", (String)"qd", TestString.Value);
87 // SqlString (int, SqlCompareOptions, byte[], int, int)
88 TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 0, 2);
89 Assert ("#A07", !TestString.IsNull);
91 // SqlString (int, SqlCompareOptions, byte[], int, int, bool)
92 TestString = new SqlString (2057, SqlCompareOptions.IgnoreCase, new byte [3] {100, 111, 50}, 1, 2, false);
93 AssertEquals ("#A08", "o2", TestString.Value);
94 TestString = new SqlString (2057, SqlCompareOptions.IgnoreCase, new byte [3] {123, 111, 222}, 1, 2, true);
95 Assert ("#A09", !TestString.IsNull);
99 [ExpectedException(typeof (ArgumentOutOfRangeException))]
100 public void CtorArgumentOutOfRangeException1 ()
102 SqlString TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 2, 1);
106 [ExpectedException(typeof (ArgumentOutOfRangeException))]
107 public void CtorArgumentOutOfRangeException2 ()
109 SqlString TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 0, 4);
112 // Test public fields
114 public void PublicFields()
117 AssertEquals ("#B01", 32768, SqlString.BinarySort);
120 AssertEquals ("#B02", 1, SqlString.IgnoreCase);
123 AssertEquals ("#B03", 8, SqlString.IgnoreKanaType);
126 AssertEquals ("#B04", 2, SqlString.IgnoreNonSpace);
129 AssertEquals ("#B05", 16, SqlString.IgnoreWidth);
132 Assert ("#B06", SqlString.Null.IsNull);
137 public void Properties()
139 /* Comment out: This test will succeed only when you're in en-au culture.
141 AssertEquals ("#C01", 3081, Test1.CompareInfo.LCID);
144 AssertEquals ("#C02", 3081, Test1.CultureInfo.LCID);
147 AssertEquals ("#C05", 3081, Test1.LCID);
151 Assert ("#C03", !Test1.IsNull);
152 Assert ("#C04", SqlString.Null.IsNull);
155 AssertEquals ("#C06", "IgnoreCase, IgnoreKanaType, IgnoreWidth",
156 Test1.SqlCompareOptions.ToString ());
159 AssertEquals ("#C07", "First TestString", Test1.Value);
166 [ExpectedException(typeof(ArgumentException))]
167 public void CompareToArgumentException ()
169 SqlByte Test = new SqlByte (1);
170 Test1.CompareTo (Test);
174 [ExpectedException(typeof(SqlTypeException))]
175 public void CompareToSqlTypeException ()
177 SqlString T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
178 SqlString T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
183 public void CompareTo()
185 SqlByte Test = new SqlByte (1);
187 Assert ("#D01", Test1.CompareTo (Test3) < 0);
188 Assert ("#D02", Test2.CompareTo (Test1) > 0);
189 Assert ("#D03", Test2.CompareTo (Test3) == 0);
190 Assert ("#D04", Test3.CompareTo (SqlString.Null) > 0);
193 SqlString T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
194 SqlString T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
197 T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
198 T2 = new SqlString ("TEST", 2057, SqlCompareOptions.IgnoreCase);
199 Assert ("#D09", T2.CompareTo (T1) == 0);
201 T1 = new SqlString ("test", 2057);
202 T2 = new SqlString ("TEST", 2057);
203 Assert ("#D10", T2.CompareTo (T1) == 0);
205 T1 = new SqlString ("test", 2057, SqlCompareOptions.None);
206 T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
207 Assert ("#D11", T2.CompareTo (T1) != 0);
210 T1 = new SqlString ("TEST\xF1", 2057, SqlCompareOptions.IgnoreNonSpace);
211 T2 = new SqlString ("TESTn", 2057, SqlCompareOptions.IgnoreNonSpace);
212 Assert ("#D12", T2.CompareTo (T1) == 0);
214 T1 = new SqlString ("TESTñ", 2057, SqlCompareOptions.None);
215 T2 = new SqlString ("TESTn", 2057, SqlCompareOptions.None);
216 Assert ("#D13", T2.CompareTo (T1) != 0);
219 T1 = new SqlString ("01_", 2057, SqlCompareOptions.BinarySort);
220 T2 = new SqlString ("_01", 2057, SqlCompareOptions.BinarySort);
221 Assert ("#D14", T1.CompareTo (T2) < 0);
223 T1 = new SqlString ("01_", 2057, SqlCompareOptions.None);
224 T2 = new SqlString ("_01", 2057, SqlCompareOptions.None);
225 Assert ("#D15", T1.CompareTo (T2) > 0);
229 public void EqualsMethods()
231 Assert ("#E01", !Test1.Equals (Test2));
232 Assert ("#E02", !Test3.Equals (Test1));
233 Assert ("#E03", !Test2.Equals (new SqlString ("TEST")));
234 Assert ("#E04", Test2.Equals (Test3));
236 // Static Equals()-method
237 Assert ("#E05", SqlString.Equals (Test2, Test3).Value);
238 Assert ("#E06", !SqlString.Equals (Test1, Test2).Value);
242 public void GetHashCodeTest()
244 // FIXME: Better way to test HashCode
245 AssertEquals ("#F01", Test1.GetHashCode (),
246 Test1.GetHashCode ());
247 Assert ("#F02", Test1.GetHashCode () != Test2.GetHashCode ());
248 Assert ("#F03", Test2.GetHashCode () == Test2.GetHashCode ());
252 public void GetTypeTest()
254 AssertEquals ("#G01", "System.Data.SqlTypes.SqlString",
255 Test1.GetType ().ToString ());
256 AssertEquals ("#G02", "System.String",
257 Test1.Value.GetType ().ToString ());
261 public void Greaters()
265 Assert ("#H01", !SqlString.GreaterThan (Test1, Test2).Value);
266 Assert ("#H02", SqlString.GreaterThan (Test2, Test1).Value);
267 Assert ("#H03", !SqlString.GreaterThan (Test2, Test3).Value);
269 // GreaterTharOrEqual ()
270 Assert ("#H04", !SqlString.GreaterThanOrEqual (Test1, Test2).Value);
271 Assert ("#H05", SqlString.GreaterThanOrEqual (Test2, Test1).Value);
272 Assert ("#H06", SqlString.GreaterThanOrEqual (Test2, Test3).Value);
276 public void Lessers()
279 Assert ("#I01", !SqlString.LessThan (Test2, Test3).Value);
280 Assert ("#I02", !SqlString.LessThan (Test2, Test1).Value);
281 Assert ("#I03", SqlString.LessThan (Test1, Test2).Value);
283 // LessThanOrEqual ()
284 Assert ("#I04", SqlString.LessThanOrEqual (Test1, Test2).Value);
285 Assert ("#I05", !SqlString.LessThanOrEqual (Test2, Test1).Value);
286 Assert ("#I06", SqlString.LessThanOrEqual (Test3, Test2).Value);
287 Assert ("#I07", SqlString.LessThanOrEqual (Test2, SqlString.Null).IsNull);
291 public void NotEquals()
293 Assert ("#J01", SqlString.NotEquals (Test1, Test2).Value);
294 Assert ("#J02", SqlString.NotEquals (Test2, Test1).Value);
295 Assert ("#J03", SqlString.NotEquals (Test3, Test1).Value);
296 Assert ("#J04", !SqlString.NotEquals (Test2, Test3).Value);
298 Assert ("#J05", SqlString.NotEquals (SqlString.Null, Test3).IsNull);
304 Test1 = new SqlString ("First TestString");
305 Test2 = new SqlString ("This is just a test SqlString");
306 Test3 = new SqlString ("This is just a test SqlString");
308 AssertEquals ("#K01",
309 (SqlString)"First TestStringThis is just a test SqlString",
310 SqlString.Concat (Test1, Test2));
312 AssertEquals ("#K02", SqlString.Null,
313 SqlString.Concat (Test1, SqlString.Null));
319 SqlString TestSqlString = Test1.Clone ();
320 AssertEquals ("#L01", Test1, TestSqlString);
324 public void CompareOptionsFromSqlCompareOptions()
326 AssertEquals ("#M01", CompareOptions.IgnoreCase,
327 SqlString.CompareOptionsFromSqlCompareOptions (
328 SqlCompareOptions.IgnoreCase));
329 AssertEquals ("#M02", CompareOptions.IgnoreCase,
330 SqlString.CompareOptionsFromSqlCompareOptions (
331 SqlCompareOptions.IgnoreCase));
334 CompareOptions test = SqlString.CompareOptionsFromSqlCompareOptions (
335 SqlCompareOptions.BinarySort);
337 } catch (Exception e) {
338 AssertEquals ("#M04", typeof (ArgumentOutOfRangeException), e.GetType ());
343 public void UnicodeBytes()
345 AssertEquals ("#N01", (byte)105, Test1.GetNonUnicodeBytes () [1]);
346 AssertEquals ("#N02", (byte)32, Test1.GetNonUnicodeBytes () [5]);
348 AssertEquals ("#N03", (byte)70, Test1.GetUnicodeBytes () [0]);
349 AssertEquals ("#N03b", (byte)70, Test1.GetNonUnicodeBytes () [0]);
350 AssertEquals ("#N03c", (byte)0, Test1.GetUnicodeBytes () [1]);
351 AssertEquals ("#N03d", (byte)105, Test1.GetNonUnicodeBytes () [1]);
352 AssertEquals ("#N03e", (byte)105, Test1.GetUnicodeBytes () [2]);
353 AssertEquals ("#N03f", (byte)114, Test1.GetNonUnicodeBytes () [2]);
354 AssertEquals ("#N03g", (byte)0, Test1.GetUnicodeBytes () [3]);
355 AssertEquals ("#N03h", (byte)115, Test1.GetNonUnicodeBytes () [3]);
356 AssertEquals ("#N03i", (byte)114, Test1.GetUnicodeBytes () [4]);
357 AssertEquals ("#N03j", (byte)116, Test1.GetNonUnicodeBytes () [4]);
359 AssertEquals ("#N04", (byte)105, Test1.GetUnicodeBytes () [2]);
362 byte test = Test1.GetUnicodeBytes () [105];
364 } catch (Exception e) {
365 AssertEquals ("#N06", typeof (IndexOutOfRangeException), e.GetType());
370 [ExpectedException(typeof (FormatException))]
371 public void ConversionBoolFormatException1 ()
373 bool test = Test1.ToSqlBoolean ().Value;
377 [ExpectedException(typeof (FormatException))]
378 public void ConversionByteFormatException ()
380 byte test = Test1.ToSqlByte ().Value;
384 [ExpectedException(typeof (FormatException))]
385 public void ConversionDecimalFormatException1 ()
387 Decimal d = Test1.ToSqlDecimal ().Value;
391 [ExpectedException(typeof (FormatException))]
392 public void ConversionDecimalFormatException2 ()
394 SqlString String9E300 = new SqlString ("9E+300");
395 SqlDecimal test = String9E300.ToSqlDecimal ();
399 [ExpectedException(typeof (FormatException))]
400 public void ConversionGuidFormatException ()
402 SqlString String9E300 = new SqlString ("9E+300");
403 SqlGuid test = String9E300.ToSqlGuid ();
407 [ExpectedException(typeof (FormatException))]
408 public void ConversionInt16FormatException ()
410 SqlString String9E300 = new SqlString ("9E+300");
411 SqlInt16 test = String9E300.ToSqlInt16().Value;
415 [ExpectedException(typeof (FormatException))]
416 public void ConversionInt32FormatException1 ()
418 SqlString String9E300 = new SqlString ("9E+300");
419 SqlInt32 test = String9E300.ToSqlInt32 ().Value;
423 [ExpectedException(typeof (FormatException))]
424 public void ConversionInt32FormatException2 ()
426 SqlInt32 test = Test1.ToSqlInt32 ().Value;
430 [ExpectedException(typeof (FormatException))]
431 public void ConversionInt64FormatException ()
433 SqlString String9E300 = new SqlString ("9E+300");
434 SqlInt64 test = String9E300.ToSqlInt64 ().Value;
438 [ExpectedException(typeof (FormatException))]
439 public void ConversionIntMoneyFormatException2 ()
441 SqlString String9E300 = new SqlString ("9E+300");
442 SqlMoney test = String9E300.ToSqlMoney ().Value;
446 [ExpectedException(typeof(OverflowException))]
447 public void ConversionByteOverflowException ()
449 SqlByte b = (new SqlString ("2500")).ToSqlByte ();
453 [ExpectedException(typeof(OverflowException))]
454 public void ConversionDoubleOverflowException ()
456 SqlDouble test = (new SqlString ("4e400")).ToSqlDouble ();
460 [ExpectedException(typeof(OverflowException))]
461 public void ConversionSingleOverflowException ()
463 SqlString String9E300 = new SqlString ("9E+300");
464 SqlSingle test = String9E300.ToSqlSingle().Value;
468 public void Conversions()
471 SqlString String250 = new SqlString ("250");
472 SqlString String9E300 = new SqlString ("9E+300");
475 Assert ("#O02", (new SqlString("1")).ToSqlBoolean ().Value);
476 Assert ("#O03", !(new SqlString("0")).ToSqlBoolean ().Value);
477 Assert ("#O04", (new SqlString("True")).ToSqlBoolean ().Value);
478 Assert ("#O05", !(new SqlString("FALSE")).ToSqlBoolean ().Value);
479 Assert ("#O06", SqlString.Null.ToSqlBoolean ().IsNull);
482 AssertEquals ("#O08", (byte)250, String250.ToSqlByte ().Value);
485 AssertEquals ("#O11", 10,
486 (new SqlString ("2002-10-10")).ToSqlDateTime ().Value.Day);
489 AssertEquals ("#O16", (decimal)250, String250.ToSqlDecimal ().Value);
492 AssertEquals ("#O19", (SqlDouble)9E+300, String9E300.ToSqlDouble ());
495 SqlString TestGuid = new SqlString("11111111-1111-1111-1111-111111111111");
496 AssertEquals ("#O22", new SqlGuid("11111111-1111-1111-1111-111111111111"), TestGuid.ToSqlGuid ());
499 AssertEquals ("#O24", (short)250, String250.ToSqlInt16 ().Value);
502 AssertEquals ("#O27", (int)250, String250.ToSqlInt32 ().Value);
505 AssertEquals ("#O32", (long)250, String250.ToSqlInt64 ().Value);
508 AssertEquals ("#O35", 250.0000M, String250.ToSqlMoney ().Value);
512 AssertEquals ("#O38", (float)250, String250.ToSqlSingle ().Value);
515 AssertEquals ("#O41", "First TestString", Test1.ToString ());
521 public void ArithmeticOperators()
523 SqlString TestString = new SqlString ("...Testing...");
524 AssertEquals ("#P01", (SqlString)"First TestString...Testing...",
526 AssertEquals ("#P02", SqlString.Null,
527 Test1 + SqlString.Null);
531 public void ThanOrEqualOperators()
534 Assert ("#Q01", (Test2 == Test3).Value);
535 Assert ("#Q02", !(Test1 == Test2).Value);
536 Assert ("#Q03", (Test1 == SqlString.Null).IsNull);
539 Assert ("#Q04", !(Test3 != Test2).Value);
540 Assert ("#Q05", !(Test2 != Test3).Value);
541 Assert ("#Q06", (Test1 != Test3).Value);
542 Assert ("#Q07", (Test1 != SqlString.Null).IsNull);
545 Assert ("#Q08", (Test2 > Test1).Value);
546 Assert ("#Q09", !(Test1 > Test3).Value);
547 Assert ("#Q10", !(Test2 > Test3).Value);
548 Assert ("#Q11", (Test1 > SqlString.Null).IsNull);
551 Assert ("#Q12", !(Test1 >= Test3).Value);
552 Assert ("#Q13", (Test3 >= Test1).Value);
553 Assert ("#Q14", (Test2 >= Test3).Value);
554 Assert ("#Q15", (Test1 >= SqlString.Null).IsNull);
557 Assert ("#Q16", (Test1 < Test2).Value);
558 Assert ("#Q17", (Test1 < Test3).Value);
559 Assert ("#Q18", !(Test2 < Test3).Value);
560 Assert ("#Q19", (Test1 < SqlString.Null).IsNull);
563 Assert ("#Q20", (Test1 <= Test3).Value);
564 Assert ("#Q21", !(Test3 <= Test1).Value);
565 Assert ("#Q22", (Test2 <= Test3).Value);
566 Assert ("#Q23", (Test1 <= SqlString.Null).IsNull);
570 public void SqlBooleanToSqlString()
572 SqlBoolean TestBoolean = new SqlBoolean (true);
573 SqlBoolean TestBoolean2 = new SqlBoolean (false);
576 Result = (SqlString)TestBoolean;
577 AssertEquals ("#R01", "True", Result.Value);
579 Result = (SqlString)TestBoolean2;
580 AssertEquals ("#R02", "False", Result.Value);
582 Result = (SqlString)SqlBoolean.Null;
583 Assert ("#R03", Result.IsNull);
587 public void SqlByteToBoolean()
589 SqlByte TestByte = new SqlByte (250);
590 AssertEquals ("#S01", "250", ((SqlString)TestByte).Value);
592 SqlString test = ((SqlString)SqlByte.Null).Value;
594 } catch (Exception e) {
595 AssertEquals ("#S03", typeof (SqlNullValueException), e.GetType ());
600 public void SqlDateTimeToSqlString()
602 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-AU");
603 SqlDateTime TestTime = new SqlDateTime(2002, 10, 22, 9, 52, 30);
604 AssertEquals ("#T01", "22/10/2002 9:52:30 AM", ((SqlString)TestTime).Value);
608 public void SqlDecimalToSqlString()
610 SqlDecimal TestDecimal = new SqlDecimal (1000.2345);
611 AssertEquals ("#U01", "1000.2345000000000", ((SqlString)TestDecimal).Value);
615 public void SqlDoubleToSqlString()
617 SqlDouble TestDouble = new SqlDouble (64E+64);
618 AssertEquals ("#V01", "6.4E+65", ((SqlString)TestDouble).Value);
622 public void SqlGuidToSqlString()
624 byte [] b = new byte [16];
627 SqlGuid TestGuid = new SqlGuid (b);
629 AssertEquals ("#W01", "00004064-0000-0000-0000-000000000000",
630 ((SqlString)TestGuid).Value);
632 SqlString test = ((SqlString)SqlGuid.Null).Value;
634 } catch (Exception e) {
635 AssertEquals ("#W03", typeof (SqlNullValueException), e.GetType());
640 public void SqlInt16ToSqlString()
642 SqlInt16 TestInt = new SqlInt16(20012);
643 AssertEquals ("#X01", "20012", ((SqlString)TestInt).Value);
645 SqlString test = ((SqlString)SqlInt16.Null).Value;
647 } catch (Exception e) {
648 AssertEquals ("#X03", typeof (SqlNullValueException), e.GetType ());
653 public void SqlInt32ToSqlString()
655 SqlInt32 TestInt = new SqlInt32(-12456);
656 AssertEquals ("#Y01", "-12456", ((SqlString)TestInt).Value);
658 SqlString test = ((SqlString)SqlInt32.Null).Value;
660 } catch (Exception e) {
661 AssertEquals ("#Y03", typeof (SqlNullValueException), e.GetType ());
666 public void SqlInt64ToSqlString()
668 SqlInt64 TestInt = new SqlInt64(10101010);
669 AssertEquals ("#Z01", "10101010", ((SqlString)TestInt).Value);
673 public void SqlMoneyToSqlString()
675 SqlMoney TestMoney = new SqlMoney (646464.6464);
676 AssertEquals ("#AA01", "646464.6464", ((SqlString)TestMoney).Value);
680 public void SqlSingleToSqlString()
682 SqlSingle TestSingle = new SqlSingle (3E+20);
683 AssertEquals ("#AB01", "3E+20", ((SqlString)TestSingle).Value);
687 public void SqlStringToString()
689 AssertEquals ("#AC01", "First TestString",(String)Test1);
693 public void StringToSqlString()
695 String TestString = "Test String";
696 AssertEquals ("#AD01", "Test String", ((SqlString)TestString).Value);