2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / corlib / Test / System / BooleanTest.cs
1 //
2 // BooleanTest.cs - NUnit Test Cases for the System.Boolean class
3 //
4 // Authors
5 //      Bob Doan <bdoan@sicompos.com>
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004 Novell (http://www.novell.com)
10 //
11
12 using NUnit.Framework;
13 using System;
14 using System.Globalization;
15
16 namespace MonoTests.System {
17
18 [TestFixture]
19 public class BooleanTest  {
20
21         [Test]
22         public void Strings ()
23         {
24                 Assert.AreEqual("False", Boolean.FalseString, "Wrong False string");
25                 Assert.AreEqual("True", Boolean.TrueString, "Wrong True string");
26         }
27         
28         [Test]
29         public void CompareTo ()
30         {
31                 Boolean t=true,f=false;
32                 Assert.IsTrue (f.CompareTo(t) < 0, "f.CompareTo(t) < 0");
33                 Assert.IsTrue (f.CompareTo(f) == 0, "f.CompareTo(f)");
34                 Assert.IsTrue (t.CompareTo(t) == 0, "t.CompareTo(t) == 0");
35                 Assert.IsTrue (t.CompareTo(f) > 0, "t.CompareTo(f) > 0");
36                 Assert.IsTrue (t.CompareTo(null) > 0, "t.CompareTo(null) > 0");
37
38                 byte[] array = new byte [1] { 0x02 };
39                 bool t2 = BitConverter.ToBoolean (array, 0);
40                 Assert.IsTrue (f.CompareTo(t2) < 0, "f.CompareTo(t2) < 0");
41                 Assert.IsTrue (t2.CompareTo(t2) == 0, "t2.CompareTo(t2) == 0");
42                 Assert.IsTrue (t2.CompareTo(f) > 0, "t2.CompareTo(f) > 0");
43                 Assert.IsTrue (t2.CompareTo(null) > 0, "t2.CompareTo(null) > 0");
44         }
45
46         [Test]
47         [ExpectedException (typeof (ArgumentException))]
48         public void CompareToInvalidString ()
49         {
50                 true.CompareTo ("What Ever");
51         }
52
53         [Test]
54         public void TestEquals ()
55         {
56                 Boolean t=true, f=false;
57                 string s = "What Ever";
58                 Assert.IsTrue (t.Equals(t), "t.Equals(t)");
59                 Assert.IsTrue (f.Equals(f), "f.Equals(f)");
60                 Assert.IsTrue (!t.Equals(f), "!t.Equals(f)");
61                 Assert.IsTrue (!f.Equals(t), "!f.Equals(t)");
62                 Assert.IsTrue (!t.Equals(null), "!t.Equals(null)");
63                 Assert.IsTrue (!f.Equals(null), "!f.Equals(null)");
64                 Assert.IsTrue (!t.Equals(s), "!t.Equals(s)");
65                 Assert.IsTrue (!f.Equals(s), "!f.Equals(s)");
66
67                 byte[] array = new byte [1] { 0x02 };
68                 bool t2 = BitConverter.ToBoolean (array, 0);
69                 Assert.IsTrue (t2.Equals(t2), "t2.Equals(t2)");
70                 Assert.IsTrue (t.Equals(t2), "t.Equals(t2)");
71                 Assert.IsTrue (t2.Equals(t), "t2.Equals(t)");
72                 Assert.IsTrue (!f.Equals(t2), "!f.Equals(t2)");
73         }
74
75 #pragma warning disable 1718
76         [Test]
77         public void TestEqualOperator ()
78         {
79                 Boolean t=true, f=false;
80                 Assert.IsTrue (t==t, "t==t");
81                 Assert.IsTrue (f==f, "f==f");
82                 Assert.IsTrue (t!=f, "t!=f");
83                 Assert.IsTrue (f!=t, "f!=t");
84
85                 byte[] array = new byte [1] { 0x02 };
86                 bool t2 = BitConverter.ToBoolean (array, 0);
87                 Assert.IsTrue (t2==t2, "t2==t2");
88                 Assert.IsTrue (t==t2, "t==t2");
89                 Assert.IsTrue (t2==t, "t2==t");
90                 Assert.IsTrue (f!=t2, "f!=t2");
91         }
92 #pragma warning restore 1718
93
94         [Test]
95         public void TestGetHashCode ()
96         {
97                 Boolean t=true, f=false;
98                 Assert.AreEqual(1, t.GetHashCode(), "GetHashCode True failed");
99                 Assert.AreEqual(0, f.GetHashCode(), "GetHashCode True failed");
100         }
101
102         [Test]
103         public void TestGetType ()
104         {
105                 Boolean t=true, f=false;
106                 Assert.AreEqual(true, Object.ReferenceEquals(t.GetType(), f.GetType()), "GetType failed");
107         }
108
109         [Test]
110         public void GetTypeCode ()
111         {
112                 Boolean b=true;
113                 Assert.AreEqual(TypeCode.Boolean, b.GetTypeCode(), "GetTypeCode failed");
114         }
115
116         [Test]
117         public void Parse () 
118         {
119                 Assert.AreEqual(true, Boolean.Parse("True"), "Parse True failed");
120                 Assert.AreEqual(true, Boolean.Parse(" True"), "Parse True failed");
121                 Assert.AreEqual(true, Boolean.Parse("True "), "Parse True failed");
122                 Assert.AreEqual(true, Boolean.Parse("tRuE"), "Parse True failed");
123                 Assert.AreEqual(false, Boolean.Parse("False"), "Parse False failed");
124                 Assert.AreEqual(false, Boolean.Parse(" False"), "Parse False failed");
125                 Assert.AreEqual(false, Boolean.Parse("False "), "Parse False failed");
126                 Assert.AreEqual(false, Boolean.Parse("fAlSe"), "Parse False failed");
127         }
128
129         [Test]
130         [ExpectedException (typeof (FormatException))]
131         public void ParseInvalid ()
132         {
133                 Boolean.Parse ("not-t-or-f");
134         }
135
136         [Test]
137         [ExpectedException (typeof (ArgumentNullException))]
138         public void ParseNull ()
139         {
140                 Boolean.Parse (null);
141         }
142
143         [Test]
144         public void TestToString ()
145         {
146                 Boolean t=true,f=false;
147                 Assert.AreEqual(Boolean.TrueString, t.ToString(), "ToString True Failed");
148                 Assert.AreEqual(Boolean.FalseString, f.ToString(), "ToString False Failed");
149         }
150 }
151
152 }