TestMatrix.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. //
  2. // Tests for System.Drawing.Drawing2D.Matrix.cs
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernandez <[email protected]>
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005-2006 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. using NUnit.Framework;
  30. using System;
  31. using System.Drawing;
  32. using System.Drawing.Drawing2D;
  33. using System.Security.Permissions;
  34. namespace MonoTests.System.Drawing.Drawing2D
  35. {
  36. [TestFixture]
  37. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  38. public class MatrixTest : Assertion {
  39. private Matrix default_matrix;
  40. private Rectangle rect;
  41. private RectangleF rectf;
  42. [TestFixtureSetUp]
  43. public void FixtureSetUp ()
  44. {
  45. default_matrix = new Matrix ();
  46. }
  47. [Test]
  48. public void Constructor_Default ()
  49. {
  50. Matrix matrix = new Matrix ();
  51. AssertEquals ("C#1", 6, matrix.Elements.Length);
  52. }
  53. [Test]
  54. public void Constructor_SixFloats ()
  55. {
  56. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  57. AssertEquals ("C#2", 6, matrix.Elements.Length);
  58. AssertEquals ("C#3", 10, matrix.Elements[0]);
  59. AssertEquals ("C#4", 20, matrix.Elements[1]);
  60. AssertEquals ("C#5", 30, matrix.Elements[2]);
  61. AssertEquals ("C#6", 40, matrix.Elements[3]);
  62. AssertEquals ("C#7", 50, matrix.Elements[4]);
  63. AssertEquals ("C#8", 60, matrix.Elements[5]);
  64. }
  65. [Test]
  66. public void Constructor_Float ()
  67. {
  68. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  69. AssertEquals ("C#2", 6, matrix.Elements.Length);
  70. AssertEquals ("C#3", 10, matrix.Elements[0]);
  71. AssertEquals ("C#4", 20, matrix.Elements[1]);
  72. AssertEquals ("C#5", 30, matrix.Elements[2]);
  73. AssertEquals ("C#6", 40, matrix.Elements[3]);
  74. AssertEquals ("C#7", 50, matrix.Elements[4]);
  75. AssertEquals ("C#8", 60, matrix.Elements[5]);
  76. }
  77. [Test]
  78. [ExpectedException (typeof (ArgumentNullException))]
  79. public void Constructor_Int_Null ()
  80. {
  81. new Matrix (rect, null);
  82. }
  83. [Test]
  84. [ExpectedException (typeof (ArgumentException))]
  85. public void Constructor_Int_Empty ()
  86. {
  87. new Matrix (rect, new Point[0]);
  88. }
  89. [Test]
  90. [ExpectedException (typeof (ArgumentNullException))]
  91. public void Constructor_Float_Null ()
  92. {
  93. new Matrix (rectf, null);
  94. }
  95. [Test]
  96. [ExpectedException (typeof (ArgumentException))]
  97. public void Constructor_Float_Empty ()
  98. {
  99. new Matrix (rectf, new PointF[0]);
  100. }
  101. // Properties
  102. [Test]
  103. public void Invertible ()
  104. {
  105. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  106. AssertEquals ("I#1", false, matrix.IsInvertible);
  107. matrix = new Matrix (156, 46, 0, 0, 106, 19);
  108. AssertEquals ("I#2", false, matrix.IsInvertible);
  109. matrix = new Matrix (146, 66, 158, 104, 42, 150);
  110. AssertEquals ("I#3", true, matrix.IsInvertible);
  111. matrix = new Matrix (119, 140, 145, 74, 102, 58);
  112. AssertEquals ("I#4", true, matrix.IsInvertible);
  113. }
  114. [Test]
  115. public void IsIdentity ()
  116. {
  117. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  118. AssertEquals ("N#1", false, matrix.IsIdentity);
  119. matrix = new Matrix (1, 0, 0, 1, 0, 0);
  120. AssertEquals ("N#2", true, matrix.IsIdentity);
  121. }
  122. [Test]
  123. public void IsOffsetX ()
  124. {
  125. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  126. AssertEquals ("X#1", 47, matrix.OffsetX);
  127. }
  128. [Test]
  129. public void IsOffsetY ()
  130. {
  131. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  132. AssertEquals ("Y#1", 30, matrix.OffsetY);
  133. }
  134. // Elements Property is checked implicity in other test
  135. //
  136. // Methods
  137. //
  138. [Test]
  139. public void Clone ()
  140. {
  141. Matrix matsrc = new Matrix (10, 20, 30, 40, 50, 60);
  142. Matrix matrix = matsrc.Clone ();
  143. AssertEquals ("D#1", 6, matrix.Elements.Length);
  144. AssertEquals ("D#2", 10, matrix.Elements[0]);
  145. AssertEquals ("D#3", 20, matrix.Elements[1]);
  146. AssertEquals ("D#4", 30, matrix.Elements[2]);
  147. AssertEquals ("D#5", 40, matrix.Elements[3]);
  148. AssertEquals ("D#6", 50, matrix.Elements[4]);
  149. AssertEquals ("D#7", 60, matrix.Elements[5]);
  150. }
  151. [Test]
  152. public void HashCode ()
  153. {
  154. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  155. Matrix clone = matrix.Clone ();
  156. Assert ("HashCode/Clone", matrix.GetHashCode () != clone.GetHashCode ());
  157. Matrix matrix2 = new Matrix (10, 20, 30, 40, 50, 60);
  158. Assert ("HashCode/Identical", matrix.GetHashCode () != matrix2.GetHashCode ());
  159. }
  160. [Test]
  161. public void Reset ()
  162. {
  163. Matrix matrix = new Matrix (51, 52, 53, 54, 55, 56);
  164. matrix.Reset ();
  165. AssertEquals ("F#1", 6, matrix.Elements.Length);
  166. AssertEquals ("F#2", 1, matrix.Elements[0]);
  167. AssertEquals ("F#3", 0, matrix.Elements[1]);
  168. AssertEquals ("F#4", 0, matrix.Elements[2]);
  169. AssertEquals ("F#5", 1, matrix.Elements[3]);
  170. AssertEquals ("F#6", 0, matrix.Elements[4]);
  171. AssertEquals ("F#7", 0, matrix.Elements[5]);
  172. }
  173. [Test]
  174. public void Rotate ()
  175. {
  176. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  177. matrix.Rotate (180);
  178. AssertEquals ("H#1", -10, matrix.Elements[0]);
  179. AssertEquals ("H#2", -20, matrix.Elements[1]);
  180. AssertEquals ("H#3", -30, matrix.Elements[2]);
  181. AssertEquals ("H#4", -40, matrix.Elements[3]);
  182. AssertEquals ("H#5", 50, matrix.Elements[4]);
  183. AssertEquals ("H#6", 60, matrix.Elements[5]);
  184. }
  185. [Test]
  186. [ExpectedException (typeof (ArgumentException))]
  187. public void Rotate_InvalidOrder ()
  188. {
  189. new Matrix ().Rotate (180, (MatrixOrder) Int32.MinValue);
  190. }
  191. [Test]
  192. public void RotateAt ()
  193. {
  194. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  195. matrix.RotateAt (180, new PointF (10, 10));
  196. AssertEquals ("I#1", -10, matrix.Elements[0]);
  197. AssertEquals ("I#2", -20, matrix.Elements[1]);
  198. AssertEquals ("I#3", -30, matrix.Elements[2]);
  199. AssertEquals ("I#4", -40, matrix.Elements[3]);
  200. AssertEquals ("I#5", 850, matrix.Elements[4]);
  201. AssertEquals ("I#6", 1260, matrix.Elements[5]);
  202. }
  203. [Test]
  204. [ExpectedException (typeof (ArgumentException))]
  205. public void RotateAt_InvalidOrder ()
  206. {
  207. new Matrix ().RotateAt (180, new PointF (10, 10), (MatrixOrder) Int32.MinValue);
  208. }
  209. [Test]
  210. [ExpectedException (typeof (ArgumentNullException))]
  211. public void Multiply_Null ()
  212. {
  213. new Matrix (10, 20, 30, 40, 50, 60).Multiply (null);
  214. }
  215. [Test]
  216. public void Multiply ()
  217. {
  218. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  219. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60));
  220. AssertEquals ("J#1", 700, matrix.Elements[0]);
  221. AssertEquals ("J#2", 1000, matrix.Elements[1]);
  222. AssertEquals ("J#3", 1500, matrix.Elements[2]);
  223. AssertEquals ("J#4", 2200, matrix.Elements[3]);
  224. AssertEquals ("J#5", 2350, matrix.Elements[4]);
  225. AssertEquals ("J#6", 3460, matrix.Elements[5]);
  226. }
  227. [Test]
  228. [ExpectedException (typeof (ArgumentNullException))]
  229. public void Multiply_Null_Order ()
  230. {
  231. new Matrix (10, 20, 30, 40, 50, 60).Multiply (null, MatrixOrder.Append);
  232. }
  233. [Test]
  234. public void Multiply_Append ()
  235. {
  236. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  237. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Append);
  238. AssertEquals ("J#1", 700, matrix.Elements[0]);
  239. AssertEquals ("J#2", 1000, matrix.Elements[1]);
  240. AssertEquals ("J#3", 1500, matrix.Elements[2]);
  241. AssertEquals ("J#4", 2200, matrix.Elements[3]);
  242. AssertEquals ("J#5", 2350, matrix.Elements[4]);
  243. AssertEquals ("J#6", 3460, matrix.Elements[5]);
  244. }
  245. [Test]
  246. public void Multiply_Prepend ()
  247. {
  248. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  249. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Prepend);
  250. AssertEquals ("J#1", 700, matrix.Elements[0]);
  251. AssertEquals ("J#2", 1000, matrix.Elements[1]);
  252. AssertEquals ("J#3", 1500, matrix.Elements[2]);
  253. AssertEquals ("J#4", 2200, matrix.Elements[3]);
  254. AssertEquals ("J#5", 2350, matrix.Elements[4]);
  255. AssertEquals ("J#6", 3460, matrix.Elements[5]);
  256. }
  257. [Test]
  258. [ExpectedException (typeof (ArgumentException))]
  259. public void Multiply_InvalidOrder ()
  260. {
  261. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  262. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), (MatrixOrder)Int32.MinValue);
  263. }
  264. [Test]
  265. public void Equals ()
  266. {
  267. Matrix mat1 = new Matrix (10, 20, 30, 40, 50, 60);
  268. Matrix mat2 = new Matrix (10, 20, 30, 40, 50, 60);
  269. Matrix mat3 = new Matrix (10, 20, 30, 40, 50, 10);
  270. AssertEquals ("E#1", true, mat1.Equals (mat2));
  271. AssertEquals ("E#2", false, mat2.Equals (mat3));
  272. AssertEquals ("E#3", false, mat1.Equals (mat3));
  273. }
  274. [Test]
  275. public void Invert ()
  276. {
  277. Matrix matrix = new Matrix (1, 2, 3, 4, 5, 6);
  278. matrix.Invert ();
  279. AssertEquals ("V#1", -2, matrix.Elements[0]);
  280. AssertEquals ("V#2", 1, matrix.Elements[1]);
  281. AssertEquals ("V#3", 1.5, matrix.Elements[2]);
  282. AssertEquals ("V#4", -0.5, matrix.Elements[3]);
  283. AssertEquals ("V#5", 1, matrix.Elements[4]);
  284. AssertEquals ("V#6", -2, matrix.Elements[5]);
  285. }
  286. [Test]
  287. public void Scale ()
  288. {
  289. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  290. matrix.Scale (2, 4);
  291. AssertEquals ("S#1", 20, matrix.Elements[0]);
  292. AssertEquals ("S#2", 40, matrix.Elements[1]);
  293. AssertEquals ("S#3", 120, matrix.Elements[2]);
  294. AssertEquals ("S#4", 160, matrix.Elements[3]);
  295. AssertEquals ("S#5", 50, matrix.Elements[4]);
  296. AssertEquals ("S#6", 60, matrix.Elements[5]);
  297. }
  298. [Test]
  299. public void Scale_Negative ()
  300. {
  301. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  302. matrix.Scale (-2, -4);
  303. AssertEquals ("S#1", -20, matrix.Elements[0]);
  304. AssertEquals ("S#2", -40, matrix.Elements[1]);
  305. AssertEquals ("S#3", -120, matrix.Elements[2]);
  306. AssertEquals ("S#4", -160, matrix.Elements[3]);
  307. AssertEquals ("S#5", 50, matrix.Elements[4]);
  308. AssertEquals ("S#6", 60, matrix.Elements[5]);
  309. }
  310. [Test]
  311. [ExpectedException (typeof (ArgumentException))]
  312. public void Scale_InvalidOrder ()
  313. {
  314. new Matrix ().Scale (2, 1, (MatrixOrder) Int32.MinValue);
  315. }
  316. [Test]
  317. public void Shear ()
  318. {
  319. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  320. matrix.Shear (2, 4);
  321. AssertEquals ("H#1", 130, matrix.Elements[0]);
  322. AssertEquals ("H#2", 180, matrix.Elements[1]);
  323. AssertEquals ("H#3", 50, matrix.Elements[2]);
  324. AssertEquals ("H#4", 80, matrix.Elements[3]);
  325. AssertEquals ("H#5", 50, matrix.Elements[4]);
  326. AssertEquals ("H#6", 60, matrix.Elements[5]);
  327. matrix = new Matrix (5, 3, 9, 2, 2, 1);
  328. matrix.Shear (10, 20);
  329. AssertEquals ("H#7", 185, matrix.Elements[0]);
  330. AssertEquals ("H#8", 43, matrix.Elements[1]);
  331. AssertEquals ("H#9", 59, matrix.Elements[2]);
  332. AssertEquals ("H#10", 32, matrix.Elements[3]);
  333. AssertEquals ("H#11", 2, matrix.Elements[4]);
  334. AssertEquals ("H#12", 1, matrix.Elements[5]);
  335. }
  336. [Test]
  337. [ExpectedException (typeof (ArgumentException))]
  338. public void Shear_InvalidOrder ()
  339. {
  340. new Matrix ().Shear (-1, 1, (MatrixOrder) Int32.MinValue);
  341. }
  342. [Test]
  343. public void TransformPoints ()
  344. {
  345. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  346. PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
  347. matrix.TransformPoints (pointsF);
  348. AssertEquals ("K#1", 38, pointsF[0].X);
  349. AssertEquals ("K#2", 52, pointsF[0].Y);
  350. AssertEquals ("K#3", 66, pointsF[1].X);
  351. AssertEquals ("K#4", 92, pointsF[1].Y);
  352. Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
  353. matrix.TransformPoints (points);
  354. AssertEquals ("K#5", 38, pointsF[0].X);
  355. AssertEquals ("K#6", 52, pointsF[0].Y);
  356. AssertEquals ("K#7", 66, pointsF[1].X);
  357. AssertEquals ("K#8", 92, pointsF[1].Y);
  358. }
  359. [Test]
  360. [ExpectedException (typeof (ArgumentNullException))]
  361. public void TransformPoints_Point_Null ()
  362. {
  363. new Matrix ().TransformPoints ((Point[]) null);
  364. }
  365. [Test]
  366. [ExpectedException (typeof (ArgumentNullException))]
  367. public void TransformPoints_PointF_Null ()
  368. {
  369. new Matrix ().TransformPoints ((PointF[]) null);
  370. }
  371. [Test]
  372. [ExpectedException (typeof (ArgumentException))]
  373. public void TransformPoints_Point_Empty ()
  374. {
  375. new Matrix ().TransformPoints (new Point[0]);
  376. }
  377. [Test]
  378. [ExpectedException (typeof (ArgumentException))]
  379. public void TransformPoints_PointF_Empty ()
  380. {
  381. new Matrix ().TransformPoints (new PointF[0]);
  382. }
  383. [Test]
  384. public void TransformVectors ()
  385. {
  386. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  387. PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
  388. matrix.TransformVectors (pointsF);
  389. AssertEquals ("N#1", 28, pointsF[0].X);
  390. AssertEquals ("N#2", 40, pointsF[0].Y);
  391. AssertEquals ("N#3", 56, pointsF[1].X);
  392. AssertEquals ("N#4", 80, pointsF[1].Y);
  393. Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
  394. matrix.TransformVectors (points);
  395. AssertEquals ("N#5", 28, pointsF[0].X);
  396. AssertEquals ("N#6", 40, pointsF[0].Y);
  397. AssertEquals ("N#7", 56, pointsF[1].X);
  398. AssertEquals ("N#8", 80, pointsF[1].Y);
  399. }
  400. [Test]
  401. [ExpectedException (typeof (ArgumentNullException))]
  402. public void TransformVectors_Point_Null ()
  403. {
  404. new Matrix ().TransformVectors ((Point[]) null);
  405. }
  406. [Test]
  407. [ExpectedException (typeof (ArgumentNullException))]
  408. public void TransformVectors_PointF_Null ()
  409. {
  410. new Matrix ().TransformVectors ((PointF[]) null);
  411. }
  412. [Test]
  413. [ExpectedException (typeof (ArgumentException))]
  414. public void TransformVectors_Point_Empty ()
  415. {
  416. new Matrix ().TransformVectors (new Point[0]);
  417. }
  418. [Test]
  419. [ExpectedException (typeof (ArgumentException))]
  420. public void TransformVectors_PointF_Empty ()
  421. {
  422. new Matrix ().TransformVectors (new PointF[0]);
  423. }
  424. [Test]
  425. public void Translate ()
  426. {
  427. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  428. matrix.Translate (5, 10);
  429. AssertEquals ("Y#1", 2, matrix.Elements[0]);
  430. AssertEquals ("Y#2", 4, matrix.Elements[1]);
  431. AssertEquals ("Y#3", 6, matrix.Elements[2]);
  432. AssertEquals ("Y#4", 8, matrix.Elements[3]);
  433. AssertEquals ("Y#5", 80, matrix.Elements[4]);
  434. AssertEquals ("Y#6", 112, matrix.Elements[5]);
  435. }
  436. [Test]
  437. [ExpectedException (typeof (ArgumentException))]
  438. public void Translate_InvalidOrder ()
  439. {
  440. new Matrix ().Translate (-1, 1, (MatrixOrder) Int32.MinValue);
  441. }
  442. [Test]
  443. [ExpectedException (typeof (ArgumentNullException))]
  444. public void VectorTransformPoints_Null ()
  445. {
  446. new Matrix ().VectorTransformPoints ((Point[]) null);
  447. }
  448. [Test]
  449. [ExpectedException (typeof (ArgumentException))]
  450. public void VectorTransformPoints_Empty ()
  451. {
  452. new Matrix ().VectorTransformPoints (new Point[0]);
  453. }
  454. }
  455. }