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

Side by Side Diff: third_party/protobuf/objectivec/Tests/GPBStringTests.m

Issue 1322483002: Revert https://codereview.chromium.org/1291903002 (protobuf roll). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 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
(Empty)
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 #import <XCTest/XCTest.h>
32
33 #import "GPBCodedInputStream_PackagePrivate.h"
34 #import "GPBTestUtilities.h"
35
36 @interface TestClass : NSObject
37 @property(nonatomic, retain) NSString *foo;
38 @end
39
40 @implementation TestClass
41 @synthesize foo;
42 @end
43
44 @interface GPBStringTests : XCTestCase {
45 NSMutableArray *nsStrings_;
46 NSMutableArray *gpbStrings_;
47 }
48
49 @end
50
51 @implementation GPBStringTests
52
53 - (void)setUp {
54 [super setUp];
55 const char *strings[] = {
56 "ascii string",
57 "non-ascii string \xc3\xa9", // e with acute accent
58 "\xe2\x99\xa1", // White Heart
59 "mix \xe2\x99\xa4 string", // White Spade
60
61 // Decomposed forms from http://www.unicode.org/reports/tr15/
62 // 1.2 Singletons
63 "\xe2\x84\xa8 = A\xcc\x8a = \xc3\x85", "\xe2\x84\xa6 = \xce\xa9",
64
65 // 1.2 Canonical Composites
66 "A\xcc\x8a = \xc3\x85",
67 "o\xcc\x82 = \xc3\xb4",
68
69 // 1.2 Multiple Combining Marks
70 "s\xcc\xa3\xcc\x87 = \xe1\xb9\xa9",
71 "\xe1\xb8\x8b\xcc\xa3 = d\xcc\xa3\xcc\x87 = \xe1\xb8\x8d \xcc\x87",
72 "q\xcc\x87\xcc\xa3 = q\xcc\xa3\xcc\x87",
73
74 // BOM
75 "\xEF\xBB\xBF String with BOM",
76 "String with \xEF\xBB\xBF in middle",
77 "String with end bom \xEF\xBB\xBF",
78 "\xEF\xBB\xBF\xe2\x99\xa1", // BOM White Heart
79 "\xEF\xBB\xBF\xEF\xBB\xBF String with Two BOM",
80
81 // Supplementary Plane
82 "\xf0\x9d\x84\x9e", // MUSICAL SYMBOL G CLEF
83
84 // Tags
85 "\xf3\xa0\x80\x81", // Language Tag
86
87 // Variation Selectors
88 "\xf3\xa0\x84\x80", // VARIATION SELECTOR-17
89
90 // Specials
91 "\xef\xbb\xbf\xef\xbf\xbd\xef\xbf\xbf",
92
93 // Left To Right/Right To Left
94 // http://unicode.org/reports/tr9/
95
96 // Hello! <RTL marker>!Merhaba<LTR marker>
97 "Hello! \xE2\x80\x8F!\xd9\x85\xd8\xb1\xd8\xad\xd8\xa8\xd8\xa7\xE2\x80\x8E" ,
98
99 "\xE2\x80\x8E LTR At Start",
100 "LTR At End\xE2\x80\x8E",
101 "\xE2\x80\x8F RTL At Start",
102 "RTL At End\xE2\x80\x8F",
103 "\xE2\x80\x8E\xE2\x80\x8E Double LTR \xE2\x80\x8E\xE2\x80\x8E",
104 "\xE2\x80\x8F\xE2\x80\x8F Double RTL \xE2\x80\x8F\xE2\x80\x8F",
105 "\xE2\x80\x8F\xE2\x80\x8E LTR-RTL LTR-RTL \xE2\x80\x8E\xE2\x80\x8F",
106 };
107 size_t stringsSize = GPBARRAYSIZE(strings);
108 size_t numberUnicodeStrings = 17375;
109 nsStrings_ = [[NSMutableArray alloc]
110 initWithCapacity:stringsSize + numberUnicodeStrings];
111 gpbStrings_ = [[NSMutableArray alloc]
112 initWithCapacity:stringsSize + numberUnicodeStrings];
113 for (size_t i = 0; i < stringsSize; ++i) {
114 size_t length = strlen(strings[i]);
115 NSString *nsString = [[NSString alloc] initWithBytes:strings[i]
116 length:length
117 encoding:NSUTF8StringEncoding];
118 [nsStrings_ addObject:nsString];
119 [nsString release];
120 GPBString *gpbString = GPBCreateGPBStringWithUTF8(strings[i], length);
121 [gpbStrings_ addObject:gpbString];
122 [gpbString release];
123 }
124
125 // Generate all UTF8 characters in a variety of strings
126 // UTF8-1 - 1 Byte UTF 8 chars
127 int length = 0x7F + 1;
128 char *buffer = (char *)calloc(length, 1);
129 for (int i = 0; i < length; ++i) {
130 buffer[i] = (char)i;
131 }
132 NSString *nsString = [[NSString alloc] initWithBytes:buffer
133 length:length
134 encoding:NSUTF8StringEncoding];
135 [nsStrings_ addObject:nsString];
136 [nsString release];
137 GPBString *gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
138 [gpbStrings_ addObject:gpbString];
139 [gpbString release];
140
141 // UTF8-2 - 2 Byte UTF 8 chars
142 int pointLength = 0xbf - 0x80 + 1;
143 length = pointLength * 2;
144 buffer = (char *)calloc(length, 1);
145 for (int i = 0xc2; i <= 0xdf; ++i) {
146 char *bufferPtr = buffer;
147 for (int j = 0x80; j <= 0xbf; ++j) {
148 (*bufferPtr++) = (char)i;
149 (*bufferPtr++) = (char)j;
150 }
151 nsString = [[NSString alloc] initWithBytes:buffer
152 length:length
153 encoding:NSUTF8StringEncoding];
154 [nsStrings_ addObject:nsString];
155 [nsString release];
156 gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
157 [gpbStrings_ addObject:gpbString];
158 [gpbString release];
159 }
160 free(buffer);
161
162 // UTF8-3 - 3 Byte UTF 8 chars
163 length = pointLength * 3;
164 buffer = (char *)calloc(length, 1);
165 for (int i = 0xa0; i <= 0xbf; ++i) {
166 char *bufferPtr = buffer;
167 for (int j = 0x80; j <= 0xbf; ++j) {
168 (*bufferPtr++) = (char)0xE0;
169 (*bufferPtr++) = (char)i;
170 (*bufferPtr++) = (char)j;
171 }
172 nsString = [[NSString alloc] initWithBytes:buffer
173 length:length
174 encoding:NSUTF8StringEncoding];
175 [nsStrings_ addObject:nsString];
176 [nsString release];
177 gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
178 [gpbStrings_ addObject:gpbString];
179 [gpbString release];
180 }
181 for (int i = 0xe1; i <= 0xec; ++i) {
182 for (int j = 0x80; j <= 0xbf; ++j) {
183 char *bufferPtr = buffer;
184 for (int k = 0x80; k <= 0xbf; ++k) {
185 (*bufferPtr++) = (char)i;
186 (*bufferPtr++) = (char)j;
187 (*bufferPtr++) = (char)k;
188 }
189 nsString = [[NSString alloc] initWithBytes:buffer
190 length:length
191 encoding:NSUTF8StringEncoding];
192 [nsStrings_ addObject:nsString];
193 [nsString release];
194 gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
195 [gpbStrings_ addObject:gpbString];
196 [gpbString release];
197 }
198 }
199 for (int i = 0x80; i <= 0x9f; ++i) {
200 char *bufferPtr = buffer;
201 for (int j = 0x80; j <= 0xbf; ++j) {
202 (*bufferPtr++) = (char)0xED;
203 (*bufferPtr++) = (char)i;
204 (*bufferPtr++) = (char)j;
205 }
206 nsString = [[NSString alloc] initWithBytes:buffer
207 length:length
208 encoding:NSUTF8StringEncoding];
209 [nsStrings_ addObject:nsString];
210 [nsString release];
211 gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
212 [gpbStrings_ addObject:gpbString];
213 [gpbString release];
214 }
215 for (int i = 0xee; i <= 0xef; ++i) {
216 for (int j = 0x80; j <= 0xbf; ++j) {
217 char *bufferPtr = buffer;
218 for (int k = 0x80; k <= 0xbf; ++k) {
219 (*bufferPtr++) = (char)i;
220 (*bufferPtr++) = (char)j;
221 (*bufferPtr++) = (char)k;
222 }
223 nsString = [[NSString alloc] initWithBytes:buffer
224 length:length
225 encoding:NSUTF8StringEncoding];
226 [nsStrings_ addObject:nsString];
227 [nsString release];
228 gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
229 [gpbStrings_ addObject:gpbString];
230 [gpbString release];
231 }
232 }
233 free(buffer);
234
235 // UTF8-4 - 4 Byte UTF 8 chars
236 length = pointLength * 4;
237 buffer = (char *)calloc(length, 1);
238 for (int i = 0x90; i <= 0xbf; ++i) {
239 for (int j = 0x80; j <= 0xbf; ++j) {
240 char *bufferPtr = buffer;
241 for (int k = 0x80; k <= 0xbf; ++k) {
242 (*bufferPtr++) = (char)0xF0;
243 (*bufferPtr++) = (char)i;
244 (*bufferPtr++) = (char)j;
245 (*bufferPtr++) = (char)k;
246 }
247 nsString = [[NSString alloc] initWithBytes:buffer
248 length:length
249 encoding:NSUTF8StringEncoding];
250 [nsStrings_ addObject:nsString];
251 [nsString release];
252 gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
253 [gpbStrings_ addObject:gpbString];
254 [gpbString release];
255 }
256 }
257 for (int i = 0xf1; i <= 0xf3; ++i) {
258 for (int j = 0x80; j <= 0xbf; ++j) {
259 for (int k = 0x80; k <= 0xbf; ++k) {
260 char *bufferPtr = buffer;
261 for (int m = 0x80; m <= 0xbf; ++m) {
262 (*bufferPtr++) = (char)i;
263 (*bufferPtr++) = (char)j;
264 (*bufferPtr++) = (char)k;
265 (*bufferPtr++) = (char)m;
266 }
267 nsString = [[NSString alloc] initWithBytes:buffer
268 length:length
269 encoding:NSUTF8StringEncoding];
270 [nsStrings_ addObject:nsString];
271 [nsString release];
272 gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
273 [gpbStrings_ addObject:gpbString];
274 [gpbString release];
275 }
276 }
277 }
278 for (int i = 0x80; i <= 0x8f; ++i) {
279 for (int j = 0x80; j <= 0xbf; ++j) {
280 char *bufferPtr = buffer;
281 for (int k = 0x80; k <= 0xbf; ++k) {
282 (*bufferPtr++) = (char)0xF4;
283 (*bufferPtr++) = (char)i;
284 (*bufferPtr++) = (char)j;
285 (*bufferPtr++) = (char)k;
286 }
287 nsString = [[NSString alloc] initWithBytes:buffer
288 length:length
289 encoding:NSUTF8StringEncoding];
290 [nsStrings_ addObject:nsString];
291 [nsString release];
292 gpbString = GPBCreateGPBStringWithUTF8(buffer, length);
293 [gpbStrings_ addObject:gpbString];
294 [gpbString release];
295 }
296 }
297 free(buffer);
298 }
299
300 - (void)tearDown {
301 [nsStrings_ release];
302 [gpbStrings_ release];
303 [super tearDown];
304 }
305
306 - (void)testLength {
307 size_t i = 0;
308 for (NSString *nsString in nsStrings_) {
309 GPBString *gpbString = gpbStrings_[i];
310 XCTAssertEqual([nsString length], [gpbString length], @"%@ %@", nsString,
311 gpbString);
312 ++i;
313 }
314 }
315
316 - (void)testLengthOfBytesUsingEncoding {
317 NSStringEncoding encodings[] = {
318 NSUTF8StringEncoding,
319 NSASCIIStringEncoding,
320 NSISOLatin1StringEncoding,
321 NSMacOSRomanStringEncoding,
322 NSUTF16StringEncoding,
323 NSUTF32StringEncoding,
324 };
325
326 for (size_t j = 0; j < GPBARRAYSIZE(encodings); ++j) {
327 NSStringEncoding testEncoding = encodings[j];
328 size_t i = 0;
329 for (NSString *nsString in nsStrings_) {
330 GPBString *gpbString = gpbStrings_[i];
331 XCTAssertEqual([nsString lengthOfBytesUsingEncoding:testEncoding],
332 [gpbString lengthOfBytesUsingEncoding:testEncoding],
333 @"%@ %@", nsString, gpbString);
334 ++i;
335 }
336 }
337 }
338
339 - (void)testHash {
340 size_t i = 0;
341 for (NSString *nsString in nsStrings_) {
342 GPBString *gpbString = gpbStrings_[i];
343 XCTAssertEqual([nsString hash], [gpbString hash], @"%@ %@", nsString,
344 gpbString);
345 ++i;
346 }
347 }
348
349 - (void)testEquality {
350 size_t i = 0;
351 for (NSString *nsString in nsStrings_) {
352 GPBString *gpbString = gpbStrings_[i];
353 XCTAssertEqualObjects(nsString, gpbString);
354 ++i;
355 }
356 }
357
358 - (void)testCharacterAtIndex {
359 size_t i = 0;
360 for (NSString *nsString in nsStrings_) {
361 GPBString *gpbString = gpbStrings_[i];
362 NSUInteger length = [nsString length];
363 for (size_t j = 0; j < length; ++j) {
364 unichar nsChar = [nsString characterAtIndex:j];
365 unichar pbChar = [gpbString characterAtIndex:j];
366 XCTAssertEqual(nsChar, pbChar, @"%@ %@ %zu", nsString, gpbString, j);
367 }
368 ++i;
369 }
370 }
371
372 - (void)testCopy {
373 size_t i = 0;
374 for (NSString *nsString in nsStrings_) {
375 GPBString *gpbString = [[gpbStrings_[i] copy] autorelease];
376 XCTAssertEqualObjects(nsString, gpbString);
377 ++i;
378 }
379 }
380
381 - (void)testMutableCopy {
382 size_t i = 0;
383 for (NSString *nsString in nsStrings_) {
384 GPBString *gpbString = [[gpbStrings_[i] mutableCopy] autorelease];
385 XCTAssertEqualObjects(nsString, gpbString);
386 ++i;
387 }
388 }
389
390 - (void)testGetBytes {
391 // Do an attempt at a reasonably exhaustive test of get bytes.
392 // Get bytes with options other than 0 should always fall through to Apple
393 // code so we don't bother testing that path.
394 size_t i = 0;
395 char pbBuffer[256];
396 char nsBuffer[256];
397 int count = 0;
398 for (NSString *nsString in nsStrings_) {
399 GPBString *gpbString = gpbStrings_[i];
400 for (int j = 0; j < 100; ++j) {
401 // [NSString getBytes:maxLength:usedLength:encoding:options:range:remainin gRange]
402 // does not return reliable results if the maxLength argument is 0,
403 // or range is 0,0.
404 // Radar 16385183
405 NSUInteger length = [nsString length];
406 NSUInteger maxBufferCount = (arc4random() % (length + 3)) + 1;
407 NSUInteger rangeStart = arc4random() % length;
408 NSUInteger rangeLength = arc4random() % (length - rangeStart);
409
410 NSRange range = NSMakeRange(rangeStart, rangeLength);
411
412 NSStringEncoding encodings[] = {
413 NSASCIIStringEncoding,
414 NSUTF8StringEncoding,
415 NSUTF16StringEncoding,
416 };
417
418 for (size_t k = 0; k < GPBARRAYSIZE(encodings); ++k) {
419 NSStringEncoding encoding = encodings[k];
420 NSUInteger pbUsedBufferCount = 0;
421 NSUInteger nsUsedBufferCount = 0;
422 NSRange pbLeftOver = NSMakeRange(0, 0);
423 NSRange nsLeftOver = NSMakeRange(0, 0);
424
425 BOOL pbGotBytes = [gpbString getBytes:pbBuffer
426 maxLength:maxBufferCount
427 usedLength:&pbUsedBufferCount
428 encoding:encoding
429 options:0
430 range:range
431 remainingRange:&pbLeftOver];
432 BOOL nsGotBytes = [nsString getBytes:nsBuffer
433 maxLength:maxBufferCount
434 usedLength:&nsUsedBufferCount
435 encoding:encoding
436 options:0
437 range:range
438 remainingRange:&nsLeftOver];
439 XCTAssertEqual(
440 (bool)pbGotBytes, (bool)nsGotBytes,
441 @"PB %d '%@' vs '%@' Encoding:%tu MaxLength: %tu Range: %@ "
442 @"Used: %tu, %tu LeftOver %@, %@)",
443 count, gpbString, nsString, encoding, maxBufferCount,
444 NSStringFromRange(range), pbUsedBufferCount, nsUsedBufferCount,
445 NSStringFromRange(pbLeftOver), NSStringFromRange(nsLeftOver));
446 XCTAssertEqual(
447 pbUsedBufferCount, nsUsedBufferCount,
448 @"PB %d '%@' vs '%@' Encoding:%tu MaxLength: %tu Range: %@ "
449 @"Used: %tu, %tu LeftOver %@, %@)",
450 count, gpbString, nsString, encoding, maxBufferCount,
451 NSStringFromRange(range), pbUsedBufferCount, nsUsedBufferCount,
452 NSStringFromRange(pbLeftOver), NSStringFromRange(nsLeftOver));
453 XCTAssertEqual(
454 pbLeftOver.location, nsLeftOver.location,
455 @"PB %d '%@' vs '%@' Encoding:%tu MaxLength: %tu Range: %@ "
456 @"Used: %tu, %tu LeftOver %@, %@)",
457 count, gpbString, nsString, encoding, maxBufferCount,
458 NSStringFromRange(range), pbUsedBufferCount, nsUsedBufferCount,
459 NSStringFromRange(pbLeftOver), NSStringFromRange(nsLeftOver));
460 XCTAssertEqual(
461 pbLeftOver.length, nsLeftOver.length,
462 @"PB %d '%@' vs '%@' Encoding:%tu MaxLength: %tu Range: %@ "
463 @"Used: %tu, %tu LeftOver %@, %@)",
464 count, gpbString, nsString, encoding, maxBufferCount,
465 NSStringFromRange(range), pbUsedBufferCount, nsUsedBufferCount,
466 NSStringFromRange(pbLeftOver), NSStringFromRange(nsLeftOver));
467 ++count;
468 }
469 }
470 ++i;
471 }
472 }
473
474 - (void)testLengthAndGetBytes {
475 // This test exists as an attempt to ferret out a bug.
476 // http://b/13516532
477 size_t i = 0;
478 char pbBuffer[256];
479 char nsBuffer[256];
480 for (NSString *nsString in nsStrings_) {
481 GPBString *gpbString = gpbStrings_[i++];
482 NSUInteger nsLength =
483 [nsString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
484 NSUInteger pbLength =
485 [gpbString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
486 XCTAssertEqual(nsLength, pbLength, @"%@ %@", nsString, gpbString);
487 NSUInteger pbUsedBufferCount = 0;
488 NSUInteger nsUsedBufferCount = 0;
489 NSRange pbLeftOver = NSMakeRange(0, 0);
490 NSRange nsLeftOver = NSMakeRange(0, 0);
491 NSRange range = NSMakeRange(0, [gpbString length]);
492 BOOL pbGotBytes = [gpbString getBytes:pbBuffer
493 maxLength:sizeof(pbBuffer)
494 usedLength:&pbUsedBufferCount
495 encoding:NSUTF8StringEncoding
496 options:0
497 range:range
498 remainingRange:&pbLeftOver];
499 BOOL nsGotBytes = [nsString getBytes:nsBuffer
500 maxLength:sizeof(nsBuffer)
501 usedLength:&nsUsedBufferCount
502 encoding:NSUTF8StringEncoding
503 options:0
504 range:range
505 remainingRange:&nsLeftOver];
506 XCTAssertTrue(pbGotBytes, @"%@", gpbString);
507 XCTAssertTrue(nsGotBytes, @"%@", nsString);
508 XCTAssertEqual(pbUsedBufferCount, pbLength, @"%@", gpbString);
509 XCTAssertEqual(nsUsedBufferCount, nsLength, @"%@", nsString);
510 }
511 }
512
513 @end
OLDNEW
« no previous file with comments | « third_party/protobuf/objectivec/Tests/GPBPerfTests.m ('k') | third_party/protobuf/objectivec/Tests/GPBSwiftTests.swift » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698