New test.
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing.Drawing2D / TestMatrix.cs
index 5b02ee7c43f69fa8d3d7ff30ccb9d3b62ea7b9a8..73c9cc48d5eca0c34bedc007214feb00a0ff3912 100644 (file)
@@ -1,10 +1,11 @@
 //
 // Tests for System.Drawing.Drawing2D.Matrix.cs
 //
-// Author:
-//  Jordi Mas i Hernandez <jordi@ximian.com>
+// Authors:
+//     Jordi Mas i Hernandez <jordi@ximian.com>
+//     Sebastien Pouliot  <sebastien@ximian.com>
 //
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005-2006 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -36,33 +37,119 @@ namespace MonoTests.System.Drawing.Drawing2D
 {
        [TestFixture]
        [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
-       public class MatrixTest : Assertion
-       {
-               [TearDown]
-               public void TearDown () { }
+       public class MatrixTest : Assertion {
 
-               [SetUp]
-               public void SetUp () { }
+               private Matrix default_matrix;
+               private Rectangle rect;
+               private RectangleF rectf;
+
+               [TestFixtureSetUp]
+               public void FixtureSetUp ()
+               {
+                       default_matrix = new Matrix ();
+               }
+
+               [Test]
+               public void Constructor_Default ()
+               {
+                       Matrix matrix = new Matrix ();
+                       AssertEquals ("C#1", 6, matrix.Elements.Length);
+               }
+
+               [Test]
+               public void Constructor_SixFloats ()
+               {
+                       Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
+                       AssertEquals ("C#2", 6, matrix.Elements.Length);
+                       AssertEquals ("C#3", 10, matrix.Elements[0]);
+                       AssertEquals ("C#4", 20, matrix.Elements[1]);
+                       AssertEquals ("C#5", 30, matrix.Elements[2]);
+                       AssertEquals ("C#6", 40, matrix.Elements[3]);
+                       AssertEquals ("C#7", 50, matrix.Elements[4]);
+                       AssertEquals ("C#8", 60, matrix.Elements[5]);
+               }
+
+               [Test]
+               public void Constructor_Float ()
+               {
+                       Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
+                       AssertEquals ("C#2", 6, matrix.Elements.Length);
+                       AssertEquals ("C#3", 10, matrix.Elements[0]);
+                       AssertEquals ("C#4", 20, matrix.Elements[1]);
+                       AssertEquals ("C#5", 30, matrix.Elements[2]);
+                       AssertEquals ("C#6", 40, matrix.Elements[3]);
+                       AssertEquals ("C#7", 50, matrix.Elements[4]);
+                       AssertEquals ("C#8", 60, matrix.Elements[5]);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void Constructor_Int_Null ()
+               {
+                       new Matrix (rect, null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_Int_Empty ()
+               {
+                       new Matrix (rect, new Point[0]);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_Int_4Point ()
+               {
+                       new Matrix (rect, new Point[4]);
+               }
+
+               [Test]
+               public void Constructor_Rect_Point ()
+               {
+                       Rectangle r = new Rectangle (100, 200, 300, 400);
+                       Matrix m = new Matrix (r, new Point[3] { new Point (10, 20), new Point (30, 40), new Point (50, 60) });
+                       float[] elements = m.Elements;
+                       AssertEquals ("0", 0.06666666, elements[0], 0.00001);
+                       AssertEquals ("1", 0.06666666, elements[1], 0.00001);
+                       AssertEquals ("2", 0.09999999, elements[2], 0.00001);
+                       AssertEquals ("3", 0.09999999, elements[3], 0.00001);
+                       AssertEquals ("4", -16.6666679, elements[4], 0.00001);
+                       AssertEquals ("5", -6.666667, elements[5], 0.00001);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void Constructor_Float_Null ()
+               {
+                       new Matrix (rectf, null);
+               }
 
                [Test]
-               public void Constructors ()
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_Float_Empty ()
                {
-                       {
-                               Matrix matrix = new Matrix ();
-                               AssertEquals ("C#1", 6, matrix.Elements.Length);
-                       }
+                       new Matrix (rectf, new PointF[0]);
+               }
 
-                       {
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Constructor_Float_2PointF ()
+               {
+                       new Matrix (rectf, new PointF[2]);
+               }
 
-                               Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
-                               AssertEquals ("C#2", 6, matrix.Elements.Length);
-                               AssertEquals ("C#3", 10, matrix.Elements[0]);
-                               AssertEquals ("C#4", 20, matrix.Elements[1]);
-                               AssertEquals ("C#5", 30, matrix.Elements[2]);
-                               AssertEquals ("C#6", 40, matrix.Elements[3]);
-                               AssertEquals ("C#7", 50, matrix.Elements[4]);
-                               AssertEquals ("C#8", 60, matrix.Elements[5]);
-                       }
+               [Test]
+               public void Constructor_RectF_PointF ()
+               {
+                       RectangleF r = new RectangleF (100, 200, 300, 400);
+                       Matrix m = new Matrix (r, new PointF[3] { new PointF (10, 20), new PointF (30, 40), new PointF (50, 60) });
+                       float[] elements = m.Elements;
+                       AssertEquals ("0", 0.06666666, elements[0], 0.00001);
+                       AssertEquals ("1", 0.06666666, elements[1], 0.00001);
+                       AssertEquals ("2", 0.09999999, elements[2], 0.00001);
+                       AssertEquals ("3", 0.09999999, elements[3], 0.00001);
+                       AssertEquals ("4", -16.6666679, elements[4], 0.00001);
+                       AssertEquals ("5", -6.666667, elements[5], 0.00001);
                }
 
                // Properties
@@ -86,11 +173,37 @@ namespace MonoTests.System.Drawing.Drawing2D
                [Test]
                public void IsIdentity ()
                {
+                       Matrix identity = new Matrix ();
                        Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
-                       AssertEquals ("N#1", false, matrix.IsIdentity);
+                       AssertEquals ("N#1-identity", false, matrix.IsIdentity);
+                       Assert ("N#1-equals", !identity.Equals (matrix));
                        
                        matrix = new Matrix (1, 0, 0, 1, 0, 0);
-                       AssertEquals ("N#2", true, matrix.IsIdentity);                  
+                       AssertEquals ("N#2-identity", true, matrix.IsIdentity);
+                       Assert ("N#2-equals", identity.Equals (matrix));
+
+                       // so what's the required precision ?
+
+                       matrix = new Matrix (1.1f, 0.1f, -0.1f, 0.9f, 0, 0);
+                       Assert ("N#3-identity", !matrix.IsIdentity);
+                       Assert ("N#3-equals", !identity.Equals (matrix));
+
+                       matrix = new Matrix (1.01f, 0.01f, -0.01f, 0.99f, 0, 0);
+                       Assert ("N#4-identity", !matrix.IsIdentity);
+                       Assert ("N#4-equals", !identity.Equals (matrix));
+
+                       matrix = new Matrix (1.001f, 0.001f, -0.001f, 0.999f, 0, 0);
+                       Assert ("N#5-identity", !matrix.IsIdentity);
+                       Assert ("N#5-equals", !identity.Equals (matrix));
+
+                       matrix = new Matrix (1.0001f, 0.0001f, -0.0001f, 0.9999f, 0, 0);
+                       Assert ("N#6-identity", matrix.IsIdentity);
+                       // note: NOT equal
+                       Assert ("N#6-equals", !identity.Equals (matrix));
+
+                       matrix = new Matrix (1.0009f, 0.0009f, -0.0009f, 0.99995f, 0, 0);
+                       Assert ("N#7-identity", !matrix.IsIdentity);
+                       Assert ("N#7-equals", !identity.Equals (matrix));
                }
                
                [Test]
@@ -129,6 +242,17 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("D#7", 60, matrix.Elements[5]);
                }
 
+               [Test]
+               public void HashCode ()
+               {
+                       Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
+                       Matrix clone = matrix.Clone ();
+                       Assert ("HashCode/Clone", matrix.GetHashCode () != clone.GetHashCode ());
+
+                       Matrix matrix2 = new Matrix (10, 20, 30, 40, 50, 60);
+                       Assert ("HashCode/Identical", matrix.GetHashCode () != matrix2.GetHashCode ());
+               }
+
                [Test]
                public void Reset ()
                {
@@ -158,6 +282,62 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("H#6", 60, matrix.Elements[5]);
                }
 
+               [Test]
+               public void Rotate_45_135 ()
+               {
+                       Matrix matrix = new Matrix ();
+                       Assert ("original.IsIdentity", matrix.IsIdentity);
+
+                       matrix.Rotate (45);
+                       Assert ("+45.!IsIdentity", !matrix.IsIdentity);
+                       float[] elements = matrix.Elements;
+                       AssertEquals ("45#1", 0.7071068, elements[0]);
+                       AssertEquals ("45#2", 0.7071068, elements[1]);
+                       AssertEquals ("45#3", -0.7071068, elements[2]);
+                       AssertEquals ("45#4", 0.7071068, elements[3]);
+                       AssertEquals ("45#5", 0, elements[4]);
+                       AssertEquals ("45#6", 0, elements[5]);
+
+                       matrix.Rotate (135);
+                       Assert ("+135.!IsIdentity", !matrix.IsIdentity);
+                       elements = matrix.Elements;
+                       AssertEquals ("180#1", -1, elements[0], 0.0001);
+                       AssertEquals ("180#2", 0, elements[1], 0.0001);
+                       AssertEquals ("180#3", 0, elements[2], 0.0001);
+                       AssertEquals ("180#4", -1, elements[3], 0.0001);
+                       AssertEquals ("180#5", 0, elements[4]);
+                       AssertEquals ("180#6", 0, elements[5]);
+               }
+
+               [Test]
+               public void Rotate_90_270_Matrix ()
+               {
+                       Matrix matrix = new Matrix ();
+                       Assert ("original.IsIdentity", matrix.IsIdentity);
+
+                       matrix.Rotate (90);
+                       Assert ("+90.!IsIdentity", !matrix.IsIdentity);
+                       float[] elements = matrix.Elements;
+                       AssertEquals ("90#1", 0, elements[0], 0.0001);
+                       AssertEquals ("90#2", 1, elements[1], 0.0001);
+                       AssertEquals ("90#3", -1, elements[2], 0.0001);
+                       AssertEquals ("90#4", 0, elements[3], 0.0001);
+                       AssertEquals ("90#5", 0, elements[4]);
+                       AssertEquals ("90#6", 0, elements[5]);
+
+                       matrix.Rotate (270);
+                       // this isn't a perfect 1, 0, 0, 1, 0, 0 matrix - but close enough
+                       Assert ("360.IsIdentity", matrix.IsIdentity);
+                       Assert ("360.Equals", !new Matrix ().Equals (matrix));
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Rotate_InvalidOrder ()
+               {
+                       new Matrix ().Rotate (180, (MatrixOrder) Int32.MinValue);
+               }
+
                [Test]
                public void RotateAt ()
                {
@@ -172,6 +352,20 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("I#6", 1260, matrix.Elements[5]);
                }
 
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void RotateAt_InvalidOrder ()
+               {
+                       new Matrix ().RotateAt (180, new PointF (10, 10), (MatrixOrder) Int32.MinValue);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void Multiply_Null ()
+               {
+                       new Matrix (10, 20, 30, 40, 50, 60).Multiply (null);
+               }
+
                [Test]
                public void Multiply ()
                {
@@ -186,6 +380,49 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("J#6", 3460, matrix.Elements[5]);
                }
 
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void Multiply_Null_Order ()
+               {
+                       new Matrix (10, 20, 30, 40, 50, 60).Multiply (null, MatrixOrder.Append);
+               }
+
+               [Test]
+               public void Multiply_Append ()
+               {
+                       Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
+                       matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Append);
+
+                       AssertEquals ("J#1", 700, matrix.Elements[0]);
+                       AssertEquals ("J#2", 1000, matrix.Elements[1]);
+                       AssertEquals ("J#3", 1500, matrix.Elements[2]);
+                       AssertEquals ("J#4", 2200, matrix.Elements[3]);
+                       AssertEquals ("J#5", 2350, matrix.Elements[4]);
+                       AssertEquals ("J#6", 3460, matrix.Elements[5]);
+               }
+
+               [Test]
+               public void Multiply_Prepend ()
+               {
+                       Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
+                       matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), MatrixOrder.Prepend);
+
+                       AssertEquals ("J#1", 700, matrix.Elements[0]);
+                       AssertEquals ("J#2", 1000, matrix.Elements[1]);
+                       AssertEquals ("J#3", 1500, matrix.Elements[2]);
+                       AssertEquals ("J#4", 2200, matrix.Elements[3]);
+                       AssertEquals ("J#5", 2350, matrix.Elements[4]);
+                       AssertEquals ("J#6", 3460, matrix.Elements[5]);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Multiply_InvalidOrder ()
+               {
+                       Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
+                       matrix.Multiply (new Matrix (10, 20, 30, 40, 50, 60), (MatrixOrder)Int32.MinValue);
+               }
+
                [Test]
                public void Equals ()
                {
@@ -211,7 +448,33 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("V#5", 1, matrix.Elements[4]);
                        AssertEquals ("V#6", -2, matrix.Elements[5]);                   
                }
-               
+
+               [Test]
+               public void Invert_Translation ()
+               {
+                       Matrix matrix = new Matrix (1, 0, 0, 1, 8, 8);
+                       matrix.Invert ();
+
+                       float[] elements = matrix.Elements;
+                       AssertEquals ("#1", 1, elements[0]);
+                       AssertEquals ("#2", 0, elements[1]);
+                       AssertEquals ("#3", 0, elements[2]);
+                       AssertEquals ("#4", 1, elements[3]);
+                       AssertEquals ("#5", -8, elements[4]);
+                       AssertEquals ("#6", -8, elements[5]);
+               }
+
+               [Test]
+               public void Invert_Identity ()
+               {
+                       Matrix matrix = new Matrix ();
+                       Assert ("IsIdentity", matrix.IsIdentity);
+                       Assert ("IsInvertible", matrix.IsInvertible);
+                       matrix.Invert ();
+                       Assert ("IsIdentity-2", matrix.IsIdentity);
+                       Assert ("IsInvertible-2", matrix.IsInvertible);
+               }
+
                [Test]
                public void Scale ()
                {
@@ -223,7 +486,37 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("S#3", 120, matrix.Elements[2]);
                        AssertEquals ("S#4", 160, matrix.Elements[3]);
                        AssertEquals ("S#5", 50, matrix.Elements[4]);
-                       AssertEquals ("S#6", 60, matrix.Elements[5]);                   
+                       AssertEquals ("S#6", 60, matrix.Elements[5]);
+
+                       matrix.Scale (0.5f, 0.25f);
+
+                       AssertEquals ("SB#1", 10, matrix.Elements[0]);
+                       AssertEquals ("SB#2", 20, matrix.Elements[1]);
+                       AssertEquals ("SB#3", 30, matrix.Elements[2]);
+                       AssertEquals ("SB#4", 40, matrix.Elements[3]);
+                       AssertEquals ("SB#5", 50, matrix.Elements[4]);
+                       AssertEquals ("SB#6", 60, matrix.Elements[5]);
+               }
+
+               [Test]
+               public void Scale_Negative ()
+               {
+                       Matrix matrix = new Matrix (10, 20, 30, 40, 50, 60);
+                       matrix.Scale (-2, -4);
+
+                       AssertEquals ("S#1", -20, matrix.Elements[0]);
+                       AssertEquals ("S#2", -40, matrix.Elements[1]);
+                       AssertEquals ("S#3", -120, matrix.Elements[2]);
+                       AssertEquals ("S#4", -160, matrix.Elements[3]);
+                       AssertEquals ("S#5", 50, matrix.Elements[4]);
+                       AssertEquals ("S#6", 60, matrix.Elements[5]);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Scale_InvalidOrder ()
+               {
+                       new Matrix ().Scale (2, 1, (MatrixOrder) Int32.MinValue);
                }
                
                [Test]
@@ -249,6 +542,13 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("H#11", 2, matrix.Elements[4]);
                        AssertEquals ("H#12", 1, matrix.Elements[5]);                       
                }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Shear_InvalidOrder ()
+               {
+                       new Matrix ().Shear (-1, 1, (MatrixOrder) Int32.MinValue);
+               }
                
                [Test]
                public void TransformPoints ()
@@ -269,6 +569,34 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("K#7", 66, pointsF[1].X);
                        AssertEquals ("K#8", 92, pointsF[1].Y);                                             
                }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TransformPoints_Point_Null ()
+               {
+                       new Matrix ().TransformPoints ((Point[]) null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TransformPoints_PointF_Null ()
+               {
+                       new Matrix ().TransformPoints ((PointF[]) null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void TransformPoints_Point_Empty ()
+               {
+                       new Matrix ().TransformPoints (new Point[0]);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void TransformPoints_PointF_Empty ()
+               {
+                       new Matrix ().TransformPoints (new PointF[0]);
+               }
                
                [Test]
                public void TransformVectors  ()
@@ -288,8 +616,36 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("N#6", 40, pointsF[0].Y);
                        AssertEquals ("N#7", 56, pointsF[1].X);
                        AssertEquals ("N#8", 80, pointsF[1].Y);                                             
-               }               
-               
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TransformVectors_Point_Null ()
+               {
+                       new Matrix ().TransformVectors ((Point[]) null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void TransformVectors_PointF_Null ()
+               {
+                       new Matrix ().TransformVectors ((PointF[]) null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void TransformVectors_Point_Empty ()
+               {
+                       new Matrix ().TransformVectors (new Point[0]);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void TransformVectors_PointF_Empty ()
+               {
+                       new Matrix ().TransformVectors (new PointF[0]);
+               }
+
                [Test]
                public void Translate  ()
                {
@@ -301,8 +657,28 @@ namespace MonoTests.System.Drawing.Drawing2D
                        AssertEquals ("Y#3", 6, matrix.Elements[2]);
                        AssertEquals ("Y#4", 8, matrix.Elements[3]);
                        AssertEquals ("Y#5", 80, matrix.Elements[4]);
-                       AssertEquals ("Y#6", 112, matrix.Elements[5]);                  
-                                                                           
-               }                       
+                       AssertEquals ("Y#6", 112, matrix.Elements[5]);  
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void Translate_InvalidOrder ()
+               {
+                       new Matrix ().Translate (-1, 1, (MatrixOrder) Int32.MinValue);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentNullException))]
+               public void VectorTransformPoints_Null ()
+               {
+                       new Matrix ().VectorTransformPoints ((Point[]) null);
+               }
+
+               [Test]
+               [ExpectedException (typeof (ArgumentException))]
+               public void VectorTransformPoints_Empty ()
+               {
+                       new Matrix ().VectorTransformPoints (new Point[0]);
+               }
        }
 }