File: test_getline.cpp

package info (click to toggle)
mapnik 3.0.12%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 17,084 kB
  • ctags: 18,454
  • sloc: cpp: 142,516; python: 1,416; sh: 769; makefile: 170; xml: 140; lisp: 13
file content (125 lines) | stat: -rw-r--r-- 3,328 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
#include "bench_framework.hpp"
#include "../plugins/input/csv/csv_getline.hpp"



class test : public benchmark::test_case
{
public:
    std::string line_data_;
    test(mapnik::parameters const& params)
     : test_case(params),
       line_data_("this is one line\nand this is a second line\nand a third line")
       {
          boost::optional<std::string> line_data = params.get<std::string>("line");
          if (line_data)
          {
              line_data_ = *line_data;
          }
       }

    bool validate() const
    {
        std::string first = line_data_.substr(line_data_.find_first_not_of('\n'));
        char newline = '\n';
        std::string csv_line;
        std::stringstream s;
        s << line_data_;
        std::getline(s,csv_line,newline);
        if (csv_line != first)
        {
            return true;
        }
        else
        {
            std::clog << "Error: the parsed line (" << csv_line << ") should be a subset of the original line (" << line_data_ << ") (ensure you pass a line with a \\n)\n";
        }
        return true;
    }
    bool operator()() const
    {
        char newline = '\n';
        std::string csv_line;
        std::stringstream s;
        s << line_data_;
        for (unsigned i=0;i<iterations_;++i)
        {
            std::getline(s,csv_line,newline);
        }
        return true;
    }
};


class test2 : public benchmark::test_case
{
public:
    std::string line_data_;
    test2(mapnik::parameters const& params)
     : test_case(params),
       line_data_("this is one line\nand this is a second line\nand a third line")
       {
          boost::optional<std::string> line_data = params.get<std::string>("line");
          if (line_data)
          {
              line_data_ = *line_data;
          }
       }

    bool validate() const
    {
        std::string first = line_data_.substr(line_data_.find_first_not_of('\n'));
        char newline = '\n';
        char quote = '"';
        std::string csv_line;
        std::stringstream s;
        s << line_data_;
        csv_utils::getline_csv(s,csv_line,newline,quote);
        if (csv_line != first)
        {
            return true;
        }
        else
        {
            std::clog << "Error: the parsed line (" << csv_line << ") should be a subset of the original line (" << line_data_ << ") (ensure you pass a line with a \\n)\n";
        }
        return true;
    }
    bool operator()() const
    {
        char newline = '\n';
        char quote = '"';
        std::string csv_line;
        std::stringstream s;
        s << line_data_;
        for (unsigned i=0;i<iterations_;++i)
        {
            csv_utils::getline_csv(s,csv_line,newline,quote);
        }
        return true;
    }
};

int main(int argc, char** argv)
{
    int return_value = 0;
    try
    {
        mapnik::parameters params;
        benchmark::handle_args(argc,argv,params);
        {
            test test_runner(params);
            return_value = return_value | run(test_runner,"std::getline");
        }
        {
            test2 test_runner2(params);
            return_value = return_value | run(test_runner2,"csv_utils::getline_csv");
        }
    }
    catch (std::exception const& ex)
    {
        std::clog << ex.what() << "\n";
        return -1;
    }
    return return_value;
}