1 | // Copyright (c) 2011 David Bellot |
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. (See |
4 | // accompanying file LICENSE_1_0.txt or copy at |
5 | // http://www.boost.org/LICENSE_1_0.txt) |
6 | |
7 | #ifndef BOOST_UBLAS_NO_ELEMENT_PROXIES |
8 | # define BOOST_UBLAS_NO_ELEMENT_PROXIES |
9 | #endif |
10 | |
11 | #include <boost/numeric/ublas/assignment.hpp> |
12 | #include <boost/numeric/ublas/vector.hpp> |
13 | #include <boost/numeric/ublas/vector_sparse.hpp> |
14 | #include <boost/numeric/ublas/vector_expression.hpp> |
15 | #include <boost/numeric/ublas/io.hpp> |
16 | #include "common/testhelper.hpp" |
17 | #include "utils.hpp" |
18 | |
19 | const double TOL = 1e-15; |
20 | |
21 | template<typename T> |
22 | bool check_sortedness(const boost::numeric::ublas::coordinate_vector<T>& vector) { |
23 | bool result = true; |
24 | typedef boost::numeric::ublas::coordinate_vector<T> vector_type; |
25 | typename vector_type::index_array_type idx = vector.index_data(); |
26 | typename vector_type::size_type size = vector.filled(); |
27 | |
28 | for (typename vector_type::size_type i = 0; i + 1 < size && result; ++ i) { |
29 | result &= (idx[i] < idx[i + 1]); |
30 | } |
31 | return result; |
32 | } |
33 | |
34 | void print_entries(size_t size, |
35 | const std::vector<size_t>& entries) |
36 | { |
37 | std::cerr << "Error entries - Size:" << size << ". Entries: " ; |
38 | for (size_t i = 0; i < entries.size(); ++ i) { |
39 | std::cerr << entries[i] << "; " ; |
40 | } |
41 | std::cerr << "\n" ; |
42 | } |
43 | |
44 | BOOST_UBLAS_TEST_DEF( test_coordinate_vector_inplace_merge_random ) |
45 | { |
46 | const size_t max_repeats = 100; |
47 | const size_t max_size = 100; |
48 | const size_t dim_var = 10; |
49 | const size_t nr_entries = 10; |
50 | |
51 | for (size_t repeats = 1; repeats < max_repeats; ++repeats ) { |
52 | for (size_t size = 1; size < max_size; size += 5) { |
53 | size_t size_vec = size + rand() % dim_var; |
54 | |
55 | boost::numeric::ublas::coordinate_vector<double> vector_coord(size_vec); |
56 | boost::numeric::ublas::vector<double> vector_dense(size_vec, 0); |
57 | |
58 | vector_coord.sort(); |
59 | |
60 | std::vector<size_t> entries; |
61 | for (size_t entry = 0; entry < nr_entries; ++ entry) { |
62 | int x = rand() % size_vec; |
63 | entries.push_back(x: x); |
64 | vector_coord.append_element(i: x, t: 1); |
65 | vector_dense(x) += 1; |
66 | } |
67 | vector_coord.sort(); |
68 | |
69 | { |
70 | bool sorted = check_sortedness(vector: vector_coord); |
71 | bool identical = compare_distance(m1: vector_coord, m2: vector_dense, tolerance: TOL); |
72 | if (!(sorted && identical)) { |
73 | print_entries(size: size_vec, entries); |
74 | } |
75 | BOOST_UBLAS_TEST_CHECK( check_sortedness(vector_coord) ); |
76 | BOOST_UBLAS_TEST_CHECK( compare_distance(vector_coord, vector_dense, TOL) ); |
77 | } |
78 | |
79 | for (size_t entry = 0; entry < nr_entries; ++ entry) { |
80 | int x = rand() % size_vec; |
81 | entries.push_back(x: x); |
82 | vector_coord(x) += 1; |
83 | vector_dense(x) += 1; |
84 | vector_coord.sort(); |
85 | } |
86 | |
87 | { |
88 | bool sorted = check_sortedness(vector: vector_coord); |
89 | bool identical = compare_distance(m1: vector_coord, m2: vector_dense, tolerance: TOL); |
90 | if (!(sorted && identical)) { |
91 | print_entries(size: size_vec, entries); |
92 | } |
93 | BOOST_UBLAS_TEST_CHECK( sorted ); |
94 | BOOST_UBLAS_TEST_CHECK( identical ); |
95 | } |
96 | } |
97 | } |
98 | } |
99 | |
100 | int main() |
101 | { |
102 | BOOST_UBLAS_TEST_BEGIN(); |
103 | |
104 | BOOST_UBLAS_TEST_DO( test_coordinate_vector_inplace_merge_random ); |
105 | |
106 | BOOST_UBLAS_TEST_END(); |
107 | } |
108 | |