kvector.cpp
Go to the documentation of this file.
1 #include <cstdlib>
2 #include <iostream>
3 #include <complex>
4 
5 #include <kfilter/kvector.hpp>
6 
7 using namespace std;
8 using namespace Kalman;
9 
10 template <typename T, K_UINT_32 BEG, bool DBG>
11 void test() {
12 
14  T values[5] = {5.0, 4.0, 3.0, 2.0, 1.0};
15 
16  // Constructor tests.
17  Vector v0;
18  Vector v1(0);
19  Vector v2(5);
20  Vector v3(0, T(1.0));
21  Vector v4(4, T(2.0));
22  Vector v5(0, values);
23  Vector v6(3, values);
24  Vector v7(v5);
25  Vector v8(v6);
26 
27  cout << "v0 : " << v0 << endl;
28  cout << "v1 : " << v1 << endl;
29  cout << "v2 : " << v2 << endl;
30  cout << "v3 : " << v3 << endl;
31  cout << "v4 : " << v4 << endl;
32  cout << "v5 : " << v5 << endl;
33  cout << "v6 : " << v6 << endl;
34  cout << "v7 : " << v7 << endl;
35  cout << "v8 : " << v8 << endl << endl;
36 
37  // Tests for operator=(const T&) and resize().
38  v4.resize(0);
39  v4 = T(-1.0);
40  v5 = T(6.0);
41  v6 = T(7.0);
42  v7.resize(2);
43  v7 = T(8.0);
44  v8.resize(6);
45  v8 = T(9.0);
46 
47  cout << "v4 : " << v4 << endl;
48  cout << "v5 : " << v5 << endl;
49  cout << "v6 : " << v6 << endl;
50  cout << "v7 : " << v7 << endl;
51  cout << "v8 : " << v8 << endl << endl;
52 
53  // Tests for assignement operator and assign().
54  // Tests swap() at the same time, under the covers.
55  v5 = v7;
56  v6 = v8;
57  v7 = v0;
58  v0.assign(0, values);
59  v1.assign(2, values);
60  v2.assign(0, values);
61  v4.assign(3, values);
62 
63  cout << "v0 : " << v0 << endl;
64  cout << "v1 : " << v1 << endl;
65  cout << "v2 : " << v2 << endl;
66  cout << "v3 : " << v3 << endl;
67  cout << "v4 : " << v4 << endl;
68  cout << "v5 : " << v5 << endl;
69  cout << "v6 : " << v6 << endl;
70  cout << "v7 : " << v7 << endl;
71  cout << "v8 : " << v8 << endl << endl;
72 
73  // Tests member access functions.
74  // Tests exceptions at the same time.
75  const Vector& ref = v4;
76 
77  cout << "v4(1) = " << v4(1) << endl;
78  cout << "ref(1) = " << ref(1) << endl;
79 
80  v4(1) = T(3.0);
81  // ref(1) = T(2.0); error !!!
82 
83  cout << "v4(1) = " << v4(1) << endl;
84  cout << "ref(1) = " << ref(1) << endl;
85 
86  if (DBG) {
87 
88  try {
89  v4(100) = T(7.0);
90  } catch(OutOfBoundError& e) {
91  cout << e.what() << endl;
92  }
93 
94  try {
95  cout << ref(100) << endl;
96  } catch(OutOfBoundError& e) {
97  cout << e.what() << endl;
98  }
99 
100  }
101 
102  cout << endl;
103 
104  // Output has already been tested extensively.
105  // Let's test input.
106  // This also tests get() and put() under the covers.
107  cout << "v0 is of size : " << v0.size() << endl;
108  cin >> v0;
109  cout << "v0 : " << v0 << endl;
110 
111  cout << "v1 is of size : " << v1.size() << endl;
112  cin >> v1;
113  cout << "v1 : " << v1 << endl;
114 
115  cout << "----------------------------" << endl << endl;
116 
117 }
118 
119 int main() {
120  test<float, 0, true>();
121  test<float, 0, false>();
122  test<float, 1, true>();
123  test<float, 1, false>();
124  test<double, 0, true>();
125  test<double, 0, false>();
126  test<double, 1, true>();
127  test<double, 1, false>();
128  test<complex<double>, 0, true>();
129  test<complex<double>, 0, false>();
130  test<complex<double>, 1, true>();
131  test<complex<double>, 1, false>();
132  return 0;
133 }
void resize(K_UINT_32 n)
Resizes the vector. Resulting vector contents are undefined.
Minimalist vector template class.
Definition: kvector.hpp:72
int main()
Definition: kvector.cpp:119
cPlaneEKF::Vector Vector
Definition: plane.h:26
void test()
Definition: kvector.cpp:11
Exception class for access to out-of-bound elements.
Definition: ktypes.hpp:85
void assign(K_UINT_32 n, const T *v)
Copies an array of n instances of T.
K_UINT_32 size() const
Returns the vector size.
Contains the interface of the KVector template class.


kfilter
Author(s): Jorge Almeida, Vincent Zalzal, Sylvain Marleau, Richard Gourdeau
autogenerated on Mon Mar 2 2015 01:31:45