test_coordinate_vector_inplace_merge.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef BOOST_UBLAS_NO_ELEMENT_PROXIES
  7. # define BOOST_UBLAS_NO_ELEMENT_PROXIES
  8. #endif
  9. #include <boost/numeric/ublas/assignment.hpp>
  10. #include <boost/numeric/ublas/vector.hpp>
  11. #include <boost/numeric/ublas/vector_sparse.hpp>
  12. #include <boost/numeric/ublas/vector_expression.hpp>
  13. #include <boost/numeric/ublas/io.hpp>
  14. #include "common/testhelper.hpp"
  15. #include "utils.hpp"
  16. const double TOL = 1e-15;
  17. template<typename T>
  18. bool check_sortedness(const boost::numeric::ublas::coordinate_vector<T>& vector) {
  19. bool result = true;
  20. typedef boost::numeric::ublas::coordinate_vector<T> vector_type;
  21. typename vector_type::index_array_type idx = vector.index_data();
  22. typename vector_type::size_type size = vector.filled();
  23. for (typename vector_type::size_type i = 0; i + 1 < size && result; ++ i) {
  24. result &= (idx[i] < idx[i + 1]);
  25. }
  26. return result;
  27. }
  28. void print_entries(size_t size,
  29. const std::vector<size_t>& entries)
  30. {
  31. std::cerr << "Error entries - Size:" << size << ". Entries: ";
  32. for (size_t i = 0; i < entries.size(); ++ i) {
  33. std::cerr << entries[i] << "; ";
  34. }
  35. std::cerr << "\n";
  36. }
  37. BOOST_UBLAS_TEST_DEF( test_coordinate_vector_inplace_merge_random )
  38. {
  39. const size_t max_repeats = 100;
  40. const size_t max_size = 100;
  41. const size_t dim_var = 10;
  42. const size_t nr_entries = 10;
  43. for (size_t repeats = 1; repeats < max_repeats; ++repeats ) {
  44. for (size_t size = 1; size < max_size; size += 5) {
  45. size_t size_vec = size + rand() % dim_var;
  46. boost::numeric::ublas::coordinate_vector<double> vector_coord(size_vec);
  47. boost::numeric::ublas::vector<double> vector_dense(size_vec, 0);
  48. vector_coord.sort();
  49. std::vector<size_t> entries;
  50. for (size_t entry = 0; entry < nr_entries; ++ entry) {
  51. int x = rand() % size_vec;
  52. entries.push_back(x);
  53. vector_coord.append_element(x, 1);
  54. vector_dense(x) += 1;
  55. }
  56. vector_coord.sort();
  57. {
  58. bool sorted = check_sortedness(vector_coord);
  59. bool identical = compare_distance(vector_coord, vector_dense, TOL);
  60. if (!(sorted && identical)) {
  61. print_entries(size_vec, entries);
  62. }
  63. BOOST_UBLAS_TEST_CHECK( check_sortedness(vector_coord) );
  64. BOOST_UBLAS_TEST_CHECK( compare_distance(vector_coord, vector_dense, TOL) );
  65. }
  66. for (size_t entry = 0; entry < nr_entries; ++ entry) {
  67. int x = rand() % size_vec;
  68. entries.push_back(x);
  69. vector_coord(x) += 1;
  70. vector_dense(x) += 1;
  71. vector_coord.sort();
  72. }
  73. {
  74. bool sorted = check_sortedness(vector_coord);
  75. bool identical = compare_distance(vector_coord, vector_dense, TOL);
  76. if (!(sorted && identical)) {
  77. print_entries(size_vec, entries);
  78. }
  79. BOOST_UBLAS_TEST_CHECK( sorted );
  80. BOOST_UBLAS_TEST_CHECK( identical );
  81. }
  82. }
  83. }
  84. }
  85. int main()
  86. {
  87. BOOST_UBLAS_TEST_BEGIN();
  88. BOOST_UBLAS_TEST_DO( test_coordinate_vector_inplace_merge_random );
  89. BOOST_UBLAS_TEST_END();
  90. }