| 73 | Iterator(const std::string& name) : Test::Base(prefix + name) {} |
| 74 | /// Perform actual tests |
| 75 | template<class Array> bool runTestForArray(Array& a) { |
| 76 | // Test/problem information. |
| 77 | const char* test = "NONE"; |
| 78 | const char* problem = "NONE"; |
| 79 | // Constant reference to the array |
| 80 | const Array& const_a = a; |
| 81 | |
| 82 | START_TEST("Iteration"); |
| 83 | { |
| 84 | typedef typename Array::reference reference; |
| 85 | typedef typename Array::pointer pointer; |
| 86 | typedef typename Array::iterator iterator; |
| 87 | const iterator begin = a.begin(), end = a.end(); |
| 88 | CHECK_TEST(end-begin==a.size(),"Distance != size"); |
| 89 | int index = 0; |
| 90 | iterator iter = begin; |
| 91 | for(; iter != end; ++iter, ++index) { |
| 92 | reference ref = *iter; |
| 93 | const pointer ptr = &ref; |
| 94 | CHECK_TEST(ptr==&a[index],"Iterator points to the wrong element (going forward)"); |
| 95 | } |
| 96 | CHECK_TEST(index==a.size(),"Iteration covered the wrong number of elements (going forward)"); |
| 97 | for(; iter != begin; --iter, --index) { |
| 98 | reference ref = *(iter-1); |
| 99 | const pointer ptr = &ref; |
| 100 | CHECK_TEST(ptr==&a[index-1],"Iterator points to the wrong element (going backwards)"); |
| 101 | } |
| 102 | CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)"); |
| 103 | } |
| 104 | START_TEST("Read-only iteration"); |
| 105 | { |
| 106 | typedef typename Array::const_reference reference; |
| 107 | typedef typename Array::const_pointer pointer; |
| 108 | typedef typename Array::const_iterator iterator; |
| 109 | const iterator begin = const_a.begin(), end = const_a.end(); |
| 110 | CHECK_TEST(end-begin==const_a.size(),"Distance != size"); |
| 111 | int index = 0; |
| 112 | iterator iter = begin; |
| 113 | for(; iter != end; ++iter, ++index) { |
| 114 | reference ref = *iter; |
| 115 | const pointer ptr = &ref; |
| 116 | CHECK_TEST(ptr==&const_a[index],"Iterator points to the wrong element (going forward)"); |
| 117 | } |
| 118 | CHECK_TEST(index==const_a.size(),"Iteration covered the wrong number of elements (going forward)"); |
| 119 | for(; iter != begin; --iter, --index) { |
| 120 | reference ref = *(iter-1); |
| 121 | const pointer ptr = &ref; |
| 122 | CHECK_TEST(ptr==&const_a[index-1],"Iterator points to the wrong element (going backwards)"); |
| 123 | } |
| 124 | CHECK_TEST(index==0,"Iteration covered the wrong number of elements (going backward)"); |
| 125 | } |
| 126 | |
| 127 | START_TEST("Reverse iteration"); |
| 128 | { |
| 129 | typedef typename Array::reference reference; |
| 130 | typedef typename Array::pointer pointer; |
| 131 | typedef typename Array::reverse_iterator iterator; |
| 132 | const iterator begin = a.rbegin(), end = a.rend(); |