diff mbox

[Branch,~jesse-barker/libmatrix/trunk] Rev 21: Add transpose tests for matrix classes. Also, add copyright comments to test

Message ID 20110622231325.20352.41085.launchpad@loganberry.canonical.com
State Accepted
Headers show

Commit Message

Jesse Barker June 22, 2011, 11:13 p.m. UTC
------------------------------------------------------------
revno: 21
committer: Jesse Barker <jesse.barker@linaro.org>
branch nick: trunk
timestamp: Wed 2011-06-22 16:11:46 -0700
message:
  Add transpose tests for matrix classes.  Also, add copyright comments to test
  modules and fixup the final link rule for creating the test executable.
added:
  test/transpose_test.cc
  test/transpose_test.h
modified:
  Makefile
  test/inverse_test.cc
  test/inverse_test.h
  test/libmatrix_test.cc
  test/libmatrix_test.h
  test/options.cc


--
lp:libmatrix
https://code.launchpad.net/~jesse-barker/libmatrix/trunk

You are subscribed to branch lp:libmatrix.
To unsubscribe from this branch go to https://code.launchpad.net/~jesse-barker/libmatrix/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'Makefile'
--- Makefile	2011-06-21 22:37:41 +0000
+++ Makefile	2011-06-22 23:11:46 +0000
@@ -6,6 +6,7 @@ 
 LIBMATRIX_TESTS = $(TESTDIR)/libmatrix_test
 TESTSRCS = $(TESTDIR)/options.cc \
            $(TESTDIR)/inverse_test.cc \
+           $(TESTDIR)/transpose_test.cc \
            $(TESTDIR)/libmatrix_test.cc
 TESTOBJS = $(TESTSRCS:.cc=.o)
 
@@ -21,10 +22,11 @@ 
 
 # Tests and execution targets here.
 $(TESTDIR)/options.o: $(TESTDIR)/options.cc $(TESTDIR)/libmatrix_test.h
-$(TESTDIR)/libmatrix_test.o: $(TESTDIR)/libmatrix_test.cc $(TESTDIR)/libmatrix_test.h $(TESTDIR)/inverse_test.h
+$(TESTDIR)/libmatrix_test.o: $(TESTDIR)/libmatrix_test.cc $(TESTDIR)/libmatrix_test.h $(TESTDIR)/inverse_test.h $(TESTDIR)/transpose_test.h
 $(TESTDIR)/inverse_test.o: $(TESTDIR)/inverse_test.cc $(TESTDIR)/inverse_test.h $(TESTDIR)/libmatrix_test.h mat.h
-$(TESTDIR)/libmatrix_test: $(TESTDIR)/options.o $(TESTDIR)/libmatrix_test.o $(TESTDIR)/inverse_test.o libmatrix.a
-	$(CXX) -o $@ $?
+$(TESTDIR)/transpose_test.o: $(TESTDIR)/transpose_test.cc $(TESTDIR)/transpose_test.h $(TESTDIR)/libmatrix_test.h mat.h
+$(TESTDIR)/libmatrix_test: $(TESTOBJS) libmatrix.a
+	$(CXX) -o $@ $^
 run_tests: $(LIBMATRIX_TESTS)
 	$(LIBMATRIX_TESTS)
 clean :

=== modified file 'test/inverse_test.cc'
--- test/inverse_test.cc	2011-06-21 22:37:41 +0000
+++ test/inverse_test.cc	2011-06-22 23:11:46 +0000
@@ -1,3 +1,14 @@ 
+//
+// Copyright (c) 2010 Linaro Limited
+//
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the MIT License which accompanies
+// this distribution, and is available at
+// http://www.opensource.org/licenses/mit-license.php
+//
+// Contributors:
+//     Jesse Barker - original implementation.
+//
 #include <iostream>
 #include "libmatrix_test.h"
 #include "inverse_test.h"

=== modified file 'test/inverse_test.h'
--- test/inverse_test.h	2011-06-21 22:37:41 +0000
+++ test/inverse_test.h	2011-06-22 23:11:46 +0000
@@ -1,3 +1,14 @@ 
+//
+// Copyright (c) 2010 Linaro Limited
+//
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the MIT License which accompanies
+// this distribution, and is available at
+// http://www.opensource.org/licenses/mit-license.php
+//
+// Contributors:
+//     Jesse Barker - original implementation.
+//
 #ifndef INVERSE_TEST_H_
 #define INVERSE_TEST_H_
 

=== modified file 'test/libmatrix_test.cc'
--- test/libmatrix_test.cc	2011-06-21 22:37:41 +0000
+++ test/libmatrix_test.cc	2011-06-22 23:11:46 +0000
@@ -14,6 +14,7 @@ 
 #include <vector>
 #include "libmatrix_test.h"
 #include "inverse_test.h"
+#include "transpose_test.h"
 
 using std::cerr;
 using std::cout;
