File: split.cpp

package info (click to toggle)
range-v3 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,652 kB
  • sloc: cpp: 76,839; xml: 226; sh: 89; python: 34; makefile: 19; perl: 15
file content (139 lines) | stat: -rw-r--r-- 4,305 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Range v3 library
//
//  Copyright Eric Niebler 2014-present
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#include <cctype>
#include <string>
#include <vector>

#include <range/v3/action/split.hpp>
#include <range/v3/action/split_when.hpp>
#include <range/v3/algorithm/move.hpp>
#include <range/v3/core.hpp>
#include <range/v3/view/c_str.hpp>
#include <range/v3/view/iota.hpp>

#include "../simple_test.hpp"
#include "../test_utils.hpp"

int main()
{
    using namespace ranges;

    {
        auto v = views::ints(1, 21) | to<std::vector>();
        std::vector<std::vector<int>> rgv = actions::split(v, 10);
        CHECK(rgv.size() == 2u);
        ::check_equal(rgv[0], {1,2,3,4,5,6,7,8,9});
        ::check_equal(rgv[1], {11,12,13,14,15,16,17,18,19,20});

        using I = std::vector<int>::iterator;
        std::vector<std::vector<int>> rgv2 = actions::split_when(
            v, [](I b, I) { return std::make_pair(0 == (*b) % 2, next(b)); });
        CHECK(rgv2.size() == 10u);
        ::check_equal(rgv2[0], {1});
        ::check_equal(rgv2[1], {3});
        ::check_equal(rgv2[2], {5});
        ::check_equal(rgv2[3], {7});
        ::check_equal(rgv2[4], {9});
        ::check_equal(rgv2[5], {11});
        ::check_equal(rgv2[6], {13});
        ::check_equal(rgv2[7], {15});
        ::check_equal(rgv2[8], {17});
        ::check_equal(rgv2[9], {19});
    }

    {
        std::string s{"This is his face"};
        std::vector<std::string> rgs = actions::split(s, views::c_str(" "));
        CHECK(rgs.size() == 4u);
        CHECK(rgs[0] == "This");
        CHECK(rgs[1] == "is");
        CHECK(rgs[2] == "his");
        CHECK(rgs[3] == "face");
    }

    {
        std::string s{"This is his face"};
        std::vector<std::string> rgs = std::move(s) | actions::split(views::c_str(" "));
        CHECK(rgs.size() == 4u);
        CHECK(rgs[0] == "This");
        CHECK(rgs[1] == "is");
        CHECK(rgs[2] == "his");
        CHECK(rgs[3] == "face");
    }

    {
        std::string s{"This is his face"};
        char ch[] =  {' '};
        std::vector<std::string> rgs = actions::split(s, ch);
        CHECK(rgs.size() == 4u);
        CHECK(rgs[0] == "This");
        CHECK(rgs[1] == "is");
        CHECK(rgs[2] == "his");
        CHECK(rgs[3] == "face");
    }

    {
        std::string s{"This is his face"};
        char ch[] =  {' '};
        std::vector<std::string> rgs = std::move(s) | actions::split(ch);
        CHECK(rgs.size() == 4u);
        CHECK(rgs[0] == "This");
        CHECK(rgs[1] == "is");
        CHECK(rgs[2] == "his");
        CHECK(rgs[3] == "face");
    }

    {
        auto rgi = views::ints(1,21);
        std::vector<std::vector<int>> rgv3 = actions::split(rgi, 10);
        CHECK(rgv3.size() == 2u);
        ::check_equal(rgv3[0], {1,2,3,4,5,6,7,8,9});
        ::check_equal(rgv3[1], {11,12,13,14,15,16,17,18,19,20});
    }

    {
        auto rgi = views::ints(1,21);
        std::vector<std::vector<int>> rgv3 = std::move(rgi) | actions::split(10);
        CHECK(rgv3.size() == 2u);
        ::check_equal(rgv3[0], {1,2,3,4,5,6,7,8,9});
        ::check_equal(rgv3[1], {11,12,13,14,15,16,17,18,19,20});
    }

    {
        std::string str("now  is \t the\ttime");
        auto toks = actions::split_when(str, +[](int i) { return std::isspace(i); });
        static_assert(std::is_same<decltype(toks), std::vector<std::string>>::value, "");
        CHECK(toks.size() == 4u);
        if(toks.size() == 4u)
        {
            CHECK(toks[0] == "now");
            CHECK(toks[1] == "is");
            CHECK(toks[2] == "the");
            CHECK(toks[3] == "time");
        }
    }

    {
        std::string str("now  is \t the\ttime");
        auto toks =
            std::move(str) | actions::split_when(+[](int i) { return std::isspace(i); });
        static_assert(std::is_same<decltype(toks), std::vector<std::string>>::value, "");
        CHECK(toks.size() == 4u);
        if(toks.size() == 4u)
        {
            CHECK(toks[0] == "now");
            CHECK(toks[1] == "is");
            CHECK(toks[2] == "the");
            CHECK(toks[3] == "time");
        }
    }

    return ::test_result();
}