Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include "range.hpp"
  2.  
  3. #include "catch.hpp"
  4.  
  5. #include <stdexcept>
  6.  
  7. TEST_CASE("range basic") {
  8. SECTION("one argument") {
  9. int i = 0;
  10. for (int x : range(5)) {
  11. REQUIRE(i < 5);
  12. REQUIRE(x == i++);
  13. }
  14.  
  15. auto r = range(5);
  16. REQUIRE(*std::next(r.begin(), 4) == 4);
  17. REQUIRE(std::next(r.begin(), 5) == r.end());
  18. }
  19.  
  20. SECTION("two arguments") {
  21. int i = 5;
  22. for (int x : range(5, 10)) {
  23. REQUIRE(i < 10);
  24. REQUIRE(x == i++);
  25. }
  26. auto r = range(5, 10);
  27. REQUIRE(*std::next(r.begin(), 4) == 9);
  28. REQUIRE(std::next(r.begin(), 5) == r.end());
  29. }
  30.  
  31. SECTION("three arguments") {
  32. int i = 5;
  33. auto r = range(5, 100, 5);
  34. for (int x : r) {
  35. REQUIRE(i < 100);
  36. REQUIRE(x == i);
  37. i += 5;
  38. }
  39. REQUIRE(*std::next(r.begin(), 18) == 95);
  40. REQUIRE(std::next(r.begin(), 19) == r.end());
  41. }
  42.  
  43. SECTION("with zero step") {
  44. REQUIRE_THROWS_AS(range(1, 2, 0), std::invalid_argument);
  45. REQUIRE_THROWS_AS(range(1.1, 2.2, 0.0), std::invalid_argument);
  46. }
  47.  
  48. SECTION("with negative step") {
  49. int i = 10;
  50. auto r = range(10, 0, -1);
  51. for (int x : r) {
  52. REQUIRE(x == i);
  53. --i;
  54. REQUIRE(i >= 0);
  55. }
  56. REQUIRE(i == 0);
  57. REQUIRE(*std::next(r.begin(), 5) == 5);
  58. REQUIRE(*std::next(r.begin(), 9) == 1);
  59. REQUIRE(std::next(r.begin(), 10) == r.end());
  60. }
  61.  
  62. SECTION("basic examples from the assignment") {
  63. int x = -1;
  64. for (int n : range(10)) {
  65. x++;
  66. REQUIRE(x == n);
  67. }
  68. REQUIRE(x == 9);
  69.  
  70. x = -5;
  71. for (int n : range(0, 30, 5)) {
  72. x += 5;
  73. REQUIRE(x == n);
  74. }
  75. REQUIRE(x == 25);
  76.  
  77. x = 1;
  78. for (int n : range(0, -10, -1)) {
  79. x--;
  80. REQUIRE(x == n);
  81. }
  82. REQUIRE(x == -9);
  83.  
  84. x = 9;
  85. for (int n : range(4, -9, -5)) {
  86. x -= 5;
  87. REQUIRE(x == n);
  88. }
  89. REQUIRE(x == -6);
  90.  
  91. x = -9;
  92. for (int n : range(-4, 9, 5)) {
  93. x += 5;
  94. REQUIRE(x == n);
  95. }
  96. REQUIRE(x == 6);
  97. }
  98.  
  99. SECTION("operations") {
  100. auto ranger = range(1, 10, 5);
  101. auto it = ranger.begin();
  102. auto it2 = ranger.begin();
  103.  
  104. REQUIRE(*it == 1);
  105.  
  106. ++it;
  107.  
  108. REQUIRE(*it == 6);
  109.  
  110. ++it2;
  111.  
  112. REQUIRE((it == it2));
  113.  
  114. REQUIRE((*(++it) == 11));
  115. REQUIRE((*it++ == 11));
  116. REQUIRE((*(++it) != 10));
  117.  
  118. REQUIRE((++it != it2));
  119.  
  120. auto it3 = it;
  121. auto it4 = it++;
  122. REQUIRE(it3 == it4);
  123. REQUIRE(it != it4);
  124.  
  125. }
  126.  
  127. SECTION("floats") {
  128. auto ranger = range(1.1, 10.1, 5.2);
  129. double x = -4.1;
  130. for (double i : ranger) {
  131. x += 5.2;
  132. REQUIRE(x - i < 0.00001);
  133. }
  134. REQUIRE(x - 6.3 < 0.00001);
  135. }
  136.  
  137. SECTION("with incorrect step") {
  138. int i = 10;
  139. auto opposite1 = range(10, 1, 1);
  140. for (int x : opposite1) {
  141. --i;
  142. --x;
  143. }
  144. REQUIRE(i == 10);
  145.  
  146. auto opposite2 = range(0, 10, -1);
  147. for (int x : opposite2) {
  148. --i;
  149. --x;
  150. }
  151. REQUIRE(i == 10);
  152.  
  153. auto opposite3 = range(1, 1, -1);
  154. for (int x : opposite3) {
  155. --i;
  156. --x;
  157. }
  158. REQUIRE(i == 10);
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement