示例#1
0
void test_rng_3()
{
    int i[3] = {1, 2, 3};
    int j[3] = {4, 5, 6};
    std::pair<Iter1, Iter2> r = ranges::swap_ranges(as_lvalue(ranges::make_iterator_range(Iter1(i), Iter1(i+3))), Iter2(j));
    CHECK(base(r.first) == i+3);
    CHECK(base(r.second) == j+3);
    CHECK(i[0] == 4);
    CHECK(i[1] == 5);
    CHECK(i[2] == 6);
    CHECK(j[0] == 1);
    CHECK(j[1] == 2);
    CHECK(j[2] == 3);

    using Sent1 = typename sentinel_type<Iter1>::type;
    r = ranges::swap_ranges(as_lvalue(ranges::make_iterator_range(Iter1(j), Sent1(j+3))), Iter2(i));
    CHECK(base(r.first) == j+3);
    CHECK(base(r.second) == i+3);
    CHECK(i[0] == 1);
    CHECK(i[1] == 2);
    CHECK(i[2] == 3);
    CHECK(j[0] == 4);
    CHECK(j[1] == 5);
    CHECK(j[2] == 6);
}
示例#2
0
void test_iter_4()
{
    int i[3] = {1, 2, 3};
    int j[4] = {4, 5, 6, 7};
    std::pair<Iter1, Iter2> r = ranges::swap_ranges(Iter1(i), Iter1(i+3), Iter2(j), Iter2(j+4));
    CHECK(base(r.first) == i+3);
    CHECK(base(r.second) == j+3);
    CHECK(i[0] == 4);
    CHECK(i[1] == 5);
    CHECK(i[2] == 6);
    CHECK(j[0] == 1);
    CHECK(j[1] == 2);
    CHECK(j[2] == 3);
    CHECK(j[3] == 7);

    using Sent1 = typename sentinel_type<Iter1>::type;
    using Sent2 = typename sentinel_type<Iter2>::type;
    r = ranges::swap_ranges(Iter1(j), Sent1(j+4), Iter2(i), Sent2(i+3));
    CHECK(base(r.first) == j+3);
    CHECK(base(r.second) == i+3);
    CHECK(i[0] == 1);
    CHECK(i[1] == 2);
    CHECK(i[2] == 3);
    CHECK(j[0] == 4);
    CHECK(j[1] == 5);
    CHECK(j[2] == 6);
    CHECK(j[3] == 7);
}
示例#3
0
void test_rng_4()
{
    int i[3] = {1, 2, 3};
    int j[4] = {4, 5, 6, 7};
    std::pair<Iter1, Iter2> r = ranges::swap_ranges(
        as_lvalue(ranges::make_iterator_range(Iter1(i), Iter1(i+3))),
        as_lvalue(ranges::make_iterator_range(Iter2(j), Iter2(j+4))));
    CHECK(base(r.first) == i+3);
    CHECK(base(r.second) == j+3);
    CHECK(i[0] == 4);
    CHECK(i[1] == 5);
    CHECK(i[2] == 6);
    CHECK(j[0] == 1);
    CHECK(j[1] == 2);
    CHECK(j[2] == 3);
    CHECK(j[3] == 7);

    using Sent1 = typename sentinel_type<Iter1>::type;
    using Sent2 = typename sentinel_type<Iter2>::type;
    r = ranges::swap_ranges(
        as_lvalue(ranges::make_iterator_range(Iter1(j), Sent1(j+4))),
        as_lvalue(ranges::make_iterator_range(Iter2(i), Sent2(i+3))));
    CHECK(base(r.first) == j+3);
    CHECK(base(r.second) == i+3);
    CHECK(i[0] == 1);
    CHECK(i[1] == 2);
    CHECK(i[2] == 3);
    CHECK(j[0] == 4);
    CHECK(j[1] == 5);
    CHECK(j[2] == 6);
    CHECK(j[3] == 7);

    auto r2 = ranges::swap_ranges(
        ranges::make_iterator_range(Iter1(j), Sent1(j+4)),
        ranges::make_iterator_range(Iter2(i), Sent2(i+3)));
    CHECK(base(r2.first.get_unsafe()) == j+3);
    CHECK(base(r2.second.get_unsafe()) == i+3);
    CHECK(i[0] == 4);
    CHECK(i[1] == 5);
    CHECK(i[2] == 6);
    CHECK(j[0] == 1);
    CHECK(j[1] == 2);
    CHECK(j[2] == 3);
    CHECK(j[3] == 7);
}
void
test_iter1()
{
    int ia[] = {1, 2, 3, 4};
    constexpr unsigned sa = ranges::size(ia);
    int ib[] = {1, 2, 3};
    CHECK(!ranges::lexicographical_compare(Iter1(ia), Sent1(ia+sa), Iter2(ib), Sent2(ib+2)));
    CHECK(ranges::lexicographical_compare(Iter1(ib), Sent1(ib+2), Iter2(ia), Sent2(ia+sa)));
    CHECK(!ranges::lexicographical_compare(Iter1(ia), Sent1(ia+sa), Iter2(ib), Sent2(ib+3)));
    CHECK(ranges::lexicographical_compare(Iter1(ib), Sent1(ib+3), Iter2(ia), Sent2(ia+sa)));
    CHECK(ranges::lexicographical_compare(Iter1(ia), Sent1(ia+sa), Iter2(ib+1), Sent2(ib+3)));
    CHECK(!ranges::lexicographical_compare(Iter1(ib+1), Sent1(ib+3), Iter2(ia), Sent2(ia+sa)));
}
void
test_iter_comp1()
{
    int ia[] = {1, 2, 3, 4};
    const unsigned sa = sizeof(ia)/sizeof(ia[0]);
    int ib[] = {1, 2, 3};
    typedef std::greater<int> C;
    C c;
    CHECK(!ranges::lexicographical_compare(Iter1(ia), Sent1(ia+sa), Iter2(ib), Sent2(ib+2), c));
    CHECK(ranges::lexicographical_compare(Iter1(ib), Sent1(ib+2), Iter2(ia), Sent2(ia+sa), c));
    CHECK(!ranges::lexicographical_compare(Iter1(ia), Sent1(ia+sa), Iter2(ib), Sent2(ib+3), c));
    CHECK(ranges::lexicographical_compare(Iter1(ib), Sent1(ib+3), Iter2(ia), Sent2(ia+sa), c));
    CHECK(!ranges::lexicographical_compare(Iter1(ia), Sent1(ia+sa), Iter2(ib+1), Sent2(ib+3), c));
    CHECK(ranges::lexicographical_compare(Iter1(ib+1), Sent1(ib+3), Iter2(ia), Sent2(ia+sa), c));
}
示例#6
0
int main()
{
    test<input_iterator<const int*>, input_iterator<const int*> >();
    test<input_iterator<const int*>, forward_iterator<const int*> >();
    test<input_iterator<const int*>, bidirectional_iterator<const int*> >();
    test<input_iterator<const int*>, random_access_iterator<const int*> >();
    test<input_iterator<const int*>, const int*>();

    test<forward_iterator<const int*>, input_iterator<const int*> >();
    test<forward_iterator<const int*>, forward_iterator<const int*> >();
    test<forward_iterator<const int*>, bidirectional_iterator<const int*> >();
    test<forward_iterator<const int*>, random_access_iterator<const int*> >();
    test<forward_iterator<const int*>, const int*>();

    test<bidirectional_iterator<const int*>, input_iterator<const int*> >();
    test<bidirectional_iterator<const int*>, forward_iterator<const int*> >();
    test<bidirectional_iterator<const int*>, bidirectional_iterator<const int*> >();
    test<bidirectional_iterator<const int*>, random_access_iterator<const int*> >();
    test<bidirectional_iterator<const int*>, const int*>();

    test<random_access_iterator<const int*>, input_iterator<const int*> >();
    test<random_access_iterator<const int*>, forward_iterator<const int*> >();
    test<random_access_iterator<const int*>, bidirectional_iterator<const int*> >();
    test<random_access_iterator<const int*>, random_access_iterator<const int*> >();
    test<random_access_iterator<const int*>, const int*>();

    test<const int*, input_iterator<const int*> >();
    test<const int*, forward_iterator<const int*> >();
    test<const int*, bidirectional_iterator<const int*> >();
    test<const int*, random_access_iterator<const int*> >();
    test<const int*, const int*>();

    // Test initializer lists:
    CHECK(ranges::inner_product({1,2,3}, {4,5,6}, 0) == 32);

    // test projections:
    {
      S a[] = {{1}, {2}, {3}, {4}, {5}, {6}};
      S b[] = {{6}, {5}, {4}, {3}, {2}, {1}};
      unsigned sa = sizeof(a) / sizeof(a[0]);

      using Iter1 = input_iterator<const S*>;
      using Sent1 = input_iterator<const S*>;
      using Iter2 = Iter1;

      // rng + bops:
      auto bops = [&](S* b1, int l1, S* b2, int i)
      {
        return ranges::inner_product(ranges::make_iterator_range(Iter1(b1), Sent1(b1+l1)),
                                     ranges::make_iterator_range(Iter2(b2), Iter2(b2+l1)), i,
                                     std::multiplies<int>(), std::plus<int>(),
                                     &S::i, &S::i);
      };

      CHECK(bops(a, 0, b, 1) == 1);
      CHECK(bops(a, 0, b, 10) == 10);
      CHECK(bops(a, 1, b, 1) == 7);
      CHECK(bops(a, 1, b, 10) == 70);
      CHECK(bops(a, 2, b, 1) == 49);
      CHECK(bops(a, 2, b, 10) == 490);
      CHECK(bops(a, sa, b, 1) == 117649);
      CHECK(bops(a, sa, b, 10) == 1176490);
    }

    {
        int a[] = {1, 2, 3, 4, 5, 6};
        int b[] = {6, 5, 4, 3, 2, 1};

        // raw array test:
        CHECK(ranges::inner_product(a, b, 0) == 56);
        CHECK(ranges::inner_product(a, b, 10) == 66);
    }

    return ::test_result();
}