Remove CAS from System.Drawing unit tests
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestSizeF.cs
1 // Tests for System.Drawing.SizeF.cs
2 //
3 // Author: Ravindra (rkumar@novell.com)
4 //
5 //      Modified TestPoint.cs for testing SizeF.cs.
6 //
7
8 //
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System;
32 using System.Drawing;
33 using System.Security.Permissions;
34 using System.Globalization;
35 using System.Threading;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Drawing 
40 {
41         [TestFixture]   
42         public class SizeFTest
43         {
44                 SizeF sz11_99;
45                 SizeF sz11_0;
46                 SizeF sz0_11;
47
48                 [SetUp]
49                 public void SetUp ()
50                 {
51                         sz11_99 = new SizeF (1.1F, 9.9F);
52                         sz11_0 = new SizeF (1.1F, 0F);
53                         sz0_11 = new SizeF (0F, 1.1F);
54                 }
55
56                 [Test]
57                 public void TestConstructors ()
58                 {
59                         SizeF sz_wh = new SizeF (1.5F, 5.8F);
60                         Assert.AreEqual (1.5F, sz_wh.Width, "C#1");
61                         Assert.AreEqual (5.8F, sz_wh.Height, "C#2");
62
63                         SizeF sz_pf = new SizeF (new PointF (1.5F, 5.8F));
64                         Assert.AreEqual (1.5F, sz_pf.Width, "C#3");
65                         Assert.AreEqual (5.8F, sz_pf.Height, "C#4");
66
67                         SizeF sz_sz = new SizeF (sz_wh);
68                         Assert.AreEqual (1.5F, sz_sz.Width, "C#5");
69                         Assert.AreEqual (5.8F, sz_sz.Height, "C#6");
70
71                         Assert.AreEqual (sz_wh, sz_pf, "C#7");
72                         Assert.AreEqual (sz_pf, sz_sz, "C#8");
73                         Assert.AreEqual (sz_wh, sz_sz, "C#9");
74                 }
75
76                 [Test]
77                 public void TestEmptyField () 
78                 {
79                         SizeF sz = new SizeF (0.0F, 0.0F);
80                         Assert.AreEqual (sz, SizeF.Empty, "EMP#1");
81                 }
82
83                 [Test]
84                 public void TestProperties () 
85                 {
86                         SizeF sz = new SizeF (0.0F, 0.0F);
87
88                         Assert.IsTrue (sz.IsEmpty, "P#1");
89                         Assert.IsFalse (sz11_99.IsEmpty, "P#2");
90                         Assert.AreEqual (1.1F, sz11_0.Width, "P#3");
91                         Assert.AreEqual (1.1F, sz0_11.Height, "P#4");
92                 }
93
94                 [Test]
95                 public void TestEquals () 
96                 {
97                         Assert.AreEqual (sz11_99, sz11_99, "EQ#1");
98                         Assert.AreEqual (sz11_99, new SizeF (1.1F, 9.9F), "EQ#2");
99                         Assert.IsFalse (sz11_99.Equals (sz11_0), "EQ#3");
100                         Assert.IsFalse (sz11_99.Equals (sz0_11), "EQ#4");
101                         Assert.IsFalse (sz11_0.Equals (sz0_11), "EQ#5");
102                 }
103
104                 [Test]
105                 public void Test2PointF ()
106                 {
107                         PointF p1 = new PointF (1.1F, 9.9F);
108                         PointF p2 = sz11_99.ToPointF ();
109
110                         Assert.AreEqual (p1, p2, "2PF#1");
111                 }
112                 
113                 [Test]
114                 public void Test2Size ()
115                 {
116                         // note: using Size (not SizeF) is normal for this test
117                         Size sz1 = new Size (1, 9);
118                         Size sz2 = sz11_99.ToSize ();
119
120                         Assert.AreEqual (sz1, sz2, "2SZ#1");
121                 }
122
123                 
124                 [Test]
125                 public void TestAddition ()
126                 {
127                         Assert.AreEqual (sz11_99, sz11_0 + new SizeF (0.0F, 9.9F), "ADD#1");
128                         Assert.AreEqual (sz11_99, new SizeF (0.0F, 0.0F) + new SizeF (1.1F, 9.9F), "ADD#2");
129                 }
130
131                 [Test]
132                 public void TestEqualityOp () 
133                 {
134 #pragma warning disable 1718 // Comparison made to same variable
135                         Assert.IsTrue (sz11_99 == sz11_99, "EOP#1");
136 #pragma warning restore 1718
137                         Assert.IsTrue (sz11_99 == new SizeF (1.1F, 9.9F), "EOP#2");
138                         Assert.IsFalse (sz11_99 == sz11_0, "EOP#3");
139                         Assert.IsFalse (sz11_99 == sz0_11, "EOP#4");
140                         Assert.IsFalse (sz11_0 == sz0_11, "EOP#5");
141                 }
142
143                 [Test]
144                 public void TestInequalityOp () 
145                 {
146 #pragma warning disable 1718 // Comparison made to same variable
147                         Assert.IsFalse (sz11_99 != sz11_99, "IOP#1");
148 #pragma warning restore 1718
149                         Assert.IsFalse (sz11_99 != new SizeF (1.1F, 9.9F), "IOP#2");
150                         Assert.IsTrue (sz11_99 != sz11_0, "IOP#3");
151                         Assert.IsTrue (sz11_99 != sz0_11, "IOP#4");
152                         Assert.IsTrue (sz11_0 != sz0_11, "IOP#5");
153                 }
154         
155                 [Test]
156                 public void TestSubtraction () 
157                 {
158                         Assert.AreEqual (sz11_0, sz11_99 - new SizeF (0.0F, 9.9F), "SUB#1");
159                         Assert.AreEqual (sz0_11, new SizeF (1.1F, 1.1F) - new SizeF (1.1F, 0.0F), "SUB#2");
160                 }
161         
162                 [Test]
163                 public void TestSizeF2PointF ()
164                 {
165                         PointF pf1 = new PointF (1.1F, 9.9F);
166                         PointF pf2 = (PointF) sz11_99;
167
168                         Assert.AreEqual (pf1, pf2, "SF2PF#1");
169                 }
170
171                 [Test]
172                 public void GetHashCodeTest ()
173                 {
174                         Assert.AreEqual (sz11_0.GetHashCode (), new SizeF (1.1f, 0).GetHashCode (), "GHC#1");
175                         Assert.AreEqual (SizeF.Empty.GetHashCode (), new SizeF (0, 0).GetHashCode (), "GHC#2");
176                 }
177
178                 [Test]
179                 public void ToStringTest () {
180                         // save current culture
181                         CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
182
183                         try {
184                                 PerformToStringTest (new CultureInfo ("en-US"));
185                                 PerformToStringTest (new CultureInfo ("nl-BE"));
186                         } finally {
187                                 // restore original culture
188                                 Thread.CurrentThread.CurrentCulture = currentCulture;
189                         }
190                 }
191
192                 private void PerformToStringTest (CultureInfo culture)
193                 {
194                         // set current culture
195                         Thread.CurrentThread.CurrentCulture = culture;
196
197                         // perform tests
198                         Assert.AreEqual (GetExpectedToString (culture, sz11_0), sz11_0.ToString (),
199                                 "TS#1-" + culture.Name);
200                         Assert.AreEqual (GetExpectedToString (culture, sz0_11), sz0_11.ToString (),
201                                 "TS#2-" + culture.Name);
202                         Assert.AreEqual (GetExpectedToString (culture, SizeF.Empty), SizeF.Empty.ToString (),
203                                 "TS#3-" + culture.Name);
204                         SizeF size = new SizeF (float.NaN, float.NegativeInfinity);
205                         Assert.AreEqual (GetExpectedToString (culture, size), size.ToString (),
206                                 "TS#4-" + culture.Name);
207                 }
208
209                 private static string GetExpectedToString (CultureInfo culture, SizeF size)
210                 {
211                         return string.Format ("{{Width={0}, Height={1}}}", size.Width.ToString (culture),
212                                 size.Height.ToString (culture));
213                 }
214
215                 [Test]
216                 public void AddTest ()
217                 {
218                         Assert.AreEqual (sz11_99, SizeF.Add (sz11_0, new SizeF (0.0F, 9.9F)), "ADD#1");
219                         Assert.AreEqual (sz11_99, SizeF.Add (new SizeF (0.0F, 0.0F), new SizeF (1.1F, 9.9F)), "ADD#2");
220                 }
221
222                 [Test]
223                 public void SubtractTest ()
224                 {
225                         Assert.AreEqual (sz11_0, SizeF.Subtract (sz11_99, new SizeF (0.0F, 9.9F)), "SUB#1");
226                         Assert.AreEqual (sz0_11, SizeF.Subtract (new SizeF (1.1F, 1.1F), new SizeF (1.1F, 0.0F)), "SUB#2");
227                 }
228
229         }
230 }
231