Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <gtest/gtest.hpp>
- #include <reduce.h>
- #include <vector>
- #include <algorithm>
- #include "commons.h"
- TEST(Correctness, Empty) {
- std::vector<int> values{1, 2, 3};
- ASSERT_EQ(6,
- Reduce(values.begin(), values.end(), 0, [](int sum, int cur) { return sum + cur; }));
- ASSERT_EQ(6,
- Reduce(values.begin(), values.end(), 1, [](int sum, int cur) { return sum * cur; }));
- ASSERT_EQ(0,
- Reduce(values.begin(), values.end(), 0, [](int prod, int cur) { return prod * cur; }));
- ASSERT_EQ(30,
- Reduce(values.begin(), values.end(), 5, [](int prod, int cur) { return prod * cur; }));
- std::vector<int> empty;
- ASSERT_EQ(0, Reduce(empty.begin(), empty.end(), 0, Summator<int>()));
- ASSERT_EQ(5,
- Reduce(empty.begin(), empty.end(), 5, [](int prod, int cur) { return prod * cur; }));
- std::vector<int> one{1};
- ASSERT_EQ(1, Reduce(one.begin(), one.end(), 0, Summator<int>()));
- ASSERT_EQ(5,
- Reduce(one.begin(), one.end(), 5, [](int prod, int cur) { return prod * cur; }));
- std::vector<int> two{1, 2};
- ASSERT_EQ(3, Reduce(two.begin(), two.end(), 0, Summator<int>()));
- ASSERT_EQ(10,
- Reduce(two.begin(), two.end(), 5, [](int prod, int cur) { return prod * cur; }));
- }
- TEST(Correctness, SimpleTest) {
- std::vector<uint32_t> lst(GenTest(1027));
- auto func = [](uint32_t cur, uint32_t next) { return cur + next; };
- ASSERT_EQ(std::accumulate(lst.begin(), lst.end(), 1, func),
- Reduce(lst.begin(), lst.end(), 1, func));
- }
- TEST(Correctness, Multiplication) {
- std::vector<uint64_t> lst(GenTestSmallNumbers(31));
- auto func = [](uint64_t cur, uint64_t next) { return cur * next; };
- ASSERT_EQ(std::accumulate(lst.begin(), lst.end(), 7, func),
- Reduce(lst.begin(), lst.end(), 7, func));
- }
- class StrangeDefaultInt {
- public:
- StrangeDefaultInt() : x(3) {}
- StrangeDefaultInt(int x_) : x(x_) {}
- StrangeDefaultInt& operator+=(const StrangeDefaultInt& rhs) {
- this->x += rhs.x;
- return *this;
- }
- friend bool operator==(const StrangeDefaultInt& lhs, const StrangeDefaultInt& rhs);
- friend bool operator!=(const StrangeDefaultInt& lhs, const StrangeDefaultInt& rhs);
- private:
- int x;
- };
- StrangeDefaultInt operator+(StrangeDefaultInt lhs, const StrangeDefaultInt& rhs) {
- lhs += rhs;
- return lhs;
- }
- bool operator==(const StrangeDefaultInt& lhs, const StrangeDefaultInt& rhs) {
- return lhs.x == rhs.x;
- }
- bool operator!=(const StrangeDefaultInt& lhs, const StrangeDefaultInt& rhs) {
- return lhs.x != rhs.x;
- }
- TEST(Correctness, StrangeDefaultConstructor) {
- std::vector<StrangeDefaultInt> lst = {1, 2, 3};
- auto func = [](StrangeDefaultInt cur, StrangeDefaultInt next) { return cur + next; };
- ASSERT_EQ(std::accumulate(lst.begin(), lst.end(), StrangeDefaultInt(1000000), func),
- Reduce(lst.begin(), lst.end(), StrangeDefaultInt(1000000), func));
- 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};
- ASSERT_EQ(std::accumulate(lst2.begin(), lst2.end(), StrangeDefaultInt(1000000), func),
- Reduce(lst2.begin(), lst2.end(), StrangeDefaultInt(1000000), func));
- }
- class NoDefaultInt {
- public:
- NoDefaultInt(int x_) : x(x_) {}
- NoDefaultInt& operator+=(const NoDefaultInt& rhs) {
- this->x += rhs.x;
- return *this;
- }
- friend bool operator==(const NoDefaultInt& lhs, const NoDefaultInt& rhs);
- friend bool operator!=(const NoDefaultInt& lhs, const NoDefaultInt& rhs);
- private:
- int x;
- };
- NoDefaultInt operator+(NoDefaultInt lhs, const NoDefaultInt& rhs) {
- lhs += rhs;
- return lhs;
- }
- bool operator==(const NoDefaultInt& lhs, const NoDefaultInt& rhs) {
- return lhs.x == rhs.x;
- }
- bool operator!=(const NoDefaultInt& lhs, const NoDefaultInt& rhs) {
- return lhs.x != rhs.x;
- }
- TEST(Correctness, NoDefaultConstructor) {
- std::vector<NoDefaultInt> lst = {1, 2, 3};
- auto func = [](NoDefaultInt cur, NoDefaultInt next) { return cur + next; };
- ASSERT_EQ(std::accumulate(lst.begin(), lst.end(), NoDefaultInt(1000000), func),
- Reduce(lst.begin(), lst.end(), NoDefaultInt(1000000), func));
- 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};
- ASSERT_EQ(std::accumulate(lst2.begin(), lst2.end(), NoDefaultInt(1000000), func),
- Reduce(lst2.begin(), lst2.end(), NoDefaultInt(1000000), func));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement