2009-06-12 Bill Holmes <billholmes54@gmail.com>
[mono.git] / mcs / class / corlib / Test / System / SingleTest.cs
1 // 
2 // System.SingleTest.cs - Unit test for Single
3 //      adapted from a subset of DoubleTest.cs
4 //
5 // Authors
6 //      Bob Doan  <bdoan@sicompos.com>
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11
12 using System;
13 using System.Globalization;
14 using System.Threading;
15
16 using NUnit.Framework;
17
18 namespace MonoTests.System
19 {
20         [TestFixture]
21         public class SingleTest : Assertion
22         {
23                 CultureInfo old_culture;
24
25                 [SetUp]
26                 public void SetUp ()
27                 {
28                         old_culture = Thread.CurrentThread.CurrentCulture;
29                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
30                 }
31
32                 [TearDown]
33                 public void TearDown ()
34                 {
35                         Thread.CurrentThread.CurrentCulture = old_culture;
36                 }
37
38                 [Test]
39                 public void Equals ()
40                 {
41                         Single s1 = 1f;
42                         Single s2 = 1f;
43                         Assert ("Equals s1==s2", s1.Equals (s2));
44                         Assert ("Equals s1!=NaN", !s1.Equals (Single.NaN));
45
46                         Assert ("Equals NaN=!s2", !Single.NaN.Equals (s2));
47                         Assert ("Equals NaN==NaN", Single.NaN.Equals (Single.NaN));
48
49                         Single p0 = 0.0f;
50                         Single m0 = -0.0f;
51                         Assert ("0.0==-0.0", p0.Equals (m0));
52                         Assert ("-0.0==0.0", m0.Equals (p0));
53                 }
54
55                 [Test]
56                 public void IsInfinity ()
57                 {
58                         Assert ("PositiveInfinity",  Single.IsInfinity (Single.PositiveInfinity));
59                         Assert ("NegativeInfinity", Single.IsInfinity (Single.NegativeInfinity));
60                         Assert ("12", !Single.IsInfinity(12));
61                         Assert ("NaN", !Single.IsInfinity (Single.NaN));
62                 }
63
64                 [Test]
65                 public void IsNan ()
66                 {
67                         Assert ("Nan", Single.IsNaN (Single.NaN));
68                         Assert ("12", !Single.IsNaN (12));
69                         Assert ("PositiveInfinity", !Single.IsNaN (Single.PositiveInfinity));
70                         Assert ("NegativeInfinity", !Single.IsNaN (Single.PositiveInfinity));
71                 }
72
73                 [Test]
74                 public void IsNegativeInfinity ()
75                 {
76                         Assert ("IsNegativeInfinity", Single.IsNegativeInfinity (Single.NegativeInfinity));
77                         Assert ("12", !Single.IsNegativeInfinity (12));
78                         Assert ("NaN", !Single.IsNegativeInfinity (Single.NaN));
79                 }
80
81                 [Test]
82                 public void IsPositiveInfinity ()
83                 {
84                         Assert ("PositiveInfinity", Single.IsPositiveInfinity (Single.PositiveInfinity));
85                         Assert ("12", !Single.IsPositiveInfinity (12));
86                         Assert ("NaN", !Single.IsPositiveInfinity (Single.NaN));
87                 }
88
89                 [Test]
90                 public void ToString_Defaults () 
91                 {
92                         Single i = 254.9f;
93                         // everything defaults to "G"
94                         string def = i.ToString ("G");
95                         AssertEquals ("ToString()", def, i.ToString ());
96                         AssertEquals ("ToString((IFormatProvider)null)", def, i.ToString ((IFormatProvider)null));
97                         AssertEquals ("ToString((string)null)", def, i.ToString ((string)null));
98                         AssertEquals ("ToString(empty)", def, i.ToString (String.Empty));
99                         AssertEquals ("ToString(null,null)", def, i.ToString (null, null));
100                         AssertEquals ("ToString(empty,null)", def, i.ToString (String.Empty, null));
101                         AssertEquals ("ToString(G)", "254.9", def);
102                 }
103
104                 [Test]
105                 [Category ("NotWorking")]
106                 public void ToString_Roundtrip ()
107                 {
108                         AssertEquals ("10.78", 10.78f.ToString ("R", NumberFormatInfo.InvariantInfo));
109                 }
110
111 #if NET_2_0
112                 [Test] // bug #72221
113                 [ExpectedException (typeof (ArgumentException))]
114                 public void HexNumber_WithHexToParse ()
115                 {
116                         float f;
117                         Single.TryParse ("0dead", NumberStyles.HexNumber, null, out f);
118                 }
119
120                 [Test]
121                 [ExpectedException (typeof (ArgumentException))]
122                 public void HexNumber_NoHexToParse ()
123                 {
124                         float f;
125                         Single.TryParse ("0", NumberStyles.HexNumber, null, out f);
126                 }
127 #endif
128         }
129 }