| 1 | // { dg-do run } |
| 2 | |
| 3 | #include <algorithm> |
| 4 | #include <testsuite_iterators.h> |
| 5 | |
| 6 | using namespace __gnu_test; |
| 7 | |
| 8 | int out[4]; |
| 9 | short out_shrt[4]; |
| 10 | short in[7] = { 1, 2, 2, 2, 3, 4, 4 }; |
| 11 | |
| 12 | template<typename T> |
| 13 | void |
| 14 | check_and_reset(T* p) |
| 15 | { |
| 16 | VERIFY( p[0] == 1 ); |
| 17 | VERIFY( p[1] == 2 ); |
| 18 | VERIFY( p[2] == 3 ); |
| 19 | VERIFY( p[3] == 4 ); |
| 20 | std::fill_n(p, 4, 0); |
| 21 | } |
| 22 | |
| 23 | struct Eq |
| 24 | { |
| 25 | bool operator()(short i, short j) const { return i == j; } |
| 26 | bool operator()(short, int) const { VERIFY(false); } |
| 27 | bool operator()(int, short) const { VERIFY(false); } |
| 28 | }; |
| 29 | |
| 30 | struct Eq2 |
| 31 | { |
| 32 | bool operator()(const short& i, const short& j) const |
| 33 | { |
| 34 | // Both arguments should be elements of the 'in' array. |
| 35 | VERIFY( in+0 <= &i && &i < in+7 ); |
| 36 | VERIFY( in+0 <= &j && &j < in+7 ); |
| 37 | VERIFY( &i < &j ); |
| 38 | return i == j; |
| 39 | } |
| 40 | bool operator()(short, int) const { VERIFY(false); } |
| 41 | bool operator()(int, short) const { VERIFY(false); } |
| 42 | }; |
| 43 | |
| 44 | struct Eq3 |
| 45 | { |
| 46 | bool operator()(const short& i, const short& j) const |
| 47 | { |
| 48 | // First argument should be element of the 'out' array. |
| 49 | // Second argument should be element of the 'in' array. |
| 50 | VERIFY( out_shrt+0 <= &i && &i < out_shrt+4 ); |
| 51 | VERIFY( in+0 <= &j && &j < in+7 ); |
| 52 | return i == j; |
| 53 | } |
| 54 | bool operator()(short, int) const { VERIFY(false); } |
| 55 | bool operator()(int, short) const { VERIFY(false); } |
| 56 | }; |
| 57 | |
| 58 | void |
| 59 | test_forward_source() |
| 60 | { |
| 61 | // The input range uses forward iterators |
| 62 | test_container<short, forward_iterator_wrapper> inc(in); |
| 63 | test_container<int, output_iterator_wrapper> outc(out); |
| 64 | std::unique_copy(inc.begin(), inc.end(), outc.begin()); |
| 65 | check_and_reset(out); |
| 66 | |
| 67 | test_container<short, forward_iterator_wrapper> inc2(in); |
| 68 | test_container<int, output_iterator_wrapper> outc2(out); |
| 69 | std::unique_copy(inc2.begin(), inc2.end(), outc2.begin(), Eq2()); |
| 70 | check_and_reset(out); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | test_output_dest() |
| 75 | { |
| 76 | // The input range uses input iterators. |
| 77 | // The output range uses output iterators. |
| 78 | test_container<short, input_iterator_wrapper> inc(in); |
| 79 | test_container<int, output_iterator_wrapper> outc(out); |
| 80 | std::unique_copy(inc.begin(), inc.end(), outc.begin()); |
| 81 | check_and_reset(out); |
| 82 | |
| 83 | test_container<short, input_iterator_wrapper> inc2(in); |
| 84 | test_container<int, output_iterator_wrapper> outc2(out); |
| 85 | std::unique_copy(inc2.begin(), inc2.end(), outc2.begin(), Eq()); |
| 86 | check_and_reset(out); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | test_forward_dest_diff_type() |
| 91 | { |
| 92 | // The input range uses input iterators. |
| 93 | // The output range uses forward iterators, but with different value type. |
| 94 | test_container<short, input_iterator_wrapper> inc(in); |
| 95 | test_container<int, forward_iterator_wrapper> outc(out); |
| 96 | std::unique_copy(inc.begin(), inc.end(), outc.begin()); |
| 97 | check_and_reset(out); |
| 98 | |
| 99 | test_container<short, input_iterator_wrapper> inc2(in); |
| 100 | test_container<int, forward_iterator_wrapper> outc2(out); |
| 101 | std::unique_copy(inc2.begin(), inc2.end(), outc2.begin(), Eq()); |
| 102 | check_and_reset(out); |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | test_forward_dest_same_type() |
| 107 | { |
| 108 | // The input range uses input iterators. |
| 109 | // The output range uses forward iterators, with same value type. |
| 110 | test_container<short, input_iterator_wrapper> inc(in); |
| 111 | test_container<short, forward_iterator_wrapper> outc(out_shrt); |
| 112 | std::unique_copy(inc.begin(), inc.end(), outc.begin()); |
| 113 | check_and_reset(out_shrt); |
| 114 | |
| 115 | test_container<short, input_iterator_wrapper> inc2(in); |
| 116 | test_container<short, forward_iterator_wrapper> outc2(out_shrt); |
| 117 | std::unique_copy(inc2.begin(), inc2.end(), outc2.begin(), Eq3()); |
| 118 | check_and_reset(out_shrt); |
| 119 | } |
| 120 | |
| 121 | int main() |
| 122 | { |
| 123 | test_forward_source(); |
| 124 | test_output_dest(); |
| 125 | test_forward_dest_diff_type(); |
| 126 | test_forward_dest_same_type(); |
| 127 | } |