piece_test.cc 8.0 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Y
Yi Wang 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
  Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

30
#include "paddle/string/piece.h"
Y
Yi Wang 已提交
31 32 33

#include <sstream>

Y
Yi Wang 已提交
34 35 36 37
#include "gtest/gtest.h"

TEST(StringPiece, Construct) {
  {
38
    paddle::string::Piece s;
Y
Yi Wang 已提交
39 40 41 42
    EXPECT_EQ(NULL, s.data());
    EXPECT_EQ(0U, s.len());
  }
  {
43 44 45 46
    EXPECT_THROW(paddle::string::Piece s(NULL, 10000U), std::invalid_argument);
  }
  {
    paddle::string::Piece s(NULL);
Y
Yi Wang 已提交
47 48 49 50 51
    EXPECT_EQ(0U, s.len());
  }
  {
    std::string a;
    EXPECT_EQ(0U, a.size());
52
    paddle::string::Piece s(a);
Y
Yi Wang 已提交
53 54 55 56 57
    EXPECT_EQ(0U, s.len());
  }
}

TEST(StringPiece, CopyAndAssign) {
58
  paddle::string::Piece empty;
Y
Yi Wang 已提交
59 60
  EXPECT_EQ(0U, empty.len());

61 62
  paddle::string::Piece a("hello");
  paddle::string::Piece b = a;
Y
Yi Wang 已提交
63 64 65 66
  EXPECT_EQ(b.len(), strlen("hello"));
  EXPECT_EQ(a, b);

  std::string storage("hello");
67
  paddle::string::Piece c(storage);
Y
Yi Wang 已提交
68 69 70
  EXPECT_EQ(a, c);
  EXPECT_NE(a.data(), c.data());
}
Y
Yi Wang 已提交
71

Y
Yi Wang 已提交
72
TEST(StringPiece, Compare) {
Y
Yi Wang 已提交
73
  {
74 75
    paddle::string::Piece a("hello");
    paddle::string::Piece b("world");
Y
Yi Wang 已提交
76 77 78 79 80 81
    EXPECT_TRUE(a != b);
    EXPECT_FALSE(a == b);
    EXPECT_TRUE(a < b);
    EXPECT_TRUE(a <= b);
    EXPECT_FALSE(a > b);
    EXPECT_FALSE(a >= b);
Y
Yi Wang 已提交
82 83
    EXPECT_LT(Compare(a, b), 0);
    EXPECT_GT(Compare(b, a), 0);
Y
Yi Wang 已提交
84 85
  }
  {
86
    paddle::string::Piece a, b;
Y
Yi Wang 已提交
87 88 89 90 91 92
    EXPECT_TRUE(a == b);
    EXPECT_FALSE(a != b);
    EXPECT_FALSE(a < b);
    EXPECT_FALSE(a > b);
    EXPECT_TRUE(a <= b);
    EXPECT_TRUE(a >= b);
Y
Yi Wang 已提交
93 94 95 96 97 98 99
    EXPECT_EQ(0, Compare(a, b));
    EXPECT_EQ(0, Compare(b, a));
  }
}

TEST(StringPiece, ToString) {
  {
100
    paddle::string::Piece s;
Y
Yi Wang 已提交
101 102 103
    EXPECT_EQ(std::string(""), s.ToString());
  }
  {
104
    paddle::string::Piece s(NULL);
Y
Yi Wang 已提交
105 106 107
    EXPECT_EQ(std::string(""), s.ToString());
  }
  {
108
    paddle::string::Piece s("hello");
Y
Yi Wang 已提交
109 110 111 112 113
    EXPECT_EQ(std::string("hello"), s.ToString());
  }
}

TEST(StringPiece, HasPrefixSuffix) {
114 115
  using paddle::string::HasPrefix;
  using paddle::string::HasSuffix;
Y
Yi Wang 已提交
116
  {
117
    paddle::string::Piece s;
Y
Yi Wang 已提交
118 119 120 121 122 123
    EXPECT_FALSE(HasPrefix(s, "something"));
    EXPECT_TRUE(HasPrefix(s, ""));
    EXPECT_FALSE(HasSuffix(s, "something"));
    EXPECT_TRUE(HasSuffix(s, ""));
  }
  {
124
    paddle::string::Piece s("app");
Y
Yi Wang 已提交
125 126 127 128 129 130 131 132 133
    EXPECT_TRUE(HasPrefix(s, ""));
    EXPECT_TRUE(HasPrefix(s, "a"));
    EXPECT_TRUE(HasPrefix(s, "ap"));
    EXPECT_TRUE(HasPrefix(s, "app"));

    EXPECT_TRUE(HasSuffix(s, ""));
    EXPECT_TRUE(HasSuffix(s, "p"));
    EXPECT_TRUE(HasSuffix(s, "pp"));
    EXPECT_TRUE(HasSuffix(s, "app"));
Y
Yi Wang 已提交
134 135
  }
}
Y
Yi Wang 已提交
136 137

