1 // Tests for System.Drawing.SizeF.cs
3 // Author: Ravindra (rkumar@novell.com)
5 // Modified TestPoint.cs for testing SizeF.cs.
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Security.Permissions;
34 using System.Globalization;
35 using System.Threading;
37 using NUnit.Framework;
39 namespace MonoTests.System.Drawing
42 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
43 public class SizeFTest
52 sz11_99 = new SizeF (1.1F, 9.9F);
53 sz11_0 = new SizeF (1.1F, 0F);
54 sz0_11 = new SizeF (0F, 1.1F);
58 public void TestConstructors ()
60 SizeF sz_wh = new SizeF (1.5F, 5.8F);
61 Assert.AreEqual (1.5F, sz_wh.Width, "C#1");
62 Assert.AreEqual (5.8F, sz_wh.Height, "C#2");
64 SizeF sz_pf = new SizeF (new PointF (1.5F, 5.8F));
65 Assert.AreEqual (1.5F, sz_pf.Width, "C#3");
66 Assert.AreEqual (5.8F, sz_pf.Height, "C#4");
68 SizeF sz_sz = new SizeF (sz_wh);
69 Assert.AreEqual (1.5F, sz_sz.Width, "C#5");
70 Assert.AreEqual (5.8F, sz_sz.Height, "C#6");
72 Assert.AreEqual (sz_wh, sz_pf, "C#7");
73 Assert.AreEqual (sz_pf, sz_sz, "C#8");
74 Assert.AreEqual (sz_wh, sz_sz, "C#9");
78 public void TestEmptyField ()
80 SizeF sz = new SizeF (0.0F, 0.0F);
81 Assert.AreEqual (sz, SizeF.Empty, "EMP#1");
85 public void TestProperties ()
87 SizeF sz = new SizeF (0.0F, 0.0F);
89 Assert.IsTrue (sz.IsEmpty, "P#1");
90 Assert.IsFalse (sz11_99.IsEmpty, "P#2");
91 Assert.AreEqual (1.1F, sz11_0.Width, "P#3");
92 Assert.AreEqual (1.1F, sz0_11.Height, "P#4");
96 public void TestEquals ()
98 Assert.AreEqual (sz11_99, sz11_99, "EQ#1");
99 Assert.AreEqual (sz11_99, new SizeF (1.1F, 9.9F), "EQ#2");
100 Assert.IsFalse (sz11_99.Equals (sz11_0), "EQ#3");
101 Assert.IsFalse (sz11_99.Equals (sz0_11), "EQ#4");
102 Assert.IsFalse (sz11_0.Equals (sz0_11), "EQ#5");
106 public void Test2PointF ()
108 PointF p1 = new PointF (1.1F, 9.9F);
109 PointF p2 = sz11_99.ToPointF ();
111 Assert.AreEqual (p1, p2, "2PF#1");
115 public void Test2Size ()
117 // note: using Size (not SizeF) is normal for this test
118 Size sz1 = new Size (1, 9);
119 Size sz2 = sz11_99.ToSize ();
121 Assert.AreEqual (sz1, sz2, "2SZ#1");
126 public void TestAddition ()
128 Assert.AreEqual (sz11_99, sz11_0 + new SizeF (0.0F, 9.9F), "ADD#1");
129 Assert.AreEqual (sz11_99, new SizeF (0.0F, 0.0F) + new SizeF (1.1F, 9.9F), "ADD#2");
133 public void TestEqualityOp ()
135 Assert.IsTrue (sz11_99 == sz11_99, "EOP#1");
136 Assert.IsTrue (sz11_99 == new SizeF (1.1F, 9.9F), "EOP#2");
137 Assert.IsFalse (sz11_99 == sz11_0, "EOP#3");
138 Assert.IsFalse (sz11_99 == sz0_11, "EOP#4");
139 Assert.IsFalse (sz11_0 == sz0_11, "EOP#5");
143 public void TestInequalityOp ()
145 Assert.IsFalse (sz11_99 != sz11_99, "IOP#1");
146 Assert.IsFalse (sz11_99 != new SizeF (1.1F, 9.9F), "IOP#2");
147 Assert.IsTrue (sz11_99 != sz11_0, "IOP#3");
148 Assert.IsTrue (sz11_99 != sz0_11, "IOP#4");
149 Assert.IsTrue (sz11_0 != sz0_11, "IOP#5");
153 public void TestSubtraction ()
155 Assert.AreEqual (sz11_0, sz11_99 - new SizeF (0.0F, 9.9F), "SUB#1");
156 Assert.AreEqual (sz0_11, new SizeF (1.1F, 1.1F) - new SizeF (1.1F, 0.0F), "SUB#2");
160 public void TestSizeF2PointF ()
162 PointF pf1 = new PointF (1.1F, 9.9F);
163 PointF pf2 = (PointF) sz11_99;
165 Assert.AreEqual (pf1, pf2, "SF2PF#1");
169 public void GetHashCodeTest ()
171 Assert.AreEqual (sz11_0.GetHashCode (), new SizeF (1.1f, 0).GetHashCode (), "GHC#1");
172 Assert.AreEqual (SizeF.Empty.GetHashCode (), new SizeF (0, 0).GetHashCode (), "GHC#2");
176 public void ToStringTest () {
177 // save current culture
178 CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
181 PerformToStringTest (new CultureInfo ("en-US"));
182 PerformToStringTest (new CultureInfo ("nl-BE"));
184 // restore original culture
185 Thread.CurrentThread.CurrentCulture = currentCulture;
189 private void PerformToStringTest (CultureInfo culture)
191 // set current culture
192 Thread.CurrentThread.CurrentCulture = culture;
195 Assert.AreEqual (GetExpectedToString (culture, sz11_0), sz11_0.ToString (),
196 "TS#1-" + culture.Name);
197 Assert.AreEqual (GetExpectedToString (culture, sz0_11), sz0_11.ToString (),
198 "TS#2-" + culture.Name);
199 Assert.AreEqual (GetExpectedToString (culture, SizeF.Empty), SizeF.Empty.ToString (),
200 "TS#3-" + culture.Name);
201 SizeF size = new SizeF (float.NaN, float.NegativeInfinity);
202 Assert.AreEqual (GetExpectedToString (culture, size), size.ToString (),
203 "TS#4-" + culture.Name);
206 private static string GetExpectedToString (CultureInfo culture, SizeF size)
208 return string.Format ("{{Width={0}, Height={1}}}", size.Width.ToString (culture),
209 size.Height.ToString (culture));
213 public void AddTest ()
215 Assert.AreEqual (sz11_99, SizeF.Add (sz11_0, new SizeF (0.0F, 9.9F)), "ADD#1");
216 Assert.AreEqual (sz11_99, SizeF.Add (new SizeF (0.0F, 0.0F), new SizeF (1.1F, 9.9F)), "ADD#2");
220 public void SubtractTest ()
222 Assert.AreEqual (sz11_0, SizeF.Subtract (sz11_99, new SizeF (0.0F, 9.9F)), "SUB#1");
223 Assert.AreEqual (sz0_11, SizeF.Subtract (new SizeF (1.1F, 1.1F), new SizeF (1.1F, 0.0F)), "SUB#2");