root/library/tests/square_mat_stress.cpp @ 426

Revision 426, 2.7 kB (checked in by vbarta, 15 years ago)

added unit & stress tests for ldmat & chmat (as well as other descendants of square_mat)

Line 
1#include "../bdm/math/square_mat.h"
2#include "../bdm/math/chmat.h"
3#include "mat_checks.h"
4#include "UnitTest++.h"
5#include "TestReporterStdout.h"
6#include <iostream>
7#include <iomanip>
8#include <stdlib.h>
9#include <string.h>
10
11using std::cout;
12using std::cerr;
13using std::endl;
14
15double epsilon = 0.00001;
16
17bool fast = false;
18
19namespace UnitTest
20{
21
22void CheckClose(TestResults &results, const itpp::mat &expected,
23                const itpp::mat &actual, double tolerance,
24                TestDetails const& details) {
25    if (!AreClose(expected, actual, tolerance)) { 
26        MemoryOutStream stream;
27        stream << "failed at " << expected.rows()
28               << " x " << expected.cols();
29
30        results.OnTestFailure(details, stream.GetText());
31    }
32}
33
34}
35
36template<typename TMatrix>
37void test_until_overflow() {
38    Real_Timer tt;
39    int sz = 7;
40    while (true) {
41        mat A0 = randu(sz, sz);
42        mat A = A0 * A0.T();
43       
44        tt.tic();
45        TMatrix sqmat(A);
46        double elapsed = tt.toc();
47        cout << "ctor(" << sz << " x " << sz << "): " << elapsed << " s" << endl;
48
49        tt.tic();
50        mat res = sqmat.to_mat();
51        elapsed = tt.toc();
52
53        if (!fast) {
54            CHECK_CLOSE(A, res, epsilon);
55        }
56
57        cout << "to_mat: " << elapsed << " s" << endl;
58
59        vec v = randu(sz);
60        double w = randu();
61        TMatrix sqmat2 = sqmat;
62       
63        tt.tic();
64        sqmat2.opupdt(v, w);
65        elapsed = tt.toc();
66
67        if (!fast) {
68            mat expA = A + w * outer_product(v, v);
69            CHECK_CLOSE(expA, sqmat2.to_mat(), epsilon);
70        }
71
72        cout << "opupdt: " << elapsed << " s" << endl;
73
74        sz *= 7;
75    }
76}
77
78SUITE(ldmat) {
79    TEST(cycle) {
80        test_until_overflow<ldmat>();
81    }
82}
83
84SUITE(fsqmat) {
85    TEST(cycle) {
86        test_until_overflow<fsqmat>();
87    }
88}
89
90SUITE(chmat) {
91    TEST(cycle) {
92        test_until_overflow<chmat>();
93    }
94}
95
96int main(int argc, char const *argv[]) {
97    bool unknown = false;
98    int update_next = 0; // 1 suite, 2 epsilon
99    const char *suite = "ldmat";
100    const char **param = argv + 1;
101    while (*param && !unknown) {
102        if (update_next) {
103            if (update_next == 1) {
104                suite = *param;
105            } else {
106                double eps = atof(*param);
107                if (eps > 0) {
108                    epsilon = eps;
109                } else {
110                    cerr << "invalid epsilon value ignored" << endl;
111                }
112            }
113
114            update_next = 0;
115        } else {
116            if (!strcmp(*param, "-c")) {
117                update_next = 1;
118            } else if (!strcmp(*param, "-e")) {
119                update_next = 2;
120            } else if (!strcmp(*param, "-f")) {
121                fast = true;
122            } else {
123                unknown = true;
124            }
125        }
126
127        ++param;
128    }
129
130    if (unknown || update_next) {
131        cerr << "usage: " << argv[0] << " [ -f ] [ -e epsilon ] [ -c class ]" << endl;
132    } else {
133        UnitTest::TestReporterStdout reporter;
134        UnitTest::TestRunner runner(reporter);
135        return runner.RunTestsIf(UnitTest::Test::GetTestList(),
136            suite,
137            UnitTest::True(),
138            0);
139    }
140}
Note: See TracBrowser for help on using the browser.