Merge pull request #1464 from akoeplinger/fix-portable-target
[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                         Assert.IsTrue (pt11_99 == pt11_99, "EOP#1");
108                         Assert.IsTrue (pt11_99 == new PointF (1.1F, 9.9F), "EOP#2");
109                         Assert.IsFalse (pt11_99 == pt11_0, "EOP#3");
110                         Assert.IsFalse (pt11_99 == pt0_11, "EOP#4");
111                         Assert.IsFalse (pt11_0 == pt0_11, "EOP#5");
112                 }
113
114                 [Test]
115                 public void TestInequalityOp () 
116                 {
117                         Assert.IsFalse (pt11_99 != pt11_99, "IOP#1");
118                         Assert.IsFalse (pt11_99 != new PointF (1.1F, 9.9F), "IOP#2");
119                         Assert.IsTrue (pt11_99 != pt11_0, "IOP#3");
120                         Assert.IsTrue (pt11_99 != pt0_11, "IOP#4");
121                         Assert.IsTrue (pt11_0 != pt0_11, "IOP#5");
122                 }
123         
124                 [Test]
125                 public void TestSubtraction () 
126                 {
127                         Assert.AreEqual (pt11_0, pt11_0 - new Size (0, 0), "SUB#1");
128                         Assert.AreEqual (pt0_11, pt0_11 - new Size (0, 0), "SUB#2");
129                         PointF expected = new PointF (0.1F, 1.9F);
130                         PointF actual = pt11_99 - new Size (1, 8);
131                         //need to permit a small delta on floating point
132                         Assert.AreEqual (expected.X, actual.X, 1e-5, "SUB#3");
133                         Assert.AreEqual (expected.Y, actual.Y, 1e-5, "SUB#4");
134                 }
135
136                 [Test]
137                 public void GetHashCodeTest ()
138                 {
139                         PointF pt = new PointF (1.1F, 9.9F);
140                         Assert.AreEqual (pt.GetHashCode (), pt11_99.GetHashCode (), "GHC#1");
141                 }
142
143                 [Test]
144                 public void ToStringTest ()
145                 {
146                         // save current culture
147                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
148
149                         try {
150                                 PerformToStringTest (new CultureInfo ("en-US"));
151                                 PerformToStringTest (new CultureInfo ("nl-BE"));
152                         } finally {
153                                 // restore original culture
154                                 Thread.CurrentThread.CurrentCulture = currentCulture;
155                         }
156                 }
157
158                 private void PerformToStringTest(CultureInfo culture)
159                 {
160                         // set current culture
161                         Thread.CurrentThread.CurrentCulture = culture;
162
163                         // perform tests
164                         Assert.AreEqual (GetExpectedToString (culture, pt0_11), pt0_11.ToString (),
165                                 "TS#1-" + culture.Name);
166                         Assert.AreEqual (GetExpectedToString (culture, pt11_0), pt11_0.ToString (),
167                                 "TS#2-" + culture.Name);
168                         Assert.AreEqual (GetExpectedToString (culture, pt11_99), pt11_99.ToString (),
169                                 "TS#3-" + culture.Name);
170                         PointF pt = new PointF (float.NaN, float.NegativeInfinity);
171                         Assert.AreEqual (GetExpectedToString (culture, pt), pt.ToString (),
172                                 "TS#4-" + culture.Name);
173                 }
174
175                 private static string GetExpectedToString (CultureInfo culture, PointF point)
176                 {
177                         return string.Format ("{{X={0}, Y={1}}}", point.X.ToString (culture),
178                                 point.Y.ToString (culture));
179                 }
180
181
182                 [Test]
183                 public void AddTest ()
184                 {
185                         Assert.AreEqual (new PointF (3, 4), PointF.Add (new PointF (1, 1), new Size (2, 3)), "ADDTEST#1");
186                         Assert.AreEqual (new PointF (4, 5), PointF.Add (new PointF (2, 2), new SizeF (2, 3)), "ADDTEST#2");                     
187                 }
188
189                 [Test]
190                 public void SubtractTest ()
191                 {
192                         Assert.AreEqual (new PointF (2, 1), PointF.Subtract (new PointF (4, 4), new Size (2, 3)), "SUBTEST#1");
193                         Assert.AreEqual (new PointF (3, 3), PointF.Subtract (new PointF (5, 6), new SizeF (2, 3)), "SUBTEST#2");                                                
194                 }
195
196
197         }
198 }
199