File: example.cpp

package info (click to toggle)
cppad 2017.00.00.4-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 26,780 kB
  • ctags: 12,220
  • sloc: xml: 216,822; cpp: 63,586; sh: 9,233; makefile: 1,321; python: 637; ansic: 170
file content (124 lines) | stat: -rw-r--r-- 3,329 bytes parent folder | download
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
// $Id: example.cpp 3788 2016-02-09 15:50:06Z bradbell $
/* --------------------------------------------------------------------------
CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-16 Bradley M. Bell

CppAD is distributed under multiple licenses. This distribution is under
the terms of the
                    GNU General Public License Version 3.

A copy of this license is included in the COPYING file of this distribution.
Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
-------------------------------------------------------------------------- */

/*
$begin speed_example.cpp$$
$spell
$$



$section Run the Speed Examples$$
$mindex program$$

$head Running Tests$$
To build this program and run its correctness tests see $cref cmake_check$$.


$code
$srcfile%speed/example/example.cpp%0%// BEGIN C++%// END C++%1%$$
$$

$end
*/
// BEGIN C++

# include <cppad/cppad.hpp>

// various example routines
extern bool det_of_minor(void);
extern bool det_by_lu(void);
extern bool det_by_minor(void);
extern bool elapsed_seconds(void);
extern bool mat_sum_sq(void);
extern bool ode_evaluate(void);
extern bool sparse_hes_fun(void);
extern bool sparse_jac_fun(void);
extern bool speed_test(void);
extern bool time_test(void);

namespace {
	// function that runs one test
	size_t Run_ok_count    = 0;
	size_t Run_error_count = 0;
	const char* exception_list[] = {
		"elapsed_seconds",
		"speed_test",
		"time_test"
	};
	size_t n_exception = sizeof(exception_list) / sizeof(exception_list[0]);
	bool Run(bool TestOk(void), std::string name)
	{	bool ok               = true;
		std::streamsize width =  20;
		std::cout.width( width );
		std::cout.setf( std::ios_base::left );
		std::cout << name;
		bool exception = false;
		for(size_t i = 0; i < n_exception; i++)
			exception |= exception_list[i] == name;
		//
		ok &= name.size() < size_t(width);
		ok &= TestOk();
		if( ok )
		{	std::cout << "OK" << std::endl;
			Run_ok_count++;
		}
		else if ( exception )
		{	std::cout << "Error: perhaps too many other programs running";
			std::cout << std::endl;
			// no change to Run_ok_count
			ok = true;
		}
		else
		{	std::cout << "Error" << std::endl;
			Run_error_count++;
		}
		return ok;
	}
}

// main program that runs all the tests
int main(void)
{	bool ok = true;
	using std::cout;
	using std::endl;

	ok &= Run(det_of_minor,          "det_of_minor"   );
	ok &= Run(det_by_minor,         "det_by_minor"    );
	ok &= Run(det_by_lu,               "det_by_lu"    );
	ok &= Run(elapsed_seconds,   "elapsed_seconds"    );
	ok &= Run(mat_sum_sq,             "mat_sum_sq"    );
	ok &= Run(ode_evaluate,         "ode_evaluate"    );
	ok &= Run(sparse_hes_fun,    "sparse_hes_fun"     );
	ok &= Run(sparse_jac_fun,    "sparse_jac_fun"     );
	ok &= Run(speed_test,             "speed_test"    );
	ok &= Run(time_test,               "time_test"    );
	assert( ok || (Run_error_count > 0) );

	// check for memory leak in previous calculations
	if( ! CppAD::thread_alloc::free_all() )
	{	ok = false;
		cout << "Error: memroy leak detected" << endl;
	}

	if( ok )
	{	cout << "All " << int(Run_ok_count) << " tests passed ";
		cout << "(possibly excepting elapsed_seconds).";
	}
	else	cout << int(Run_error_count) << " tests failed.";
	cout << endl;


	return static_cast<int>( ! ok );
}

// END C++