1 // Copyright 2002 The Trustees of Indiana University. 2 3 // Use, modification and distribution is subject to the Boost Software 4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 // Boost.MultiArray Library 8 // Authors: Ronald Garcia 9 // Jeremy Siek 10 // Andrew Lumsdaine 11 // See http://www.boost.org/libs/multi_array for documentation. 12 13 // 14 // range1.cpp - test of index_range 15 // 16 17 18 #include <boost/multi_array/index_range.hpp> 19 20 #include <boost/core/lightweight_test.hpp> 21 22 #include <boost/array.hpp> 23 #include <cstddef> 24 25 int main()26main() 27 { 28 typedef boost::detail::multi_array::index_range<int,std::size_t> range; 29 30 { 31 // typical range creation and extraction 32 range r1(-3,5); 33 BOOST_TEST(r1.start() == -3); 34 BOOST_TEST(r1.finish() == 5); 35 BOOST_TEST(r1.stride() == 1); 36 BOOST_TEST(r1.size(0) == 8); 37 BOOST_TEST(!r1.is_degenerate()); 38 BOOST_TEST(r1.get_start(0) == -3); 39 BOOST_TEST(r1.get_finish(100) == 5); 40 } 41 42 { 43 range r2(-3,5,2); 44 BOOST_TEST(r2.start() == -3); 45 BOOST_TEST(r2.finish() == 5); 46 BOOST_TEST(r2.stride() == 2); 47 BOOST_TEST(r2.size(0) == 4); 48 BOOST_TEST(!r2.is_degenerate()); 49 } 50 51 { 52 // degenerate creation 53 range r3(5); 54 BOOST_TEST(r3.start() == 5); 55 BOOST_TEST(r3.finish() == 6); 56 BOOST_TEST(r3.stride() == 1); 57 BOOST_TEST(r3.size(0) == 1); 58 BOOST_TEST(r3.is_degenerate()); 59 } 60 61 { 62 // default range creation 63 range r4; 64 BOOST_TEST(r4.get_start(0) == 0); 65 BOOST_TEST(r4.get_finish(100) == 100); 66 BOOST_TEST(r4.stride() == 1); 67 BOOST_TEST(r4.size(0) == 0); 68 } 69 70 { 71 // create a range using the setter methods 72 range r5 = range().stride(2).start(-3).finish(7); 73 BOOST_TEST(r5.start() == -3); 74 BOOST_TEST(r5.stride() == 2); 75 BOOST_TEST(r5.finish() == 7); 76 BOOST_TEST(r5.size(0) == 5); 77 } 78 79 // try out all the comparison operators 80 { 81 range r6 = -3 <= range().stride(2) < 7; 82 BOOST_TEST(r6.start() == -3); 83 BOOST_TEST(r6.stride() == 2); 84 BOOST_TEST(r6.finish() == 7); 85 BOOST_TEST(r6.size(0) == 5); 86 } 87 88 { 89 range r7 = -3 < range() <= 7; 90 BOOST_TEST(r7.start() == -2); 91 BOOST_TEST(r7.stride() == 1); 92 BOOST_TEST(r7.finish() == 8); 93 BOOST_TEST(r7.size(0) == 10); 94 } 95 96 // arithmetic operators 97 { 98 range r8 = range(0,5) + 2; 99 BOOST_TEST(r8.start() == 2); 100 BOOST_TEST(r8.stride() == 1); 101 BOOST_TEST(r8.finish() == 7); 102 BOOST_TEST(r8.size(0) == 5); 103 } 104 105 { 106 range r9 = range(0,5) - 2; 107 BOOST_TEST(r9.start() == -2); 108 BOOST_TEST(r9.stride() == 1); 109 BOOST_TEST(r9.finish() == 3); 110 BOOST_TEST(r9.size(0) == 5); 111 } 112 113 return boost::report_errors(); 114 } 115