blob: a0924dcb724d2d0221c1518e675490e56873234d [file] [log] [blame]
Irina Tirdeab5f053b2012-09-08 09:17:54 +03001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
Anna Tikhonova036154b2012-10-05 15:21:11 +040020#include <math.h>
Irina Tirdeab5f053b2012-09-08 09:17:54 +030021#include <string.h>
22
Anna Tikhonova036154b2012-10-05 15:21:11 +040023#define KB 1024
24#define SMALL 1*KB
25#define LARGE 64*KB
26
27static int signum(int i) {
28 if (i < 0) {
29 return -1;
30 } else if (i > 0) {
31 return 1;
32 }
33 return 0;
34}
35
Irina Tirdeab5f053b2012-09-08 09:17:54 +030036TEST(string, strerror) {
37 // Valid.
38 ASSERT_STREQ("Success", strerror(0));
39 ASSERT_STREQ("Operation not permitted", strerror(1));
40
41 // Invalid.
Elliott Hughese6e60062013-01-10 16:01:59 -080042 ASSERT_STREQ("Unknown error -1", strerror(-1));
Irina Tirdeab5f053b2012-09-08 09:17:54 +030043 ASSERT_STREQ("Unknown error 1234", strerror(1234));
44}
45
Elliott Hughesad88a082012-10-24 18:37:21 -070046#if __BIONIC__ // glibc's strerror isn't thread safe, only its strsignal.
47
48static void* ConcurrentStrErrorFn(void*) {
Irina Tirdeab5f053b2012-09-08 09:17:54 +030049 bool equal = (strcmp("Unknown error 2002", strerror(2002)) == 0);
50 return reinterpret_cast<void*>(equal);
51}
52
Irina Tirdeab5f053b2012-09-08 09:17:54 +030053TEST(string, strerror_concurrent) {
54 const char* strerror1001 = strerror(1001);
55 ASSERT_STREQ("Unknown error 1001", strerror1001);
56
57 pthread_t t;
58 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrErrorFn, NULL));
59 void* result;
60 ASSERT_EQ(0, pthread_join(t, &result));
61 ASSERT_TRUE(static_cast<bool>(result));
62
63 ASSERT_STREQ("Unknown error 1001", strerror1001);
64}
Elliott Hughesad88a082012-10-24 18:37:21 -070065
Irina Tirdeab5f053b2012-09-08 09:17:54 +030066#endif
67
68#if __BIONIC__ // glibc's strerror_r doesn't even have the same signature as the POSIX one.
69TEST(string, strerror_r) {
70 char buf[256];
71
72 // Valid.
73 ASSERT_EQ(0, strerror_r(0, buf, sizeof(buf)));
74 ASSERT_STREQ("Success", buf);
75 ASSERT_EQ(0, strerror_r(1, buf, sizeof(buf)));
76 ASSERT_STREQ("Operation not permitted", buf);
77
78 // Invalid.
79 ASSERT_EQ(0, strerror_r(-1, buf, sizeof(buf)));
Nick Kralevich60605892013-01-15 10:35:09 -080080 ASSERT_STREQ("Unknown error -1", buf);
Irina Tirdeab5f053b2012-09-08 09:17:54 +030081 ASSERT_EQ(0, strerror_r(1234, buf, sizeof(buf)));
82 ASSERT_STREQ("Unknown error 1234", buf);
83
84 // Buffer too small.
85 ASSERT_EQ(-1, strerror_r(0, buf, 2));
86 ASSERT_EQ(ERANGE, errno);
87}
88#endif
89
90TEST(string, strsignal) {
91 // A regular signal.
92 ASSERT_STREQ("Hangup", strsignal(1));
93
94 // A real-time signal.
95#ifdef __GLIBC__ // glibc reserves real-time signals for internal use, and doesn't count those.
96 ASSERT_STREQ("Real-time signal 14", strsignal(48));
97#else
98 ASSERT_STREQ("Real-time signal 16", strsignal(48));
99#endif
100
101 // Errors.
102 ASSERT_STREQ("Unknown signal -1", strsignal(-1)); // Too small.
103 ASSERT_STREQ("Unknown signal 0", strsignal(0)); // Still too small.
104 ASSERT_STREQ("Unknown signal 1234", strsignal(1234)); // Too large.
105}
106
Elliott Hughesad88a082012-10-24 18:37:21 -0700107static void* ConcurrentStrSignalFn(void*) {
Irina Tirdeab5f053b2012-09-08 09:17:54 +0300108 bool equal = (strcmp("Unknown signal 2002", strsignal(2002)) == 0);
109 return reinterpret_cast<void*>(equal);
110}
111
112TEST(string, strsignal_concurrent) {
113 const char* strsignal1001 = strsignal(1001);
114 ASSERT_STREQ("Unknown signal 1001", strsignal1001);
115
116 pthread_t t;
117 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrSignalFn, NULL));
118 void* result;
119 ASSERT_EQ(0, pthread_join(t, &result));
120 ASSERT_TRUE(static_cast<bool>(result));
121
122 ASSERT_STREQ("Unknown signal 1001", strsignal1001);
123}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400124
125// TODO: where did these numbers come from?
126#define POS_ITER 10
127#define ITER 500
128
129// For every length we want to test, vary and change alignment
130// of allocated memory, fill it with some values, calculate
131// expected result and then run function and compare what we got.
132// These tests contributed by Intel Corporation.
133// TODO: make these tests more intention-revealing and less random.
134struct StringTestState {
135 StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN) {
136 int max_alignment = 64;
137
138 // TODO: fix the tests to not sometimes use twice their specified "MAX_LEN".
139 glob_ptr = reinterpret_cast<char*>(valloc(2 * MAX_LEN + max_alignment));
140 glob_ptr1 = reinterpret_cast<char*>(valloc(2 * MAX_LEN + max_alignment));
141 glob_ptr2 = reinterpret_cast<char*>(valloc(2 * MAX_LEN + max_alignment));
142
143 InitLenArray();
144
145 srandom(1234);
146 }
147
148 ~StringTestState() {
149 free(glob_ptr);
150 free(glob_ptr1);
151 free(glob_ptr2);
152 }
153
154 void NewIteration() {
155 int alignments[] = { 24, 32, 16, 48, 1, 2, 3, 0, 5, 11 };
156 int usable_alignments = 10;
157 int align1 = alignments[random() % (usable_alignments - 1)];
158 int align2 = alignments[random() % (usable_alignments - 1)];
159
160 ptr = glob_ptr + align1;
161 ptr1 = glob_ptr1 + align1;
162 ptr2 = glob_ptr2 + align2;
163 }
164
165 const size_t MAX_LEN;
166 char *ptr, *ptr1, *ptr2;
167 size_t n;
168 int len[ITER + 1];
169
170 private:
171 char *glob_ptr, *glob_ptr1, *glob_ptr2;
172
173 // Calculate input lengths and fill state.len with them.
174 // Test small lengths with more density than big ones. Manually push
175 // smallest (0) and biggest (MAX_LEN) lengths. Avoid repeats.
176 // Return number of lengths to test.
177 void InitLenArray() {
178 n = 0;
179 len[n++] = 0;
180 for (size_t i = 1; i < ITER; ++i) {
181 int l = (int) exp(log((double) MAX_LEN) * i / ITER);
182 if (l != len[n - 1]) {
183 len[n++] = l;
184 }
185 }
186 len[n++] = MAX_LEN;
187 }
188};
189
190TEST(string, strcat) {
191 StringTestState state(SMALL);
192 for (size_t i = 1; i < state.n; i++) {
193 for (size_t j = 0; j < POS_ITER; j++) {
194 state.NewIteration();
195
196 memset(state.ptr2, '\2', state.MAX_LEN);
197 state.ptr2[state.MAX_LEN - 1] = '\0';
198 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
199
200 memset(state.ptr1, random() & 255, state.len[i]);
201 state.ptr1[random() % state.len[i]] = '\0';
202 state.ptr1[state.len[i] - 1] = '\0';
203
204 strcpy(state.ptr + state.MAX_LEN - 1, state.ptr1);
205
206 EXPECT_TRUE(strcat(state.ptr2, state.ptr1) == state.ptr2);
207 EXPECT_TRUE(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN) == 0);
208 }
209 }
210}
211
Nick Kralevichcf870192013-05-30 16:48:53 -0700212TEST(string, strcat2) {
213 char buf[10];
214 memset(buf, 'A', sizeof(buf));
215 buf[0] = 'a';
216 buf[1] = '\0';
217 char* res = strcat(buf, "01234");
218 ASSERT_EQ(buf, res);
219 ASSERT_EQ('a', buf[0]);
220 ASSERT_EQ('0', buf[1]);
221 ASSERT_EQ('1', buf[2]);
222 ASSERT_EQ('2', buf[3]);
223 ASSERT_EQ('3', buf[4]);
224 ASSERT_EQ('4', buf[5]);
225 ASSERT_EQ('\0', buf[6]);
226 ASSERT_EQ('A', buf[7]);
227 ASSERT_EQ('A', buf[8]);
228 ASSERT_EQ('A', buf[9]);
229}
230
231TEST(string, strcat3) {
232 char buf[10];
233 memset(buf, 'A', sizeof(buf));
234 buf[0] = 'a';
235 buf[1] = '\0';
236 char* res = strcat(buf, "01234567");
237 ASSERT_EQ(buf, res);
238 ASSERT_EQ('a', buf[0]);
239 ASSERT_EQ('0', buf[1]);
240 ASSERT_EQ('1', buf[2]);
241 ASSERT_EQ('2', buf[3]);
242 ASSERT_EQ('3', buf[4]);
243 ASSERT_EQ('4', buf[5]);
244 ASSERT_EQ('5', buf[6]);
245 ASSERT_EQ('6', buf[7]);
246 ASSERT_EQ('7', buf[8]);
247 ASSERT_EQ('\0', buf[9]);
248}
249
250TEST(string, strncat2) {
251 char buf[10];
252 memset(buf, 'A', sizeof(buf));
253 buf[0] = 'a';
254 buf[1] = '\0';
255 char* res = strncat(buf, "01234", sizeof(buf) - strlen(buf) - 1);
256 ASSERT_EQ(buf, res);
257 ASSERT_EQ('a', buf[0]);
258 ASSERT_EQ('0', buf[1]);
259 ASSERT_EQ('1', buf[2]);
260 ASSERT_EQ('2', buf[3]);
261 ASSERT_EQ('3', buf[4]);
262 ASSERT_EQ('4', buf[5]);
263 ASSERT_EQ('\0', buf[6]);
264 ASSERT_EQ('A', buf[7]);
265 ASSERT_EQ('A', buf[8]);
266 ASSERT_EQ('A', buf[9]);
267}
268
269TEST(string, strncat3) {
270 char buf[10];
271 memset(buf, 'A', sizeof(buf));
272 buf[0] = 'a';
273 buf[1] = '\0';
274 char* res = strncat(buf, "0123456789", 5);
275 ASSERT_EQ(buf, res);
276 ASSERT_EQ('a', buf[0]);
277 ASSERT_EQ('0', buf[1]);
278 ASSERT_EQ('1', buf[2]);
279 ASSERT_EQ('2', buf[3]);
280 ASSERT_EQ('3', buf[4]);
281 ASSERT_EQ('4', buf[5]);
282 ASSERT_EQ('\0', buf[6]);
283 ASSERT_EQ('A', buf[7]);
284 ASSERT_EQ('A', buf[8]);
285 ASSERT_EQ('A', buf[9]);
286}
287
288TEST(string, strncat4) {
289 char buf[10];
290 memset(buf, 'A', sizeof(buf));
291 buf[0] = 'a';
292 buf[1] = '\0';
293 char* res = strncat(buf, "01234567", 8);
294 ASSERT_EQ(buf, res);
295 ASSERT_EQ('a', buf[0]);
296 ASSERT_EQ('0', buf[1]);
297 ASSERT_EQ('1', buf[2]);
298 ASSERT_EQ('2', buf[3]);
299 ASSERT_EQ('3', buf[4]);
300 ASSERT_EQ('4', buf[5]);
301 ASSERT_EQ('5', buf[6]);
302 ASSERT_EQ('6', buf[7]);
303 ASSERT_EQ('7', buf[8]);
304 ASSERT_EQ('\0', buf[9]);
305}
306
307TEST(string, strncat5) {
308 char buf[10];
309 memset(buf, 'A', sizeof(buf));
310 buf[0] = 'a';
311 buf[1] = '\0';
312 char* res = strncat(buf, "01234567", 9);
313 ASSERT_EQ(buf, res);
314 ASSERT_EQ('a', buf[0]);
315 ASSERT_EQ('0', buf[1]);
316 ASSERT_EQ('1', buf[2]);
317 ASSERT_EQ('2', buf[3]);
318 ASSERT_EQ('3', buf[4]);
319 ASSERT_EQ('4', buf[5]);
320 ASSERT_EQ('5', buf[6]);
321 ASSERT_EQ('6', buf[7]);
322 ASSERT_EQ('7', buf[8]);
323 ASSERT_EQ('\0', buf[9]);
324}
325
Nick Kralevich4f40e512013-04-19 16:54:22 -0700326TEST(string, strchr_with_0) {
327 char buf[10];
328 const char* s = "01234";
329 memcpy(buf, s, strlen(s) + 1);
330 EXPECT_TRUE(strchr(buf, '\0') == (buf + strlen(s)));
331}
332
Anna Tikhonova036154b2012-10-05 15:21:11 +0400333TEST(string, strchr) {
334 int seek_char = random() & 255;
335
336 StringTestState state(SMALL);
337 for (size_t i = 1; i < state.n; i++) {
338 for (size_t j = 0; j < POS_ITER; j++) {
339 state.NewIteration();
340
341 if (~seek_char > 0) {
342 memset(state.ptr1, ~seek_char, state.len[i]);
343 } else {
344 memset(state.ptr1, '\1', state.len[i]);
345 }
346 state.ptr1[state.len[i] - 1] = '\0';
347
348 int pos = random() % state.MAX_LEN;
349 char* expected;
350 if (pos >= state.len[i] - 1) {
351 if (seek_char == 0) {
352 expected = state.ptr1 + state.len[i] - 1;
353 } else {
354 expected = NULL;
355 }
356 } else {
357 state.ptr1[pos] = seek_char;
358 expected = state.ptr1 + pos;
359 }
360
361 ASSERT_TRUE(strchr(state.ptr1, seek_char) == expected);
362 }
363 }
364}
365
366TEST(string, strcmp) {
367 StringTestState state(SMALL);
368 for (size_t i = 1; i < state.n; i++) {
369 for (size_t j = 0; j < POS_ITER; j++) {
370 state.NewIteration();
371
372 memset(state.ptr1, 'v', state.MAX_LEN);
373 memset(state.ptr2, 'n', state.MAX_LEN);
374 state.ptr1[state.len[i] - 1] = '\0';
375 state.ptr2[state.len[i] - 1] = '\0';
376
377 int pos = 1 + (random() % (state.MAX_LEN - 1));
378 int actual;
379 int expected;
380 if (pos >= state.len[i] - 1) {
381 memcpy(state.ptr1, state.ptr2, state.len[i]);
382 expected = 0;
383 actual = strcmp(state.ptr1, state.ptr2);
384 } else {
385 memcpy(state.ptr1, state.ptr2, pos);
386 if (state.ptr1[pos] > state.ptr2[pos]) {
387 expected = 1;
388 } else if (state.ptr1[pos] == state.ptr2[pos]) {
389 state.ptr1[pos + 1] = '\0';
390 state.ptr2[pos + 1] = '\0';
391 expected = 0;
392 } else {
393 expected = -1;
394 }
395 actual = strcmp(state.ptr1, state.ptr2);
396 }
397
398 ASSERT_EQ(expected, signum(actual));
399 }
400 }
401}
402
403TEST(string, strcpy) {
404 StringTestState state(SMALL);
405 for (size_t j = 0; j < POS_ITER; j++) {
406 state.NewIteration();
407
408 size_t pos = random() % state.MAX_LEN;
409
410 memset(state.ptr1, '\2', pos);
411 state.ptr1[pos] = '\0';
412 state.ptr1[state.MAX_LEN - 1] = '\0';
413
414 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
415
416 memset(state.ptr2, '\1', state.MAX_LEN);
417 state.ptr2[state.MAX_LEN - 1] = '\0';
418
419 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
420 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
421 state.ptr[2 * state.MAX_LEN - 1] = '\0';
422
423 ASSERT_TRUE(strcpy(state.ptr2, state.ptr1) == state.ptr2);
424 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
425 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
426 }
427}
428
Nick Kralevichdcab1b22013-01-10 17:12:29 -0800429
430#if __BIONIC__
Anna Tikhonova036154b2012-10-05 15:21:11 +0400431TEST(string, strlcat) {
432 StringTestState state(SMALL);
433 for (size_t i = 0; i < state.n; i++) {
434 for (size_t j = 0; j < POS_ITER; j++) {
435 state.NewIteration();
436
437 memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]);
438 state.ptr2[state.MAX_LEN - 1] = '\0';
439 memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]);
440
441 int pos = random() % state.MAX_LEN;
442 memset(state.ptr1, '\3', pos);
443 state.ptr1[pos] = '\0';
444 if (pos < state.len[i]) {
445 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, pos + 1);
446 } else {
447 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, state.len[i]);
448 state.ptr[state.MAX_LEN + state.len[i] - 1] = '\0';
449 }
450
451 strlcat(state.ptr2, state.ptr1, state.MAX_LEN + state.len[i]);
452
453 ASSERT_TRUE(memcmp(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]) == 0);
454 }
455 }
456}
457#endif
458
459#if __BIONIC__
460TEST(string, strlcpy) {
461 StringTestState state(SMALL);
462 for (size_t j = 0; j < POS_ITER; j++) {
463 state.NewIteration();
464
465 int rand = random() & 255;
466 if (rand < 1) {
467 rand = 1;
468 }
469 memset(state.ptr1, rand, state.MAX_LEN);
470
471 size_t pos = random() % state.MAX_LEN;
472 if (pos < state.MAX_LEN) {
473 state.ptr1[pos] = '\0';
474 }
475 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
476
477 memset(state.ptr2, random() & 255, state.MAX_LEN);
478 memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN);
479
480 if (pos > state.MAX_LEN - 1) {
481 memcpy(state.ptr + state.MAX_LEN, state.ptr1, state.MAX_LEN);
482 state.ptr[2 * state.MAX_LEN - 1] = '\0';
483 } else {
484 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
485 }
486
487 ASSERT_EQ(strlcpy(state.ptr2, state.ptr1, state.MAX_LEN), strlen(state.ptr1));
488 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0) ||
489 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
490 }
491}
492#endif
493
494TEST(string, strncat) {
495 StringTestState state(SMALL);
496 for (size_t i = 1; i < state.n; i++) {
497 for (size_t j = 0; j < POS_ITER; j++) {
498 state.NewIteration();
499
500 memset(state.ptr2, '\2', state.MAX_LEN);
501 state.ptr2[state.MAX_LEN - 1] = '\0';
502 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
503
504 memset(state.ptr1, random() & 255, state.len[i]);
505 state.ptr1[random() % state.len[i]] = '\0';
506 state.ptr1[state.len[i] - 1] = '\0';
507
508 size_t pos = strlen(state.ptr1);
509
510 size_t actual = random() % state.len[i];
511 strncpy(state.ptr + state.MAX_LEN - 1, state.ptr1, std::min(actual, pos));
512 state.ptr[state.MAX_LEN + std::min(actual, pos) - 1] = '\0';
513
514 ASSERT_TRUE(strncat(state.ptr2, state.ptr1, actual) == state.ptr2);
515 ASSERT_EQ(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN), 0);
516 }
517 }
518}
519
520TEST(string, strncmp) {
521 StringTestState state(SMALL);
522 for (size_t i = 1; i < state.n; i++) {
523 for (size_t j = 0; j < POS_ITER; j++) {
524 state.NewIteration();
525
526 memset(state.ptr1, 'v', state.MAX_LEN);
527 memset(state.ptr2, 'n', state.MAX_LEN);
528 state.ptr1[state.len[i] - 1] = '\0';
529 state.ptr2[state.len[i] - 1] = '\0';
530
531 int pos = 1 + (random() % (state.MAX_LEN - 1));
532 int actual;
533 int expected;
534 if (pos >= state.len[i] - 1) {
535 memcpy(state.ptr1, state.ptr2, state.len[i]);
536 expected = 0;
537 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
538 } else {
539 memcpy(state.ptr1, state.ptr2, pos);
540 if (state.ptr1[pos] > state.ptr2[pos]) {
541 expected = 1;
542 } else if (state.ptr1[pos] == state.ptr2[pos]) {
543 state.ptr1[pos + 1] = '\0';
544 state.ptr2[pos + 1] = '\0';
545 expected = 0;
546 } else {
547 expected = -1;
548 }
549 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
550 }
551
552 ASSERT_EQ(expected, signum(actual));
553 }
554 }
555}
556
557TEST(string, strncpy) {
558 StringTestState state(SMALL);
559 for (size_t j = 0; j < ITER; j++) {
560 state.NewIteration();
561
562 memset(state.ptr1, random() & 255, state.MAX_LEN);
563 state.ptr1[random () % state.MAX_LEN] = '\0';
564 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
565
566 memset(state.ptr2, '\1', state.MAX_LEN);
567
568 size_t pos;
569 if (memchr(state.ptr1, 0, state.MAX_LEN)) {
570 pos = strlen(state.ptr1);
571 } else {
572 pos = state.MAX_LEN - 1;
573 }
574
575 memset(state.ptr + state.MAX_LEN, '\0', state.MAX_LEN);
576 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
577
578 ASSERT_TRUE(strncpy(state.ptr2, state.ptr1, state.MAX_LEN) == state.ptr2);
579 ASSERT_FALSE(memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0 ||
580 memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0);
581 }
582}
583
584TEST(string, strrchr) {
585 int seek_char = random() & 255;
586 StringTestState state(SMALL);
587 for (size_t i = 1; i < state.n; i++) {
588 for (size_t j = 0; j < POS_ITER; j++) {
589 state.NewIteration();
590
591 if (~seek_char > 0) {
592 memset(state.ptr1, ~seek_char, state.len[i]);
593 } else {
594 memset(state.ptr1, '\1', state.len[i]);
595 }
596 state.ptr1[state.len[i] - 1] = '\0';
597
598 int pos = random() % state.MAX_LEN;
599 char* expected;
600 if (pos >= state.len[i] - 1) {
601 if (seek_char == 0) {
602 expected = state.ptr1 + state.len[i] - 1;
603 } else {
604 expected = NULL;
605 }
606 } else {
607 state.ptr1[pos] = seek_char;
608 expected = state.ptr1 + pos;
609 }
610
611 ASSERT_TRUE(strrchr(state.ptr1, seek_char) == expected);
612 }
613 }
614}
615
616TEST(string, memchr) {
617 int seek_char = random() & 255;
618 StringTestState state(SMALL);
619 for (size_t i = 0; i < state.n; i++) {
620 for (size_t j = 0; j < POS_ITER; j++) {
621 state.NewIteration();
622
623 memset(state.ptr1, ~seek_char, state.len[i]);
624
625 int pos = random() % state.MAX_LEN;
626 char* expected;
627 if (pos >= state.len[i]) {
628 expected = NULL;
629 } else {
630 state.ptr1[pos] = seek_char;
631 expected = state.ptr1 + pos;
632 }
633
634 ASSERT_TRUE(memchr(state.ptr1, seek_char, state.len[i]) == expected);
635 }
636 }
637}
638
639TEST(string, memrchr) {
640 int seek_char = random() & 255;
641 StringTestState state(SMALL);
642 for (size_t i = 0; i < state.n; i++) {
643 for (size_t j = 0; j < POS_ITER; j++) {
644 state.NewIteration();
645
646 memset(state.ptr1, ~seek_char, state.len[i]);
647
648 int pos = random() % state.MAX_LEN;
649 char* expected;
650 if (pos >= state.len[i]) {
651 expected = NULL;
652 } else {
653 state.ptr1[pos] = seek_char;
654 expected = state.ptr1 + pos;
655 }
656
657 ASSERT_TRUE(memrchr(state.ptr1, seek_char, state.len[i]) == expected);
658 }
659 }
660}
661
662TEST(string, memcmp) {
663 StringTestState state(SMALL);
664 for (size_t i = 0; i < state.n; i++) {
665 for (size_t j = 0; j < POS_ITER; j++) {
666 state.NewIteration();
667
668 int c1 = random() & 0xff;
669 int c2 = random() & 0xff;
670 memset(state.ptr1, c1, state.MAX_LEN);
671 memset(state.ptr2, c1, state.MAX_LEN);
672
673 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
674 state.ptr2[pos] = c2;
675
676 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
677 int actual = memcmp(state.ptr1, state.ptr2, state.MAX_LEN);
678
679 ASSERT_EQ(signum(expected), signum(actual));
680 }
681 }
682}
683
684TEST(string, memcpy) {
685 StringTestState state(LARGE);
686 int rand = random() & 255;
687 for (size_t i = 0; i < state.n - 1; i++) {
688 for (size_t j = 0; j < POS_ITER; j++) {
689 state.NewIteration();
690
691 size_t pos = random() % (state.MAX_LEN - state.len[i]);
692
693 memset(state.ptr1, rand, state.len[i]);
694 memset(state.ptr1 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
695
696 memset(state.ptr2, rand, state.len[i]);
697 memset(state.ptr2 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
698 memset(state.ptr2 + pos, '\0', state.len[i]);
699
700 ASSERT_FALSE(memcpy(state.ptr2 + pos, state.ptr1 + pos, state.len[i]) != state.ptr2 + pos);
701 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
702 }
703 }
704}
705
706TEST(string, memset) {
707 StringTestState state(LARGE);
708 char ch = random () & 255;
709 for (size_t i = 0; i < state.n - 1; i++) {
710 for (size_t j = 0; j < POS_ITER; j++) {
711 state.NewIteration();
712
713 memset(state.ptr1, ~ch, state.MAX_LEN);
714 memcpy(state.ptr2, state.ptr1, state.MAX_LEN);
715
716 size_t pos = random () % (state.MAX_LEN - state.len[i]);
717 for (size_t k = pos; k < pos + state.len[i]; k++) {
718 state.ptr1[k] = ch;
719 }
720
721 ASSERT_TRUE(memset(state.ptr2 + pos, ch, state.len[i]) == state.ptr2 + pos);
722
723 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
724 }
725 }
726}
727
728TEST(string, memmove) {
729 StringTestState state(LARGE);
730 for (size_t i = 0; i < state.n - 1; i++) {
731 for (size_t j = 0; j < POS_ITER; j++) {
732 state.NewIteration();
733
734 memset(state.ptr1, random() & 255, 2 * state.MAX_LEN);
735
736 size_t pos = random() % (state.MAX_LEN - state.len[i]);
737
738 memset(state.ptr1, random() & 255, state.len[i]);
739 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
740 memcpy(state.ptr, state.ptr1, state.len[i]);
741 memcpy(state.ptr1 + pos, state.ptr, state.len[i]);
742
743 ASSERT_TRUE(memmove(state.ptr2 + pos, state.ptr2, state.len[i]) == state.ptr2 + pos);
744 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr1, 2 * state.MAX_LEN));
745 }
746 }
747}
748
749TEST(string, bcopy) {
750 StringTestState state(LARGE);
751 for (size_t i = 0; i < state.n; i++) {
752 for (size_t j = 0; j < POS_ITER; j++) {
753 state.NewIteration();
754
755 memset(state.ptr1, random() & 255, state.MAX_LEN);
756 memset(state.ptr1 + state.MAX_LEN, random() & 255, state.MAX_LEN);
757 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
758
759 size_t start = random() % (2 * state.MAX_LEN - state.len[i]);
760 memcpy(state.ptr2 + start, state.ptr1, state.len[i]);
761
762 bcopy(state.ptr1, state.ptr1 + start, state.len[i]);
763 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, 2 * state.MAX_LEN));
764 }
765 }
766}
767
768TEST(string, bzero) {
769 StringTestState state(LARGE);
770 for (size_t j = 0; j < ITER; j++) {
771 state.NewIteration();
772
773 memset(state.ptr1, random() & 255, state.MAX_LEN);
774
775 size_t start = random() % state.MAX_LEN;
776 size_t end = start + random() % (state.MAX_LEN - start);
777
778 memcpy(state.ptr2, state.ptr1, start);
779 memset(state.ptr2 + start, '\0', end - start);
780 memcpy(state.ptr2 + end, state.ptr1 + end, state.MAX_LEN - end);
781
782 bzero(state.ptr1 + start, end - start);
783
784 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
785 }
786}