Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. #include <gtest/gtest.hpp>
  2. #include <reduce.h>
  3. #include <vector>
  4. #include <algorithm>
  5. #include "commons.h"
  6.  
  7. TEST(Correctness, Empty) {
  8. std::vector<int> values{1, 2, 3};
  9. ASSERT_EQ(6,
  10. Reduce(values.begin(), values.end(), 0, [](int sum, int cur) { return sum + cur; }));
  11.  
  12. ASSERT_EQ(6,
  13. Reduce(values.begin(), values.end(), 1, [](int sum, int cur) { return sum * cur; }));
  14.  
  15. ASSERT_EQ(0,
  16. Reduce(values.begin(), values.end(), 0, [](int prod, int cur) { return prod * cur; }));
  17.  
  18. ASSERT_EQ(30,
  19. Reduce(values.begin(), values.end(), 5, [](int prod, int cur) { return prod * cur; }));
  20.  
  21. std::vector<int> empty;
  22. ASSERT_EQ(0, Reduce(empty.begin(), empty.end(), 0, Summator<int>()));
  23.  
  24. ASSERT_EQ(5,
  25. Reduce(empty.begin(), empty.end(), 5, [](int prod, int cur) { return prod * cur; }));
  26.  
  27. std::vector<int> one{1};
  28. ASSERT_EQ(1, Reduce(one.begin(), one.end(), 0, Summator<int>()));
  29. ASSERT_EQ(5,
  30. Reduce(one.begin(), one.end(), 5, [](int prod, int cur) { return prod * cur; }));
  31.  
  32. std::vector<int> two{1, 2};
  33. ASSERT_EQ(3, Reduce(two.begin(), two.end(), 0, Summator<int>()));
  34. ASSERT_EQ(10,
  35. Reduce(two.begin(), two.end(), 5, [](int prod, int cur) { return prod * cur; }));
  36.  
  37. }
  38.  
  39. TEST(Correctness, SimpleTest) {
  40. std::vector<uint32_t> lst(GenTest(1027));
  41.  
  42. auto func = [](uint32_t cur, uint32_t next) { return cur + next; };
  43. ASSERT_EQ(std::accumulate(lst.begin(), lst.end(), 1, func),
  44. Reduce(lst.begin(), lst.end(), 1, func));
  45. }
  46.  
  47. TEST(Correctness, Multiplication) {
  48. std::vector<uint64_t> lst(GenTestSmallNumbers(31));
  49. auto func = [](uint64_t cur, uint64_t next) { return cur * next; };
  50. ASSERT_EQ(std::accumulate(lst.begin(), lst.end(), 7, func),
  51. Reduce(lst.begin(), lst.end(), 7, func));
  52. }
  53.  
  54. class StrangeDefaultInt {
  55. public:
  56. StrangeDefaultInt() : x(3) {}
  57.  
  58. StrangeDefaultInt(int x_) : x(x_) {}
  59.  
  60. StrangeDefaultInt& operator+=(const StrangeDefaultInt& rhs) {
  61. this->x += rhs.x;
  62. return *this;
  63. }
  64.  
  65. friend bool operator==(const StrangeDefaultInt& lhs, const StrangeDefaultInt& rhs);
  66.  
  67. friend bool operator!=(const StrangeDefaultInt& lhs, const StrangeDefaultInt& rhs);
  68.  
  69. private:
  70. int x;
  71. };
  72.  
  73. StrangeDefaultInt operator+(StrangeDefaultInt lhs, const StrangeDefaultInt& rhs) {
  74. lhs += rhs;
  75. return lhs;
  76. }
  77.  
  78. bool operator==(const StrangeDefaultInt& lhs, const StrangeDefaultInt& rhs) {
  79. return lhs.x == rhs.x;
  80. }
  81.  
  82. bool operator!=(const StrangeDefaultInt& lhs, const StrangeDefaultInt& rhs) {
  83. return lhs.x != rhs.x;
  84. }
  85.  
  86. TEST(Correctness, StrangeDefaultConstructor) {
  87. std::vector<StrangeDefaultInt> lst = {1, 2, 3};
  88. auto func = [](StrangeDefaultInt cur, StrangeDefaultInt next) { return cur + next; };
  89. ASSERT_EQ(std::accumulate(lst.begin(), lst.end(), StrangeDefaultInt(1000000), func),
  90. Reduce(lst.begin(), lst.end(), StrangeDefaultInt(1000000), func));
  91.  
  92. std::vector<StrangeDefaultInt> lst2 = {1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
  93. ASSERT_EQ(std::accumulate(lst2.begin(), lst2.end(), StrangeDefaultInt(1000000), func),
  94. Reduce(lst2.begin(), lst2.end(), StrangeDefaultInt(1000000), func));
  95. }
  96.  
  97. class NoDefaultInt {
  98. public:
  99. NoDefaultInt(int x_) : x(x_) {}
  100.  
  101. NoDefaultInt& operator+=(const NoDefaultInt& rhs) {
  102. this->x += rhs.x;
  103. return *this;
  104. }
  105.  
  106. friend bool operator==(const NoDefaultInt& lhs, const NoDefaultInt& rhs);
  107.  
  108. friend bool operator!=(const NoDefaultInt& lhs, const NoDefaultInt& rhs);
  109.  
  110. private:
  111. int x;
  112. };
  113.  
  114. NoDefaultInt operator+(NoDefaultInt lhs, const NoDefaultInt& rhs) {
  115. lhs += rhs;
  116. return lhs;
  117. }
  118.  
  119. bool operator==(const NoDefaultInt& lhs, const NoDefaultInt& rhs) {
  120. return lhs.x == rhs.x;
  121. }
  122.  
  123. bool operator!=(const NoDefaultInt& lhs, const NoDefaultInt& rhs) {
  124. return lhs.x != rhs.x;
  125. }
  126.  
  127. TEST(Correctness, NoDefaultConstructor) {
  128. std::vector<NoDefaultInt> lst = {1, 2, 3};
  129. auto func = [](NoDefaultInt cur, NoDefaultInt next) { return cur + next; };
  130. ASSERT_EQ(std::accumulate(lst.begin(), lst.end(), NoDefaultInt(1000000), func),
  131. Reduce(lst.begin(), lst.end(), NoDefaultInt(1000000), func));
  132.  
  133. std::vector<NoDefaultInt> lst2 = {1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};
  134. ASSERT_EQ(std::accumulate(lst2.begin(), lst2.end(), NoDefaultInt(1000000), func),
  135. Reduce(lst2.begin(), lst2.end(), NoDefaultInt(1000000), func));
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement