Commit c94cd9a7 authored by Marco Bubke's avatar Marco Bubke

Add ranges example

parent b177e1a1
......@@ -7,3 +7,6 @@
[submodule "ranges"]
path = ranges
url = https://github.com/ericniebler/range-v3.git
[submodule "range"]
path = range
url = https://github.com/ericniebler/range-v3.git
Subproject commit aad6a5fa767529d3353bd3beb89e126c7b0868ca
Subproject commit c9f2693ea97e94c8afcefb57d3074c6a6236ca23
......@@ -5,4 +5,5 @@ SUBDIRS = \
sorting \
exceptions \
fold-expression \
set_functions
set_functions \
ranges
Subproject commit 4f4beb45c5e56aca4233e4d4c760208e21fff2ec
#include <googletest.h>
#include <range/v3/all.hpp>
#include <QStringList>
using namespace ranges;
bool is_prime(int n)
{
int i,count=0;
if(n==1)
return false;
if(n==2)
return true;
if(n%2==0)
return false;
for(i=1;i<=n;i++)
if(n%i==0) count++;
if(count==2) // Why ?
return true;
else
return false;
}
bool is_short(const QString &string)
{
return string.size() <= 1;
}
TEST(Ranges, Reverse)
{
std::vector<int> input = {1, 2, 3};
std::vector<int> output;
for ( auto value : input | view::reverse)
output.push_back(value);
ASSERT_THAT(output, ElementsAre(3, 2, 1));
}
TEST(Ranges, ReverseIota)
{
std::vector<int> output;
for (auto value : view::iota(1, 4) | view::reverse)
output.push_back(value);
ASSERT_THAT(output, ElementsAre(3, 2, 1));
}
TEST(Ranges, Filter)
{
std::vector<int> output;
for (auto value : view::iota(1, 10) | view::filter([](auto x) { return x % 2 == 1; }))
output.push_back(value);
ASSERT_THAT(output, ElementsAre(1, 3, 5, 7, 9));
}
TEST(Ranges, Take)
{
std::vector<int> output;
for (auto value : view::iota(1) | view::take(3))
output.push_back(value);
ASSERT_THAT(output, ElementsAre(1, 2, 3));
}
TEST(Ranges, FilterTake)
{
std::vector<int> output;
for (auto value : view::iota(1) | view::filter(is_prime) | view::take(10))
output.push_back(value);
ASSERT_THAT(output, ElementsAre(2, 3, 5, 7, 11, 13, 17, 19, 23, 29));
}
TEST(Ranges, Split)
{
std::string text = "quick brown fox";
std::vector<std::string> output;
for (auto value : text | view::split(' '))
output.push_back(value);
ASSERT_THAT(output, ElementsAre("quick", "brown", "fox"));
}
TEST(Ranges, Join)
{
std::vector<std::string> text = {"quick", "brown", "fox"};
std::string output = text | view::join(' ');
ASSERT_THAT(output, StrEq("quick brown fox"));
}
TEST(Ranges, Enumerate)
{
std::vector<std::string> text = {"quick", "brown", "fox"};
std::vector<std::pair<int, std::string>> output;
for (auto && [index, value] : text | view::enumerate)
output.emplace_back(index + 1, value);
ASSERT_THAT(output, ElementsAre(Pair(1, "quick"), Pair(2, "brown"), Pair(3, "fox")));
}
TEST(Ranges, Drop)
{
auto input = {1, 2, 3, 4, 5, 6, 7};
std::vector<int> output = input | view::drop(3);
ASSERT_THAT(output, ElementsAre(4, 5, 6, 7));
}
TEST(Ranges, Zip)
{
std::vector<std::string> text = {"quick", "brown", "fox"};
std::vector<std::string> text2 = {"slow", "black", "cat", "walking"};
std::vector<std::pair<std::string, std::string>> output = view::zip(text, text2);
ASSERT_THAT(output,
ElementsAre(Pair("quick", "slow"), Pair("brown", "black"), Pair("fox", "cat")));
}
TEST(Ranges, Unique)
{
std::vector values = {1, 2, 3, 3, 4, 4, 5, 6, 7};
values |= action::unique;
ASSERT_THAT(values, ElementsAre(1, 2, 3, 4, 5, 6, 7));
}
TEST(Ranges, SortUnique)
{
std::vector values = {7, 2, 3, 4, 3, 4, 5, 4, 1, 6};
values |= action::sort | action::unique;
ASSERT_THAT(values, ElementsAre(1, 2, 3, 4, 5, 6, 7));
}
TEST(Ranges, Keys)
{
std::map<std::string, int> map = {{"Yi", 1}, {"Er", 2}, {"San", 3}};
std::vector<std::string> output = map | view::keys;
ASSERT_THAT(output, ElementsAre("Er", "San", "Yi"));
}
TEST(Ranges, Values)
{
std::map<std::string, int> map = {
{"Yi", 1}, {"Er", 2}, {"San", 3}, {"One", 1}, {"Two", 2}, {"Three", 3}};
std::vector<int> values = map | view::values;
values |= action::sort | action::unique;
ASSERT_THAT(values, ElementsAre(1, 2, 3));
}
TEST(Ranges, SetUnion)
{
std::vector<int> values = {1, 2, 4, 5, 7};
std::vector<int> values2 = {7, 2, 3, 4, 3, 4, 4, 1, 6};
values2 |= action::sort | action::unique;
std::vector<int> output = view::set_union(values, values2);
ASSERT_THAT(output, ElementsAre(1, 2, 3, 4, 5, 6, 7));
}
TEST(Ranges, SetIntersetion)
{
std::vector<int> values = {1, 2, 4, 5, 7};
std::vector<int> values2 = {1, 2, 3, 4, 6, 7};
std::vector<int> output = view::set_intersection(values, values2);
ASSERT_THAT(output, ElementsAre(1, 2, 4, 7));
}
TEST(Ranges, SetDifference)
{
std::vector<int> values = {1, 2, 4, 5, 7};
std::vector<int> values2 = {2, 3, 4, 6, 7};
std::vector<int> output = view::set_difference(values, values2);
ASSERT_THAT(output, ElementsAre(1, 5));
}
TEST(Ranges, Transform)
{
QStringList values = {"1", "2", "6", "4", "5", "7", "42"};
auto to_int = [](auto &&s) { return s.toInt(); };
std::vector<int> output = values | view::filter(is_short) | view::transform(to_int)
| view::remove_if(is_prime);
ASSERT_THAT(output, ElementsAre(1, 6, 4));
}
include(../shared/test.pri)
INCLUDEPATH += ../range/include
TARGET = ranges
SOURCES += \
ranges-test.cpp
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include <gmock/gmock.h>
using testing::_;
using testing::A;
using testing::AllOf;
using testing::An;
using testing::AnyNumber;
using testing::AnyOf;
using testing::Assign;
using testing::ByMove;
using testing::ByRef;
using testing::Contains;
using testing::ContainerEq;
using testing::ElementsAre;
using testing::Field;
using testing::HasSubstr;
using testing::InSequence;
using testing::Invoke;
using testing::IsEmpty;
using testing::IsNull;
using testing::Matcher;
using testing::Mock;
using testing::MockFunction;
using testing::NiceMock;
using testing::NotNull;
using testing::Not;
using testing::Pair;
using testing::PrintToString;
using testing::Property;
using testing::Return;
using testing::ReturnRef;
using testing::SafeMatcherCast;
using testing::Sequence;
using testing::SizeIs;
using testing::StrEq;
using testing::Throw;
using testing::TypedEq;
using testing::UnorderedElementsAre;
using testing::Eq;
using testing::Ge;
using testing::Gt;
using testing::Le;
using testing::Lt;
using testing::Ne;
......@@ -29,3 +29,5 @@
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include "google-using-declarations.h"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment