Пример #1
0
int main() {
    std::vector<int> ivec{1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4};
    for (auto& i : dropwhile([] (int i) {return i < 5;}, ivec)) {
        std::cout << i << '\n';
        i = 69;
    }
    assert(ivec.at(0) == 1);
    assert(ivec.at(4) == 69);

    for (auto i : dropwhile([] (int i) {return i < 5;}, range(10))) {
        std::cout << i << '\n';
    }

    for (auto i : dropwhile([] (int i) {return i < 5;}, 
                {1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    for (auto i : dropwhile([] (int i) {return i < 5;}, 
                std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    return 0;
}
Пример #2
0
int main()
{
    for (auto i : range(10)) {
        std::cout << i << std::endl;
    }
    for (auto i : range(20, 30)) {
        std::cout << i << std::endl;
    }
    for (auto i : range(50, 60, 2)) {
        std::cout << i << std::endl;
    }

    std::cout << "Negative Tests\n";
    for (auto i: range(-10, 0)) {
        std::cout << i << std::endl;
    }

    for (auto i : range(-10, 10, 2)) {
        std::cout << i << std::endl;
    }

    std::cout << "Tests where (stop - start)%step != 0" << std::endl;
    for (auto i : range(1, 10, 2)) {
        std::cout << i << std::endl;
    }

    for (auto i : range(-1, -10, -2)) {
        std::cout << i << std::endl;
    }
    
    // invalid ranges:
    std::cout << "Should not print anything after this line until exception\n";
    for (auto i : range(-10, 0, -1)) {
        std::cout << i << std::endl;
    }

    for (auto i : range(0, 1, -1)) {
        std::cout << i << std::endl;
    }

    std::cout << "Should see exception now\n";
    for (auto i : range(0, 10, 0) ) {
        std::cout << i << std::endl;
    }

    return 0;
}
Пример #3
0
int main() {
    std::vector<int> ivec{1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1};
    for (auto i : takewhile([] (int i) {return i < 5;}, ivec)) {
        std::cout << i << '\n';
    }

    for (auto i : takewhile([] (int i) {return i < 5;}, range(10))) {
        std::cout << i << '\n';
    }

    for (auto i : takewhile([] (int i) {return i < 5;}, 
                {1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    std::cout << "with temporary\n";
    for (auto i : takewhile([] (int i) {return i < 5;}, 
                std::vector<int>{1, 2, 3, 4, 5, 6, 7, 8, 9})) {
        std::cout << i << '\n';
    }

    return 0;
}
Пример #4
0
#include "range.hpp"

#include <vector>
#include <iterator>
#include <algorithm>

#include "catch.hpp"

using Vec = const std::vector<int>;
using iter::range;


TEST_CASE("Range works with only stop", "[range]") {
    auto r = range(5);
    Vec v(std::begin(r), std::end(r));
    Vec vc{0, 1, 2, 3, 4};

    REQUIRE( v == vc );
}

TEST_CASE("Range works with start and stop", "[range]") {
    auto r = range(1, 5);
    Vec v(std::begin(r), std::end(r));
    Vec vc {1, 2, 3, 4};

    REQUIRE( v == vc );
}

TEST_CASE("Range works with positive step > 1", "[range]") {
    auto r = range(1, 10, 3);
    Vec v(std::begin(r), std::end(r));