aboutsummaryrefslogtreecommitdiff
path: root/src/libmatrix/test/inverse_test.cc
blob: 51a8072a70eeaa87754ee23f28a29738be456b91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// 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"
#include "../mat.h"

using LibMatrix::mat2;
using LibMatrix::mat3;
using LibMatrix::mat4;
using std::cout;
using std::endl;

void
MatrixTest2x2Inverse::run(const Options& options)
{
    mat2 m;

    if (options.beVerbose())
    {
        cout << "Starting with mat2 (should be identity): " << endl << endl;
        m.print();
    }

    m[0][1] = -2.5;
    
    if (options.beVerbose())
    {
        cout << endl << "Matrix should now have (0, 1) == -2.500000" << endl << endl;
        m.print();
    }
    
    mat2 mi(m);

    if (options.beVerbose())
    {
        cout << endl << "Copy of previous matrix (should have (0, 1) == -2.500000)" << endl << endl;
        mi.print();
    }

    mi.inverse();

    if (options.beVerbose())
    {
        cout << endl << "Inverse of copy: " << endl << endl;
        mi.print();
    }

    mat2 i = m * mi;

    if (options.beVerbose())
    {
        cout << endl << "Product of original and inverse (should be identity): " << endl << endl;
        i.print();
    }

    mat2 ident;
    if (i == ident)
    {
        pass_ = true;
    }
}

void
MatrixTest3x3Inverse::run(const Options& options)
{
    mat3 m;

    if (options.beVerbose())
    {
        cout << "Starting with mat3 (should be identity): " << endl << endl;
        m.print();
    }

    m[1][2] = -2.5;
    
    if (options.beVerbose())
    {
        cout << endl << "Matrix should now have (1, 2) == -2.500000" << endl << endl;
        m.print();
    }

    mat3 mi(m);

    if (options.beVerbose())
    {
        cout << endl << "Copy of previous matrix (should have (1, 2) == -2.500000)" << endl << endl;
        mi.print();
    }

    mi.inverse();

    if (options.beVerbose())
    {
        cout << endl << "Inverse of copy: " << endl << endl;
        mi.print();
    }

    mat3 i = m * mi;

    if (options.beVerbose())
    {
        cout << endl << "Product of original and inverse (should be identity): " << endl << endl;
        i.print();
    }

    mat3 ident;
    if (i == ident)
    {
        pass_ = true;
    }
}

void
MatrixTest4x4Inverse::run(const Options& options)
{
    mat4 m;

    if (options.beVerbose())
    {
        cout << "Starting with mat4 (should be identity): " << endl << endl;
        m.print();
    }

    m[2][3] = -2.5;
    
    if (options.beVerbose())
    {
        cout << endl << "Matrix should now have (2, 3) == -2.500000" << endl << endl;
        m.print();
    }

    mat4 mi(m);

    if (options.beVerbose())
    {
        cout << endl << "Copy of previous matrix (should have (2, 3) == -2.500000)" << endl << endl;
        mi.print();
    }

    mi.inverse();

    if (options.beVerbose())
    {
        cout << endl << "Inverse of copy: " << endl << endl;
        mi.print();
    }

    mat4 i = m * mi;

    if (options.beVerbose())
    {
        cout << endl <<  "Product of original and inverse (should be identity): " << endl << endl;
        i.print();
    }

    mat4 ident;
    if (i == ident)
    {
        pass_ = true;
    }
}