TestMatrix.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // Tests for System.Drawing.Drawing2D.Matrix.cs
  3. //
  4. // Author:
  5. // Jordi Mas i Hernandez <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.Drawing;
  31. using System.Drawing.Drawing2D;
  32. namespace MonoTests.System.Drawing.Drawing2D
  33. {
  34. [TestFixture]
  35. public class MatrixTest : Assertion
  36. {
  37. [TearDown]
  38. public void TearDown () { }
  39. [SetUp]
  40. public void SetUp () { }
  41. [Test]
  42. public void Constructors ()
  43. {
  44. {
  45. Matrix matrix = new Matrix ();
  46. AssertEquals ("C#1", 6, matrix.Elements.Length);
  47. }
  48. {
  49. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  50. AssertEquals ("C#2", 6, matrix.Elements.Length);
  51. AssertEquals ("C#3", 10, matrix.Elements[0]);
  52. AssertEquals ("C#4", 20, matrix.Elements[1]);
  53. AssertEquals ("C#5", 30, matrix.Elements[2]);
  54. AssertEquals ("C#6", 40, matrix.Elements[3]);
  55. AssertEquals ("C#7", 50, matrix.Elements[4]);
  56. AssertEquals ("C#8", 60, matrix.Elements[5]);
  57. }
  58. }
  59. // Properties
  60. [Test]
  61. public void Invertible ()
  62. {
  63. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  64. AssertEquals ("I#1", false, matrix.IsInvertible);
  65. matrix = new Matrix (156, 46, 0, 0, 106, 19);
  66. AssertEquals ("I#2", false, matrix.IsInvertible);
  67. matrix = new Matrix (146, 66, 158, 104, 42, 150);
  68. AssertEquals ("I#3", true, matrix.IsInvertible);
  69. matrix = new Matrix (119, 140, 145, 74, 102, 58);
  70. AssertEquals ("I#4", true, matrix.IsInvertible);
  71. }
  72. [Test]
  73. public void IsIdentity ()
  74. {
  75. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  76. AssertEquals ("N#1", false, matrix.IsIdentity);
  77. matrix = new Matrix (1, 0, 0, 1, 0, 0);
  78. AssertEquals ("N#2", true, matrix.IsIdentity);
  79. }
  80. [Test]
  81. public void IsOffsetX ()
  82. {
  83. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  84. AssertEquals ("X#1", 47, matrix.OffsetX);
  85. }
  86. [Test]
  87. public void IsOffsetY ()
  88. {
  89. Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
  90. AssertEquals ("Y#1", 30, matrix.OffsetY);
  91. }
  92. // Elements Property is checked implicity in other test
  93. //
  94. // Methods
  95. //
  96. [Test]
  97. public void Clone ()
  98. {
  99. Matrix matsrc = new Matrix (10, 20, 30, 40, 50, 60);
  100. Matrix matrix = matsrc.Clone ();
  101. AssertEquals ("D#1", 6, matrix.Elements.Length);
  102. AssertEquals ("D#2", 10, matrix.Elements[0]);
  103. AssertEquals ("D#3", 20, matrix.Elements[1]);
  104. AssertEquals ("D#4", 30, matrix.Elements[2]);
  105. AssertEquals ("D#5", 40, matrix.Elements[3]);
  106. AssertEquals ("D#6", 50, matrix.Elements[4]);
  107. AssertEquals ("D#7", 60, matrix.Elements[5]);
  108. }
  109. [Test]
  110. public void Reset ()
  111. {
  112. Matrix matrix = new Matrix (51, 52, 53, 54, 55, 56);
  113. matrix.Reset ();
  114. AssertEquals ("F#1", 6, matrix.Elements.Length);
  115. AssertEquals ("F#2", 1, matrix.Elements[0]);
  116. AssertEquals ("F#3", 0, matrix.Elements[1]);
  117. AssertEquals ("F#4", 0, matrix.Elements[2]);
  118. AssertEquals ("F#5", 1, matrix.Elements[3]);
  119. AssertEquals ("F#6", 0, matrix.Elements[4]);
  120. AssertEquals ("F#7", 0, matrix.Elements[5]);
  121. }
  122. [Test]
  123. public void Rotate ()
  124. {
  125. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  126. matrix.Rotate (180);
  127. AssertEquals ("H#1", -10, matrix.Elements[0]);
  128. AssertEquals ("H#2", -20, matrix.Elements[1]);
  129. AssertEquals ("H#3", -30, matrix.Elements[2]);
  130. AssertEquals ("H#4", -40, matrix.Elements[3]);
  131. AssertEquals ("H#5", 50, matrix.Elements[4]);
  132. AssertEquals ("H#6", 60, matrix.Elements[5]);
  133. }
  134. [Test]
  135. public void RotateAt ()
  136. {
  137. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  138. matrix.RotateAt (180, new PointF (10, 10));
  139. AssertEquals ("I#1", -10, matrix.Elements[0]);
  140. AssertEquals ("I#2", -20, matrix.Elements[1]);
  141. AssertEquals ("I#3", -30, matrix.Elements[2]);
  142. AssertEquals ("I#4", -40, matrix.Elements[3]);
  143. AssertEquals ("I#5", 850, matrix.Elements[4]);
  144. AssertEquals ("I#6", 1260, matrix.Elements[5]);
  145. }
  146. [Test]
  147. public void Multiply ()
  148. {
  149. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  150. matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60));
  151. AssertEquals ("J#1", 700, matrix.Elements[0]);
  152. AssertEquals ("J#2", 1000, matrix.Elements[1]);
  153. AssertEquals ("J#3", 1500, matrix.Elements[2]);
  154. AssertEquals ("J#4", 2200, matrix.Elements[3]);
  155. AssertEquals ("J#5", 2350, matrix.Elements[4]);
  156. AssertEquals ("J#6", 3460, matrix.Elements[5]);
  157. }
  158. [Test]
  159. public void Equals ()
  160. {
  161. Matrix mat1 = new Matrix (10, 20, 30, 40, 50, 60);
  162. Matrix mat2 = new Matrix (10, 20, 30, 40, 50, 60);
  163. Matrix mat3 = new Matrix (10, 20, 30, 40, 50, 10);
  164. AssertEquals ("E#1", true, mat1.Equals (mat2));
  165. AssertEquals ("E#2", false, mat2.Equals (mat3));
  166. AssertEquals ("E#3", false, mat1.Equals (mat3));
  167. }
  168. [Test]
  169. public void Invert ()
  170. {
  171. Matrix matrix = new Matrix (1, 2, 3, 4, 5, 6);
  172. matrix.Invert ();
  173. AssertEquals ("V#1", -2, matrix.Elements[0]);
  174. AssertEquals ("V#2", 1, matrix.Elements[1]);
  175. AssertEquals ("V#3", 1.5, matrix.Elements[2]);
  176. AssertEquals ("V#4", -0.5, matrix.Elements[3]);
  177. AssertEquals ("V#5", 1, matrix.Elements[4]);
  178. AssertEquals ("V#6", -2, matrix.Elements[5]);
  179. }
  180. [Test]
  181. public void Scale ()
  182. {
  183. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  184. matrix.Scale (2, 4);
  185. AssertEquals ("S#1", 20, matrix.Elements[0]);
  186. AssertEquals ("S#2", 40, matrix.Elements[1]);
  187. AssertEquals ("S#3", 120, matrix.Elements[2]);
  188. AssertEquals ("S#4", 160, matrix.Elements[3]);
  189. AssertEquals ("S#5", 50, matrix.Elements[4]);
  190. AssertEquals ("S#6", 60, matrix.Elements[5]);
  191. }
  192. [Test]
  193. public void Shear ()
  194. {
  195. Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
  196. matrix.Shear (2, 4);
  197. AssertEquals ("H#1", 130, matrix.Elements[0]);
  198. AssertEquals ("H#2", 180, matrix.Elements[1]);
  199. AssertEquals ("H#3", 50, matrix.Elements[2]);
  200. AssertEquals ("H#4", 80, matrix.Elements[3]);
  201. AssertEquals ("H#5", 50, matrix.Elements[4]);
  202. AssertEquals ("H#6", 60, matrix.Elements[5]);
  203. matrix = new Matrix (5, 3, 9, 2, 2, 1);
  204. matrix.Shear (10, 20);
  205. AssertEquals ("H#7", 185, matrix.Elements[0]);
  206. AssertEquals ("H#8", 43, matrix.Elements[1]);
  207. AssertEquals ("H#9", 59, matrix.Elements[2]);
  208. AssertEquals ("H#10", 32, matrix.Elements[3]);
  209. AssertEquals ("H#11", 2, matrix.Elements[4]);
  210. AssertEquals ("H#12", 1, matrix.Elements[5]);
  211. }
  212. [Test]
  213. public void TransformPoints ()
  214. {
  215. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  216. PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
  217. matrix.TransformPoints (pointsF);
  218. AssertEquals ("K#1", 38, pointsF[0].X);
  219. AssertEquals ("K#2", 52, pointsF[0].Y);
  220. AssertEquals ("K#3", 66, pointsF[1].X);
  221. AssertEquals ("K#4", 92, pointsF[1].Y);
  222. Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
  223. matrix.TransformPoints (points);
  224. AssertEquals ("K#5", 38, pointsF[0].X);
  225. AssertEquals ("K#6", 52, pointsF[0].Y);
  226. AssertEquals ("K#7", 66, pointsF[1].X);
  227. AssertEquals ("K#8", 92, pointsF[1].Y);
  228. }
  229. [Test]
  230. public void TransformVectors ()
  231. {
  232. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  233. PointF [] pointsF = new PointF [] {new PointF (2, 4), new PointF (4, 8)};
  234. matrix.TransformVectors (pointsF);
  235. AssertEquals ("N#1", 28, pointsF[0].X);
  236. AssertEquals ("N#2", 40, pointsF[0].Y);
  237. AssertEquals ("N#3", 56, pointsF[1].X);
  238. AssertEquals ("N#4", 80, pointsF[1].Y);
  239. Point [] points = new Point [] {new Point (2, 4), new Point (4, 8)};
  240. matrix.TransformVectors (points);
  241. AssertEquals ("N#5", 28, pointsF[0].X);
  242. AssertEquals ("N#6", 40, pointsF[0].Y);
  243. AssertEquals ("N#7", 56, pointsF[1].X);
  244. AssertEquals ("N#8", 80, pointsF[1].Y);
  245. }
  246. [Test]
  247. public void Translate ()
  248. {
  249. Matrix matrix = new Matrix (2, 4, 6, 8, 10, 12);
  250. matrix.Translate (5, 10);
  251. AssertEquals ("Y#1", 2, matrix.Elements[0]);
  252. AssertEquals ("Y#2", 4, matrix.Elements[1]);
  253. AssertEquals ("Y#3", 6, matrix.Elements[2]);
  254. AssertEquals ("Y#4", 8, matrix.Elements[3]);
  255. AssertEquals ("Y#5", 80, matrix.Elements[4]);
  256. AssertEquals ("Y#6", 112, matrix.Elements[5]);
  257. }
  258. }
  259. }