TEST(StringPiece, SkipPrefixSuffix) {
138 139
  using paddle::string::SkipPrefix;
  using paddle::string::SkipSuffix;
Y
Yi Wang 已提交
140
  {
141
    paddle::string::Piece s;
Y
Yi Wang 已提交
142 143 144 145 146 147 148
    EXPECT_EQ("", SkipPrefix(s, 0));
    EXPECT_THROW(SkipPrefix(s, 1), std::invalid_argument);

    EXPECT_EQ("", SkipSuffix(s, 0));
    EXPECT_THROW(SkipSuffix(s, 1), std::invalid_argument);
  }
  {
149
    paddle::string::Piece s("app");
Y
Yi Wang 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    EXPECT_EQ("app", SkipPrefix(s, 0));
    EXPECT_EQ("pp", SkipPrefix(s, 1));
    EXPECT_EQ("p", SkipPrefix(s, 2));
    EXPECT_EQ("", SkipPrefix(s, 3));
    EXPECT_THROW(SkipPrefix(s, 4), std::invalid_argument);

    EXPECT_EQ("app", SkipSuffix(s, 0));
    EXPECT_EQ("ap", SkipSuffix(s, 1));
    EXPECT_EQ("a", SkipSuffix(s, 2));
    EXPECT_EQ("", SkipSuffix(s, 3));
    EXPECT_THROW(SkipSuffix(s, 4), std::invalid_argument);
  }
}

TEST(StringPiece, TrimPrefixSuffix) {
165 166
  using paddle::string::TrimPrefix;
  using paddle::string::TrimSuffix;
Y
Yi Wang 已提交
167
  {
168
    paddle::string::Piece s;
Y
Yi Wang 已提交
169 170 171 172 173 174 175
    EXPECT_EQ("", TrimPrefix(s, ""));
    EXPECT_EQ("", TrimPrefix(s, "something"));

    EXPECT_EQ("", TrimSuffix(s, ""));
    EXPECT_EQ("", TrimSuffix(s, "something"));
  }
  {
176
    paddle::string::Piece s("app");
Y
Yi Wang 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    EXPECT_EQ("app", TrimPrefix(s, ""));
    EXPECT_EQ("pp", TrimPrefix(s, "a"));
    EXPECT_EQ("p", TrimPrefix(s, "ap"));
    EXPECT_EQ("", TrimPrefix(s, "app"));
    EXPECT_EQ("app", TrimPrefix(s, "something"));

    EXPECT_EQ("app", TrimSuffix(s, ""));
    EXPECT_EQ("ap", TrimSuffix(s, "p"));
    EXPECT_EQ("a", TrimSuffix(s, "pp"));
    EXPECT_EQ("", TrimSuffix(s, "app"));
    EXPECT_EQ("app", TrimSuffix(s, "something"));
  }
}

TEST(StringPiece, Contains) {
192
  using paddle::string::Contains;
Y
Yi Wang 已提交
193
  {
194
    paddle::string::Piece s;
Y
Yi Wang 已提交
195 196 197 198
    EXPECT_FALSE(Contains(s, ""));
    EXPECT_FALSE(Contains(s, "something"));
  }
  {
199
    paddle::string::Piece s("app");
Y
Yi Wang 已提交
200 201 202 203 204 205 206 207 208 209 210
    EXPECT_TRUE(Contains(s, ""));
    EXPECT_TRUE(Contains(s, "a"));
    EXPECT_TRUE(Contains(s, "p"));
    EXPECT_TRUE(Contains(s, "ap"));
    EXPECT_TRUE(Contains(s, "pp"));
    EXPECT_TRUE(Contains(s, "app"));
    EXPECT_FALSE(Contains(s, "something"));
  }
}

