Ticket #4436: test_tie.cpp

File test_tie.cpp, 2.4 KB (added by anonymous, 12 years ago)
Line 
1/*
2 * $Rev: 6074 $
3 * Author: Jesse Perla (c) 2010
4 * Use, modification and distribution are subject to the
5 * Boost Software License, Version 1.0. (See accompanying file
6 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7*/
8
9#define BOOST_TEST_MODULE test_tie
10#include <etk/declarations.hpp>
11#include <boost/test/unit_test.hpp>
12#include <etk/linear_algebra/ublas/tie.hpp> //For the ublas::tie
13#include <boost/numeric/ublas/vector.hpp>
14#include <boost/numeric/ublas/matrix.hpp>
15#include <boost/numeric/ublas/matrix_proxy.hpp>
16#include <boost/numeric/ublas/io.hpp> //For <<
17
18BOOST_AUTO_TEST_CASE( test_vector_tie_1)
19{
20
21
22 double x;
23
24 ublas::vector<double> v(1);
25 v(0) = 1.1;
26
27 ublas::tie(x) = v;
28 BOOST_REQUIRE_CLOSE(x, v(0), .1);
29}
30
31BOOST_AUTO_TEST_CASE( test_vector_tie_2)
32{
33 double x;
34 double y;
35
36 ublas::vector<double> v(2);
37 v(0) = 1.1;
38 v(1) = 2.1;
39
40 ublas::tie(x, y) = v;
41 BOOST_REQUIRE_CLOSE(x, v(0), .1);
42 BOOST_REQUIRE_CLOSE(y, v(1), .1);
43}
44
45BOOST_AUTO_TEST_CASE( test_vector_tie_3)
46{
47 double x;
48 double y;
49 double z;
50
51 ublas::vector<double> v(3);
52 v(0) = 1.1;
53 v(1) = 2.1;
54 v(2) = 3.1;
55
56 ublas::tie(x, y, z) = v;
57 BOOST_REQUIRE_CLOSE(x, v(0), .1);
58 BOOST_REQUIRE_CLOSE(y, v(1), .1);
59 BOOST_REQUIRE_CLOSE(z, v(2), .1);
60}
61
62BOOST_AUTO_TEST_CASE( test_vector_tie_4)
63{
64 double x;
65 double y;
66 double z;
67 double w;
68
69 ublas::vector<double> v(4);
70 v(0) = 1.1;
71 v(1) = 2.1;
72 v(2) = 3.1;
73 v(3) = 4.1;
74
75 ublas::tie(x, y, z, w) = v;
76 BOOST_REQUIRE_CLOSE(x, v(0), .1);
77 BOOST_REQUIRE_CLOSE(y, v(1), .1);
78 BOOST_REQUIRE_CLOSE(z, v(2), .1);
79 BOOST_REQUIRE_CLOSE(w, v(3), .1);
80}
81
82
83BOOST_AUTO_TEST_CASE( test_vector_tie_different_types)
84{
85 double x;
86 int y;
87
88 ublas::vector<int> v(2);
89 v(0) = 1;
90 v(1) = 2;
91
92 ublas::tie(x, y) = v;
93 BOOST_REQUIRE_CLOSE(x, (double)v(0), .1);
94 BOOST_REQUIRE_EQUAL(y, v(1));
95}
96
97ublas::vector<double> f()
98{
99 ublas::vector<double> v(2);
100 v(0) = 1.1;
101 v(1) = 2.1;
102 return v;
103}
104
105BOOST_AUTO_TEST_CASE( test_vector_tie_func)
106{
107 double x;
108 double y;
109
110 ublas::tie(x, y) = f();
111 BOOST_REQUIRE_CLOSE(x, f()[0], .1);
112 BOOST_REQUIRE_CLOSE(y, f()[1], .1);
113}
114
115
116BOOST_AUTO_TEST_CASE( test_vector_tie_matrix)
117{
118 double x;
119 double y;
120 ublas::matrix<double> mat(2,2);
121 mat(0,0) = 1; mat(0,1) = 2;
122 mat(1,0) = 3; mat(1,1) = 4;
123
124 ublas::tie(x, y) = row(mat, 0);
125
126 BOOST_REQUIRE_CLOSE(x, mat(0,0), .1);
127 BOOST_REQUIRE_CLOSE(y, mat(0,1), .1);
128}