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);
69 TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 2, 1);
71 } catch (Exception e) {
72 AssertEquals ("#A07c", typeof (ArgumentOutOfRangeException), e.GetType ());
76 TestString = new SqlString (2057, SqlCompareOptions.BinarySort, new byte [2] {113, 100}, 0, 4);
78 } catch (Exception e) {
79 AssertEquals ("#A07e", typeof (ArgumentOutOfRangeException), e.GetType ());
82 // SqlString (int, SqlCompareOptions, byte[], int, int, bool)
83 TestString = new SqlString (2057, SqlCompareOptions.IgnoreCase, new byte [3] {100, 111, 50}, 1, 2, false);
84 AssertEquals ("#A08", "o2", TestString.Value);
85 TestString = new SqlString (2057, SqlCompareOptions.IgnoreCase, new byte [3] {123, 111, 222}, 1, 2, true);
86 Assert ("#A09", !TestString.IsNull);
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);
140 public void CompareTo()
142 SqlByte Test = new SqlByte (1);
144 Assert ("#D01", Test1.CompareTo (Test3) < 0);
145 Assert ("#D02", Test2.CompareTo (Test1) > 0);
146 Assert ("#D03", Test2.CompareTo (Test3) == 0);
147 Assert ("#D04", Test3.CompareTo (SqlString.Null) > 0);
150 Test1.CompareTo (Test);
152 } catch(Exception e) {
153 AssertEquals ("#D06", typeof (ArgumentException), e.GetType ());
156 SqlString T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
157 SqlString T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
162 } catch (Exception e) {
163 AssertEquals ("#D08", typeof (SqlTypeException), e.GetType ());
167 T1 = new SqlString ("test", 2057, SqlCompareOptions.IgnoreCase);
168 T2 = new SqlString ("TEST", 2057, SqlCompareOptions.IgnoreCase);
169 Assert ("#D09", T2.CompareTo (T1) == 0);
171 T1 = new SqlString ("test", 2057);
172 T2 = new SqlString ("TEST", 2057);
173 Assert ("#D10", T2.CompareTo (T1) == 0);
175 T1 = new SqlString ("test", 2057, SqlCompareOptions.None);
176 T2 = new SqlString ("TEST", 2057, SqlCompareOptions.None);
177 Assert ("#D11", T2.CompareTo (T1) != 0);
180 T1 = new SqlString ("TESTñ", 2057, SqlCompareOptions.IgnoreNonSpace);
181 T2 = new SqlString ("TESTn", 2057, SqlCompareOptions.IgnoreNonSpace);
182 Assert ("#D12", T2.CompareTo (T1) == 0);
184 T1 = new SqlString ("TESTñ", 2057, SqlCompareOptions.None);
185 T2 = new SqlString ("TESTn", 2057, SqlCompareOptions.None);
186 Assert ("#D13", T2.CompareTo (T1) != 0);
189 T1 = new SqlString ("01_", 2057, SqlCompareOptions.BinarySort);
190 T2 = new SqlString ("_01", 2057, SqlCompareOptions.BinarySort);
191 Assert ("#D14", T1.CompareTo (T2) < 0);
193 T1 = new SqlString ("01_", 2057, SqlCompareOptions.None);
194 T2 = new SqlString ("_01", 2057, SqlCompareOptions.None);
195 Assert ("#D15", T1.CompareTo (T2) > 0);
199 public void EqualsMethods()
201 Assert ("#E01", !Test1.Equals (Test2));
202 Assert ("#E02", !Test3.Equals (Test1));
203 Assert ("#E03", !Test2.Equals (new SqlString ("TEST")));
204 Assert ("#E04", Test2.Equals (Test3));
206 // Static Equals()-method
207 Assert ("#E05", SqlString.Equals (Test2, Test3).Value);
208 Assert ("#E06", !SqlString.Equals (Test1, Test2).Value);
212 public void GetHashCodeTest()
214 // FIXME: Better way to test HashCode
215 AssertEquals ("#F01", Test1.GetHashCode (),
216 Test1.GetHashCode ());
217 Assert ("#F02", Test1.GetHashCode () != Test2.GetHashCode ());
218 Assert ("#F03", Test2.GetHashCode () == Test2.GetHashCode ());
222 public void GetTypeTest()
224 AssertEquals ("#G01", "System.Data.SqlTypes.SqlString",
225 Test1.GetType ().ToString ());
226 AssertEquals ("#G02", "System.String",
227 Test1.Value.GetType ().ToString ());
231 public void Greaters()
235 Assert ("#H01", !SqlString.GreaterThan (Test1, Test2).Value);
236 Assert ("#H02", SqlString.GreaterThan (Test2, Test1).Value);
237 Assert ("#H03", !SqlString.GreaterThan (Test2, Test3).Value);
239 // GreaterTharOrEqual ()
240 Assert ("#H04", !SqlString.GreaterThanOrEqual (Test1, Test2).Value);
241 Assert ("#H05", SqlString.GreaterThanOrEqual (Test2, Test1).Value);
242 Assert ("#H06", SqlString.GreaterThanOrEqual (Test2, Test3).Value);
246 public void Lessers()
249 Assert ("#I01", !SqlString.LessThan (Test2, Test3).Value);
250 Assert ("#I02", !SqlString.LessThan (Test2, Test1).Value);
251 Assert ("#I03", SqlString.LessThan (Test1, Test2).Value);
253 // LessThanOrEqual ()
254 Assert ("#I04", SqlString.LessThanOrEqual (Test1, Test2).Value);
255 Assert ("#I05", !SqlString.LessThanOrEqual (Test2, Test1).Value);
256 Assert ("#I06", SqlString.LessThanOrEqual (Test3, Test2).Value);
257 Assert ("#I07", SqlString.LessThanOrEqual (Test2, SqlString.Null).IsNull);
261 public void NotEquals()
263 Assert ("#J01", SqlString.NotEquals (Test1, Test2).Value);
264 Assert ("#J02", SqlString.NotEquals (Test2, Test1).Value);
265 Assert ("#J03", SqlString.NotEquals (Test3, Test1).Value);
266 Assert ("#J04", !SqlString.NotEquals (Test2, Test3).Value);
268 Assert ("#J05", SqlString.NotEquals (SqlString.Null, Test3).IsNull);
274 Test1 = new SqlString ("First TestString");
275 Test2 = new SqlString ("This is just a test SqlString");
276 Test3 = new SqlString ("This is just a test SqlString");
278 AssertEquals ("#K01",
279 (SqlString)"First TestStringThis is just a test SqlString",
280 SqlString.Concat (Test1, Test2));
282 AssertEquals ("#K02", SqlString.Null,
283 SqlString.Concat (Test1, SqlString.Null));
289 SqlString TestSqlString = Test1.Clone ();
290 AssertEquals ("#L01", Test1, TestSqlString);
294 public void CompareOptionsFromSqlCompareOptions()
296 AssertEquals ("#M01", CompareOptions.IgnoreCase,
297 SqlString.CompareOptionsFromSqlCompareOptions (
298 SqlCompareOptions.IgnoreCase));
299 AssertEquals ("#M02", CompareOptions.IgnoreCase,
300 SqlString.CompareOptionsFromSqlCompareOptions (
301 SqlCompareOptions.IgnoreCase));
304 CompareOptions test = SqlString.CompareOptionsFromSqlCompareOptions (
305 SqlCompareOptions.BinarySort);
307 } catch (Exception e) {
308 AssertEquals ("#M04", typeof (ArgumentOutOfRangeException), e.GetType ());
313 public void UnicodeBytes()
315 AssertEquals ("#N01", (byte)105, Test1.GetNonUnicodeBytes () [1]);
316 AssertEquals ("#N02", (byte)32, Test1.GetNonUnicodeBytes () [5]);
318 AssertEquals ("#N03", (byte)70, Test1.GetUnicodeBytes () [0]);
319 AssertEquals ("#N03b", (byte)70, Test1.GetNonUnicodeBytes () [0]);
320 AssertEquals ("#N03c", (byte)0, Test1.GetUnicodeBytes () [1]);
321 AssertEquals ("#N03d", (byte)105, Test1.GetNonUnicodeBytes () [1]);
322 AssertEquals ("#N03e", (byte)105, Test1.GetUnicodeBytes () [2]);
323 AssertEquals ("#N03f", (byte)114, Test1.GetNonUnicodeBytes () [2]);
324 AssertEquals ("#N03g", (byte)0, Test1.GetUnicodeBytes () [3]);
325 AssertEquals ("#N03h", (byte)115, Test1.GetNonUnicodeBytes () [3]);
326 AssertEquals ("#N03i", (byte)114, Test1.GetUnicodeBytes () [4]);
327 AssertEquals ("#N03j", (byte)116, Test1.GetNonUnicodeBytes () [4]);
329 AssertEquals ("#N04", (byte)105, Test1.GetUnicodeBytes () [2]);
332 byte test = Test1.GetUnicodeBytes () [105];
334 } catch (Exception e) {
335 AssertEquals ("#N06", typeof (IndexOutOfRangeException), e.GetType());
340 public void Conversions()
343 SqlString String250 = new SqlString ("250");
344 SqlString String9E300 = new SqlString ("9E+300");
349 bool test = Test1.ToSqlBoolean ().Value;
351 } catch (Exception e) {
352 AssertEquals ("#01.5", typeof (FormatException), e.GetType());
355 Assert ("#O02", (new SqlString("1")).ToSqlBoolean ().Value);
356 Assert ("#O03", !(new SqlString("0")).ToSqlBoolean ().Value);
357 Assert ("#O04", (new SqlString("True")).ToSqlBoolean ().Value);
358 Assert ("#O05", !(new SqlString("FALSE")).ToSqlBoolean ().Value);
359 Assert ("#O06", SqlString.Null.ToSqlBoolean ().IsNull);
363 byte test = Test1.ToSqlByte ().Value;
365 } catch (Exception e) {
366 AssertEquals ("#O07.5", typeof (FormatException), e.GetType());
369 AssertEquals ("#O08", (byte)250, String250.ToSqlByte ().Value);
371 SqlByte b = (byte)(new SqlString ("2500")).ToSqlByte ();
373 } catch (Exception e) {
374 AssertEquals ("#O10", typeof (OverflowException), e.GetType ());
378 AssertEquals ("#O11", 10,
379 (new SqlString ("2002-10-10")).ToSqlDateTime ().Value.Day);
383 AssertEquals ("#O13", (decimal)250, Test1.ToSqlDecimal ().Value);
385 } catch (Exception e) {
386 AssertEquals ("#O15", typeof (FormatException), e.GetType ());
389 AssertEquals ("#O16", (decimal)250, String250.ToSqlDecimal ().Value);
392 SqlDecimal test = String9E300.ToSqlDecimal ().Value;
394 } catch (Exception e) {
395 AssertEquals ("#O18", typeof (FormatException), e.GetType ());
399 AssertEquals ("#O19", (SqlDouble)9E+300, String9E300.ToSqlDouble ());
402 SqlDouble test = (new SqlString ("4e400")).ToSqlDouble ();
404 } catch (Exception e) {
405 AssertEquals ("#O21", typeof (OverflowException), e.GetType ());
409 SqlString TestGuid = new SqlString("11111111-1111-1111-1111-111111111111");
410 AssertEquals ("#O22", new SqlGuid("11111111-1111-1111-1111-111111111111"), TestGuid.ToSqlGuid ());
413 SqlGuid test = String9E300.ToSqlGuid ();
414 } catch (Exception e) {
415 AssertEquals ("#O23", typeof (FormatException), e.GetType ());
419 AssertEquals ("#O24", (short)250, String250.ToSqlInt16 ().Value);
422 SqlInt16 test = String9E300.ToSqlInt16().Value;
424 } catch (Exception e) {
425 AssertEquals ("#O26", typeof (FormatException), e.GetType ());
429 AssertEquals ("#O27", (int)250, String250.ToSqlInt32 ().Value);
432 SqlInt32 test = String9E300.ToSqlInt32 ().Value;
434 } catch (Exception e) {
435 AssertEquals ("#O29", typeof (FormatException), e.GetType ());
439 SqlInt32 test = Test1.ToSqlInt32 ().Value;
441 } catch (Exception e) {
442 AssertEquals ("#O31", typeof (FormatException), e.GetType ());
446 AssertEquals ("#O32", (long)250, String250.ToSqlInt64 ().Value);
449 SqlInt64 test = String9E300.ToSqlInt64 ().Value;
451 } catch (Exception e) {
452 AssertEquals ("#O34", typeof (FormatException), e.GetType ());
456 AssertEquals ("#O35", (decimal)250, String250.ToSqlMoney ().Value);
459 SqlMoney test = String9E300.ToSqlMoney ().Value;
461 } catch (Exception e) {
462 AssertEquals ("#O37", typeof (FormatException), e.GetType ());
466 AssertEquals ("#O38", (float)250, String250.ToSqlSingle ().Value);
469 SqlSingle test = String9E300.ToSqlSingle().Value;
471 } catch (Exception e) {
472 AssertEquals ("#O40", typeof (OverflowException), e.GetType ());
476 AssertEquals ("#O41", "First TestString", Test1.ToString ());
482 public void ArithmeticOperators()
484 SqlString TestString = new SqlString ("...Testing...");
485 AssertEquals ("#P01", (SqlString)"First TestString...Testing...",
487 AssertEquals ("#P02", SqlString.Null,
488 Test1 + SqlString.Null);
492 public void ThanOrEqualOperators()
495 Assert ("#Q01", (Test2 == Test3).Value);
496 Assert ("#Q02", !(Test1 == Test2).Value);
497 Assert ("#Q03", (Test1 == SqlString.Null).IsNull);
500 Assert ("#Q04", !(Test3 != Test2).Value);
501 Assert ("#Q05", !(Test2 != Test3).Value);
502 Assert ("#Q06", (Test1 != Test3).Value);
503 Assert ("#Q07", (Test1 != SqlString.Null).IsNull);
506 Assert ("#Q08", (Test2 > Test1).Value);
507 Assert ("#Q09", !(Test1 > Test3).Value);
508 Assert ("#Q10", !(Test2 > Test3).Value);
509 Assert ("#Q11", (Test1 > SqlString.Null).IsNull);
512 Assert ("#Q12", !(Test1 >= Test3).Value);
513 Assert ("#Q13", (Test3 >= Test1).Value);
514 Assert ("#Q14", (Test2 >= Test3).Value);
515 Assert ("#Q15", (Test1 >= SqlString.Null).IsNull);
518 Assert ("#Q16", (Test1 < Test2).Value);
519 Assert ("#Q17", (Test1 < Test3).Value);
520 Assert ("#Q18", !(Test2 < Test3).Value);
521 Assert ("#Q19", (Test1 < SqlString.Null).IsNull);
524 Assert ("#Q20", (Test1 <= Test3).Value);
525 Assert ("#Q21", !(Test3 <= Test1).Value);
526 Assert ("#Q22", (Test2 <= Test3).Value);
527 Assert ("#Q23", (Test1 <= SqlString.Null).IsNull);
531 public void SqlBooleanToSqlString()
533 SqlBoolean TestBoolean = new SqlBoolean (true);
534 SqlBoolean TestBoolean2 = new SqlBoolean (false);
537 Result = (SqlString)TestBoolean;
538 AssertEquals ("#R01", "True", Result.Value);
540 Result = (SqlString)TestBoolean2;
541 AssertEquals ("#R02", "False", Result.Value);
543 Result = (SqlString)SqlBoolean.Null;
544 Assert ("#R03", Result.IsNull);
548 public void SqlByteToBoolean()
550 SqlByte TestByte = new SqlByte (250);
551 AssertEquals ("#S01", "250", ((SqlString)TestByte).Value);
553 SqlString test = ((SqlString)SqlByte.Null).Value;
555 } catch (Exception e) {
556 AssertEquals ("#S03", typeof (SqlNullValueException), e.GetType ());
561 public void SqlDateTimeToSqlString()
563 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-AU");
564 SqlDateTime TestTime = new SqlDateTime(2002, 10, 22, 9, 52, 30);
565 AssertEquals ("#T01", "22/10/2002 9:52:30 AM", ((SqlString)TestTime).Value);
569 public void SqlDecimalToSqlString()
571 SqlDecimal TestDecimal = new SqlDecimal (1000.2345);
572 AssertEquals ("#U01", "1000.2345000000000", ((SqlString)TestDecimal).Value);
576 public void SqlDoubleToSqlString()
578 SqlDouble TestDouble = new SqlDouble (64E+64);
579 AssertEquals ("#V01", "6.4E+65", ((SqlString)TestDouble).Value);
583 public void SqlGuidToSqlString()
585 byte [] b = new byte [16];
588 SqlGuid TestGuid = new SqlGuid (b);
590 AssertEquals ("#W01", "00004064-0000-0000-0000-000000000000",
591 ((SqlString)TestGuid).Value);
593 SqlString test = ((SqlString)SqlGuid.Null).Value;
595 } catch (Exception e) {
596 AssertEquals ("#W03", typeof (SqlNullValueException), e.GetType());
601 public void SqlInt16ToSqlString()
603 SqlInt16 TestInt = new SqlInt16(20012);
604 AssertEquals ("#X01", "20012", ((SqlString)TestInt).Value);
606 SqlString test = ((SqlString)SqlInt16.Null).Value;
608 } catch (Exception e) {
609 AssertEquals ("#X03", typeof (SqlNullValueException), e.GetType ());
614 public void SqlInt32ToSqlString()
616 SqlInt32 TestInt = new SqlInt32(-12456);
617 AssertEquals ("#Y01", "-12456", ((SqlString)TestInt).Value);
619 SqlString test = ((SqlString)SqlInt32.Null).Value;
621 } catch (Exception e) {
622 AssertEquals ("#Y03", typeof (SqlNullValueException), e.GetType ());
627 public void SqlInt64ToSqlString()
629 SqlInt64 TestInt = new SqlInt64(10101010);
630 AssertEquals ("#Z01", "10101010", ((SqlString)TestInt).Value);
634 public void SqlMoneyToSqlString()
636 SqlMoney TestMoney = new SqlMoney (646464.6464);
637 AssertEquals ("#AA01", "646464.6464", ((SqlString)TestMoney).Value);
641 public void SqlSingleToSqlString()
643 SqlSingle TestSingle = new SqlSingle (3E+20);
644 AssertEquals ("#AB01", "3E+20", ((SqlString)TestSingle).Value);
648 public void SqlStringToString()
650 AssertEquals ("#AC01", "First TestString",(String)Test1);
654 public void StringToSqlString()
656 String TestString = "Test String";
657 AssertEquals ("#AD01", "Test String", ((SqlString)TestString).Value);