TEST(StringPiece, Index) {
211 212
  using paddle::string::Index;
  auto npos = paddle::string::Piece::npos;
Y
Yi Wang 已提交
213
  {
214
    paddle::string::Piece s;
Y
Yi Wang 已提交
215 216 217 218
    EXPECT_EQ(npos, Index(s, ""));
    EXPECT_EQ(npos, Index(s, "something"));
  }
  {
219
    paddle::string::Piece s("app");
Y
Yi Wang 已提交
220 221 222 223 224 225 226 227 228 229 230
    EXPECT_EQ(0U, Index(s, ""));
    EXPECT_EQ(0U, Index(s, "a"));
    EXPECT_EQ(1U, Index(s, "p"));
    EXPECT_EQ(0U, Index(s, "ap"));
    EXPECT_EQ(1U, Index(s, "pp"));
    EXPECT_EQ(0U, Index(s, "app"));
    EXPECT_EQ(npos, Index(s, "something"));
  }
}

TEST(StringPiece, Find) {
231 232
  using paddle::string::Find;
  auto npos = paddle::string::Piece::npos;
Y
Yi Wang 已提交
233
  {
234
    paddle::string::Piece s;
Y
Yi Wang 已提交
235 236 237
    EXPECT_EQ(npos, Find(s, 'a', 0U));
  }
  {
238
    paddle::string::Piece s("app");
Y
Yi Wang 已提交
239 240 241 242 243 244 245 246 247
    EXPECT_EQ(0U, Find(s, 'a', 0U));
    EXPECT_EQ(1U, Find(s, 'p', 0U));
    EXPECT_EQ(1U, Find(s, 'p', 1U));
    EXPECT_EQ(2U, Find(s, 'p', 2U));
    EXPECT_EQ(npos, Find(s, 'z', 2U));
  }
}

TEST(StringPiece, RFind) {
248 249
  using paddle::string::RFind;
  auto npos = paddle::string::Piece::npos;
Y
Yi Wang 已提交
250
  {
251
    paddle::string::Piece s;
Y
Yi Wang 已提交
252 253 254
    EXPECT_EQ(npos, RFind(s, 'a', 0U));
  }
  {
255
    paddle::string::Piece s("app");
Y
Yi Wang 已提交
256 257 258 259 260 261 262 263 264
    EXPECT_EQ(2U, RFind(s, 'p', 2U));
    EXPECT_EQ(0U, RFind(s, 'a', 2U));
    EXPECT_EQ(1U, RFind(s, 'p', 1U));
    EXPECT_EQ(0U, RFind(s, 'a', 0));
    EXPECT_EQ(npos, RFind(s, 'z', 2U));
  }
}

TEST(StringPiece, SubStr) {
265
  using paddle::string::SubStr;
Y
Yi Wang 已提交
266
  {
267
    paddle::string::Piece s;
Y
Yi Wang 已提交
268 269 270 271 272
    EXPECT_EQ("", SubStr(s, 0, 0));
    EXPECT_EQ("", SubStr(s, 0, 1));
    EXPECT_EQ("", SubStr(s, 1, 0));
  }
  {
273
    paddle::string::Piece s("app");
Y
Yi Wang 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    EXPECT_EQ("", SubStr(s, 0, 0));
    EXPECT_EQ("", SubStr(s, 1, 0));
    EXPECT_EQ("", SubStr(s, 2, 0));
    EXPECT_EQ("", SubStr(s, 3, 0));

    EXPECT_EQ("a", SubStr(s, 0, 1));
    EXPECT_EQ("p", SubStr(s, 1, 1));
    EXPECT_EQ("p", SubStr(s, 2, 1));
    EXPECT_EQ("", SubStr(s, 3, 1));

    EXPECT_EQ("ap", SubStr(s, 0, 2));
    EXPECT_EQ("pp", SubStr(s, 1, 2));
    EXPECT_EQ("p", SubStr(s, 2, 2));
    EXPECT_EQ("", SubStr(s, 3, 2));

    EXPECT_EQ("app", SubStr(s, 0, 3));
    EXPECT_EQ("pp", SubStr(s, 1, 3));
    EXPECT_EQ("p", SubStr(s, 2, 3));
    EXPECT_EQ("", SubStr(s, 3, 3));
  }
}

TEST(StringPiece, StreamOutput) {
297
  using paddle::string::Piece;
Y
Yi Wang 已提交
298 299

  std::stringstream o;
300
  o << paddle::string::Piece();
Y
Yi Wang 已提交
301 302
  EXPECT_EQ("", o.str());

303
  o << paddle::string::Piece("hello");
Y
Yi Wang 已提交
304 305
  EXPECT_EQ("hello", o.str());

306
  o << paddle::string::Piece();
Y
Yi Wang 已提交
307 308
  EXPECT_EQ("hello", o.str());
}