**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Data / Test / System.Data.SqlTypes / SqlSingleTest.cs
blob3bf11bcaa1de153bcf99a1025cda17fb04423161
1 //
2 // SqlSingleTest.cs - NUnit Test Cases for System.Data.SqlTypes.SqlSingle
3 //
4 // Authors:
5 // Ville Palo (vi64pa@koti.soon.fi)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
8 // (C) 2002 Ville Palo
9 // (C) 2003 Martin Willemoes Hansen
10 //
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
22 //
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
25 //
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using NUnit.Framework;
36 using System;
37 using System.Data.SqlTypes;
38 using System.Threading;
39 using System.Globalization;
41 namespace MonoTests.System.Data.SqlTypes
43 [TestFixture]
44 public class SqlSingleTest : Assertion {
46 [SetUp]
47 public void GetReady()
49 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
52 // Test constructor
53 [Test]
54 public void Create()
56 SqlSingle Test= new SqlSingle ((float)34.87);
57 SqlSingle Test2 = 45.2f;
59 AssertEquals ("#A01", 34.87f, Test.Value);
60 AssertEquals ("#A02", 45.2f, Test2.Value);
62 Test = new SqlSingle (-9000.6543);
63 AssertEquals ("#A03", -9000.6543f, Test.Value);
66 // Test public fields
67 [Test]
68 public void PublicFields()
70 AssertEquals ("#B01", 3.40282346638528859E+38f,
71 SqlSingle.MaxValue.Value);
72 AssertEquals ("#B02", -3.40282346638528859E+38f,
73 SqlSingle.MinValue.Value);
74 Assert ("#B03", SqlSingle.Null.IsNull);
75 AssertEquals ("#B04", 0f, SqlSingle.Zero.Value);
78 // Test properties
79 [Test]
80 public void Properties()
82 SqlSingle Test = new SqlSingle (5443e12f);
83 SqlSingle Test1 = new SqlSingle (1);
85 Assert ("#C01", SqlSingle.Null.IsNull);
86 AssertEquals ("#C02", 5443e12f, Test.Value);
87 AssertEquals ("#C03", (float)1, Test1.Value);
90 // PUBLIC METHODS
92 [Test]
93 public void ArithmeticMethods()
95 SqlSingle Test0 = new SqlSingle (0);
96 SqlSingle Test1 = new SqlSingle (15E+18);
97 SqlSingle Test2 = new SqlSingle (-65E+6);
98 SqlSingle Test3 = new SqlSingle (5E+30);
99 SqlSingle Test4 = new SqlSingle (5E+18);
100 SqlSingle TestMax = new SqlSingle (SqlSingle.MaxValue.Value);
102 // Add()
103 AssertEquals ("#D01A", 15E+18f, SqlSingle.Add (Test1, Test0).Value);
104 AssertEquals ("#D02A", 1.5E+19f, SqlSingle.Add (Test1, Test2).Value);
106 try {
107 SqlSingle test = SqlSingle.Add (SqlSingle.MaxValue,
108 SqlSingle.MaxValue);
109 Fail ("#D03A");
110 } catch (Exception e) {
111 AssertEquals ("#D04A", typeof (OverflowException), e.GetType ());
114 // Divide()
115 AssertEquals ("#D01B", (SqlSingle)3, SqlSingle.Divide (Test1, Test4));
116 AssertEquals ("#D02B", -1.3E-23f, SqlSingle.Divide (Test2, Test3).Value);
118 try {
119 SqlSingle test = SqlSingle.Divide(Test1, Test0).Value;
120 Fail ("#D03B");
121 } catch(Exception e) {
122 AssertEquals ("#D04B", typeof (DivideByZeroException),
123 e.GetType ());
126 // Multiply()
127 AssertEquals ("#D01D", (float)(7.5E+37),
128 SqlSingle.Multiply (Test1, Test4).Value);
129 AssertEquals ("#D02D", (float)0, SqlSingle.Multiply (Test1, Test0).Value);
131 try {
132 SqlSingle test = SqlSingle.Multiply (TestMax, Test1);
133 Fail ("#D03D");
134 } catch (Exception e) {
135 AssertEquals ("#D04D", typeof (OverflowException), e.GetType ());
139 // Subtract()
140 AssertEquals ("#D01F", (float)(-5E+30),
141 SqlSingle.Subtract (Test1, Test3).Value);
143 try {
144 SqlSingle test = SqlSingle.Subtract(
145 SqlSingle.MinValue, SqlSingle.MaxValue);
146 Fail ("D02F");
147 } catch (Exception e) {
148 AssertEquals ("#D03F", typeof (OverflowException), e.GetType ());
152 [Test]
153 public void CompareTo()
155 SqlSingle Test1 = new SqlSingle (4E+30);
156 SqlSingle Test11 = new SqlSingle (4E+30);
157 SqlSingle Test2 = new SqlSingle (-9E+30);
158 SqlSingle Test3 = new SqlSingle (10000);
159 SqlString TestString = new SqlString ("This is a test");
161 Assert ("#E01", Test1.CompareTo (Test3) > 0);
162 Assert ("#E02", Test2.CompareTo (Test3) < 0);
163 Assert ("#E03", Test1.CompareTo (Test11) == 0);
164 Assert ("#E04", Test11.CompareTo (SqlSingle.Null) > 0);
166 try {
167 Test1.CompareTo (TestString);
168 Fail("#E05");
169 } catch(Exception e) {
170 AssertEquals ("#E06", typeof (ArgumentException), e.GetType ());
174 [Test]
175 public void EqualsMethods()
177 SqlSingle Test0 = new SqlSingle (0);
178 SqlSingle Test1 = new SqlSingle (1.58e30);
179 SqlSingle Test2 = new SqlSingle (1.8e32);
180 SqlSingle Test22 = new SqlSingle (1.8e32);
182 Assert ("#F01", !Test0.Equals (Test1));
183 Assert ("#F02", !Test1.Equals (Test2));
184 Assert ("#F03", !Test2.Equals (new SqlString ("TEST")));
185 Assert ("#F04", Test2.Equals (Test22));
187 // Static Equals()-method
188 Assert ("#F05", SqlSingle.Equals (Test2, Test22).Value);
189 Assert ("#F06", !SqlSingle.Equals (Test1, Test2).Value);
192 [Test]
193 public void GetHashCodeTest()
195 SqlSingle Test15 = new SqlSingle (15);
197 // FIXME: Better way to test HashCode
198 AssertEquals ("#G01", Test15.GetHashCode (), Test15.GetHashCode ());
201 [Test]
202 public void GetTypeTest()
204 SqlSingle Test = new SqlSingle (84);
205 AssertEquals ("#H01", "System.Data.SqlTypes.SqlSingle",
206 Test.GetType ().ToString ());
207 AssertEquals ("#H02", "System.Single", Test.Value.GetType ().ToString ());
210 [Test]
211 public void Greaters()
213 SqlSingle Test1 = new SqlSingle (1e10);
214 SqlSingle Test11 = new SqlSingle (1e10);
215 SqlSingle Test2 = new SqlSingle (64e14);
217 // GreateThan ()
218 Assert ("#I01", !SqlSingle.GreaterThan (Test1, Test2).Value);
219 Assert ("#I02", SqlSingle.GreaterThan (Test2, Test1).Value);
220 Assert ("#I03", !SqlSingle.GreaterThan (Test1, Test11).Value);
222 // GreaterTharOrEqual ()
223 Assert ("#I04", !SqlSingle.GreaterThanOrEqual (Test1, Test2).Value);
224 Assert ("#I05", SqlSingle.GreaterThanOrEqual (Test2, Test1).Value);
225 Assert ("#I06", SqlSingle.GreaterThanOrEqual (Test1, Test11).Value);
228 [Test]
229 public void Lessers()
231 SqlSingle Test1 = new SqlSingle(1.8e10);
232 SqlSingle Test11 = new SqlSingle (1.8e10);
233 SqlSingle Test2 = new SqlSingle (64e14);
235 // LessThan()
236 Assert ("#J01", !SqlSingle.LessThan (Test1, Test11).Value);
237 Assert ("#J02", !SqlSingle.LessThan (Test2, Test1).Value);
238 Assert ("#J03", SqlSingle.LessThan (Test11, Test2).Value);
240 // LessThanOrEqual ()
241 Assert ("#J04", SqlSingle.LessThanOrEqual (Test1, Test2).Value);
242 Assert ("#J05", !SqlSingle.LessThanOrEqual (Test2, Test1).Value);
243 Assert ("#J06", SqlSingle.LessThanOrEqual (Test11, Test1).Value);
244 Assert ("#J07", SqlSingle.LessThanOrEqual (Test11, SqlSingle.Null).IsNull);
247 [Test]
248 public void NotEquals()
250 SqlSingle Test1 = new SqlSingle (12800000000001);
251 SqlSingle Test2 = new SqlSingle (128e10);
252 SqlSingle Test22 = new SqlSingle (128e10);
254 Assert ("#K01", SqlSingle.NotEquals (Test1, Test2).Value);
255 Assert ("#K02", SqlSingle.NotEquals (Test2, Test1).Value);
256 Assert ("#K03", SqlSingle.NotEquals (Test22, Test1).Value);
257 Assert ("#K04", !SqlSingle.NotEquals (Test22, Test2).Value);
258 Assert ("#K05", !SqlSingle.NotEquals (Test2, Test22).Value);
259 Assert ("#K06", SqlSingle.NotEquals (SqlSingle.Null, Test22).IsNull);
260 Assert ("#K07", SqlSingle.NotEquals (SqlSingle.Null, Test22).IsNull);
263 [Test]
264 public void Parse()
266 try {
267 SqlSingle.Parse (null);
268 Fail ("#L01");
269 } catch (Exception e) {
270 AssertEquals ("#L02", typeof (ArgumentNullException), e.GetType ());
273 try {
274 SqlSingle.Parse ("not-a-number");
275 Fail ("#L03");
276 } catch (Exception e) {
277 AssertEquals ("#L04", typeof (FormatException), e.GetType ());
280 try {
281 SqlSingle.Parse ("9e44");
282 Fail ("#L05");
283 } catch (Exception e) {
284 AssertEquals ("#L06", typeof (OverflowException), e.GetType ());
287 AssertEquals("#L07", (float)150, SqlSingle.Parse ("150").Value);
290 [Test]
291 public void Conversions()
293 SqlSingle Test0 = new SqlSingle (0);
294 SqlSingle Test1 = new SqlSingle (250);
295 SqlSingle Test2 = new SqlSingle (64E+16);
296 SqlSingle Test3 = new SqlSingle (64E+30);
297 SqlSingle TestNull = SqlSingle.Null;
299 // ToSqlBoolean ()
300 Assert ("#M01A", Test1.ToSqlBoolean ().Value);
301 Assert ("#M02A", !Test0.ToSqlBoolean ().Value);
302 Assert ("#M03A", TestNull.ToSqlBoolean ().IsNull);
304 // ToSqlByte ()
305 AssertEquals ("#M01B", (byte)250, Test1.ToSqlByte ().Value);
306 AssertEquals ("#M02B", (byte)0, Test0.ToSqlByte ().Value);
308 try {
309 SqlByte b = (byte)Test2.ToSqlByte ();
310 Fail ("#M03B");
311 } catch (Exception e) {
312 AssertEquals ("#M04B", typeof (OverflowException), e.GetType ());
315 // ToSqlDecimal ()
316 AssertEquals ("#M01C", 250.00000000000000M, Test1.ToSqlDecimal ().Value);
317 AssertEquals ("#M02C", (decimal)0, Test0.ToSqlDecimal ().Value);
319 try {
320 SqlDecimal test = Test3.ToSqlDecimal ().Value;
321 Fail ("#M03C");
322 } catch (Exception e) {
323 AssertEquals ("#M04C", typeof (OverflowException), e.GetType ());
326 // ToSqlInt16 ()
327 AssertEquals ("#M01D", (short)250, Test1.ToSqlInt16 ().Value);
328 AssertEquals ("#M02D", (short)0, Test0.ToSqlInt16 ().Value);
330 try {
331 SqlInt16 test = Test2.ToSqlInt16().Value;
332 Fail ("#M03D");
333 } catch (Exception e) {
334 AssertEquals ("#M04D", typeof (OverflowException), e.GetType ());
337 // ToSqlInt32 ()
338 AssertEquals ("#M01E", (int)250, Test1.ToSqlInt32 ().Value);
339 AssertEquals ("#M02E", (int)0, Test0.ToSqlInt32 ().Value);
341 try {
342 SqlInt32 test = Test2.ToSqlInt32 ().Value;
343 Fail ("#M03E");
344 } catch (Exception e) {
345 AssertEquals ("#M04E", typeof (OverflowException), e.GetType ());
348 // ToSqlInt64 ()
349 AssertEquals ("#M01F", (long)250, Test1.ToSqlInt64 ().Value);
350 AssertEquals ("#M02F", (long)0, Test0.ToSqlInt64 ().Value);
352 try {
353 SqlInt64 test = Test3.ToSqlInt64 ().Value;
354 Fail ("#M03F");
355 } catch (Exception e) {
356 AssertEquals ("#M04F", typeof (OverflowException), e.GetType ());
359 // ToSqlMoney ()
360 AssertEquals ("#M01G", 250.0000M, Test1.ToSqlMoney ().Value);
361 AssertEquals ("#M02G", (decimal)0, Test0.ToSqlMoney ().Value);
363 try {
364 SqlMoney test = Test3.ToSqlMoney ().Value;
365 Fail ("#M03G");
366 } catch (Exception e) {
367 AssertEquals ("#M04G", typeof (OverflowException), e.GetType ());
371 // ToSqlString ()
372 AssertEquals ("#M01H", "250", Test1.ToSqlString ().Value);
373 AssertEquals ("#M02H", "0", Test0.ToSqlString ().Value);
374 AssertEquals ("#M03H", "6.4E+17", Test2.ToSqlString ().Value);
376 // ToString ()
377 AssertEquals ("#M01I", "250", Test1.ToString ());
378 AssertEquals ("#M02I", "0", Test0.ToString ());
379 AssertEquals ("#M03I", "6.4E+17", Test2.ToString ());
382 // OPERATORS
384 [Test]
385 public void ArithmeticOperators()
387 SqlSingle Test0 = new SqlSingle (0);
388 SqlSingle Test1 = new SqlSingle (24E+11);
389 SqlSingle Test2 = new SqlSingle (64E+32);
390 SqlSingle Test3 = new SqlSingle (12E+11);
391 SqlSingle Test4 = new SqlSingle (1E+10);
392 SqlSingle Test5 = new SqlSingle (2E+10);
394 // "+"-operator
395 AssertEquals ("#N01", (SqlSingle)3E+10, Test4 + Test5);
397 try {
398 SqlSingle test = SqlSingle.MaxValue + SqlSingle.MaxValue;
399 Fail ("#N02");
400 } catch (Exception e) {
401 AssertEquals ("#N03", typeof (OverflowException), e.GetType ());
404 try {
405 SqlSingle test = SqlSingle.MaxValue + SqlSingle.MaxValue;
406 } catch (Exception e) {
407 AssertEquals ("#N03a", typeof (OverflowException), e.GetType ());
410 // "/"-operator
411 AssertEquals ("#N04", (SqlSingle)2, Test1 / Test3);
413 try {
414 SqlSingle test = Test3 / Test0;
415 Fail ("#N05");
416 } catch (Exception e) {
417 AssertEquals ("#N06", typeof (DivideByZeroException), e.GetType ());
420 // "*"-operator
421 AssertEquals ("#N07", (SqlSingle)2E+20, Test4 * Test5);
423 try {
424 SqlSingle test = SqlSingle.MaxValue * Test1;
425 Fail ("#N08");
426 } catch (Exception e) {
427 AssertEquals ("#N09", typeof (OverflowException), e.GetType ());
430 // "-"-operator
431 AssertEquals ("#N10", (SqlSingle)12e11, Test1 - Test3);
433 try {
434 SqlSingle test = SqlSingle.MinValue - SqlSingle.MaxValue;
435 Fail ("#N11");
436 } catch (Exception e) {
437 AssertEquals ("#N12", typeof (OverflowException), e.GetType ());
441 [Test]
442 public void ThanOrEqualOperators()
444 SqlSingle Test1 = new SqlSingle (1.0E+14f);
445 SqlSingle Test2 = new SqlSingle (9.7E+11);
446 SqlSingle Test22 = new SqlSingle (9.7E+11);
447 SqlSingle Test3 = new SqlSingle (2.0E+22f);
449 // == -operator
450 Assert ("#O01", (Test2 == Test22).Value);
451 Assert ("#O02", !(Test1 == Test2).Value);
452 Assert ("#O03", (Test1 == SqlSingle.Null).IsNull);
454 // != -operator
455 Assert ("#O04", !(Test2 != Test22).Value);
456 Assert ("#O05", (Test2 != Test3).Value);
457 Assert ("#O06", (Test1 != Test3).Value);
458 Assert ("#O07", (Test1 != SqlSingle.Null).IsNull);
460 // > -operator
461 Assert ("#O08", (Test1 > Test2).Value);
462 Assert ("#O09", !(Test1 > Test3).Value);
463 Assert ("#O10", !(Test2 > Test22).Value);
464 Assert ("#O11", (Test1 > SqlSingle.Null).IsNull);
466 // >= -operator
467 Assert ("#O12", !(Test1 >= Test3).Value);
468 Assert ("#O13", (Test3 >= Test1).Value);
469 Assert ("#O14", (Test2 >= Test22).Value);
470 Assert ("#O15", (Test1 >= SqlSingle.Null).IsNull);
472 // < -operator
473 Assert ("#O16", !(Test1 < Test2).Value);
474 Assert ("#O17", (Test1 < Test3).Value);
475 Assert ("#O18", !(Test2 < Test22).Value);
476 Assert ("#O19", (Test1 < SqlSingle.Null).IsNull);
478 // <= -operator
479 Assert ("#O20", (Test1 <= Test3).Value);
480 Assert ("#O21", !(Test3 <= Test1).Value);
481 Assert ("#O22", (Test2 <= Test22).Value);
482 Assert ("#O23", (Test1 <= SqlSingle.Null).IsNull);
485 [Test]
486 public void UnaryNegation()
488 SqlSingle Test = new SqlSingle (2000000001);
489 SqlSingle TestNeg = new SqlSingle (-3000);
491 SqlSingle Result = -Test;
492 AssertEquals ("#P01", (float)(-2000000001), Result.Value);
494 Result = -TestNeg;
495 AssertEquals ("#P02", (float)3000, Result.Value);
498 [Test]
499 public void SqlBooleanToSqlSingle()
501 SqlBoolean TestBoolean = new SqlBoolean (true);
502 SqlSingle Result;
504 Result = (SqlSingle)TestBoolean;
506 AssertEquals ("#Q01", (float)1, Result.Value);
508 Result = (SqlSingle)SqlBoolean.Null;
509 Assert ("#Q02", Result.IsNull);
512 [Test]
513 public void SqlDoubleToSqlSingle()
515 SqlDouble Test = new SqlDouble (12e12);
516 SqlSingle TestSqlSingle = (SqlSingle)Test;
517 AssertEquals ("R01", 12e12f, TestSqlSingle.Value);
520 [Test]
521 public void SqlSingleToSingle()
523 SqlSingle Test = new SqlSingle (12e12);
524 Single Result = (Single)Test;
525 AssertEquals ("#S01", 12e12f, Result);
528 [Test]
529 public void SqlStringToSqlSingle()
531 SqlString TestString = new SqlString ("Test string");
532 SqlString TestString100 = new SqlString ("100");
534 AssertEquals ("#T01", (float)100, ((SqlSingle)TestString100).Value);
536 try {
537 SqlSingle test = (SqlSingle)TestString;
538 Fail ("#T02");
539 } catch(Exception e) {
540 AssertEquals ("#T03", typeof (FormatException), e.GetType ());
544 [Test]
545 public void ByteToSqlSingle()
547 short TestShort = 14;
548 AssertEquals ("#U01", (float)14, ((SqlSingle)TestShort).Value);
551 [Test]
552 public void SqlDecimalToSqlSingle()
554 SqlDecimal TestDecimal64 = new SqlDecimal (64);
556 AssertEquals ("#V01", (float)64, ((SqlSingle)TestDecimal64).Value);
557 AssertEquals ("#V02", SqlSingle.Null, ((SqlSingle)SqlDecimal.Null));
560 [Test]
561 public void SqlIntToSqlSingle()
563 SqlInt16 Test64 = new SqlInt16 (64);
564 SqlInt32 Test640 = new SqlInt32 (640);
565 SqlInt64 Test64000 = new SqlInt64 (64000);
566 AssertEquals ("#W01", (float)64, ((SqlSingle)Test64).Value);
567 AssertEquals ("#W02", (float)640, ((SqlSingle)Test640).Value);
568 AssertEquals ("#W03", (float)64000, ((SqlSingle)Test64000).Value);
571 [Test]
572 public void SqlMoneyToSqlSingle()
574 SqlMoney TestMoney64 = new SqlMoney(64);
575 AssertEquals ("#X01", (float)64, ((SqlSingle)TestMoney64).Value);
578 [Test]
579 public void SingleToSqlSingle()
581 Single TestSingle64 = 64;
582 AssertEquals ("#Y01", (float)64, ((SqlSingle)TestSingle64).Value);