@@ -35,12 +36,19 @@ 
     testVec.push_back(new MatrixTest2x2Inverse());
     testVec.push_back(new MatrixTest3x3Inverse());
     testVec.push_back(new MatrixTest4x4Inverse());
+    testVec.push_back(new MatrixTest2x2Transpose());
+    testVec.push_back(new MatrixTest3x3Transpose());
+    testVec.push_back(new MatrixTest4x4Transpose());
 
     for (vector<MatrixTest*>::iterator testIt = testVec.begin();
          testIt != testVec.end();
          testIt++)
     {
         MatrixTest* curTest = *testIt;
+        if (testOptions.beVerbose())
+        {
+            cout << "Running test " << curTest->name() << endl;
+        }
         curTest->run(testOptions);
         if (!curTest->passed())
         {

=== modified file 'test/libmatrix_test.h'
--- test/libmatrix_test.h	2011-06-21 22:37:41 +0000
+++ test/libmatrix_test.h	2011-06-22 23:11:46 +0000
@@ -1,3 +1,14 @@ 
+//
+// Copyright (c) 2010 Linaro Limited
+//
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the MIT License which accompanies
+// this distribution, and is available at
+// http://www.opensource.org/licenses/mit-license.php
+//
+// Contributors:
+//     Jesse Barker - original implementation.
+//
 #ifndef LIBMATRIX_TEST_H_
 #define LIBMATRIX_TEST_H_
 

=== modified file 'test/options.cc'
--- test/options.cc	2011-06-21 22:37:41 +0000
+++ test/options.cc	2011-06-22 23:11:46 +0000
@@ -1,3 +1,14 @@ 
+//
+// Copyright (c) 2010 Linaro Limited
+//
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the MIT License which accompanies
+// this distribution, and is available at
+// http://www.opensource.org/licenses/mit-license.php
+//
+// Contributors:
+//     Jesse Barker - original implementation.
+//
 #include <iostream>
 #include <iomanip>
 #include <getopt.h>

=== added file 'test/transpose_test.cc'
--- test/transpose_test.cc	1970-01-01 00:00:00 +0000
+++ test/transpose_test.cc	2011-06-22 23:11:46 +0000
@@ -0,0 +1,297 @@ 
+//
+// Copyright (c) 2010 Linaro Limited
+//
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the MIT License which accompanies
+// this distribution, and is available at
+// http://www.opensource.org/licenses/mit-license.php
+//
+// Contributors:
+//     Jesse Barker - original implementation.
+//
+#include <iostream>
+#include "libmatrix_test.h"
+#include "transpose_test.h"
+#include "../mat.h"
+
+using LibMatrix::mat2;
+using LibMatrix::mat3;
+using LibMatrix::mat4;
+using std::cout;
+using std::endl;
+
+void
+MatrixTest2x2Transpose::run(const Options& options)
+{
+    // First, a simple test to ensure that the transpose of the identity is
+    // the identity.
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl;
+    }
+
+    mat2 m;
+
+    if (options.beVerbose())
+    {
+        cout << "Starting with mat2 (should be identity): " << endl << endl;
+        m.print();
+    }
+
+    m.transpose();
+
+    if (options.beVerbose())
+    {
+        cout << endl << "Transpose of identity (should be identity): " << endl << endl;
+        m.print();
+    }
+
+    mat2 mi;
+    if (m != mi)
+    {
+        // FAIL! Transpose of the identity is the identity.
+        return;
+    }
+
+    // At this point, we have 2 identity matrices.
+    // Next, set an element in the matrix and transpose twice.  We should see
+    // the original matrix (with i,j set).
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl;
+    }
+
+    m[0][1] = 6.3;
+
+    if (options.beVerbose())
+    {
+        cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl;
+        m.print();
+    }
+    
+    mi = m;
+
+    m.transpose().transpose();
+
+    if (options.beVerbose())
+    {
+        cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl;
+        m.print();
+    }
+    
+    if (m != mi)
+    {
+        // FAIL! Transposing the same matrix twice should yield the original.
+        return;
+    }    
+
+    // Next, reset mi back to the identity.  Set element element j,i in this
+    // matrix and transpose m.  They should now be equal.
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl;
+    }
+
+    mi.setIdentity();
+    mi[1][0] = 6.3;
+
+    m.transpose();
+
+    if (options.beVerbose())
+    {
+        cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl;
+        m.print();
+        cout << endl;
+    }
+    
+    if (m == mi)
+    {
+        pass_ = true;
+    }
+
+    // FAIL! Transposing the same matrix twice should yield the original.
+}
+
+void
+MatrixTest3x3Transpose::run(const Options& options)
+{
+    // First, a simple test to ensure that the transpose of the identity is
+    // the identity.
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl;
+    }
+
+    mat3 m;
+
+    if (options.beVerbose())
+    {
+        cout << "Starting with mat2 (should be identity): " << endl << endl;
+        m.print();
+    }
+
+    m.transpose();
+
+    if (options.beVerbose())
+    {
+        cout << endl << "Transpose of identity (should be identity): " << endl << endl;
+        m.print();
+    }
+
+    mat3 mi;
+    if (m != mi)
+    {
+        // FAIL! Transpose of the identity is the identity.
+        return;
+    }
+
+    // At this point, we have 2 identity matrices.
+    // Next, set an element in the matrix and transpose twice.  We should see
+    // the original matrix (with i,j set).
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl;
+    }
+
+    m[0][1] = 6.3;
+
+    if (options.beVerbose())
+    {
+        cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl;
+        m.print();
+    }
+    
+    mi = m;
+
+    m.transpose().transpose();
+
+    if (options.beVerbose())
+    {
+        cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl;
+        m.print();
+    }
+    
+    if (m != mi)
+    {
+        // FAIL! Transposing the same matrix twice should yield the original.
+        return;
+    }    
+
+    // Next, reset mi back to the identity.  Set element element j,i in this
+    // matrix and transpose m.  They should now be equal.
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl;
+    }
+
+    mi.setIdentity();
+    mi[1][0] = 6.3;
+
+    m.transpose();
+
+    if (options.beVerbose())
+    {
+        cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl;
+        m.print();
+        cout << endl;
+    }
+    
+    if (m == mi)
+    {
+        pass_ = true;
+    }
+
+    // FAIL! Transposing the same matrix twice should yield the original.
+}
+
+void
+MatrixTest4x4Transpose::run(const Options& options)
+{
+    // First, a simple test to ensure that the transpose of the identity is
+    // the identity.
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 1: Transpose of the identity is the identity." << endl << endl;
+    }
+
+    mat4 m;
+
+    if (options.beVerbose())
+    {
+        cout << "Starting with mat2 (should be identity): " << endl << endl;
+        m.print();
+    }
+
+    m.transpose();
+
+    if (options.beVerbose())
+    {
+        cout << endl << "Transpose of identity (should be identity): " << endl << endl;
+        m.print();
+    }
+
+    mat4 mi;
+    if (m != mi)
+    {
+        // FAIL! Transpose of the identity is the identity.
+        return;
+    }
+
+    // At this point, we have 2 identity matrices.
+    // Next, set an element in the matrix and transpose twice.  We should see
+    // the original matrix (with i,j set).
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 2: Transposing a matrix twice yields the original matrix." << endl << endl;
+    }
+
+    m[0][1] = 6.3;
+
+    if (options.beVerbose())
+    {
+        cout << "Matrix should now have (0, 1) == 6.300000" << endl << endl;
+        m.print();
+    }
+    
+    mi = m;
+
+    m.transpose().transpose();
+
+    if (options.beVerbose())
+    {
+        cout << endl << "Matrix should now have (0, 1) == 6.300000" << endl << endl;
+        m.print();
+    }
+    
+    if (m != mi)
+    {
+        // FAIL! Transposing the same matrix twice should yield the original.
+        return;
+    }    
+
+    // Next, reset mi back to the identity.  Set element element j,i in this
+    // matrix and transpose m.  They should now be equal.
+    if (options.beVerbose())
+    {
+        cout << endl << "Assertion 3: Transpose of matrix (i,j) == x is equal to matrix (j,i) == x." << endl << endl;
+    }
+
+    mi.setIdentity();
+    mi[1][0] = 6.3;
+
+    m.transpose();
+
+    if (options.beVerbose())
+    {
+        cout << "Matrix should now have (1, 0) == 6.300000" << endl << endl;
+        m.print();
+        cout << endl;
+    }
+    
+    if (m == mi)
+    {
+        pass_ = true;
+    }
+
+    // FAIL! Transposing the same matrix twice should yield the original.
+}

