Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "range.hpp"
- #include "catch.hpp"
- #include <stdexcept>
- TEST_CASE("range basic") {
- SECTION("one argument") {
- int i = 0;
- for (int x : range(5)) {
- REQUIRE(i < 5);
- REQUIRE(x == i++);
- }
- auto r = range(5);
- REQUIRE(*std::next(r.begin(), 4) == 4);
- REQUIRE(std::next(r.begin(), 5) == r.end());
- }
- SECTION("two arguments") {
- int i = 5;
- for (int x : range(5, 10)) {
- REQUIRE(i < 10);
- REQUIRE(x == i++);
- }
- auto r = range(5, 10);
- REQUIRE(*std::next(r.begin(), 4) == 9);
- REQUIRE(std::next(r.begin(), 5) == r.end());
- }
- SECTION("three arguments") {
- int i = 5;
- auto r = range(5, 100, 5);
- for (int x : r) {
- REQUIRE(i < 100);
- REQUIRE(x == i);
- i += 5;
- }
- REQUIRE(*std::next(r.begin(), 18) == 95);
- REQUIRE(std::next(r.begin(), 19) == r.end());
- }
- SECTION("with zero step") {
- REQUIRE_THROWS_AS(range(1, 2, 0), std::invalid_argument);
- REQUIRE_THROWS_AS(range(1.1, 2.2, 0.0), std::invalid_argument);
- }
- SECTION("with negative step") {
- int i = 10;
- auto r = range(10, 0, -1);
- for (int x : r) {
- REQUIRE(x == i);
- --i;
- REQUIRE(i >= 0);
- }
- REQUIRE(i == 0);
- REQUIRE(*std::next(r.begin(), 5) == 5);
- REQUIRE(*std::next(r.begin(), 9) == 1);
- REQUIRE(std::next(r.begin(), 10) == r.end());
- }
- SECTION("basic examples from the assignment") {
- int x = -1;
- for (int n : range(10)) {
- x++;
- REQUIRE(x == n);
- }
- REQUIRE(x == 9);
- x = -5;
- for (int n : range(0, 30, 5)) {
- x += 5;
- REQUIRE(x == n);
- }
- REQUIRE(x == 25);
- x = 1;
- for (int n : range(0, -10, -1)) {
- x--;
- REQUIRE(x == n);
- }
- REQUIRE(x == -9);
- x = 9;
- for (int n : range(4, -9, -5)) {
- x -= 5;
- REQUIRE(x == n);
- }
- REQUIRE(x == -6);
- x = -9;
- for (int n : range(-4, 9, 5)) {
- x += 5;
- REQUIRE(x == n);
- }
- REQUIRE(x == 6);
- }
- SECTION("operations") {
- auto ranger = range(1, 10, 5);
- auto it = ranger.begin();
- auto it2 = ranger.begin();
- REQUIRE(*it == 1);
- ++it;
- REQUIRE(*it == 6);
- ++it2;
- REQUIRE((it == it2));
- REQUIRE((*(++it) == 11));
- REQUIRE((*it++ == 11));
- REQUIRE((*(++it) != 10));
- REQUIRE((++it != it2));
- auto it3 = it;
- auto it4 = it++;
- REQUIRE(it3 == it4);
- REQUIRE(it != it4);
- }
- SECTION("floats") {
- auto ranger = range(1.1, 10.1, 5.2);
- double x = -4.1;
- for (double i : ranger) {
- x += 5.2;
- REQUIRE(x - i < 0.00001);
- }
- REQUIRE(x - 6.3 < 0.00001);
- }
- SECTION("with incorrect step") {
- int i = 10;
- auto opposite1 = range(10, 1, 1);
- for (int x : opposite1) {
- --i;
- --x;
- }
- REQUIRE(i == 10);
- auto opposite2 = range(0, 10, -1);
- for (int x : opposite2) {
- --i;
- --x;
- }
- REQUIRE(i == 10);
- auto opposite3 = range(1, 1, -1);
- for (int x : opposite3) {
- --i;
- --x;
- }
- REQUIRE(i == 10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement