Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(425)

Side by Side Diff: core/src/fxcrt/fx_basic_bstring_unittest.cpp

Issue 1114313006: Make constructors recognize char[n] vs. char* and avoid strlen calls. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Delegated constructors everywhere. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "testing/gtest/include/gtest/gtest.h" 5 #include "testing/gtest/include/gtest/gtest.h"
6 #include "../../../testing/fx_string_testhelpers.h" 6 #include "../../../testing/fx_string_testhelpers.h"
7 #include "../../include/fxcrt/fx_basic.h" 7 #include "../../include/fxcrt/fx_basic.h"
8 8
9 TEST(fxcrt, ByteStringNotNull) {
10 CFX_ByteString string3("abc");
11 CFX_ByteString string6("abcdef");
12 CFX_ByteString alternate_string3("abcdef", 3);
13 CFX_ByteStringC illegal_string7("abcdef", 7);
14 EXPECT_EQ(3, string3.GetLength());
15 EXPECT_EQ(6, string6.GetLength());
16 EXPECT_EQ(3, alternate_string3.GetLength());
17 EXPECT_EQ(7, illegal_string7.GetLength());
18
19 // Prove constructor uses sizeof(), not strlen() against literals.
20 CFX_ByteString literal_string("abc\0def");
21 EXPECT_EQ(7, literal_string.GetLength());
22
23 // Prove constructor uses strlen(), not sizeof() against mutable arrays.
24 char array[32];
25 memcpy(array, "abc\0def", 7);
26 CFX_ByteString array_string(array);
27 EXPECT_EQ(3, array_string.GetLength());
28
29 // Prove constructor uses strlen(), not sizeof() against pointers.
30 const char* ptr = "abc\0def";
31 CFX_ByteString pointer_string(ptr);
32 EXPECT_EQ(3, pointer_string.GetLength());
33 }
34
9 TEST(fxcrt, ByteStringOperatorSubscript) { 35 TEST(fxcrt, ByteStringOperatorSubscript) {
10 // CFX_ByteString includes the NUL terminator for non-empty strings. 36 // CFX_ByteString includes the NUL terminator for non-empty strings.
11 CFX_ByteString abc("abc"); 37 CFX_ByteString abc("abc");
12 EXPECT_EQ('a', abc[0]); 38 EXPECT_EQ('a', abc[0]);
13 EXPECT_EQ('b', abc[1]); 39 EXPECT_EQ('b', abc[1]);
14 EXPECT_EQ('c', abc[2]); 40 EXPECT_EQ('c', abc[2]);
15 EXPECT_EQ(0, abc[3]); 41 EXPECT_EQ(0, abc[3]);
16 } 42 }
17 43
18 TEST(fxcrt, ByteStringOperatorLT) { 44 TEST(fxcrt, ByteStringOperatorLT) {
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 CFX_ByteStringC non_null_string("a"); 211 CFX_ByteStringC non_null_string("a");
186 EXPECT_NE(null_string, non_null_string); 212 EXPECT_NE(null_string, non_null_string);
187 } 213 }
188 214
189 TEST(fxcrt, ByteStringCNotNull) { 215 TEST(fxcrt, ByteStringCNotNull) {
190 CFX_ByteStringC string3("abc"); 216 CFX_ByteStringC string3("abc");
191 CFX_ByteStringC string6("abcdef"); 217 CFX_ByteStringC string6("abcdef");
192 CFX_ByteStringC alternate_string3("abcdef", 3); 218 CFX_ByteStringC alternate_string3("abcdef", 3);
193 CFX_ByteStringC embedded_nul_string7("abc\0def", 7); 219 CFX_ByteStringC embedded_nul_string7("abc\0def", 7);
194 CFX_ByteStringC illegal_string7("abcdef", 7); 220 CFX_ByteStringC illegal_string7("abcdef", 7);
195
196 EXPECT_EQ(3, string3.GetLength()); 221 EXPECT_EQ(3, string3.GetLength());
197 EXPECT_EQ(6, string6.GetLength()); 222 EXPECT_EQ(6, string6.GetLength());
198 EXPECT_EQ(3, alternate_string3.GetLength()); 223 EXPECT_EQ(3, alternate_string3.GetLength());
199 EXPECT_EQ(7, embedded_nul_string7.GetLength()); 224 EXPECT_EQ(7, embedded_nul_string7.GetLength());
200 EXPECT_EQ(7, illegal_string7.GetLength()); 225 EXPECT_EQ(7, illegal_string7.GetLength());
201 226
227 // Prove constructor uses sizeof(), not strlen() against literals.
228 CFX_ByteStringC literal_string("abc\0def");
229 EXPECT_EQ(7, literal_string.GetLength());
230
231 // Prove constructor uses strlen(), not sizeof() against mutable arrays.
232 char array[32];
233 memcpy(array, "abc\0def", 7);
234 CFX_ByteStringC array_string(array);
235 EXPECT_EQ(3, array_string.GetLength());
236
237 // Prove constructor uses strlen(), not sizeof() against pointers.
238 const char* ptr = "abc\0def";
239 CFX_ByteStringC pointer_string(ptr);
240 EXPECT_EQ(3, pointer_string.GetLength());
241
202 EXPECT_NE(string3, string6); 242 EXPECT_NE(string3, string6);
203 EXPECT_EQ(string3, alternate_string3); 243 EXPECT_EQ(string3, alternate_string3);
204 EXPECT_NE(string3, embedded_nul_string7); 244 EXPECT_NE(string3, embedded_nul_string7);
205 EXPECT_NE(string3, illegal_string7); 245 EXPECT_NE(string3, illegal_string7);
206 EXPECT_NE(string6, alternate_string3); 246 EXPECT_NE(string6, alternate_string3);
207 EXPECT_NE(string6, embedded_nul_string7); 247 EXPECT_NE(string6, embedded_nul_string7);
208 EXPECT_NE(string6, illegal_string7); 248 EXPECT_NE(string6, illegal_string7);
209 EXPECT_NE(alternate_string3, embedded_nul_string7); 249 EXPECT_NE(alternate_string3, embedded_nul_string7);
210 EXPECT_NE(alternate_string3, illegal_string7); 250 EXPECT_NE(alternate_string3, illegal_string7);
211 EXPECT_NE(embedded_nul_string7, illegal_string7); 251 EXPECT_NE(embedded_nul_string7, illegal_string7);
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 const char* c_string2 = "hellp"; 507 const char* c_string2 = "hellp";
468 const char* c_string3 = "hellod"; 508 const char* c_string3 = "hellod";
469 ASSERT_TRUE(byte_string_c != c_string1); 509 ASSERT_TRUE(byte_string_c != c_string1);
470 ASSERT_TRUE(byte_string_c != c_string2); 510 ASSERT_TRUE(byte_string_c != c_string2);
471 ASSERT_TRUE(byte_string_c != c_string3); 511 ASSERT_TRUE(byte_string_c != c_string3);
472 512
473 ASSERT_TRUE(c_string1 != byte_string_c); 513 ASSERT_TRUE(c_string1 != byte_string_c);
474 ASSERT_TRUE(c_string2 != byte_string_c); 514 ASSERT_TRUE(c_string2 != byte_string_c);
475 ASSERT_TRUE(c_string3 != byte_string_c); 515 ASSERT_TRUE(c_string3 != byte_string_c);
476 } 516 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698