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

Side by Side Diff: core/src/fxcrt/fx_basic_wstring_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, WideStringNotNull) {
10 // Prove constructor uses sizeof(), not strlen() against literals.
11 CFX_WideString literal_string(L"ab\0c");
12 EXPECT_EQ(4, literal_string.GetLength());
13
14 // Prove constructor uses strlen(), not sizeof() against mutable arrays.
15 wchar_t array[32];
16 memcpy(array, L"ab\0c", 5 * sizeof(wchar_t));
17 CFX_WideString array_string(array);
18 EXPECT_EQ(2, array_string.GetLength());
19
20 // Prove constructor uses strlen(), not sizeof() against pointers.
21 const wchar_t* ptr = L"ab\0c";
22 CFX_WideString pointer_string(ptr);
23 EXPECT_EQ(2, pointer_string.GetLength());
24 }
25
9 TEST(fxcrt, WideStringOperatorSubscript) { 26 TEST(fxcrt, WideStringOperatorSubscript) {
10 // CFX_WideString includes the NUL terminator for non-empty strings. 27 // CFX_WideString includes the NUL terminator for non-empty strings.
11 CFX_WideString abc(L"abc"); 28 CFX_WideString abc(L"abc");
12 EXPECT_EQ(L'a', abc[0]); 29 EXPECT_EQ(L'a', abc[0]);
13 EXPECT_EQ(L'b', abc[1]); 30 EXPECT_EQ(L'b', abc[1]);
14 EXPECT_EQ(L'c', abc[2]); 31 EXPECT_EQ(L'c', abc[2]);
15 EXPECT_EQ(L'\0', abc[3]); 32 EXPECT_EQ(L'\0', abc[3]);
16 } 33 }
17 34
18 TEST(fxcrt, WideStringOperatorLT) { 35 TEST(fxcrt, WideStringOperatorLT) {
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 { L"\x3132\x6162", ByteStringLiteral("\x32\x31\x62\x61\0\0") }, 180 { L"\x3132\x6162", ByteStringLiteral("\x32\x31\x62\x61\0\0") },
164 }; 181 };
165 182
166 for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) { 183 for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) {
167 EXPECT_EQ(utf16le_encode_cases[i].bs, 184 EXPECT_EQ(utf16le_encode_cases[i].bs,
168 utf16le_encode_cases[i].ws.UTF16LE_Encode()) 185 utf16le_encode_cases[i].ws.UTF16LE_Encode())
169 << " for case number " << i; 186 << " for case number " << i;
170 } 187 }
171 } 188 }
172 189
190 TEST(fxcrt, WideStringCNotNull) {
191 // Prove constructor uses sizeof(), not strlen() against literals.
192 CFX_WideStringC literal_string(L"ab\0c");
193 EXPECT_EQ(4, literal_string.GetLength());
194
195 // Prove constructor uses strlen(), not sizeof() against mutable arrays.
196 wchar_t array[32];
197 memcpy(array, L"ab\0c", 5 * sizeof(wchar_t));
198 CFX_WideStringC array_string(array);
199 EXPECT_EQ(2, array_string.GetLength());
200
201 // Prove constructor uses strlen(), not sizeof() against pointers.
202 const wchar_t* ptr = L"ab\0c";
203 CFX_WideStringC pointer_string(ptr);
204 EXPECT_EQ(2, pointer_string.GetLength());
205 }
206
173 TEST(fxcrt, WideStringCOperatorSubscript) { 207 TEST(fxcrt, WideStringCOperatorSubscript) {
174 // CFX_WideStringC includes the NUL terminator for non-empty strings. 208 // CFX_WideStringC includes the NUL terminator for non-empty strings.
175 CFX_WideStringC abc(L"abc"); 209 CFX_WideStringC abc(L"abc");
176 EXPECT_EQ(L'a', abc[0]); 210 EXPECT_EQ(L'a', abc[0]);
177 EXPECT_EQ(L'b', abc[1]); 211 EXPECT_EQ(L'b', abc[1]);
178 EXPECT_EQ(L'c', abc[2]); 212 EXPECT_EQ(L'c', abc[2]);
179 EXPECT_EQ(L'\0', abc[3]); 213 EXPECT_EQ(L'\0', abc[3]);
180 } 214 }
181 215
182 TEST(fxcrt, WideStringCOperatorLT) { 216 TEST(fxcrt, WideStringCOperatorLT) {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 const wchar_t* c_string2 = L"hellp"; 339 const wchar_t* c_string2 = L"hellp";
306 const wchar_t* c_string3 = L"hellod"; 340 const wchar_t* c_string3 = L"hellod";
307 ASSERT_TRUE(wide_string_c != c_string1); 341 ASSERT_TRUE(wide_string_c != c_string1);
308 ASSERT_TRUE(wide_string_c != c_string2); 342 ASSERT_TRUE(wide_string_c != c_string2);
309 ASSERT_TRUE(wide_string_c != c_string3); 343 ASSERT_TRUE(wide_string_c != c_string3);
310 344
311 ASSERT_TRUE(c_string1 != wide_string_c); 345 ASSERT_TRUE(c_string1 != wide_string_c);
312 ASSERT_TRUE(c_string2 != wide_string_c); 346 ASSERT_TRUE(c_string2 != wide_string_c);
313 ASSERT_TRUE(c_string3 != wide_string_c); 347 ASSERT_TRUE(c_string3 != wide_string_c);
314 } 348 }
OLDNEW
« core/include/fxcrt/fx_string.h ('K') | « core/src/fxcrt/fx_basic_bstring_unittest.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698