1 // Tests for System.Drawing.PointF.cs
4 // Ravindra (rkumar@novell.com)
8 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
32 using System.Globalization;
33 using System.Security.Permissions;
34 using System.Threading;
36 using NUnit.Framework;
38 namespace MonoTests.System.Drawing
41 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42 public class PointFTest
49 public void TearDown () {}
54 pt11_99 = new PointF (1.1F, 9.9F);
55 pt11_0 = new PointF (1.1F, 0F);
56 pt0_11 = new PointF (0F, 1.1F);
60 public void TestConstructors ()
62 PointF pt = new PointF (1.5F, 5.8F);
63 Assert.AreEqual (1.5F, pt.X, "C#1");
64 Assert.AreEqual (5.8F, pt.Y, "C#2");
68 public void TestEmptyField ()
70 PointF pt = new PointF (0.0F, 0.0F);
71 Assert.AreEqual (pt, PointF.Empty, "#EMP1");
75 public void TestProperties ()
77 PointF pt = new PointF (0.0F, 0.0F);
79 Assert.IsTrue (pt.IsEmpty, "P#1");
80 Assert.IsTrue (!pt11_99.IsEmpty, "P#2");
81 Assert.AreEqual (1.1F, pt11_0.X, "P#3");
82 Assert.AreEqual (1.1F, pt0_11.Y, "P#4");
86 public void TestEquals ()
88 Assert.AreEqual (pt11_99, pt11_99, "EQ#1");
89 Assert.AreEqual (pt11_99, new PointF (1.1F, 9.9F), "EQ#2");
90 Assert.IsFalse (pt11_99.Equals (pt11_0), "EQ#3");
91 Assert.IsFalse (pt11_99.Equals (pt0_11), "EQ#4");
92 Assert.IsFalse (pt11_0.Equals (pt0_11), "EQ#5");
97 public void TestAddition ()
99 Assert.AreEqual (pt11_0, pt11_0 + new Size (0, 0), "ADD#1");
100 Assert.AreEqual (pt0_11, pt0_11 + new Size (0, 0), "ADD#2");
101 Assert.AreEqual (new PointF (2, 5.1F), pt0_11 + new Size (2, 4), "ADD#3");
105 public void TestEqualityOp ()
107 #pragma warning disable 1718 // Comparison made to same variable
108 Assert.IsTrue (pt11_99 == pt11_99, "EOP#1");
109 #pragma warning restore 1718
110 Assert.IsTrue (pt11_99 == new PointF (1.1F, 9.9F), "EOP#2");
111 Assert.IsFalse (pt11_99 == pt11_0, "EOP#3");
112 Assert.IsFalse (pt11_99 == pt0_11, "EOP#4");
113 Assert.IsFalse (pt11_0 == pt0_11, "EOP#5");
117 public void TestInequalityOp ()
119 #pragma warning disable 1718 // Comparison made to same variable
120 Assert.IsFalse (pt11_99 != pt11_99, "IOP#1");
121 #pragma warning restore 1718
122 Assert.IsFalse (pt11_99 != new PointF (1.1F, 9.9F), "IOP#2");
123 Assert.IsTrue (pt11_99 != pt11_0, "IOP#3");
124 Assert.IsTrue (pt11_99 != pt0_11, "IOP#4");
125 Assert.IsTrue (pt11_0 != pt0_11, "IOP#5");
129 public void TestSubtraction ()
131 Assert.AreEqual (pt11_0, pt11_0 - new Size (0, 0), "SUB#1");
132 Assert.AreEqual (pt0_11, pt0_11 - new Size (0, 0), "SUB#2");
133 PointF expected = new PointF (0.1F, 1.9F);
134 PointF actual = pt11_99 - new Size (1, 8);
135 //need to permit a small delta on floating point
136 Assert.AreEqual (expected.X, actual.X, 1e-5, "SUB#3");
137 Assert.AreEqual (expected.Y, actual.Y, 1e-5, "SUB#4");
141 public void GetHashCodeTest ()
143 PointF pt = new PointF (1.1F, 9.9F);
144 Assert.AreEqual (pt.GetHashCode (), pt11_99.GetHashCode (), "GHC#1");
148 public void ToStringTest ()
150 // save current culture
151 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
154 PerformToStringTest (new CultureInfo ("en-US"));
155 PerformToStringTest (new CultureInfo ("nl-BE"));
157 // restore original culture
158 Thread.CurrentThread.CurrentCulture = currentCulture;
162 private void PerformToStringTest(CultureInfo culture)
164 // set current culture
165 Thread.CurrentThread.CurrentCulture = culture;
168 Assert.AreEqual (GetExpectedToString (culture, pt0_11), pt0_11.ToString (),
169 "TS#1-" + culture.Name);
170 Assert.AreEqual (GetExpectedToString (culture, pt11_0), pt11_0.ToString (),
171 "TS#2-" + culture.Name);
172 Assert.AreEqual (GetExpectedToString (culture, pt11_99), pt11_99.ToString (),
173 "TS#3-" + culture.Name);
174 PointF pt = new PointF (float.NaN, float.NegativeInfinity);
175 Assert.AreEqual (GetExpectedToString (culture, pt), pt.ToString (),
176 "TS#4-" + culture.Name);
179 private static string GetExpectedToString (CultureInfo culture, PointF point)
181 return string.Format ("{{X={0}, Y={1}}}", point.X.ToString (culture),
182 point.Y.ToString (culture));
187 public void AddTest ()
189 Assert.AreEqual (new PointF (3, 4), PointF.Add (new PointF (1, 1), new Size (2, 3)), "ADDTEST#1");
190 Assert.AreEqual (new PointF (4, 5), PointF.Add (new PointF (2, 2), new SizeF (2, 3)), "ADDTEST#2");
194 public void SubtractTest ()
196 Assert.AreEqual (new PointF (2, 1), PointF.Subtract (new PointF (4, 4), new Size (2, 3)), "SUBTEST#1");
197 Assert.AreEqual (new PointF (3, 3), PointF.Subtract (new PointF (5, 6), new SizeF (2, 3)), "SUBTEST#2");