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
11 using NUnit.Framework;
13 using System.Data.SqlTypes;
14 using System.Globalization;
15 using System.Threading;
17 namespace MonoTests.System.Data.SqlTypes
20 public class SqlStringTest : Assertion {
22 private SqlString Test1 = null;
23 private SqlString Test2 = null;
24 private SqlString Test3 = null;
27 public void GetReady()
29 Test1 = new SqlString ("First TestString");
30 Test2 = new SqlString ("This is just a test SqlString");
31 Test3 = new SqlString ("This is just a test SqlString");
32 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-AU");
41 SqlString TestString = new SqlString ("Test");
42 AssertEquals ("#A01", "Test", TestString.Value);
44 // SqlString (String, int)
45 TestString = new SqlString ("Test", 2057);
46 AssertEquals ("#A02", 2057, TestString.LCID);
48 // SqlString (int, SqlCompareOptions, byte[])
49 TestString = new SqlString (2057,
50 SqlCompareOptions.BinarySort|SqlCompareOptions.IgnoreCase,
51 new byte [2] {123, 221});
52 AssertEquals ("#A03", 2057, TestString.CompareInfo.LCID);
54 // SqlString(string, int, SqlCompareOptions)
55 TestString = new SqlString ("Test", 2057, SqlCompareOptions.IgnoreNonSpace);
56 Assert ("#A04", !TestString.IsNull);
58 // SqlString (int, SqlCompareOptions, byte[], bool)
59 TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [4] {100, 100, 200, 45}, true);
60 AssertEquals ("#A05", (byte)63, TestString.GetNonUnicodeBytes () [0]);
61 TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, false);
62 AssertEquals ("#A06", (String)"qd", TestString.Value);
64 // SqlString (int, SqlCompareOptions, byte[], int, int)
65 TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 0, 2);
66 Assert ("#A07", !TestString.IsNull);
68 // SqlString (int, SqlCompareOptions, byte[], int, int, bool)
69 TestString = new SqlString (2057, SqlCompareOptions.IgnoreCase, new byte [3] {100, 111, 50}, 1, 2, false);
70 AssertEquals ("#A08", "o2", TestString.Value);
71 TestString = new SqlString (2057, SqlCompareOptions.IgnoreCase, new byte [3] {123, 111, 222}, 1, 2, true);
72 Assert ("#A09", !TestString.IsNull);
76 [ExpectedException(typeof (ArgumentOutOfRangeException))]
77 public void CtorArgumentOutOfRangeException1 ()
79 SqlString TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 2, 1);
83 [ExpectedException(typeof (ArgumentOutOfRangeException))]
84 public void CtorArgumentOutOfRangeException2 ()
86 SqlString TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 0, 4);
91 public void PublicFields()
94 AssertEquals ("#B01", 32768, SqlString.BinarySort);
97 AssertEquals ("#B02", 1, SqlString.IgnoreCase);
100 AssertEquals ("#B03", 8, SqlString.IgnoreKanaType);
103 AssertEquals ("#B04", 2, SqlString.IgnoreNonSpace);
106 AssertEquals ("#B05", 16, SqlString.IgnoreWidth);
109 Assert ("#B06", SqlString.Null.IsNull);
114 public void Properties()
117 AssertEquals ("#C01", 3081, Test1.CompareInfo.LCID);
120 AssertEquals ("#C02", 3081, Test1.CultureInfo.LCID);
123 Assert ("#C03", !Test1.IsNull);
124 Assert ("#C04", SqlString.Null.IsNull);
127 AssertEquals ("#C05", 3081, Test1.LCID);
130 AssertEquals ("#C06", "IgnoreCase, IgnoreKanaType, IgnoreWidth",
131 Test1.SqlCompareOptions.ToString ());
134 AssertEquals ("#C07", "First TestString", Test1.Value);
141 [ExpectedException(typeof(ArgumentException))]
142 public void CompareToArgumentException ()
144 SqlByte Test = new SqlByte (1);
145 Test1.CompareTo (Test);
149 [ExpectedException(typeof(SqlTypeException))]
150 public void CompareToSqlTypeException ()
152 SqlString T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
153 SqlString T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
158 public void CompareTo()
160 SqlByte Test = new SqlByte (1);
162 Assert ("#D01", Test1.CompareTo (Test3) < 0);
163 Assert ("#D02", Test2.CompareTo (Test1) > 0);
164 Assert ("#D03", Test2.CompareTo (Test3) == 0);
165 Assert ("#D04", Test3.CompareTo (SqlString.Null) > 0);
168 SqlString T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
169 SqlString T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
172 T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
173 T2 = new SqlString ("TEST", 2057, SqlCompareOptions.IgnoreCase);
174 Assert ("#D09", T2.CompareTo (T1) == 0);
176 T1 = new SqlString ("test", 2057);
177 T2 = new SqlString ("TEST", 2057);
178 Assert ("#D10", T2.CompareTo (T1) == 0);
180 T1 = new SqlString ("test", 2057, SqlCompareOptions.None);
181 T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
182 Assert ("#D11", T2.CompareTo (T1) != 0);
185 T1 = new SqlString ("TESTñ", 2057, SqlCompareOptions.IgnoreNonSpace);
186 T2 = new SqlString ("TESTn", 2057, SqlCompareOptions.IgnoreNonSpace);
187 Assert ("#D12", T2.CompareTo (T1) == 0);
189 T1 = new SqlString ("TESTñ", 2057, SqlCompareOptions.None);
190 T2 = new SqlString ("TESTn", 2057, SqlCompareOptions.None);
191 Assert ("#D13", T2.CompareTo (T1) != 0);
194 T1 = new SqlString ("01_", 2057, SqlCompareOptions.BinarySort);
195 T2 = new SqlString ("_01", 2057, SqlCompareOptions.BinarySort);
196 Assert ("#D14", T1.CompareTo (T2) < 0);
198 T1 = new SqlString ("01_", 2057, SqlCompareOptions.None);
199 T2 = new SqlString ("_01", 2057, SqlCompareOptions.None);
200 Assert ("#D15", T1.CompareTo (T2) > 0);
204 public void EqualsMethods()
206 Assert ("#E01", !Test1.Equals (Test2));
207 Assert ("#E02", !Test3.Equals (Test1));
208 Assert ("#E03", !Test2.Equals (new SqlString ("TEST")));
209 Assert ("#E04", Test2.Equals (Test3));
211 // Static Equals()-method
212 Assert ("#E05", SqlString.Equals (Test2, Test3).Value);
213 Assert ("#E06", !SqlString.Equals (Test1, Test2).Value);
217 public void GetHashCodeTest()
219 // FIXME: Better way to test HashCode
220 AssertEquals ("#F01", Test1.GetHashCode (),
221 Test1.GetHashCode ());
222 Assert ("#F02", Test1.GetHashCode () != Test2.GetHashCode ());
223 Assert ("#F03", Test2.GetHashCode () == Test2.GetHashCode ());
227 public void GetTypeTest()
229 AssertEquals ("#G01", "System.Data.SqlTypes.SqlString",
230 Test1.GetType ().ToString ());
231 AssertEquals ("#G02", "System.String",
232 Test1.Value.GetType ().ToString ());
236 public void Greaters()
240 Assert ("#H01", !SqlString.GreaterThan (Test1, Test2).Value);
241 Assert ("#H02", SqlString.GreaterThan (Test2, Test1).Value);
242 Assert ("#H03", !SqlString.GreaterThan (Test2, Test3).Value);
244 // GreaterTharOrEqual ()
245 Assert ("#H04", !SqlString.GreaterThanOrEqual (Test1, Test2).Value);
246 Assert ("#H05", SqlString.GreaterThanOrEqual (Test2, Test1).Value);
247 Assert ("#H06", SqlString.GreaterThanOrEqual (Test2, Test3).Value);
251 public void Lessers()
254 Assert ("#I01", !SqlString.LessThan (Test2, Test3).Value);
255 Assert ("#I02", !SqlString.LessThan (Test2, Test1).Value);
256 Assert ("#I03", SqlString.LessThan (Test1, Test2).Value);
258 // LessThanOrEqual ()
259 Assert ("#I04", SqlString.LessThanOrEqual (Test1, Test2).Value);
260 Assert ("#I05", !SqlString.LessThanOrEqual (Test2, Test1).Value);
261 Assert ("#I06", SqlString.LessThanOrEqual (Test3, Test2).Value);
262 Assert ("#I07", SqlString.LessThanOrEqual (Test2, SqlString.Null).IsNull);
266 public void NotEquals()
268 Assert ("#J01", SqlString.NotEquals (Test1, Test2).Value);
269 Assert ("#J02", SqlString.NotEquals (Test2, Test1).Value);
270 Assert ("#J03", SqlString.NotEquals (Test3, Test1).Value);
271 Assert ("#J04", !SqlString.NotEquals (Test2, Test3).Value);
273 Assert ("#J05", SqlString.NotEquals (SqlString.Null, Test3).IsNull);
279 Test1 = new SqlString ("First TestString");
280 Test2 = new SqlString ("This is just a test SqlString");
281 Test3 = new SqlString ("This is just a test SqlString");
283 AssertEquals ("#K01",
284 (SqlString)"First TestStringThis is just a test SqlString",
285 SqlString.Concat (Test1, Test2));
287 AssertEquals ("#K02", SqlString.Null,
288 SqlString.Concat (Test1, SqlString.Null));
294 SqlString TestSqlString = Test1.Clone ();
295 AssertEquals ("#L01", Test1, TestSqlString);
299 public void CompareOptionsFromSqlCompareOptions()
301 AssertEquals ("#M01", CompareOptions.IgnoreCase,
302 SqlString.CompareOptionsFromSqlCompareOptions (
303 SqlCompareOptions.IgnoreCase));
304 AssertEquals ("#M02", CompareOptions.IgnoreCase,
305 SqlString.CompareOptionsFromSqlCompareOptions (
306 SqlCompareOptions.IgnoreCase));
309 CompareOptions test = SqlString.CompareOptionsFromSqlCompareOptions (
310 SqlCompareOptions.BinarySort);
312 } catch (Exception e) {
313 AssertEquals ("#M04", typeof (ArgumentOutOfRangeException), e.GetType ());
318 public void UnicodeBytes()
320 AssertEquals ("#N01", (byte)105, Test1.GetNonUnicodeBytes () [1]);
321 AssertEquals ("#N02", (byte)32, Test1.GetNonUnicodeBytes () [5]);
323 AssertEquals ("#N03", (byte)70, Test1.GetUnicodeBytes () [0]);
324 AssertEquals ("#N03b", (byte)70, Test1.GetNonUnicodeBytes () [0]);
325 AssertEquals ("#N03c", (byte)0, Test1.GetUnicodeBytes () [1]);
326 AssertEquals ("#N03d", (byte)105, Test1.GetNonUnicodeBytes () [1]);
327 AssertEquals ("#N03e", (byte)105, Test1.GetUnicodeBytes () [2]);
328 AssertEquals ("#N03f", (byte)114, Test1.GetNonUnicodeBytes () [2]);
329 AssertEquals ("#N03g", (byte)0, Test1.GetUnicodeBytes () [3]);
330 AssertEquals ("#N03h", (byte)115, Test1.GetNonUnicodeBytes () [3]);
331 AssertEquals ("#N03i", (byte)114, Test1.GetUnicodeBytes () [4]);
332 AssertEquals ("#N03j", (byte)116, Test1.GetNonUnicodeBytes () [4]);
334 AssertEquals ("#N04", (byte)105, Test1.GetUnicodeBytes () [2]);
337 byte test = Test1.GetUnicodeBytes () [105];
339 } catch (Exception e) {
340 AssertEquals ("#N06", typeof (IndexOutOfRangeException), e.GetType());
345 [ExpectedException(typeof (FormatException))]
346 public void ConversionBoolFormatException1 ()
348 bool test = Test1.ToSqlBoolean ().Value;
352 [ExpectedException(typeof (FormatException))]
353 public void ConversionByteFormatException ()
355 byte test = Test1.ToSqlByte ().Value;
359 [ExpectedException(typeof (FormatException))]
360 public void ConversionDecimalFormatException1 ()
362 Decimal d = Test1.ToSqlDecimal ().Value;
366 [ExpectedException(typeof (FormatException))]
367 public void ConversionDecimalFormatException2 ()
369 SqlString String9E300 = new SqlString ("9E+300");
370 SqlDecimal test = String9E300.ToSqlDecimal ();
374 [ExpectedException(typeof (FormatException))]
375 public void ConversionGuidFormatException ()
377 SqlString String9E300 = new SqlString ("9E+300");
378 SqlGuid test = String9E300.ToSqlGuid ();
382 [ExpectedException(typeof (FormatException))]
383 public void ConversionInt16FormatException ()
385 SqlString String9E300 = new SqlString ("9E+300");
386 SqlInt16 test = String9E300.ToSqlInt16().Value;
390 [ExpectedException(typeof (FormatException))]
391 public void ConversionInt32FormatException1 ()
393 SqlString String9E300 = new SqlString ("9E+300");
394 SqlInt32 test = String9E300.ToSqlInt32 ().Value;
398 [ExpectedException(typeof (FormatException))]
399 public void ConversionInt32FormatException2 ()
401 SqlInt32 test = Test1.ToSqlInt32 ().Value;
405 [ExpectedException(typeof (FormatException))]
406 public void ConversionInt64FormatException ()
408 SqlString String9E300 = new SqlString ("9E+300");
409 SqlInt64 test = String9E300.ToSqlInt64 ().Value;
413 [ExpectedException(typeof (FormatException))]
414 public void ConversionIntMoneyFormatException2 ()
416 SqlString String9E300 = new SqlString ("9E+300");
417 SqlMoney test = String9E300.ToSqlMoney ().Value;
421 [ExpectedException(typeof(OverflowException))]
422 public void ConversionByteOverflowException ()
424 SqlByte b = (new SqlString ("2500")).ToSqlByte ();
428 [ExpectedException(typeof(OverflowException))]
429 public void ConversionDoubleOverflowException ()
431 SqlDouble test = (new SqlString ("4e400")).ToSqlDouble ();
435 [ExpectedException(typeof(OverflowException))]
436 public void ConversionSingleOverflowException ()
438 SqlString String9E300 = new SqlString ("9E+300");
439 SqlSingle test = String9E300.ToSqlSingle().Value;
443 public void Conversions()
446 SqlString String250 = new SqlString ("250");
447 SqlString String9E300 = new SqlString ("9E+300");
450 Assert ("#O02", (new SqlString("1")).ToSqlBoolean ().Value);
451 Assert ("#O03", !(new SqlString("0")).ToSqlBoolean ().Value);
452 Assert ("#O04", (new SqlString("True")).ToSqlBoolean ().Value);
453 Assert ("#O05", !(new SqlString("FALSE")).ToSqlBoolean ().Value);
454 Assert ("#O06", SqlString.Null.ToSqlBoolean ().IsNull);
457 AssertEquals ("#O08", (byte)250, String250.ToSqlByte ().Value);
460 AssertEquals ("#O11", 10,
461 (new SqlString ("2002-10-10")).ToSqlDateTime ().Value.Day);
464 AssertEquals ("#O16", (decimal)250, String250.ToSqlDecimal ().Value);
467 AssertEquals ("#O19", (SqlDouble)9E+300, String9E300.ToSqlDouble ());
470 SqlString TestGuid = new SqlString("11111111-1111-1111-1111-111111111111");
471 AssertEquals ("#O22", new SqlGuid("11111111-1111-1111-1111-111111111111"), TestGuid.ToSqlGuid ());
474 AssertEquals ("#O24", (short)250, String250.ToSqlInt16 ().Value);
477 AssertEquals ("#O27", (int)250, String250.ToSqlInt32 ().Value);
480 AssertEquals ("#O32", (long)250, String250.ToSqlInt64 ().Value);
483 AssertEquals ("#O35", (decimal)250, String250.ToSqlMoney ().Value);
487 AssertEquals ("#O38", (float)250, String250.ToSqlSingle ().Value);
490 AssertEquals ("#O41", "First TestString", Test1.ToString ());
496 public void ArithmeticOperators()
498 SqlString TestString = new SqlString ("...Testing...");
499 AssertEquals ("#P01", (SqlString)"First TestString...Testing...",
501 AssertEquals ("#P02", SqlString.Null,
502 Test1 + SqlString.Null);
506 public void ThanOrEqualOperators()
509 Assert ("#Q01", (Test2 == Test3).Value);
510 Assert ("#Q02", !(Test1 == Test2).Value);
511 Assert ("#Q03", (Test1 == SqlString.Null).IsNull);
514 Assert ("#Q04", !(Test3 != Test2).Value);
515 Assert ("#Q05", !(Test2 != Test3).Value);
516 Assert ("#Q06", (Test1 != Test3).Value);
517 Assert ("#Q07", (Test1 != SqlString.Null).IsNull);
520 Assert ("#Q08", (Test2 > Test1).Value);
521 Assert ("#Q09", !(Test1 > Test3).Value);
522 Assert ("#Q10", !(Test2 > Test3).Value);
523 Assert ("#Q11", (Test1 > SqlString.Null).IsNull);
526 Assert ("#Q12", !(Test1 >= Test3).Value);
527 Assert ("#Q13", (Test3 >= Test1).Value);
528 Assert ("#Q14", (Test2 >= Test3).Value);
529 Assert ("#Q15", (Test1 >= SqlString.Null).IsNull);
532 Assert ("#Q16", (Test1 < Test2).Value);
533 Assert ("#Q17", (Test1 < Test3).Value);
534 Assert ("#Q18", !(Test2 < Test3).Value);
535 Assert ("#Q19", (Test1 < SqlString.Null).IsNull);
538 Assert ("#Q20", (Test1 <= Test3).Value);
539 Assert ("#Q21", !(Test3 <= Test1).Value);
540 Assert ("#Q22", (Test2 <= Test3).Value);
541 Assert ("#Q23", (Test1 <= SqlString.Null).IsNull);
545 public void SqlBooleanToSqlString()
547 SqlBoolean TestBoolean = new SqlBoolean (true);
548 SqlBoolean TestBoolean2 = new SqlBoolean (false);
551 Result = (SqlString)TestBoolean;
552 AssertEquals ("#R01", "True", Result.Value);
554 Result = (SqlString)TestBoolean2;
555 AssertEquals ("#R02", "False", Result.Value);
557 Result = (SqlString)SqlBoolean.Null;
558 Assert ("#R03", Result.IsNull);
562 public void SqlByteToBoolean()
564 SqlByte TestByte = new SqlByte (250);
565 AssertEquals ("#S01", "250", ((SqlString)TestByte).Value);
567 SqlString test = ((SqlString)SqlByte.Null).Value;
569 } catch (Exception e) {
570 AssertEquals ("#S03", typeof (SqlNullValueException), e.GetType ());
575 public void SqlDateTimeToSqlString()
577 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-AU");
578 SqlDateTime TestTime = new SqlDateTime(2002, 10, 22, 9, 52, 30);
579 AssertEquals ("#T01", "22/10/2002 9:52:30 AM", ((SqlString)TestTime).Value);
583 public void SqlDecimalToSqlString()
585 SqlDecimal TestDecimal = new SqlDecimal (1000.2345);
586 AssertEquals ("#U01", "1000.2345000000000", ((SqlString)TestDecimal).Value);
590 public void SqlDoubleToSqlString()
592 SqlDouble TestDouble = new SqlDouble (64E+64);
593 AssertEquals ("#V01", "6.4E+65", ((SqlString)TestDouble).Value);
597 public void SqlGuidToSqlString()
599 byte [] b = new byte [16];
602 SqlGuid TestGuid = new SqlGuid (b);
604 AssertEquals ("#W01", "00004064-0000-0000-0000-000000000000",
605 ((SqlString)TestGuid).Value);
607 SqlString test = ((SqlString)SqlGuid.Null).Value;
609 } catch (Exception e) {
610 AssertEquals ("#W03", typeof (SqlNullValueException), e.GetType());
615 public void SqlInt16ToSqlString()
617 SqlInt16 TestInt = new SqlInt16(20012);
618 AssertEquals ("#X01", "20012", ((SqlString)TestInt).Value);
620 SqlString test = ((SqlString)SqlInt16.Null).Value;
622 } catch (Exception e) {
623 AssertEquals ("#X03", typeof (SqlNullValueException), e.GetType ());
628 public void SqlInt32ToSqlString()
630 SqlInt32 TestInt = new SqlInt32(-12456);
631 AssertEquals ("#Y01", "-12456", ((SqlString)TestInt).Value);
633 SqlString test = ((SqlString)SqlInt32.Null).Value;
635 } catch (Exception e) {
636 AssertEquals ("#Y03", typeof (SqlNullValueException), e.GetType ());
641 public void SqlInt64ToSqlString()
643 SqlInt64 TestInt = new SqlInt64(10101010);
644 AssertEquals ("#Z01", "10101010", ((SqlString)TestInt).Value);
648 public void SqlMoneyToSqlString()
650 SqlMoney TestMoney = new SqlMoney (646464.6464);
651 AssertEquals ("#AA01", "646464.6464", ((SqlString)TestMoney).Value);
655 public void SqlSingleToSqlString()
657 SqlSingle TestSingle = new SqlSingle (3E+20);
658 AssertEquals ("#AB01", "3E+20", ((SqlString)TestSingle).Value);
662 public void SqlStringToString()
664 AssertEquals ("#AC01", "First TestString",(String)Test1);
668 public void StringToSqlString()
670 String TestString = "Test String";
671 AssertEquals ("#AD01", "Test String", ((SqlString)TestString).Value);