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 CtorArgumentOutOfRangeException ()
79 SqlString TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 2, 1);
84 public void PublicFields()
87 AssertEquals ("#B01", 32768, SqlString.BinarySort);
90 AssertEquals ("#B02", 1, SqlString.IgnoreCase);
93 AssertEquals ("#B03", 8, SqlString.IgnoreKanaType);
96 AssertEquals ("#B04", 2, SqlString.IgnoreNonSpace);
99 AssertEquals ("#B05", 16, SqlString.IgnoreWidth);
102 Assert ("#B06", SqlString.Null.IsNull);
107 public void Properties()
110 AssertEquals ("#C01", 3081, Test1.CompareInfo.LCID);
113 AssertEquals ("#C02", 3081, Test1.CultureInfo.LCID);
116 Assert ("#C03", !Test1.IsNull);
117 Assert ("#C04", SqlString.Null.IsNull);
120 AssertEquals ("#C05", 3081, Test1.LCID);
123 AssertEquals ("#C06", "IgnoreCase, IgnoreKanaType, IgnoreWidth",
124 Test1.SqlCompareOptions.ToString ());
127 AssertEquals ("#C07", "First TestString", Test1.Value);
133 public void CompareTo()
135 SqlByte Test = new SqlByte (1);
137 Assert ("#D01", Test1.CompareTo (Test3) < 0);
138 Assert ("#D02", Test2.CompareTo (Test1) > 0);
139 Assert ("#D03", Test2.CompareTo (Test3) == 0);
140 Assert ("#D04", Test3.CompareTo (SqlString.Null) > 0);
143 Test1.CompareTo (Test);
145 } catch(Exception e) {
146 AssertEquals ("#D06", typeof (ArgumentException), e.GetType ());
149 SqlString T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
150 SqlString T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
155 } catch (Exception e) {
156 AssertEquals ("#D08", typeof (SqlTypeException), e.GetType ());
160 T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
161 T2 = new SqlString ("TEST", 2057, SqlCompareOptions.IgnoreCase);
162 Assert ("#D09", T2.CompareTo (T1) == 0);
164 T1 = new SqlString ("test", 2057);
165 T2 = new SqlString ("TEST", 2057);
166 Assert ("#D10", T2.CompareTo (T1) == 0);
168 T1 = new SqlString ("test", 2057, SqlCompareOptions.None);
169 T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
170 Assert ("#D11", T2.CompareTo (T1) != 0);
173 T1 = new SqlString ("TESTñ", 2057, SqlCompareOptions.IgnoreNonSpace);
174 T2 = new SqlString ("TESTn", 2057, SqlCompareOptions.IgnoreNonSpace);
175 Assert ("#D12", T2.CompareTo (T1) == 0);
177 T1 = new SqlString ("TESTñ", 2057, SqlCompareOptions.None);
178 T2 = new SqlString ("TESTn", 2057, SqlCompareOptions.None);
179 Assert ("#D13", T2.CompareTo (T1) != 0);
182 T1 = new SqlString ("01_", 2057, SqlCompareOptions.BinarySort);
183 T2 = new SqlString ("_01", 2057, SqlCompareOptions.BinarySort);
184 Assert ("#D14", T1.CompareTo (T2) < 0);
186 T1 = new SqlString ("01_", 2057, SqlCompareOptions.None);
187 T2 = new SqlString ("_01", 2057, SqlCompareOptions.None);
188 Assert ("#D15", T1.CompareTo (T2) > 0);
192 public void EqualsMethods()
194 Assert ("#E01", !Test1.Equals (Test2));
195 Assert ("#E02", !Test3.Equals (Test1));
196 Assert ("#E03", !Test2.Equals (new SqlString ("TEST")));
197 Assert ("#E04", Test2.Equals (Test3));
199 // Static Equals()-method
200 Assert ("#E05", SqlString.Equals (Test2, Test3).Value);
201 Assert ("#E06", !SqlString.Equals (Test1, Test2).Value);
205 public void GetHashCodeTest()
207 // FIXME: Better way to test HashCode
208 AssertEquals ("#F01", Test1.GetHashCode (),
209 Test1.GetHashCode ());
210 Assert ("#F02", Test1.GetHashCode () != Test2.GetHashCode ());
211 Assert ("#F03", Test2.GetHashCode () == Test2.GetHashCode ());
215 public void GetTypeTest()
217 AssertEquals ("#G01", "System.Data.SqlTypes.SqlString",
218 Test1.GetType ().ToString ());
219 AssertEquals ("#G02", "System.String",
220 Test1.Value.GetType ().ToString ());
224 public void Greaters()
228 Assert ("#H01", !SqlString.GreaterThan (Test1, Test2).Value);
229 Assert ("#H02", SqlString.GreaterThan (Test2, Test1).Value);
230 Assert ("#H03", !SqlString.GreaterThan (Test2, Test3).Value);
232 // GreaterTharOrEqual ()
233 Assert ("#H04", !SqlString.GreaterThanOrEqual (Test1, Test2).Value);
234 Assert ("#H05", SqlString.GreaterThanOrEqual (Test2, Test1).Value);
235 Assert ("#H06", SqlString.GreaterThanOrEqual (Test2, Test3).Value);
239 public void Lessers()
242 Assert ("#I01", !SqlString.LessThan (Test2, Test3).Value);
243 Assert ("#I02", !SqlString.LessThan (Test2, Test1).Value);
244 Assert ("#I03", SqlString.LessThan (Test1, Test2).Value);
246 // LessThanOrEqual ()
247 Assert ("#I04", SqlString.LessThanOrEqual (Test1, Test2).Value);
248 Assert ("#I05", !SqlString.LessThanOrEqual (Test2, Test1).Value);
249 Assert ("#I06", SqlString.LessThanOrEqual (Test3, Test2).Value);
250 Assert ("#I07", SqlString.LessThanOrEqual (Test2, SqlString.Null).IsNull);
254 public void NotEquals()
256 Assert ("#J01", SqlString.NotEquals (Test1, Test2).Value);
257 Assert ("#J02", SqlString.NotEquals (Test2, Test1).Value);
258 Assert ("#J03", SqlString.NotEquals (Test3, Test1).Value);
259 Assert ("#J04", !SqlString.NotEquals (Test2, Test3).Value);
261 Assert ("#J05", SqlString.NotEquals (SqlString.Null, Test3).IsNull);
267 Test1 = new SqlString ("First TestString");
268 Test2 = new SqlString ("This is just a test SqlString");
269 Test3 = new SqlString ("This is just a test SqlString");
271 AssertEquals ("#K01",
272 (SqlString)"First TestStringThis is just a test SqlString",
273 SqlString.Concat (Test1, Test2));
275 AssertEquals ("#K02", SqlString.Null,
276 SqlString.Concat (Test1, SqlString.Null));
282 SqlString TestSqlString = Test1.Clone ();
283 AssertEquals ("#L01", Test1, TestSqlString);
287 public void CompareOptionsFromSqlCompareOptions()
289 AssertEquals ("#M01", CompareOptions.IgnoreCase,
290 SqlString.CompareOptionsFromSqlCompareOptions (
291 SqlCompareOptions.IgnoreCase));
292 AssertEquals ("#M02", CompareOptions.IgnoreCase,
293 SqlString.CompareOptionsFromSqlCompareOptions (
294 SqlCompareOptions.IgnoreCase));
297 CompareOptions test = SqlString.CompareOptionsFromSqlCompareOptions (
298 SqlCompareOptions.BinarySort);
300 } catch (Exception e) {
301 AssertEquals ("#M04", typeof (ArgumentOutOfRangeException), e.GetType ());
306 public void UnicodeBytes()
308 AssertEquals ("#N01", (byte)105, Test1.GetNonUnicodeBytes () [1]);
309 AssertEquals ("#N02", (byte)32, Test1.GetNonUnicodeBytes () [5]);
311 AssertEquals ("#N03", (byte)70, Test1.GetUnicodeBytes () [0]);
312 AssertEquals ("#N03b", (byte)70, Test1.GetNonUnicodeBytes () [0]);
313 AssertEquals ("#N03c", (byte)0, Test1.GetUnicodeBytes () [1]);
314 AssertEquals ("#N03d", (byte)105, Test1.GetNonUnicodeBytes () [1]);
315 AssertEquals ("#N03e", (byte)105, Test1.GetUnicodeBytes () [2]);
316 AssertEquals ("#N03f", (byte)114, Test1.GetNonUnicodeBytes () [2]);
317 AssertEquals ("#N03g", (byte)0, Test1.GetUnicodeBytes () [3]);
318 AssertEquals ("#N03h", (byte)115, Test1.GetNonUnicodeBytes () [3]);
319 AssertEquals ("#N03i", (byte)114, Test1.GetUnicodeBytes () [4]);
320 AssertEquals ("#N03j", (byte)116, Test1.GetNonUnicodeBytes () [4]);
322 AssertEquals ("#N04", (byte)105, Test1.GetUnicodeBytes () [2]);
325 byte test = Test1.GetUnicodeBytes () [105];
327 } catch (Exception e) {
328 AssertEquals ("#N06", typeof (IndexOutOfRangeException), e.GetType());
333 public void Conversions()
336 SqlString String250 = new SqlString ("250");
337 SqlString String9E300 = new SqlString ("9E+300");
342 bool test = Test1.ToSqlBoolean ().Value;
344 } catch (Exception e) {
345 AssertEquals ("#01.5", typeof (FormatException), e.GetType());
348 Assert ("#O02", (new SqlString("1")).ToSqlBoolean ().Value);
349 Assert ("#O03", !(new SqlString("0")).ToSqlBoolean ().Value);
350 Assert ("#O04", (new SqlString("True")).ToSqlBoolean ().Value);
351 Assert ("#O05", !(new SqlString("FALSE")).ToSqlBoolean ().Value);
352 Assert ("#O06", SqlString.Null.ToSqlBoolean ().IsNull);
356 byte test = Test1.ToSqlByte ().Value;
358 } catch (Exception e) {
359 AssertEquals ("#O07.5", typeof (FormatException), e.GetType());
362 AssertEquals ("#O08", (byte)250, String250.ToSqlByte ().Value);
364 SqlByte b = (byte)(new SqlString ("2500")).ToSqlByte ();
366 } catch (Exception e) {
367 AssertEquals ("#O10", typeof (OverflowException), e.GetType ());
371 AssertEquals ("#O11", 10,
372 (new SqlString ("2002-10-10")).ToSqlDateTime ().Value.Day);
376 AssertEquals ("#O13", (decimal)250, Test1.ToSqlDecimal ().Value);
378 } catch (Exception e) {
379 AssertEquals ("#O15", typeof (FormatException), e.GetType ());
382 AssertEquals ("#O16", (decimal)250, String250.ToSqlDecimal ().Value);
385 SqlDecimal test = String9E300.ToSqlDecimal ().Value;
387 } catch (Exception e) {
388 AssertEquals ("#O18", typeof (FormatException), e.GetType ());
392 AssertEquals ("#O19", (SqlDouble)9E+300, String9E300.ToSqlDouble ());
395 SqlDouble test = (new SqlString ("4e400")).ToSqlDouble ();
397 } catch (Exception e) {
398 AssertEquals ("#O21", typeof (OverflowException), e.GetType ());
402 SqlString TestGuid = new SqlString("11111111-1111-1111-1111-111111111111");
403 AssertEquals ("#O22", new SqlGuid("11111111-1111-1111-1111-111111111111"), TestGuid.ToSqlGuid ());
406 SqlGuid test = String9E300.ToSqlGuid ();
407 } catch (Exception e) {
408 AssertEquals ("#O23", typeof (FormatException), e.GetType ());
412 AssertEquals ("#O24", (short)250, String250.ToSqlInt16 ().Value);
415 SqlInt16 test = String9E300.ToSqlInt16().Value;
417 } catch (Exception e) {
418 AssertEquals ("#O26", typeof (FormatException), e.GetType ());
422 AssertEquals ("#O27", (int)250, String250.ToSqlInt32 ().Value);
425 SqlInt32 test = String9E300.ToSqlInt32 ().Value;
427 } catch (Exception e) {
428 AssertEquals ("#O29", typeof (FormatException), e.GetType ());
432 SqlInt32 test = Test1.ToSqlInt32 ().Value;
434 } catch (Exception e) {
435 AssertEquals ("#O31", typeof (FormatException), e.GetType ());
439 AssertEquals ("#O32", (long)250, String250.ToSqlInt64 ().Value);
442 SqlInt64 test = String9E300.ToSqlInt64 ().Value;
444 } catch (Exception e) {
445 AssertEquals ("#O34", typeof (FormatException), e.GetType ());
449 AssertEquals ("#O35", (decimal)250, String250.ToSqlMoney ().Value);
452 SqlMoney test = String9E300.ToSqlMoney ().Value;
454 } catch (Exception e) {
455 AssertEquals ("#O37", typeof (FormatException), e.GetType ());
459 AssertEquals ("#O38", (float)250, String250.ToSqlSingle ().Value);
462 SqlSingle test = String9E300.ToSqlSingle().Value;
464 } catch (Exception e) {
465 AssertEquals ("#O40", typeof (OverflowException), e.GetType ());
469 AssertEquals ("#O41", "First TestString", Test1.ToString ());
475 public void ArithmeticOperators()
477 SqlString TestString = new SqlString ("...Testing...");
478 AssertEquals ("#P01", (SqlString)"First TestString...Testing...",
480 AssertEquals ("#P02", SqlString.Null,
481 Test1 + SqlString.Null);
485 public void ThanOrEqualOperators()
488 Assert ("#Q01", (Test2 == Test3).Value);
489 Assert ("#Q02", !(Test1 == Test2).Value);
490 Assert ("#Q03", (Test1 == SqlString.Null).IsNull);
493 Assert ("#Q04", !(Test3 != Test2).Value);
494 Assert ("#Q05", !(Test2 != Test3).Value);
495 Assert ("#Q06", (Test1 != Test3).Value);
496 Assert ("#Q07", (Test1 != SqlString.Null).IsNull);
499 Assert ("#Q08", (Test2 > Test1).Value);
500 Assert ("#Q09", !(Test1 > Test3).Value);
501 Assert ("#Q10", !(Test2 > Test3).Value);
502 Assert ("#Q11", (Test1 > SqlString.Null).IsNull);
505 Assert ("#Q12", !(Test1 >= Test3).Value);
506 Assert ("#Q13", (Test3 >= Test1).Value);
507 Assert ("#Q14", (Test2 >= Test3).Value);
508 Assert ("#Q15", (Test1 >= SqlString.Null).IsNull);
511 Assert ("#Q16", (Test1 < Test2).Value);
512 Assert ("#Q17", (Test1 < Test3).Value);
513 Assert ("#Q18", !(Test2 < Test3).Value);
514 Assert ("#Q19", (Test1 < SqlString.Null).IsNull);
517 Assert ("#Q20", (Test1 <= Test3).Value);
518 Assert ("#Q21", !(Test3 <= Test1).Value);
519 Assert ("#Q22", (Test2 <= Test3).Value);
520 Assert ("#Q23", (Test1 <= SqlString.Null).IsNull);
524 public void SqlBooleanToSqlString()
526 SqlBoolean TestBoolean = new SqlBoolean (true);
527 SqlBoolean TestBoolean2 = new SqlBoolean (false);
530 Result = (SqlString)TestBoolean;
531 AssertEquals ("#R01", "True", Result.Value);
533 Result = (SqlString)TestBoolean2;
534 AssertEquals ("#R02", "False", Result.Value);
536 Result = (SqlString)SqlBoolean.Null;
537 Assert ("#R03", Result.IsNull);
541 public void SqlByteToBoolean()
543 SqlByte TestByte = new SqlByte (250);
544 AssertEquals ("#S01", "250", ((SqlString)TestByte).Value);
546 SqlString test = ((SqlString)SqlByte.Null).Value;
548 } catch (Exception e) {
549 AssertEquals ("#S03", typeof (SqlNullValueException), e.GetType ());
554 public void SqlDateTimeToSqlString()
556 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-AU");
557 SqlDateTime TestTime = new SqlDateTime(2002, 10, 22, 9, 52, 30);
558 AssertEquals ("#T01", "22/10/2002 9:52:30 AM", ((SqlString)TestTime).Value);
562 public void SqlDecimalToSqlString()
564 SqlDecimal TestDecimal = new SqlDecimal (1000.2345);
565 AssertEquals ("#U01", "1000.2345000000000", ((SqlString)TestDecimal).Value);
569 public void SqlDoubleToSqlString()
571 SqlDouble TestDouble = new SqlDouble (64E+64);
572 AssertEquals ("#V01", "6.4E+65", ((SqlString)TestDouble).Value);
576 public void SqlGuidToSqlString()
578 byte [] b = new byte [16];
581 SqlGuid TestGuid = new SqlGuid (b);
583 AssertEquals ("#W01", "00004064-0000-0000-0000-000000000000",
584 ((SqlString)TestGuid).Value);
586 SqlString test = ((SqlString)SqlGuid.Null).Value;
588 } catch (Exception e) {
589 AssertEquals ("#W03", typeof (SqlNullValueException), e.GetType());
594 public void SqlInt16ToSqlString()
596 SqlInt16 TestInt = new SqlInt16(20012);
597 AssertEquals ("#X01", "20012", ((SqlString)TestInt).Value);
599 SqlString test = ((SqlString)SqlInt16.Null).Value;
601 } catch (Exception e) {
602 AssertEquals ("#X03", typeof (SqlNullValueException), e.GetType ());
607 public void SqlInt32ToSqlString()
609 SqlInt32 TestInt = new SqlInt32(-12456);
610 AssertEquals ("#Y01", "-12456", ((SqlString)TestInt).Value);
612 SqlString test = ((SqlString)SqlInt32.Null).Value;
614 } catch (Exception e) {
615 AssertEquals ("#Y03", typeof (SqlNullValueException), e.GetType ());
620 public void SqlInt64ToSqlString()
622 SqlInt64 TestInt = new SqlInt64(10101010);
623 AssertEquals ("#Z01", "10101010", ((SqlString)TestInt).Value);
627 public void SqlMoneyToSqlString()
629 SqlMoney TestMoney = new SqlMoney (646464.6464);
630 AssertEquals ("#AA01", "646464.6464", ((SqlString)TestMoney).Value);
634 public void SqlSingleToSqlString()
636 SqlSingle TestSingle = new SqlSingle (3E+20);
637 AssertEquals ("#AB01", "3E+20", ((SqlString)TestSingle).Value);
641 public void SqlStringToString()
643 AssertEquals ("#AC01", "First TestString",(String)Test1);
647 public void StringToSqlString()
649 String TestString = "Test String";
650 AssertEquals ("#AD01", "Test String", ((SqlString)TestString).Value);