TestMatrix.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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 {
  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. Assert.AreEqual (6, matrix.Elements.Length, "C#1");
  52. }
  53. [Test]
  54. public void Constructor_SixFloats ()
  55. {
  56. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  57. Assert.AreEqual (6, matrix.Elements.Length, "C#2");
  58. Assert.AreEqual (10, matrix.Elements[0], "C#3");
  59. Assert.AreEqual (20, matrix.Elements[1], "C#4");
  60. Assert.AreEqual (30, matrix.Elements[2], "C#5");
  61. Assert.AreEqual (40, matrix.Elements[3], "C#6");
  62. Assert.AreEqual (50, matrix.Elements[4], "C#7");
  63. Assert.AreEqual (60, matrix.Elements[5], "C#8");
  64. }
  65. [Test]
  66. public void Constructor_Float ()
  67. {
  68. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  69. Assert.AreEqual (6, matrix.Elements.Length, "C#2");
  70. Assert.AreEqual (10, matrix.Elements[0], "C#3");
  71. Assert.AreEqual (20, matrix.Elements[1], "C#4");
  72. Assert.AreEqual (30, matrix.Elements[2], "C#5");
  73. Assert.AreEqual (40, matrix.Elements[3], "C#6");
  74. Assert.AreEqual (50, matrix.Elements[4], "C#7");
  75. Assert.AreEqual (60, matrix.Elements[5], "C#8");
  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 (ArgumentException))]
  91. public void Constructor_Int_4Point ()
  92. {
  93. new Matrix (rect, new Point[4]);
  94. }
  95. [Test]
  96. public void Constructor_Rect_Point ()
  97. {
  98. Rectangle r = new Rectangle (100, 200, 300, 400);
  99. Matrix m = new Matrix (r, new Point[3] { new Point (10, 20), new Point (30, 40), new Point (50, 60) });
  100. float[] elements = m.Elements;
  101. Assert.AreEqual (0.06666666, elements[0], 0.00001, "0");
  102. Assert.AreEqual (0.06666666, elements[1], 0.00001, "1");
  103. Assert.AreEqual (0.09999999, elements[2], 0.00001, "2");
  104. Assert.AreEqual (0.09999999, elements[3], 0.00001, "3");
  105. Assert.AreEqual (-16.6666679, elements[4], 0.00001, "4");
  106. Assert.AreEqual (-6.666667, elements[5], 0.00001, "5");
  107. }
  108. [Test]
  109. [ExpectedException (typeof (ArgumentNullException))]
  110. public void Constructor_Float_Null ()
  111. {
  112. new Matrix (rectf, null);
  113. }
  114. [Test]
  115. [ExpectedException (typeof (ArgumentException))]
  116. public void Constructor_Float_Empty ()
  117. {
  118. new Matrix (rectf, new PointF[0]);
  119. }
  120. [Test]
  121. [ExpectedException (typeof (ArgumentException))]
  122. public void Constructor_Float_2PointF ()
  123. {
  124. new Matrix (rectf, new PointF[2]);
  125. }
  126. [Test]
  127. public void Constructor_RectF_PointF ()
  128. {
  129. RectangleF r = new RectangleF (100, 200, 300, 400);
  130. Matrix m = new Matrix (r, new PointF[3] { new PointF (10, 20), new PointF (30, 40), new PointF (50, 60) });
  131. float[] elements = m.Elements;
  132. Assert.AreEqual (0.06666666, elements[0], 0.00001, "0");
  133. Assert.AreEqual (0.06666666, elements[1], 0.00001, "1");
  134. Assert.AreEqual (0.09999999, elements[2], 0.00001, "2");
  135. Assert.AreEqual (0.09999999, elements[3], 0.00001, "3");
  136. Assert.AreEqual (-16.6666679, elements[4], 0.00001, "4");
  137. Assert.AreEqual (-6.666667, elements[5], 0.00001, "5");
  138. }
  139. // Properties
  140. [Test]
  141. public void Invertible ()
  142. {
  143. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  144. Assert.AreEqual (false, matrix.IsInvertible, "I#1");
  145. matrix = new Matrix (156, 46, 0, 0, 106, 19);
  146. Assert.AreEqual (false, matrix.IsInvertible, "I#2");
  147. matrix = new Matrix (146, 66, 158, 104, 42, 150);
  148. Assert.AreEqual (true, matrix.IsInvertible, "I#3");
  149. matrix = new Matrix (119, 140, 145, 74, 102, 58);
  150. Assert.AreEqual (true, matrix.IsInvertible, "I#4");
  151. }
  152. [Test]
  153. public void IsIdentity ()
  154. {
  155. Matrix identity = new Matrix ();
  156. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  157. Assert.AreEqual (false, matrix.IsIdentity, "N#1-identity");
  158. Assert.IsTrue (!identity.Equals (matrix), "N#1-equals");
  159. matrix = new Matrix (1, 0, 0, 1, 0, 0);
  160. Assert.AreEqual (true, matrix.IsIdentity, "N#2-identity");
  161. Assert.IsTrue (identity.Equals (matrix), "N#2-equals");
  162. // so what's the required precision ?
  163. matrix = new Matrix (1.1f, 0.1f, -0.1f, 0.9f, 0, 0);
  164. Assert.IsTrue (!matrix.IsIdentity, "N#3-identity");
  165. Assert.IsTrue (!identity.Equals (matrix), "N#3-equals");
  166. matrix = new Matrix (1.01f, 0.01f, -0.01f, 0.99f, 0, 0);
  167. Assert.IsTrue (!matrix.IsIdentity, "N#4-identity");
  168. Assert.IsTrue (!identity.Equals (matrix), "N#4-equals");
  169. matrix = new Matrix (1.001f, 0.001f, -0.001f, 0.999f, 0, 0);
  170. Assert.IsTrue (!matrix.IsIdentity, "N#5-identity");
  171. Assert.IsTrue (!identity.Equals (matrix), "N#5-equals");
  172. matrix = new Matrix (1.0001f, 0.0001f, -0.0001f, 0.9999f, 0, 0);
  173. Assert.IsTrue (matrix.IsIdentity, "N#6-identity");
  174. // note: NOT equal
  175. Assert.IsTrue (!identity.Equals (matrix), "N#6-equals");
  176. matrix = new Matrix (1.0009f, 0.0009f, -0.0009f, 0.99995f, 0, 0);
  177. Assert.IsTrue (!matrix.IsIdentity, "N#7-identity");
  178. Assert.IsTrue (!identity.Equals (matrix), "N#7-equals");
  179. }
  180. [Test]
  181. public void IsOffsetX ()
  182. {
  183. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  184. Assert.AreEqual (47, matrix.OffsetX, "X#1");
  185. }
  186. [Test]
  187. public void IsOffsetY ()
  188. {
  189. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  190. Assert.AreEqual (30, matrix.OffsetY, "Y#1");
  191. }
  192. // Elements Property is checked implicity in other test
  193. //
  194. // Methods
  195. //
  196. [Test]
  197. public void Clone ()
  198. {
  199. Matrix matsrc = new Matrix (10, 20, 30, 40, 50, 60);
  200. Matrix matrix = matsrc.Clone ();
  201. Assert.AreEqual (6, matrix.Elements.Length, "D#1");
  202. Assert.AreEqual (10, matrix.Elements[0], "D#2");
  203. Assert.AreEqual (20, matrix.Elements[1], "D#3");
  204. Assert.AreEqual (30, matrix.Elements[2], "D#4");
  205. Assert.AreEqual (40, matrix.Elements[3], "D#5");
  206. Assert.AreEqual (50, matrix.Elements[4], "D#6");
  207. Assert.AreEqual (60, matrix.Elements[5], "D#7");
  208. }
  209. [Test]
  210. public void HashCode ()
  211. {
  212. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  213. Matrix clone = matrix.Clone ();
  214. Assert.IsTrue (matrix.GetHashCode () != clone.GetHashCode (), "HashCode/Clone");
  215. Matrix matrix2 = new Matrix (10, 20, 30, 40, 50, 60);
  216. Assert.IsTrue (matrix.GetHashCode () != matrix2.GetHashCode (), "HashCode/Identical");
  217. }
  218. [Test]
  219. public void Reset ()
  220. {
  221. Matrix matrix = new Matrix (51, 52, 53, 54, 55, 56);
  222. matrix.Reset ();
  223. Assert.AreEqual (6, matrix.Elements.Length, "F#1");
  224. Assert.AreEqual (1, matrix.Elements[0], "F#2");
  225. Assert.AreEqual (0, matrix.Elements[1], "F#3");
  226. Assert.AreEqual (0, matrix.Elements[2], "F#4");
  227. Assert.AreEqual (1, matrix.Elements[3], "F#5");
  228. Assert.AreEqual (0, matrix.Elements[4], "F#6");
  229. Assert.AreEqual (0, matrix.Elements[5], "F#7");
  230. }
  231. [Test]
  232. public void Rotate ()
  233. {
  234. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  235. matrix.Rotate (180);
  236. Assert.AreEqual (-10.0f, matrix.Elements[0], 0.0001, "H#1");
  237. Assert.AreEqual (-20, matrix.Elements[1], 0.0001, "H#2");
  238. Assert.AreEqual (-30.0000019f, matrix.Elements[2], 0.0001, "H#3");
  239. Assert.AreEqual (-40.0000038f, matrix.Elements[3], 0.0001, "H#4");
  240. Assert.AreEqual (50, matrix.Elements[4], "H#5");
  241. Assert.AreEqual (60, matrix.Elements[5], "H#6");
  242. }
  243. [Test]
  244. public void Rotate_45_135 ()
  245. {
  246. Matrix matrix = new Matrix ();
  247. Assert.IsTrue (matrix.IsIdentity, "original.IsIdentity");
  248. matrix.Rotate (45);
  249. Assert.IsTrue (!matrix.IsIdentity, "+45.!IsIdentity");
  250. float[] elements = matrix.Elements;
  251. Assert.AreEqual (0.707106769f, elements[0], 0.0001, "45#1");
  252. Assert.AreEqual (0.707106769f, elements[1], 0.0001, "45#2");
  253. Assert.AreEqual (-0.707106829f, elements[2], 0.0001, "45#3");
  254. Assert.AreEqual (0.707106769f, elements[3], 0.0001, "45#4");
  255. Assert.AreEqual (0, elements[4], 0.001f, "45#5");
  256. Assert.AreEqual (0, elements[5], 0.001f, "45#6");
  257. matrix.Rotate (135);
  258. Assert.IsTrue (!matrix.IsIdentity, "+135.!IsIdentity");
  259. elements = matrix.Elements;
  260. Assert.AreEqual (-1, elements[0], 0.0001, "180#1");
  261. Assert.AreEqual (0, elements[1], 0.0001, "180#2");
  262. Assert.AreEqual (0, elements[2], 0.0001, "180#3");
  263. Assert.AreEqual (-1, elements[3], 0.0001, "180#4");
  264. Assert.AreEqual (0, elements[4], "180#5");
  265. Assert.AreEqual (0, elements[5], "180#6");
  266. }
  267. [Test]
  268. public void Rotate_90_270_Matrix ()
  269. {
  270. Matrix matrix = new Matrix ();
  271. Assert.IsTrue (matrix.IsIdentity, "original.IsIdentity");
  272. matrix.Rotate (90);
  273. Assert.IsTrue (!matrix.IsIdentity, "+90.!IsIdentity");
  274. float[] elements = matrix.Elements;
  275. Assert.AreEqual (0, elements[0], 0.0001, "90#1");
  276. Assert.AreEqual (1, elements[1], 0.0001, "90#2");
  277. Assert.AreEqual (-1, elements[2], 0.0001, "90#3");
  278. Assert.AreEqual (0, elements[3], 0.0001, "90#4");
  279. Assert.AreEqual (0, elements[4], "90#5");
  280. Assert.AreEqual (0, elements[5], "90#6");
  281. matrix.Rotate (270);
  282. // this isn't a perfect 1, 0, 0, 1, 0, 0 matrix - but close enough
  283. Assert.IsTrue (matrix.IsIdentity, "360.IsIdentity");
  284. Assert.IsTrue (!new Matrix ().Equals (matrix), "360.Equals");
  285. }
  286. [Test]
  287. [ExpectedException (typeof (ArgumentException))]
  288. public void Rotate_InvalidOrder ()
  289. {
  290. new Matrix ().Rotate (180, (MatrixOrder) Int32.MinValue);
  291. }
  292. [Test]
  293. public void RotateAt ()
  294. {
  295. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  296. matrix.RotateAt (180, new PointF (10, 10));
  297. Assert.AreEqual (-10, matrix.Elements[0], 0.01, "I#1");
  298. Assert.AreEqual (-20, matrix.Elements[1], 0.01, "I#2");
  299. Assert.AreEqual (-30, matrix.Elements[2], 0.01, "I#3");
  300. Assert.AreEqual (-40, matrix.Elements[3], 0.01, "I#4");
  301. Assert.AreEqual (850, matrix.Elements[4], 0.01, "I#5");
  302. Assert.AreEqual (1260, matrix.Elements[5], 0.01, "I#6");
  303. }
  304. [Test]
  305. [ExpectedException (typeof (ArgumentException))]
  306. public void RotateAt_InvalidOrder ()
  307. {
  308. new Matrix ().RotateAt (180, new PointF (10, 10), (MatrixOrder) Int32.MinValue);
  309. }
  310. [Test]
  311. [ExpectedException (typeof (ArgumentNullException))]
  312. public void Multiply_Null ()
  313. {
  314. new Matrix (10, 20, 30, 40, 50, 60).Multiply (null);
  315. }
  316. [Test]
  317. public void Multiply ()
  318. {
  319. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  320. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60));
  321. Assert.AreEqual (700, matrix.Elements[0], "J#1");
  322. Assert.AreEqual (1000, matrix.Elements[1], "J#2");
  323. Assert.AreEqual (1500, matrix.Elements[2], "J#3");
  324. Assert.AreEqual (2200, matrix.Elements[3], "J#4");
  325. Assert.AreEqual (2350, matrix.Elements[4], "J#5");
  326. Assert.AreEqual (3460, matrix.Elements[5], "J#6");
  327. }
  328. [Test]
  329. [ExpectedException (typeof (ArgumentNullException))]
  330. public void Multiply_Null_Order ()
  331. {
  332. new Matrix (10, 20, 30, 40, 50, 60).Multiply (null, MatrixOrder.Append);
  333. }
  334. [Test]
  335. public void Multiply_Append ()
  336. {
  337. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  338. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Append);
  339. Assert.AreEqual (700, matrix.Elements[0], "J#1");
  340. Assert.AreEqual (1000, matrix.Elements[1], "J#2");
  341. Assert.AreEqual (1500, matrix.Elements[2], "J#3");
  342. Assert.AreEqual (2200, matrix.Elements[3], "J#4");
  343. Assert.AreEqual (2350, matrix.Elements[4], "J#5");
  344. Assert.AreEqual (3460, matrix.Elements[5], "J#6");
  345. }
  346. [Test]
  347. public void Multiply_Prepend ()
  348. {
  349. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  350. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Prepend);
  351. Assert.AreEqual (700, matrix.Elements[0], "J#1");
  352. Assert.AreEqual (1000, matrix.Elements[1], "J#2");
  353. Assert.AreEqual (1500, matrix.Elements[2], "J#3");
  354. Assert.AreEqual (2200, matrix.Elements[3], "J#4");
  355. Assert.AreEqual (2350, matrix.Elements[4], "J#5");
  356. Assert.AreEqual (3460, matrix.Elements[5], "J#6");
  357. }
  358. [Test]
  359. [ExpectedException (typeof (ArgumentException))]
  360. public void Multiply_InvalidOrder ()
  361. {
  362. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  363. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), (MatrixOrder)Int32.MinValue);
  364. }
  365. [Test]
  366. public void Equals ()
  367. {
  368. Matrix mat1 = new Matrix (10, 20, 30, 40, 50, 60);
  369. Matrix mat2 = new Matrix (10, 20, 30, 40, 50, 60);
  370. Matrix mat3 = new Matrix (10, 20, 30, 40, 50, 10);
  371. Assert.AreEqual (true, mat1.Equals (mat2), "E#1");
  372. Assert.AreEqual (false, mat2.Equals (mat3), "E#2");
  373. Assert.AreEqual (false, mat1.Equals (mat3), "E#3");
  374. }
  375. [Test]
  376. public void Invert ()
  377. {
  378. Matrix matrix = new Matrix (1, 2, 3, 4, 5, 6);
  379. matrix.Invert ();
  380. Assert.AreEqual (-2, matrix.Elements[0], "V#1");
  381. Assert.AreEqual (1, matrix.Elements[1], "V#2");
  382. Assert.AreEqual (1.5, matrix.Elements[2], "V#3");
  383. Assert.AreEqual (-0.5, matrix.Elements[3], "V#4");
  384. Assert.AreEqual (1, matrix.Elements[4], "V#5");
  385. Assert.AreEqual (-2, matrix.Elements[5], "V#6");
  386. }
  387. [Test]
  388. public void Invert_Translation ()
  389. {
  390. Matrix matrix = new Matrix (1, 0, 0, 1, 8, 8);
  391. matrix.Invert ();
  392. float[] elements = matrix.Elements;
  393. Assert.AreEqual (1, elements[0], "#1");
  394. Assert.AreEqual (0, elements[1], "#2");
  395. Assert.AreEqual (0, elements[2], "#3");
  396. Assert.AreEqual (1, elements[3], "#4");
  397. Assert.AreEqual (-8, elements[4], "#5");
  398. Assert.AreEqual (-8, elements[5], "#6");
  399. }
  400. [Test]
  401. public void Invert_Identity ()
  402. {
  403. Matrix matrix = new Matrix ();
  404. Assert.IsTrue (matrix.IsIdentity, "IsIdentity");
  405. Assert.IsTrue (matrix.IsInvertible, "IsInvertible");
  406. matrix.Invert ();
  407. Assert.IsTrue (matrix.IsIdentity, "IsIdentity-2");
  408. Assert.IsTrue (matrix.IsInvertible, "IsInvertible-2");
  409. }
  410. [Test]
  411. public void Scale ()
  412. {
  413. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  414. matrix.Scale (2, 4);
  415. Assert.AreEqual (20, matrix.Elements[0], "S#1");
  416. Assert.AreEqual (40, matrix.Elements[1], "S#2");
  417. Assert.AreEqual (120, matrix.Elements[2], "S#3");
  418. Assert.AreEqual (160, matrix.Elements[3], "S#4");
  419. Assert.AreEqual (50, matrix.Elements[4], "S#5");
  420. Assert.AreEqual (60, matrix.Elements[5], "S#6");
  421. matrix.Scale (0.5f, 0.25f);
  422. Assert.AreEqual (10, matrix.Elements[0], "SB#1");
  423. Assert.AreEqual (20, matrix.Elements[1], "SB#2");
  424. Assert.AreEqual (30, matrix.Elements[2], "SB#3");
  425. Assert.AreEqual (40, matrix.Elements[3], "SB#4");
  426. Assert.AreEqual (50, matrix.Elements[4], "SB#5");
  427. Assert.AreEqual (60, matrix.Elements[5], "SB#6");
  428. }
  429. [Test]
  430. public void Scale_Negative ()
  431. {
  432. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  433. matrix.Scale (-2, -4);
  434. Assert.AreEqual (-20, matrix.Elements[0], "S#1");
  435. Assert.AreEqual (-40, matrix.Elements[1], "S#2");
  436. Assert.AreEqual (-120, matrix.Elements[2], "S#3");
  437. Assert.AreEqual (-160, matrix.Elements[3], "S#4");
  438. Assert.AreEqual (50, matrix.Elements[4], "S#5");
  439. Assert.AreEqual (60, matrix.Elements[5], "S#6");
  440. }
  441. [Test]
  442. [ExpectedException (typeof (ArgumentException))]
  443. public void Scale_InvalidOrder ()
  444. {
  445. new Matrix ().Scale (2, 1, (MatrixOrder) Int32.MinValue);
  446. }
  447. [Test]
  448. public void Shear ()
  449. {
  450. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  451. matrix.Shear (2, 4);
  452. Assert.AreEqual (130, matrix.Elements[0], "H#1");
  453. Assert.AreEqual (180, matrix.Elements[1], "H#2");
  454. Assert.AreEqual (50, matrix.Elements[2], "H#3");
  455. Assert.AreEqual (80, matrix.Elements[3], "H#4");
  456. Assert.AreEqual (50, matrix.Elements[4], "H#5");
  457. Assert.AreEqual (60, matrix.Elements[5], "H#6");
  458. matrix = new Matrix (5, 3, 9, 2, 2, 1);
  459. matrix.Shear (10, 20);
  460. Assert.AreEqual (185, matrix.Elements[0], "H#7");
  461. Assert.AreEqual (43, matrix.Elements[1], "H#8");
  462. Assert.AreEqual (59, matrix.Elements[2], "H#9");
  463. Assert.AreEqual (32, matrix.Elements[3], "H#10");
  464. Assert.AreEqual (2, matrix.Elements[4], "H#11");
  465. Assert.AreEqual (1, matrix.Elements[5], "H#12");
  466. }
  467. [Test]
  468. [ExpectedException (typeof (ArgumentException))]
  469. public void Shear_InvalidOrder ()
  470. {
  471. new Matrix ().Shear (-1, 1, (MatrixOrder) Int32.MinValue);
  472. }
  473. [Test]
  474. public void TransformPoints ()
  475. {
  476. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  477. PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
  478. matrix.TransformPoints (pointsF);
  479. Assert.AreEqual (38, pointsF[0].X, "K#1");
  480. Assert.AreEqual (52, pointsF[0].Y, "K#2");
  481. Assert.AreEqual (66, pointsF[1].X, "K#3");
  482. Assert.AreEqual (92, pointsF[1].Y, "K#4");
  483. Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
  484. matrix.TransformPoints (points);
  485. Assert.AreEqual (38, pointsF[0].X, "K#5");
  486. Assert.AreEqual (52, pointsF[0].Y, "K#6");
  487. Assert.AreEqual (66, pointsF[1].X, "K#7");
  488. Assert.AreEqual (92, pointsF[1].Y, "K#8");
  489. }
  490. [Test]
  491. [ExpectedException (typeof (ArgumentNullException))]
  492. public void TransformPoints_Point_Null ()
  493. {
  494. new Matrix ().TransformPoints ((Point[]) null);
  495. }
  496. [Test]
  497. [ExpectedException (typeof (ArgumentNullException))]
  498. public void TransformPoints_PointF_Null ()
  499. {
  500. new Matrix ().TransformPoints ((PointF[]) null);
  501. }
  502. [Test]
  503. [ExpectedException (typeof (ArgumentException))]
  504. public void TransformPoints_Point_Empty ()
  505. {
  506. new Matrix ().TransformPoints (new Point[0]);
  507. }
  508. [Test]
  509. [ExpectedException (typeof (ArgumentException))]
  510. public void TransformPoints_PointF_Empty ()
  511. {
  512. new Matrix ().TransformPoints (new PointF[0]);
  513. }
  514. [Test]
  515. public void TransformVectors ()
  516. {
  517. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  518. PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
  519. matrix.TransformVectors (pointsF);
  520. Assert.AreEqual (28, pointsF[0].X, "N#1");
  521. Assert.AreEqual (40, pointsF[0].Y, "N#2");
  522. Assert.AreEqual (56, pointsF[1].X, "N#3");
  523. Assert.AreEqual (80, pointsF[1].Y, "N#4");
  524. Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
  525. matrix.TransformVectors (points);
  526. Assert.AreEqual (28, pointsF[0].X, "N#5");
  527. Assert.AreEqual (40, pointsF[0].Y, "N#6");
  528. Assert.AreEqual (56, pointsF[1].X, "N#7");
  529. Assert.AreEqual (80, pointsF[1].Y, "N#8");
  530. }
  531. [Test]
  532. [ExpectedException (typeof (ArgumentNullException))]
  533. public void TransformVectors_Point_Null ()
  534. {
  535. new Matrix ().TransformVectors ((Point[]) null);
  536. }
  537. [Test]
  538. [ExpectedException (typeof (ArgumentNullException))]
  539. public void TransformVectors_PointF_Null ()
  540. {
  541. new Matrix ().TransformVectors ((PointF[]) null);
  542. }
  543. [Test]
  544. [ExpectedException (typeof (ArgumentException))]
  545. public void TransformVectors_Point_Empty ()
  546. {
  547. new Matrix ().TransformVectors (new Point[0]);
  548. }
  549. [Test]
  550. [ExpectedException (typeof (ArgumentException))]
  551. public void TransformVectors_PointF_Empty ()
  552. {
  553. new Matrix ().TransformVectors (new PointF[0]);
  554. }
  555. [Test]
  556. public void Translate ()
  557. {
  558. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  559. matrix.Translate (5, 10);
  560. Assert.AreEqual (2, matrix.Elements[0], "Y#1");
  561. Assert.AreEqual (4, matrix.Elements[1], "Y#2");
  562. Assert.AreEqual (6, matrix.Elements[2], "Y#3");
  563. Assert.AreEqual (8, matrix.Elements[3], "Y#4");
  564. Assert.AreEqual (80, matrix.Elements[4], "Y#5");
  565. Assert.AreEqual (112, matrix.Elements[5], "Y#6");
  566. }
  567. [Test]
  568. [ExpectedException (typeof (ArgumentException))]
  569. public void Translate_InvalidOrder ()
  570. {
  571. new Matrix ().Translate (-1, 1, (MatrixOrder) Int32.MinValue);
  572. }
  573. [Test]
  574. [ExpectedException (typeof (ArgumentNullException))]
  575. public void VectorTransformPoints_Null ()
  576. {
  577. new Matrix ().VectorTransformPoints ((Point[]) null);
  578. }
  579. [Test]
  580. [ExpectedException (typeof (ArgumentException))]
  581. public void VectorTransformPoints_Empty ()
  582. {
  583. new Matrix ().VectorTransformPoints (new Point[0]);
  584. }
  585. }
  586. }