1 // Tests for System.Drawing.PointF.cs 2 // 3 // Author: 4 // Ravindra (rkumar@novell.com) 5 // 6 7 // 8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com) 9 // 10 // Permission is hereby granted, free of charge, to any person obtaining 11 // a copy of this software and associated documentation files (the 12 // "Software"), to deal in the Software without restriction, including 13 // without limitation the rights to use, copy, modify, merge, publish, 14 // distribute, sublicense, and/or sell copies of the Software, and to 15 // permit persons to whom the Software is furnished to do so, subject to 16 // the following conditions: 17 // 18 // The above copyright notice and this permission notice shall be 19 // included in all copies or substantial portions of the Software. 20 // 21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28 // 29 30 using System; 31 using System.Drawing; 32 using System.Globalization; 33 using System.Security.Permissions; 34 using System.Threading; 35 36 using NUnit.Framework; 37 38 namespace MonoTests.System.Drawing 39 { 40 [TestFixture] 41 public class PointFTest 42 { 43 PointF pt11_99; 44 PointF pt11_0; 45 PointF pt0_11; 46 47 [TearDown] TearDown()48 public void TearDown () {} 49 50 [SetUp] SetUp()51 public void SetUp () 52 { 53 pt11_99 = new PointF (1.1F, 9.9F); 54 pt11_0 = new PointF (1.1F, 0F); 55 pt0_11 = new PointF (0F, 1.1F); 56 } 57 58 [Test] TestConstructors()59 public void TestConstructors () 60 { 61 PointF pt = new PointF (1.5F, 5.8F); 62 Assert.AreEqual (1.5F, pt.X, "C#1"); 63 Assert.AreEqual (5.8F, pt.Y, "C#2"); 64 } 65 66 [Test] TestEmptyField()67 public void TestEmptyField () 68 { 69 PointF pt = new PointF (0.0F, 0.0F); 70 Assert.AreEqual (pt, PointF.Empty, "#EMP1"); 71 } 72 73 [Test] TestProperties()74 public void TestProperties () 75 { 76 PointF pt = new PointF (0.0F, 0.0F); 77 78 Assert.IsTrue (pt.IsEmpty, "P#1"); 79 Assert.IsTrue (!pt11_99.IsEmpty, "P#2"); 80 Assert.AreEqual (1.1F, pt11_0.X, "P#3"); 81 Assert.AreEqual (1.1F, pt0_11.Y, "P#4"); 82 } 83 84 [Test] TestEquals()85 public void TestEquals () 86 { 87 Assert.AreEqual (pt11_99, pt11_99, "EQ#1"); 88 Assert.AreEqual (pt11_99, new PointF (1.1F, 9.9F), "EQ#2"); 89 Assert.IsFalse (pt11_99.Equals (pt11_0), "EQ#3"); 90 Assert.IsFalse (pt11_99.Equals (pt0_11), "EQ#4"); 91 Assert.IsFalse (pt11_0.Equals (pt0_11), "EQ#5"); 92 } 93 94 95 [Test] TestAddition()96 public void TestAddition () 97 { 98 Assert.AreEqual (pt11_0, pt11_0 + new Size (0, 0), "ADD#1"); 99 Assert.AreEqual (pt0_11, pt0_11 + new Size (0, 0), "ADD#2"); 100 Assert.AreEqual (new PointF (2, 5.1F), pt0_11 + new Size (2, 4), "ADD#3"); 101 } 102 103 [Test] TestEqualityOp()104 public void TestEqualityOp () 105 { 106 #pragma warning disable 1718 // Comparison made to same variable 107 Assert.IsTrue (pt11_99 == pt11_99, "EOP#1"); 108 #pragma warning restore 1718 109 Assert.IsTrue (pt11_99 == new PointF (1.1F, 9.9F), "EOP#2"); 110 Assert.IsFalse (pt11_99 == pt11_0, "EOP#3"); 111 Assert.IsFalse (pt11_99 == pt0_11, "EOP#4"); 112 Assert.IsFalse (pt11_0 == pt0_11, "EOP#5"); 113 } 114 115 [Test] TestInequalityOp()116 public void TestInequalityOp () 117 { 118 #pragma warning disable 1718 // Comparison made to same variable 119 Assert.IsFalse (pt11_99 != pt11_99, "IOP#1"); 120 #pragma warning restore 1718 121 Assert.IsFalse (pt11_99 != new PointF (1.1F, 9.9F), "IOP#2"); 122 Assert.IsTrue (pt11_99 != pt11_0, "IOP#3"); 123 Assert.IsTrue (pt11_99 != pt0_11, "IOP#4"); 124 Assert.IsTrue (pt11_0 != pt0_11, "IOP#5"); 125 } 126 127 [Test] TestSubtraction()128 public void TestSubtraction () 129 { 130 Assert.AreEqual (pt11_0, pt11_0 - new Size (0, 0), "SUB#1"); 131 Assert.AreEqual (pt0_11, pt0_11 - new Size (0, 0), "SUB#2"); 132 PointF expected = new PointF (0.1F, 1.9F); 133 PointF actual = pt11_99 - new Size (1, 8); 134 //need to permit a small delta on floating point 135 Assert.AreEqual (expected.X, actual.X, 1e-5, "SUB#3"); 136 Assert.AreEqual (expected.Y, actual.Y, 1e-5, "SUB#4"); 137 } 138 139 [Test] GetHashCodeTest()140 public void GetHashCodeTest () 141 { 142 PointF pt = new PointF (1.1F, 9.9F); 143 Assert.AreEqual (pt.GetHashCode (), pt11_99.GetHashCode (), "GHC#1"); 144 } 145 146 [Test] ToStringTest()147 public void ToStringTest () 148 { 149 // save current culture 150 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; 151 152 try { 153 PerformToStringTest (new CultureInfo ("en-US")); 154 PerformToStringTest (new CultureInfo ("nl-BE")); 155 } finally { 156 // restore original culture 157 Thread.CurrentThread.CurrentCulture = currentCulture; 158 } 159 } 160 PerformToStringTest(CultureInfo culture)161 private void PerformToStringTest(CultureInfo culture) 162 { 163 // set current culture 164 Thread.CurrentThread.CurrentCulture = culture; 165 166 // perform tests 167 Assert.AreEqual (GetExpectedToString (culture, pt0_11), pt0_11.ToString (), 168 "TS#1-" + culture.Name); 169 Assert.AreEqual (GetExpectedToString (culture, pt11_0), pt11_0.ToString (), 170 "TS#2-" + culture.Name); 171 Assert.AreEqual (GetExpectedToString (culture, pt11_99), pt11_99.ToString (), 172 "TS#3-" + culture.Name); 173 PointF pt = new PointF (float.NaN, float.NegativeInfinity); 174 Assert.AreEqual (GetExpectedToString (culture, pt), pt.ToString (), 175 "TS#4-" + culture.Name); 176 } 177 GetExpectedToString(CultureInfo culture, PointF point)178 private static string GetExpectedToString (CultureInfo culture, PointF point) 179 { 180 return string.Format ("{{X={0}, Y={1}}}", point.X.ToString (culture), 181 point.Y.ToString (culture)); 182 } 183 184 185 [Test] AddTest()186 public void AddTest () 187 { 188 Assert.AreEqual (new PointF (3, 4), PointF.Add (new PointF (1, 1), new Size (2, 3)), "ADDTEST#1"); 189 Assert.AreEqual (new PointF (4, 5), PointF.Add (new PointF (2, 2), new SizeF (2, 3)), "ADDTEST#2"); 190 } 191 192 [Test] SubtractTest()193 public void SubtractTest () 194 { 195 Assert.AreEqual (new PointF (2, 1), PointF.Subtract (new PointF (4, 4), new Size (2, 3)), "SUBTEST#1"); 196 Assert.AreEqual (new PointF (3, 3), PointF.Subtract (new PointF (5, 6), new SizeF (2, 3)), "SUBTEST#2"); 197 } 198 199 200 } 201 } 202 203