=== added file 'test/transpose_test.h'
--- test/transpose_test.h	1970-01-01 00:00:00 +0000
+++ test/transpose_test.h	2011-06-22 23:11:46 +0000
@@ -0,0 +1,38 @@ 
+//
+// Copyright (c) 2010 Linaro Limited
+//
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the MIT License which accompanies
+// this distribution, and is available at
+// http://www.opensource.org/licenses/mit-license.php
+//
+// Contributors:
+//     Jesse Barker - original implementation.
+//
+#ifndef TRANSPOSE_TEST_H_
+#define TRANSPOSE_TEST_H_
+
+class MatrixTest;
+class Options;
+
+class MatrixTest2x2Transpose : public MatrixTest
+{
+public:
+    MatrixTest2x2Transpose() : MatrixTest("mat2::transpose") {}
+    virtual void run(const Options& options);
+};
+
+class MatrixTest3x3Transpose : public MatrixTest
+{
+public:
+    MatrixTest3x3Transpose() : MatrixTest("mat3::transpose") {}
+    virtual void run(const Options& options);
+};
+
+class MatrixTest4x4Transpose : public MatrixTest
+{
+public:
+    MatrixTest4x4Transpose() : MatrixTest("mat4::transpose") {}
+    virtual void run(const Options& options);
+};
+#endif // TRANSPOSE_TEST_H_