File: test_tokenizer.cc

package info (click to toggle)
eckit 1.32.4-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 305,876 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 42
file content (135 lines) | stat: -rw-r--r-- 3,724 bytes parent folder | download | duplicates (3)
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
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include "eckit/log/Log.h"
#include "eckit/runtime/Tool.h"
#include "eckit/types/Types.h"
#include "eckit/utils/Tokenizer.h"

#include "eckit/testing/Test.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

template <class Container>
void test_single() {
    std::string source(":lolo1:lolo2::lolo3");
    Container target;

    Tokenizer parse(":");

    parse(source, target);

    EXPECT(target.size() == 3);

    size_t c = 1;
    for (typename Container::const_iterator i = target.begin(); i != target.end(); ++i, ++c) {
        ostringstream s;
        s << "lolo" << c;
        //        std::cout << *i << " ??? " << s.str() << std::endl;
        EXPECT(*i == s.str());
    }
}

//----------------------------------------------------------------------------------------------------------------------

template <class Container>
void test_multi() {
    Container target;

    Tokenizer parse("-:;");

    std::string source1("-lolo0-lolo1-lolo2;lolo3:-lolo4-");
    parse(source1, target);

    EXPECT(target.size() == 5);

    size_t c = 0;
    for (typename Container::const_iterator i = target.begin(); i != target.end(); ++i, ++c) {
        ostringstream s;
        s << "lolo" << c;
        //        std::cout << *i << " ??? " << s.str() << std::endl;
        EXPECT(*i == s.str());
    }

    std::string source2("-lolo5-lolo6-lolo7;lolo8:lolo9-");
    parse(source2, target);

    EXPECT(target.size() == 10);

    c = 0;
    for (typename Container::const_iterator i = target.begin(); i != target.end(); ++i, ++c) {
        ostringstream s;
        s << "lolo" << c;
        //        std::cout << *i << " ??? " << s.str() << std::endl;
        EXPECT(*i == s.str());
    }
}

//----------------------------------------------------------------------------------------------------------------------

void test_keep_empty_list() {
    StringList target;

    Tokenizer parse(":", true);

    std::string source1(":4i:2100:::01:::::.");
    parse(source1, target);

    Log::info() << target << std::endl;

    EXPECT(target.size() == 11);
}

//----------------------------------------------------------------------------------------------------------------------

void test_keep_empty_set() {
    StringSet target;

    Tokenizer parse(":", true);

    std::string source1(":4i:2100:::01:::::.");
    parse(source1, target);

    Log::info() << target << std::endl;

    EXPECT(target.size() == 5);  // 5 uniqe elments
}

//----------------------------------------------------------------------------------------------------------------------

CASE("Test Tokenizer StringList") {

    test_single<StringList>();
    test_multi<StringList>();
    test_keep_empty_list();
}

CASE("Test Tokenizer StringSet") {

    test_single<StringSet>();
    test_multi<StringSet>();
    test_keep_empty_set();
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

//----------------------------------------------------------------------------------------------------------------------

int main(int argc, char** argv) {
    return run_tests(argc, argv);
}