~glmark2-dev/glmark2/canvas-drm-rebranch

« back to all changes in this revision

Viewing changes to src/libmatrix/test/util_split_test.cc

  • Committer: Alexandros Frantzis
  • Date: 2012-08-13 10:09:08 UTC
  • Revision ID: alexandros.frantzis@linaro.org-20120813100908-xgh0cf880rqh8q5c
libmatrix: Sync with lp:libmatrix revision 41.

Update Util::split() invocations in glmark2 to use the new interface.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Copyright (c) 2012 Linaro Limited
 
3
//
 
4
// All rights reserved. This program and the accompanying materials
 
5
// are made available under the terms of the MIT License which accompanies
 
6
// this distribution, and is available at
 
7
// http://www.opensource.org/licenses/mit-license.php
 
8
//
 
9
// Contributors:
 
10
//     Alexandros Frantzis - original implementation.
 
11
//
 
12
#include <iostream>
 
13
#include <string>
 
14
#include <vector>
 
15
#include "libmatrix_test.h"
 
16
#include "util_split_test.h"
 
17
#include "../util.h"
 
18
 
 
19
using std::cout;
 
20
using std::endl;
 
21
using std::string;
 
22
using std::vector;
 
23
 
 
24
template <typename T> static bool
 
25
areVectorsEqual(vector<T>& vec1, vector<T>& vec2)
 
26
{
 
27
    if (vec1.size() != vec2.size())
 
28
        return false;
 
29
 
 
30
    for (unsigned int i = 0; i < vec1.size(); i++)
 
31
    {
 
32
        if (vec1[i] != vec2[i])
 
33
            return false;
 
34
    }
 
35
 
 
36
    return true;
 
37
}
 
38
 
 
39
template <typename T> static void
 
40
printVector(vector<T>& vec)
 
41
{
 
42
    cout << "[";
 
43
    for (unsigned int i = 0; i < vec.size(); i++)
 
44
    {
 
45
        cout << '"' << vec[i] << '"';
 
46
        if (i < vec.size() - 1)
 
47
            cout << ", ";
 
48
    }
 
49
    cout << "]";
 
50
}
 
51
 
 
52
void
 
53
UtilSplitTestNormal::run(const Options& options)
 
54
{
 
55
    const string test1("abc def ghi");
 
56
    const string test2(" abc: def :ghi ");
 
57
    vector<string> expected1;
 
58
    vector<string> expected2;
 
59
    vector<string> results;
 
60
 
 
61
    expected1.push_back("abc");
 
62
    expected1.push_back("def");
 
63
    expected1.push_back("ghi");
 
64
 
 
65
    expected2.push_back(" abc");
 
66
    expected2.push_back(" def ");
 
67
    expected2.push_back("ghi ");
 
68
 
 
69
    if (options.beVerbose())
 
70
    {
 
71
        cout << "Testing string \"" << test1 << "\"" << endl;
 
72
    }
 
73
 
 
74
    Util::split(test1, ' ', results, Util::SplitModeNormal);
 
75
 
 
76
    if (options.beVerbose())
 
77
    {
 
78
        cout << "Split result: ";
 
79
        printVector(results);
 
80
        cout << endl << "Expected: ";
 
81
        printVector(expected1);
 
82
        cout << endl;
 
83
    }
 
84
 
 
85
    if (!areVectorsEqual(results, expected1))
 
86
    {
 
87
        return;
 
88
    }
 
89
 
 
90
    results.clear();
 
91
 
 
92
    if (options.beVerbose())
 
93
    {
 
94
        cout << "Testing string \"" << test2 << "\"" << endl;
 
95
    }
 
96
 
 
97
    Util::split(test2, ':', results, Util::SplitModeNormal);
 
98
 
 
99
    if (options.beVerbose())
 
100
    {
 
101
        cout << "Split result: ";
 
102
        printVector(results);
 
103
        cout << endl << "Expected: ";
 
104
        printVector(expected2);
 
105
        cout << endl;
 
106
    }
 
107
 
 
108
    if (!areVectorsEqual(results, expected2))
 
109
    {
 
110
        return;
 
111
    }
 
112
 
 
113
    pass_ = true;
 
114
}
 
115
 
 
116
void
 
117
UtilSplitTestQuoted::run(const Options& options)
 
118
{
 
119
    const string test1("abc \"def' ghi\" klm\\ nop -b qr:title='123 \"456'");
 
120
    const string test2("abc: def='1:2:3:'ghi : \":jk\"");
 
121
    vector<string> expected1;
 
122
    vector<string> expected2;
 
123
    vector<string> results;
 
124
 
 
125
    expected1.push_back("abc");
 
126
    expected1.push_back("def' ghi");
 
127
    expected1.push_back("klm nop");
 
128
    expected1.push_back("-b");
 
129
    expected1.push_back("qr:title=123 \"456");
 
130
 
 
131
    expected2.push_back("abc");
 
132
    expected2.push_back(" def=1:2:3:ghi ");
 
133
    expected2.push_back(" :jk");
 
134
 
 
135
    if (options.beVerbose())
 
136
    {
 
137
        cout << "Testing string \"" << test1 << "\"" << endl;
 
138
    }
 
139
 
 
140
    Util::split(test1, ' ', results, Util::SplitModeQuoted);
 
141
 
 
142
    if (options.beVerbose())
 
143
    {
 
144
        cout << "Split result: ";
 
145
        printVector(results);
 
146
        cout << endl << "Expected: ";
 
147
        printVector(expected1);
 
148
        cout << endl;
 
149
    }
 
150
 
 
151
    if (!areVectorsEqual(results, expected1))
 
152
    {
 
153
        return;
 
154
    }
 
155
 
 
156
    results.clear();
 
157
 
 
158
    if (options.beVerbose())
 
159
    {
 
160
        cout << "Testing string \"" << test2 << "\"" << endl;
 
161
    }
 
162
 
 
163
    Util::split(test2, ':', results, Util::SplitModeQuoted);
 
164
 
 
165
    if (options.beVerbose())
 
166
    {
 
167
        cout << "Split result: ";
 
168
        printVector(results);
 
169
        cout << endl << "Expected: ";
 
170
        printVector(expected2);
 
171
        cout << endl;
 
172
    }
 
173
 
 
174
    if (!areVectorsEqual(results, expected2))
 
175
    {
 
176
        return;
 
177
    }
 
178
 
 
179
    pass_ = true;
 
180
}