Merge pull request #4481 from migueldeicaza/master
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestPointF.cs
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         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42         public class PointFTest
43         {
44                 PointF pt11_99;
45                 PointF pt11_0;
46                 PointF pt0_11;
47
48                 [TearDown]
49                 public void TearDown () {}
50
51                 [SetUp]
52                 public void SetUp ()
53                 {
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);
57                 }
58
59                 [Test]
60                 public void TestConstructors ()
61                 {
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");
65                 }
66
67                 [Test]
68                 public void TestEmptyField () 
69                 {
70                         PointF pt = new PointF (0.0F, 0.0F);
71                         Assert.AreEqual (pt, PointF.Empty, "#EMP1");
72                 }
73
74                 [Test]
75                 public void TestProperties () 
76                 {
77                         PointF pt = new PointF (0.0F, 0.0F);
78         
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");
83                 }
84
85                 [Test]
86                 public void TestEquals () 
87                 {
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");
93                 }
94
95                 
96                 [Test]
97                 public void TestAddition ()
98                 {
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");
102                 }
103
104                 [Test]
105                 public void TestEqualityOp () 
106                 {
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");
114                 }
115
116                 [Test]
117                 public void TestInequalityOp () 
118                 {
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");
126                 }
127         
128                 [Test]
129                 public void TestSubtraction () 
130                 {
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");
138                 }
139
140                 [Test]
141                 public void GetHashCodeTest ()
142                 {
143                         PointF pt = new PointF (1.1F, 9.9F);
144                         Assert.AreEqual (pt.GetHashCode (), pt11_99.GetHashCode (), "GHC#1");
145                 }
146
147                 [Test]
148                 public void ToStringTest ()
149                 {
150                         // save current culture
151                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
152
153                         try {
154                                 PerformToStringTest (new CultureInfo ("en-US"));
155                                 PerformToStringTest (new CultureInfo ("nl-BE"));
156                         } finally {
157                                 // restore original culture
158                                 Thread.CurrentThread.CurrentCulture = currentCulture;
159                         }
160                 }
161
162                 private void PerformToStringTest(CultureInfo culture)
163                 {
164                         // set current culture
165                         Thread.CurrentThread.CurrentCulture = culture;
166
167                         // perform tests
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);
177                 }
178
179                 private static string GetExpectedToString (CultureInfo culture, PointF point)
180                 {
181                         return string.Format ("{{X={0}, Y={1}}}", point.X.ToString (culture),
182                                 point.Y.ToString (culture));
183                 }
184
185
186                 [Test]
187                 public void AddTest ()
188                 {
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");                     
191                 }
192
193                 [Test]
194                 public void SubtractTest ()
195                 {
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");                                                
198                 }
199
200
201         }
202 }
203