2 // BooleanTest.cs - NUnit Test Cases for the System.Boolean class
5 // Bob Doan <bdoan@sicompos.com>
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004 Novell (http://www.novell.com)
12 using NUnit.Framework;
14 using System.Globalization;
16 namespace MonoTests.System {
19 public class BooleanTest {
22 public void Strings ()
24 Assert.AreEqual("False", Boolean.FalseString, "Wrong False string");
25 Assert.AreEqual("True", Boolean.TrueString, "Wrong True string");
29 public void CompareTo ()
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");
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");
47 [ExpectedException (typeof (ArgumentException))]
48 public void CompareToInvalidString ()
50 true.CompareTo ("What Ever");
54 public void TestEquals ()
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)");
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)");
75 #pragma warning disable 1718
77 public void TestEqualOperator ()
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");
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");
92 #pragma warning restore 1718
95 public void TestGetHashCode ()
97 Boolean t=true, f=false;
98 Assert.AreEqual(1, t.GetHashCode(), "GetHashCode True failed");
99 Assert.AreEqual(0, f.GetHashCode(), "GetHashCode True failed");
103 public void TestGetType ()
105 Boolean t=true, f=false;
106 Assert.AreEqual(true, Object.ReferenceEquals(t.GetType(), f.GetType()), "GetType failed");
110 public void GetTypeCode ()
113 Assert.AreEqual(TypeCode.Boolean, b.GetTypeCode(), "GetTypeCode failed");
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");
130 [ExpectedException (typeof (FormatException))]
131 public void ParseInvalid ()
133 Boolean.Parse ("not-t-or-f");
137 [ExpectedException (typeof (ArgumentNullException))]
138 public void ParseNull ()
140 Boolean.Parse (null);
144 public void TestToString ()
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");