gt22 icon

Untitled

gt22 | PRO | 05/07/20 09:51:23 PM UTC | 0 ⭐ | 1991 👁️ | Never ⏰ | []
C++ |

1.03 KB

|

None

|

0 👍

/

0 👎

#include "linq.hpp"
#include "doctest.h"
#include <vector>
#include <set>
#include <iterator>
 
using linq::from;
 
TEST_CASE("from_to_vector set") {
 
    std::set<bool> v = {true, false, true};
    std::vector<bool> u = from(v.begin(), v.end()).to_vector();
    bool valid = u == std::vector<bool>{false, true} || u == std::vector<bool>{false, true};
    CHECK(valid);
}
 
TEST_CASE("from_copy_to vector->set") {
    std::vector<int> v = {1, 2, 2, 3, 3, 4, 5, 6};
    std::set<int> s;
    from(v.begin(), v.end()).copy_to(std::inserter(s, s.begin()));
    CHECK(s == std::set<int>{1, 2, 3, 4, 5, 6});
}
 
TEST_CASE("complex computation") {
    std::vector<int> v = {1, 2, 2, 3, 3, 4, 5, 6};
    std::vector<int> u = from(v.begin(), v.end())
            .select([](int x){ return x + 2; })
            .where([](int x){ return x % 2 == 0; })
            .where_neq(4)
            .until([](int x){ return x >= 7; })
            .select([](int x){ return x / 2; })
            .to_vector();
    CHECK(u == std::vector<int>{3});
}

Comments