blob: 80190d7d7ff66ece3ca4a9ba04ab32d53f5cff8c [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
Elliott Hughes416d7dd2014-08-18 17:28:32 -070017#define _GNU_SOURCE 1
18
Elliott Hughes09c39d62014-08-19 14:30:30 -070019#include <string.h>
Irina Tirdeab5f053b2012-09-08 09:17:54 +030020
21#include <errno.h>
Elliott Hughes09c39d62014-08-19 14:30:30 -070022#include <gtest/gtest.h>
Dan Alberte5fdaa42014-06-14 01:04:31 +000023#include <malloc.h>
Anna Tikhonova036154b2012-10-05 15:21:11 +040024#include <math.h>
Elliott Hughesd2a9fb32015-07-27 20:55:03 -070025#include <stdint.h>
Irina Tirdeab5f053b2012-09-08 09:17:54 +030026
Christopher Ferrisb687ad32013-11-06 17:32:11 -080027#include "buffer_tests.h"
28
Christopher Ferris14687652014-11-10 13:58:17 -080029#if defined(__BIONIC__)
30#define STRLCPY_SUPPORTED
31#define STRLCAT_SUPPORTED
32#endif
33
Anna Tikhonova036154b2012-10-05 15:21:11 +040034#define KB 1024
35#define SMALL 1*KB
Christopher Ferrisb687ad32013-11-06 17:32:11 -080036#define MEDIUM 4*KB
Anna Tikhonova036154b2012-10-05 15:21:11 +040037#define LARGE 64*KB
38
39static int signum(int i) {
40 if (i < 0) {
41 return -1;
42 } else if (i > 0) {
43 return 1;
44 }
45 return 0;
46}
47
Irina Tirdeab5f053b2012-09-08 09:17:54 +030048TEST(string, strerror) {
49 // Valid.
50 ASSERT_STREQ("Success", strerror(0));
51 ASSERT_STREQ("Operation not permitted", strerror(1));
52
53 // Invalid.
Elliott Hughese6e60062013-01-10 16:01:59 -080054 ASSERT_STREQ("Unknown error -1", strerror(-1));
Irina Tirdeab5f053b2012-09-08 09:17:54 +030055 ASSERT_STREQ("Unknown error 1234", strerror(1234));
56}
57
Christopher Ferrisf04935c2013-12-20 18:43:21 -080058#if defined(__BIONIC__)
Elliott Hughesad88a082012-10-24 18:37:21 -070059static void* ConcurrentStrErrorFn(void*) {
Irina Tirdeab5f053b2012-09-08 09:17:54 +030060 bool equal = (strcmp("Unknown error 2002", strerror(2002)) == 0);
61 return reinterpret_cast<void*>(equal);
62}
Christopher Ferrisf04935c2013-12-20 18:43:21 -080063#endif // __BIONIC__
Irina Tirdeab5f053b2012-09-08 09:17:54 +030064
Christopher Ferrisf04935c2013-12-20 18:43:21 -080065// glibc's strerror isn't thread safe, only its strsignal.
Irina Tirdeab5f053b2012-09-08 09:17:54 +030066TEST(string, strerror_concurrent) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080067#if defined(__BIONIC__)
Irina Tirdeab5f053b2012-09-08 09:17:54 +030068 const char* strerror1001 = strerror(1001);
69 ASSERT_STREQ("Unknown error 1001", strerror1001);
70
71 pthread_t t;
72 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrErrorFn, NULL));
73 void* result;
74 ASSERT_EQ(0, pthread_join(t, &result));
75 ASSERT_TRUE(static_cast<bool>(result));
76
77 ASSERT_STREQ("Unknown error 1001", strerror1001);
Christopher Ferrisf04935c2013-12-20 18:43:21 -080078#else // __BIONIC__
Christopher Ferris14687652014-11-10 13:58:17 -080079 GTEST_LOG_(INFO) << "Skipping test, requires a thread safe strerror.";
Christopher Ferrisf04935c2013-12-20 18:43:21 -080080#endif // __BIONIC__
Irina Tirdeab5f053b2012-09-08 09:17:54 +030081}
Elliott Hughesad88a082012-10-24 18:37:21 -070082
Elliott Hughes416d7dd2014-08-18 17:28:32 -070083TEST(string, gnu_strerror_r) {
Irina Tirdeab5f053b2012-09-08 09:17:54 +030084 char buf[256];
85
Elliott Hughes416d7dd2014-08-18 17:28:32 -070086 // Note that glibc doesn't necessarily write into the buffer.
87
Irina Tirdeab5f053b2012-09-08 09:17:54 +030088 // Valid.
Elliott Hughes416d7dd2014-08-18 17:28:32 -070089 ASSERT_STREQ("Success", strerror_r(0, buf, sizeof(buf)));
90#if defined(__BIONIC__)
Irina Tirdeab5f053b2012-09-08 09:17:54 +030091 ASSERT_STREQ("Success", buf);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070092#endif
93 ASSERT_STREQ("Operation not permitted", strerror_r(1, buf, sizeof(buf)));
94#if defined(__BIONIC__)
Irina Tirdeab5f053b2012-09-08 09:17:54 +030095 ASSERT_STREQ("Operation not permitted", buf);
Elliott Hughes416d7dd2014-08-18 17:28:32 -070096#endif
Irina Tirdeab5f053b2012-09-08 09:17:54 +030097
98 // Invalid.
Elliott Hughes416d7dd2014-08-18 17:28:32 -070099 ASSERT_STREQ("Unknown error -1", strerror_r(-1, buf, sizeof(buf)));
Nick Kralevich60605892013-01-15 10:35:09 -0800100 ASSERT_STREQ("Unknown error -1", buf);
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700101 ASSERT_STREQ("Unknown error 1234", strerror_r(1234, buf, sizeof(buf)));
Irina Tirdeab5f053b2012-09-08 09:17:54 +0300102 ASSERT_STREQ("Unknown error 1234", buf);
103
104 // Buffer too small.
Elliott Hughes416d7dd2014-08-18 17:28:32 -0700105 errno = 0;
106 memset(buf, 0, sizeof(buf));
107 ASSERT_EQ(buf, strerror_r(4567, buf, 2));
108 ASSERT_STREQ("U", buf);
109 // The GNU strerror_r doesn't set errno (the POSIX one sets it to ERANGE).
110 ASSERT_EQ(0, errno);
Irina Tirdeab5f053b2012-09-08 09:17:54 +0300111}
Irina Tirdeab5f053b2012-09-08 09:17:54 +0300112
113TEST(string, strsignal) {
114 // A regular signal.
115 ASSERT_STREQ("Hangup", strsignal(1));
116
117 // A real-time signal.
Elliott Hughes0990d4f2014-04-30 09:45:40 -0700118 ASSERT_STREQ("Real-time signal 14", strsignal(SIGRTMIN + 14));
119 // One of the signals the C library keeps to itself.
120 ASSERT_STREQ("Unknown signal 32", strsignal(__SIGRTMIN));
Irina Tirdeab5f053b2012-09-08 09:17:54 +0300121
122 // Errors.
123 ASSERT_STREQ("Unknown signal -1", strsignal(-1)); // Too small.
124 ASSERT_STREQ("Unknown signal 0", strsignal(0)); // Still too small.
125 ASSERT_STREQ("Unknown signal 1234", strsignal(1234)); // Too large.
126}
127
Elliott Hughesad88a082012-10-24 18:37:21 -0700128static void* ConcurrentStrSignalFn(void*) {
Irina Tirdeab5f053b2012-09-08 09:17:54 +0300129 bool equal = (strcmp("Unknown signal 2002", strsignal(2002)) == 0);
130 return reinterpret_cast<void*>(equal);
131}
132
133TEST(string, strsignal_concurrent) {
134 const char* strsignal1001 = strsignal(1001);
135 ASSERT_STREQ("Unknown signal 1001", strsignal1001);
136
137 pthread_t t;
138 ASSERT_EQ(0, pthread_create(&t, NULL, ConcurrentStrSignalFn, NULL));
139 void* result;
140 ASSERT_EQ(0, pthread_join(t, &result));
141 ASSERT_TRUE(static_cast<bool>(result));
142
143 ASSERT_STREQ("Unknown signal 1001", strsignal1001);
144}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400145
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700146// TODO: where did this number come from?
Anna Tikhonova036154b2012-10-05 15:21:11 +0400147#define ITER 500
148
149// For every length we want to test, vary and change alignment
150// of allocated memory, fill it with some values, calculate
151// expected result and then run function and compare what we got.
152// These tests contributed by Intel Corporation.
153// TODO: make these tests more intention-revealing and less random.
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400154template<class Character>
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700155class StringTestState {
156 public:
157 StringTestState(size_t MAX_LEN) : MAX_LEN(MAX_LEN), align1_index_(0), align2_index_(0) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400158 int max_alignment = 64;
159
160 // TODO: fix the tests to not sometimes use twice their specified "MAX_LEN".
Dan Alberte5fdaa42014-06-14 01:04:31 +0000161 glob_ptr = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment));
162 glob_ptr1 = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment));
163 glob_ptr2 = reinterpret_cast<Character*>(memalign(sysconf(_SC_PAGESIZE), 2 * sizeof(Character) * MAX_LEN + max_alignment));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400164
165 InitLenArray();
166
167 srandom(1234);
168 }
169
170 ~StringTestState() {
171 free(glob_ptr);
172 free(glob_ptr1);
173 free(glob_ptr2);
174 }
175
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700176 void BeginIterations() {
177 align1_index_ = 0;
178 align2_index_ = 0;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400179
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700180 ResetPointers();
181 }
182
183 bool HasNextIteration() {
184 return (align1_index_ != (alignments_size - 1) || align2_index_ != (alignments_size - 1));
185 }
186
187 void NextIteration() {
188 if (align1_index_ == (alignments_size - 1) && align2_index_ == (alignments_size - 1)) {
189 return;
190 }
191
192 if (align1_index_ == (alignments_size - 1)) {
193 align1_index_ = 0;
194 align2_index_++;
195 } else {
196 align1_index_++;
197 }
198
199 ResetPointers();
Anna Tikhonova036154b2012-10-05 15:21:11 +0400200 }
201
202 const size_t MAX_LEN;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400203 Character *ptr, *ptr1, *ptr2;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400204 size_t n;
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700205 size_t len[ITER + 1];
Anna Tikhonova036154b2012-10-05 15:21:11 +0400206
207 private:
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700208 static size_t alignments[];
209 static size_t alignments_size;
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400210 Character *glob_ptr, *glob_ptr1, *glob_ptr2;
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700211 size_t align1_index_, align2_index_;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400212
213 // Calculate input lengths and fill state.len with them.
214 // Test small lengths with more density than big ones. Manually push
215 // smallest (0) and biggest (MAX_LEN) lengths. Avoid repeats.
216 // Return number of lengths to test.
217 void InitLenArray() {
218 n = 0;
219 len[n++] = 0;
220 for (size_t i = 1; i < ITER; ++i) {
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700221 size_t l = static_cast<size_t>(exp(log(static_cast<double>(MAX_LEN)) * i / ITER));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400222 if (l != len[n - 1]) {
223 len[n++] = l;
224 }
225 }
226 len[n++] = MAX_LEN;
227 }
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700228
229 void ResetPointers() {
230 if (align1_index_ == alignments_size || align2_index_ == alignments_size) {
231 ptr = ptr1 = ptr2 = nullptr;
232 } else {
233 ptr = glob_ptr + alignments[align1_index_];
234 ptr1 = glob_ptr1 + alignments[align1_index_];
235 ptr2 = glob_ptr2 + alignments[align2_index_];
236 }
237 }
Anna Tikhonova036154b2012-10-05 15:21:11 +0400238};
239
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700240template<class Character>
241size_t StringTestState<Character>::alignments[] = { 24, 32, 16, 48, 0, 1, 2, 3, 4, 5, 6, 7, 11 };
242
243template<class Character>
244size_t StringTestState<Character>::alignments_size = sizeof(alignments)/sizeof(size_t);
245
Anna Tikhonova036154b2012-10-05 15:21:11 +0400246TEST(string, strcat) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400247 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400248 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700249 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400250 memset(state.ptr2, '\2', state.MAX_LEN);
251 state.ptr2[state.MAX_LEN - 1] = '\0';
252 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
253
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700254 memset(state.ptr1, 'L', state.len[i]);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400255 state.ptr1[random() % state.len[i]] = '\0';
256 state.ptr1[state.len[i] - 1] = '\0';
257
258 strcpy(state.ptr + state.MAX_LEN - 1, state.ptr1);
259
260 EXPECT_TRUE(strcat(state.ptr2, state.ptr1) == state.ptr2);
261 EXPECT_TRUE(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN) == 0);
262 }
263 }
264}
265
Nick Kralevich13476de2013-06-03 10:58:06 -0700266// one byte target with "\0" source
267TEST(string, strcpy2) {
268 char buf[1];
269 char* orig = strdup("");
Christopher Ferris950a58e2014-04-04 14:38:18 -0700270 ASSERT_EQ(buf, strcpy(buf, orig));
Nick Kralevich13476de2013-06-03 10:58:06 -0700271 ASSERT_EQ('\0', buf[0]);
272 free(orig);
273}
274
275// multibyte target where we under fill target
276TEST(string, strcpy3) {
277 char buf[10];
278 char* orig = strdup("12345");
279 memset(buf, 'A', sizeof(buf));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700280 ASSERT_EQ(buf, strcpy(buf, orig));
281 ASSERT_STREQ("12345", buf);
Nick Kralevich13476de2013-06-03 10:58:06 -0700282 ASSERT_EQ('A', buf[6]);
283 ASSERT_EQ('A', buf[7]);
284 ASSERT_EQ('A', buf[8]);
285 ASSERT_EQ('A', buf[9]);
286 free(orig);
287}
288
289// multibyte target where we fill target exactly
290TEST(string, strcpy4) {
291 char buf[10];
292 char* orig = strdup("123456789");
293 memset(buf, 'A', sizeof(buf));
Christopher Ferris950a58e2014-04-04 14:38:18 -0700294 ASSERT_EQ(buf, strcpy(buf, orig));
295 ASSERT_STREQ("123456789", buf);
296 free(orig);
297}
298
299// one byte target with "\0" source
300TEST(string, stpcpy2) {
301 char buf[1];
302 char* orig = strdup("");
303 ASSERT_EQ(buf, stpcpy(buf, orig));
304 ASSERT_EQ('\0', buf[0]);
305 free(orig);
306}
307
308// multibyte target where we under fill target
309TEST(string, stpcpy3) {
310 char buf[10];
311 char* orig = strdup("12345");
312 memset(buf, 'A', sizeof(buf));
313 ASSERT_EQ(buf+strlen(orig), stpcpy(buf, orig));
314 ASSERT_STREQ("12345", buf);
315 ASSERT_EQ('A', buf[6]);
316 ASSERT_EQ('A', buf[7]);
317 ASSERT_EQ('A', buf[8]);
318 ASSERT_EQ('A', buf[9]);
319 free(orig);
320}
321
322// multibyte target where we fill target exactly
323TEST(string, stpcpy4) {
324 char buf[10];
325 char* orig = strdup("123456789");
326 memset(buf, 'A', sizeof(buf));
327 ASSERT_EQ(buf+strlen(orig), stpcpy(buf, orig));
328 ASSERT_STREQ("123456789", buf);
Nick Kralevich13476de2013-06-03 10:58:06 -0700329 free(orig);
330}
331
Nick Kralevichcf870192013-05-30 16:48:53 -0700332TEST(string, strcat2) {
333 char buf[10];
334 memset(buf, 'A', sizeof(buf));
335 buf[0] = 'a';
336 buf[1] = '\0';
337 char* res = strcat(buf, "01234");
338 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700339 ASSERT_STREQ("a01234", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700340 ASSERT_EQ('A', buf[7]);
341 ASSERT_EQ('A', buf[8]);
342 ASSERT_EQ('A', buf[9]);
343}
344
345TEST(string, strcat3) {
346 char buf[10];
347 memset(buf, 'A', sizeof(buf));
348 buf[0] = 'a';
349 buf[1] = '\0';
350 char* res = strcat(buf, "01234567");
351 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700352 ASSERT_STREQ("a01234567", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700353}
354
355TEST(string, strncat2) {
356 char buf[10];
357 memset(buf, 'A', sizeof(buf));
358 buf[0] = 'a';
359 buf[1] = '\0';
360 char* res = strncat(buf, "01234", sizeof(buf) - strlen(buf) - 1);
361 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700362 ASSERT_STREQ("a01234", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700363 ASSERT_EQ('A', buf[7]);
364 ASSERT_EQ('A', buf[8]);
365 ASSERT_EQ('A', buf[9]);
366}
367
368TEST(string, strncat3) {
369 char buf[10];
370 memset(buf, 'A', sizeof(buf));
371 buf[0] = 'a';
372 buf[1] = '\0';
373 char* res = strncat(buf, "0123456789", 5);
374 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700375 ASSERT_STREQ("a01234", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700376 ASSERT_EQ('A', buf[7]);
377 ASSERT_EQ('A', buf[8]);
378 ASSERT_EQ('A', buf[9]);
379}
380
381TEST(string, strncat4) {
382 char buf[10];
383 memset(buf, 'A', sizeof(buf));
384 buf[0] = 'a';
385 buf[1] = '\0';
386 char* res = strncat(buf, "01234567", 8);
387 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700388 ASSERT_STREQ("a01234567", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700389}
390
391TEST(string, strncat5) {
392 char buf[10];
393 memset(buf, 'A', sizeof(buf));
394 buf[0] = 'a';
395 buf[1] = '\0';
396 char* res = strncat(buf, "01234567", 9);
397 ASSERT_EQ(buf, res);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700398 ASSERT_STREQ("a01234567", buf);
Nick Kralevichcf870192013-05-30 16:48:53 -0700399}
400
Nick Kralevich4f40e512013-04-19 16:54:22 -0700401TEST(string, strchr_with_0) {
402 char buf[10];
403 const char* s = "01234";
404 memcpy(buf, s, strlen(s) + 1);
405 EXPECT_TRUE(strchr(buf, '\0') == (buf + strlen(s)));
406}
407
Christopher Ferris3a657d02014-06-27 12:33:22 -0700408TEST(string, strchr_multiple) {
409 char str[128];
410 memset(str, 'a', sizeof(str) - 1);
411 str[sizeof(str)-1] = '\0';
412
413 // Verify that strchr finds the first occurrence of 'a' in a string
414 // filled with 'a' characters. Iterate over the string putting
415 // non 'a' characters at the front of the string during each iteration
416 // and continue to verify that strchr can find the first occurrence
417 // properly. The idea is to cover all possible alignments of the location
418 // of the first occurrence of the 'a' character and which includes
419 // other 'a' characters close by.
420 for (size_t i = 0; i < sizeof(str) - 1; i++) {
421 EXPECT_EQ(&str[i], strchr(str, 'a'));
422 str[i] = 'b';
423 }
424}
425
Anna Tikhonova036154b2012-10-05 15:21:11 +0400426TEST(string, strchr) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700427 int seek_char = 'R';
Anna Tikhonova036154b2012-10-05 15:21:11 +0400428
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400429 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400430 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700431 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400432 if (~seek_char > 0) {
433 memset(state.ptr1, ~seek_char, state.len[i]);
434 } else {
435 memset(state.ptr1, '\1', state.len[i]);
436 }
437 state.ptr1[state.len[i] - 1] = '\0';
438
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700439 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400440 char* expected;
441 if (pos >= state.len[i] - 1) {
442 if (seek_char == 0) {
443 expected = state.ptr1 + state.len[i] - 1;
444 } else {
445 expected = NULL;
446 }
447 } else {
448 state.ptr1[pos] = seek_char;
449 expected = state.ptr1 + pos;
450 }
451
452 ASSERT_TRUE(strchr(state.ptr1, seek_char) == expected);
453 }
454 }
455}
456
Elliott Hughes7ac3c122015-08-26 09:59:29 -0700457TEST(string, strchrnul) {
458 const char* s = "01234222";
459 EXPECT_TRUE(strchrnul(s, '2') == &s[2]);
460 EXPECT_TRUE(strchrnul(s, '8') == (s + strlen(s)));
461 EXPECT_TRUE(strchrnul(s, '\0') == (s + strlen(s)));
462}
463
Anna Tikhonova036154b2012-10-05 15:21:11 +0400464TEST(string, strcmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400465 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400466 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700467 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400468 memset(state.ptr1, 'v', state.MAX_LEN);
469 memset(state.ptr2, 'n', state.MAX_LEN);
470 state.ptr1[state.len[i] - 1] = '\0';
471 state.ptr2[state.len[i] - 1] = '\0';
472
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700473 size_t pos = 1 + (random() % (state.MAX_LEN - 1));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400474 int actual;
475 int expected;
476 if (pos >= state.len[i] - 1) {
477 memcpy(state.ptr1, state.ptr2, state.len[i]);
478 expected = 0;
479 actual = strcmp(state.ptr1, state.ptr2);
480 } else {
481 memcpy(state.ptr1, state.ptr2, pos);
482 if (state.ptr1[pos] > state.ptr2[pos]) {
483 expected = 1;
484 } else if (state.ptr1[pos] == state.ptr2[pos]) {
485 state.ptr1[pos + 1] = '\0';
486 state.ptr2[pos + 1] = '\0';
487 expected = 0;
488 } else {
489 expected = -1;
490 }
491 actual = strcmp(state.ptr1, state.ptr2);
492 }
493
494 ASSERT_EQ(expected, signum(actual));
495 }
496 }
497}
498
Christopher Ferris950a58e2014-04-04 14:38:18 -0700499TEST(string, stpcpy) {
500 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700501 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700502 size_t pos = random() % state.MAX_LEN;
503
504 memset(state.ptr1, '\2', pos);
505 state.ptr1[pos] = '\0';
506 state.ptr1[state.MAX_LEN - 1] = '\0';
507
508 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
509
510 memset(state.ptr2, '\1', state.MAX_LEN);
511 state.ptr2[state.MAX_LEN - 1] = '\0';
512
513 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
514 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
515 state.ptr[2 * state.MAX_LEN - 1] = '\0';
516
517 ASSERT_TRUE(stpcpy(state.ptr2, state.ptr1) == state.ptr2 + strlen(state.ptr1));
518 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
519 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
520 }
521}
522
Anna Tikhonova036154b2012-10-05 15:21:11 +0400523TEST(string, strcpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400524 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700525 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400526 size_t pos = random() % state.MAX_LEN;
527
528 memset(state.ptr1, '\2', pos);
529 state.ptr1[pos] = '\0';
530 state.ptr1[state.MAX_LEN - 1] = '\0';
531
532 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
533
534 memset(state.ptr2, '\1', state.MAX_LEN);
535 state.ptr2[state.MAX_LEN - 1] = '\0';
536
537 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
538 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
539 state.ptr[2 * state.MAX_LEN - 1] = '\0';
540
541 ASSERT_TRUE(strcpy(state.ptr2, state.ptr1) == state.ptr2);
542 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
543 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
544 }
545}
546
Anna Tikhonova036154b2012-10-05 15:21:11 +0400547TEST(string, strlcat) {
Christopher Ferris14687652014-11-10 13:58:17 -0800548#if defined(STRLCAT_SUPPORTED)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400549 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400550 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700551 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400552 memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]);
553 state.ptr2[state.MAX_LEN - 1] = '\0';
554 memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]);
555
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700556 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400557 memset(state.ptr1, '\3', pos);
558 state.ptr1[pos] = '\0';
559 if (pos < state.len[i]) {
560 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, pos + 1);
561 } else {
562 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, state.len[i]);
563 state.ptr[state.MAX_LEN + state.len[i] - 1] = '\0';
564 }
565
566 strlcat(state.ptr2, state.ptr1, state.MAX_LEN + state.len[i]);
567
568 ASSERT_TRUE(memcmp(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]) == 0);
569 }
570 }
Christopher Ferris14687652014-11-10 13:58:17 -0800571#else
572 GTEST_LOG_(INFO) << "Skipping test, strlcat not supported on this platform.";
573#endif
Anna Tikhonova036154b2012-10-05 15:21:11 +0400574}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400575
Anna Tikhonova036154b2012-10-05 15:21:11 +0400576TEST(string, strlcpy) {
Christopher Ferris14687652014-11-10 13:58:17 -0800577#if defined(STRLCPY_SUPPORTED)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400578 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700579 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
580 int rand = 'O';
Anna Tikhonova036154b2012-10-05 15:21:11 +0400581 memset(state.ptr1, rand, state.MAX_LEN);
582
583 size_t pos = random() % state.MAX_LEN;
584 if (pos < state.MAX_LEN) {
585 state.ptr1[pos] = '\0';
586 }
587 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
588
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700589 memset(state.ptr2, 'I', state.MAX_LEN);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400590 memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN);
591
592 if (pos > state.MAX_LEN - 1) {
593 memcpy(state.ptr + state.MAX_LEN, state.ptr1, state.MAX_LEN);
594 state.ptr[2 * state.MAX_LEN - 1] = '\0';
595 } else {
596 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
597 }
598
599 ASSERT_EQ(strlcpy(state.ptr2, state.ptr1, state.MAX_LEN), strlen(state.ptr1));
600 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0) ||
601 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
602 }
Christopher Ferris14687652014-11-10 13:58:17 -0800603#else
604 GTEST_LOG_(INFO) << "Skipping test, strlcpy not supported on this platform.";
605#endif
Anna Tikhonova036154b2012-10-05 15:21:11 +0400606}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400607
608TEST(string, strncat) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400609 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400610 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700611 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400612 memset(state.ptr2, '\2', state.MAX_LEN);
613 state.ptr2[state.MAX_LEN - 1] = '\0';
614 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
615
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700616 memset(state.ptr1, 'I', state.len[i]);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400617 state.ptr1[random() % state.len[i]] = '\0';
618 state.ptr1[state.len[i] - 1] = '\0';
619
620 size_t pos = strlen(state.ptr1);
621
622 size_t actual = random() % state.len[i];
623 strncpy(state.ptr + state.MAX_LEN - 1, state.ptr1, std::min(actual, pos));
624 state.ptr[state.MAX_LEN + std::min(actual, pos) - 1] = '\0';
625
626 ASSERT_TRUE(strncat(state.ptr2, state.ptr1, actual) == state.ptr2);
627 ASSERT_EQ(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN), 0);
628 }
629 }
630}
631
632TEST(string, strncmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400633 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400634 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700635 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400636 memset(state.ptr1, 'v', state.MAX_LEN);
637 memset(state.ptr2, 'n', state.MAX_LEN);
638 state.ptr1[state.len[i] - 1] = '\0';
639 state.ptr2[state.len[i] - 1] = '\0';
640
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700641 size_t pos = 1 + (random() % (state.MAX_LEN - 1));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400642 int actual;
643 int expected;
644 if (pos >= state.len[i] - 1) {
645 memcpy(state.ptr1, state.ptr2, state.len[i]);
646 expected = 0;
647 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
648 } else {
649 memcpy(state.ptr1, state.ptr2, pos);
650 if (state.ptr1[pos] > state.ptr2[pos]) {
651 expected = 1;
652 } else if (state.ptr1[pos] == state.ptr2[pos]) {
653 state.ptr1[pos + 1] = '\0';
654 state.ptr2[pos + 1] = '\0';
655 expected = 0;
656 } else {
657 expected = -1;
658 }
659 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
660 }
661
662 ASSERT_EQ(expected, signum(actual));
663 }
664 }
665}
666
Christopher Ferris950a58e2014-04-04 14:38:18 -0700667TEST(string, stpncpy) {
668 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700669 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
670 memset(state.ptr1, 'J', state.MAX_LEN);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700671 // Choose a random size for our src buffer.
672 size_t ptr1_len = random() % state.MAX_LEN;
673 state.ptr1[ptr1_len] = '\0';
674 // Copy ptr1 into ptr, used to verify that ptr1 does not get modified.
675 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
676 // Init ptr2 to a set value.
677 memset(state.ptr2, '\1', state.MAX_LEN);
678
679 // Choose a random amount of data to copy.
680 size_t copy_len = random() % state.MAX_LEN;
681
682 // Set the second half of ptr to the expected pattern in ptr2.
683 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
684 memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len);
685 size_t expected_end;
686 if (copy_len > ptr1_len) {
687 memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len);
688 expected_end = ptr1_len;
689 } else {
690 expected_end = copy_len;
691 }
692
693 ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len));
694
695 // Verify ptr1 was not modified.
696 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN));
697 // Verify ptr2 contains the expected data.
698 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN));
699 }
700}
701
Anna Tikhonova036154b2012-10-05 15:21:11 +0400702TEST(string, strncpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400703 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700704 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700705 // Choose a random value to fill the string, except \0 (string terminator),
706 // or \1 (guarantees it's different from anything in ptr2).
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700707 memset(state.ptr1, 'K', state.MAX_LEN);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700708 // Choose a random size for our src buffer.
709 size_t ptr1_len = random() % state.MAX_LEN;
710 state.ptr1[ptr1_len] = '\0';
711 // Copy ptr1 into ptr, used to verify that ptr1 does not get modified.
Anna Tikhonova036154b2012-10-05 15:21:11 +0400712 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700713 // Init ptr2 to a set value.
Anna Tikhonova036154b2012-10-05 15:21:11 +0400714 memset(state.ptr2, '\1', state.MAX_LEN);
715
Christopher Ferris950a58e2014-04-04 14:38:18 -0700716 // Choose a random amount of data to copy.
717 size_t copy_len = random() % state.MAX_LEN;
718
719 // Set the second half of ptr to the expected pattern in ptr2.
720 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
721 memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len);
722 size_t expected_end;
723 if (copy_len > ptr1_len) {
724 memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len);
725 expected_end = ptr1_len;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400726 } else {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700727 expected_end = copy_len;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400728 }
729
Christopher Ferris950a58e2014-04-04 14:38:18 -0700730 ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400731
Christopher Ferris950a58e2014-04-04 14:38:18 -0700732 // Verify ptr1 was not modified.
733 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN));
734 // Verify ptr2 contains the expected data.
735 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400736 }
737}
738
739TEST(string, strrchr) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700740 int seek_char = 'M';
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400741 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400742 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700743 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400744 if (~seek_char > 0) {
745 memset(state.ptr1, ~seek_char, state.len[i]);
746 } else {
747 memset(state.ptr1, '\1', state.len[i]);
748 }
749 state.ptr1[state.len[i] - 1] = '\0';
750
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700751 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400752 char* expected;
753 if (pos >= state.len[i] - 1) {
754 if (seek_char == 0) {
755 expected = state.ptr1 + state.len[i] - 1;
756 } else {
757 expected = NULL;
758 }
759 } else {
760 state.ptr1[pos] = seek_char;
761 expected = state.ptr1 + pos;
762 }
763
764 ASSERT_TRUE(strrchr(state.ptr1, seek_char) == expected);
765 }
766 }
767}
768
769TEST(string, memchr) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700770 int seek_char = 'N';
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400771 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400772 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700773 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400774 memset(state.ptr1, ~seek_char, state.len[i]);
775
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700776 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400777 char* expected;
778 if (pos >= state.len[i]) {
779 expected = NULL;
780 } else {
781 state.ptr1[pos] = seek_char;
782 expected = state.ptr1 + pos;
783 }
784
785 ASSERT_TRUE(memchr(state.ptr1, seek_char, state.len[i]) == expected);
786 }
787 }
788}
789
Christopher Ferrise03e1ea2014-07-30 16:06:56 -0700790TEST(string, memchr_zero) {
791 uint8_t* buffer;
792 ASSERT_EQ(0, posix_memalign(reinterpret_cast<void**>(&buffer), 64, 64));
793 memset(buffer, 10, 64);
794 ASSERT_TRUE(NULL == memchr(buffer, 5, 0));
795 ASSERT_TRUE(NULL == memchr(buffer, 10, 0));
796}
797
Anna Tikhonova036154b2012-10-05 15:21:11 +0400798TEST(string, memrchr) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700799 int seek_char = 'P';
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400800 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400801 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700802 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400803 memset(state.ptr1, ~seek_char, state.len[i]);
804
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700805 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400806 char* expected;
807 if (pos >= state.len[i]) {
808 expected = NULL;
809 } else {
810 state.ptr1[pos] = seek_char;
811 expected = state.ptr1 + pos;
812 }
813
814 ASSERT_TRUE(memrchr(state.ptr1, seek_char, state.len[i]) == expected);
815 }
816 }
817}
818
819TEST(string, memcmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400820 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400821 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700822 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
823 int c1 = 'A';
824 int c2 = 'N';
Anna Tikhonova036154b2012-10-05 15:21:11 +0400825 memset(state.ptr1, c1, state.MAX_LEN);
826 memset(state.ptr2, c1, state.MAX_LEN);
827
828 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
829 state.ptr2[pos] = c2;
830
831 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
832 int actual = memcmp(state.ptr1, state.ptr2, state.MAX_LEN);
833
834 ASSERT_EQ(signum(expected), signum(actual));
835 }
836 }
837}
838
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400839TEST(string, wmemcmp) {
840 StringTestState<wchar_t> state(SMALL);
841
842 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700843 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400844 long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1;
845 int c1 = rand() & mask;
846 int c2 = rand() & mask;
847 wmemset(state.ptr1, c1, state.MAX_LEN);
848 wmemset(state.ptr2, c1, state.MAX_LEN);
849
850 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
851 state.ptr2[pos] = c2;
852
853 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
854 int actual = wmemcmp(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
855
856 ASSERT_EQ(signum(expected), signum(actual));
857 }
858 }
859}
860
Anna Tikhonova036154b2012-10-05 15:21:11 +0400861TEST(string, memcpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400862 StringTestState<char> state(LARGE);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700863 int rand = 4;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400864 for (size_t i = 0; i < state.n - 1; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700865 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400866 size_t pos = random() % (state.MAX_LEN - state.len[i]);
867
868 memset(state.ptr1, rand, state.len[i]);
869 memset(state.ptr1 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
870
871 memset(state.ptr2, rand, state.len[i]);
872 memset(state.ptr2 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
873 memset(state.ptr2 + pos, '\0', state.len[i]);
874
875 ASSERT_FALSE(memcpy(state.ptr2 + pos, state.ptr1 + pos, state.len[i]) != state.ptr2 + pos);
876 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
877 }
878 }
879}
880
881TEST(string, memset) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400882 StringTestState<char> state(LARGE);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700883 char ch = 'P';
Anna Tikhonova036154b2012-10-05 15:21:11 +0400884 for (size_t i = 0; i < state.n - 1; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700885 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400886 memset(state.ptr1, ~ch, state.MAX_LEN);
887 memcpy(state.ptr2, state.ptr1, state.MAX_LEN);
888
889 size_t pos = random () % (state.MAX_LEN - state.len[i]);
890 for (size_t k = pos; k < pos + state.len[i]; k++) {
891 state.ptr1[k] = ch;
892 }
893
894 ASSERT_TRUE(memset(state.ptr2 + pos, ch, state.len[i]) == state.ptr2 + pos);
895
896 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
897 }
898 }
899}
900
901TEST(string, memmove) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400902 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400903 for (size_t i = 0; i < state.n - 1; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700904 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
905 memset(state.ptr1, 'Q', 2 * state.MAX_LEN);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400906
907 size_t pos = random() % (state.MAX_LEN - state.len[i]);
908
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700909 memset(state.ptr1, 'R', state.len[i]);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400910 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
911 memcpy(state.ptr, state.ptr1, state.len[i]);
912 memcpy(state.ptr1 + pos, state.ptr, state.len[i]);
913
914 ASSERT_TRUE(memmove(state.ptr2 + pos, state.ptr2, state.len[i]) == state.ptr2 + pos);
915 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr1, 2 * state.MAX_LEN));
916 }
917 }
918}
919
Varvara Rainchikfce86142014-05-27 12:41:55 +0400920TEST(string, memmove_cache_size) {
921 size_t len = 600000;
922 int max_alignment = 31;
923 int alignments[] = {0, 5, 11, 29, 30};
924 char* ptr = reinterpret_cast<char*>(malloc(sizeof(char) * len));
925 char* ptr1 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len));
926 char* glob_ptr2 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len + max_alignment));
927 size_t pos = 64;
928
929 ASSERT_TRUE(ptr != NULL);
930 ASSERT_TRUE(ptr1 != NULL);
931 ASSERT_TRUE(glob_ptr2 != NULL);
932
933 for (int i = 0; i < 5; i++) {
934 char* ptr2 = glob_ptr2 + alignments[i];
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700935 memset(ptr1, 'S', 2 * len);
936 memset(ptr1, 'T', len);
Varvara Rainchikfce86142014-05-27 12:41:55 +0400937 memcpy(ptr2, ptr1, 2 * len);
938 memcpy(ptr, ptr1, len);
939 memcpy(ptr1 + pos, ptr, len);
940
941 ASSERT_TRUE(memmove(ptr2 + pos, ptr, len) == ptr2 + pos);
942 ASSERT_EQ(0, memcmp(ptr2, ptr1, 2 * len));
943 }
944 free(ptr);
945 free(ptr1);
946 free(glob_ptr2);
947}
948
Shu Zhang6c80ccd2014-05-12 18:12:15 +0800949static void verify_memmove(char* src_copy, char* dst, char* src, size_t size) {
950 memset(dst, 0, size);
951 memcpy(src, src_copy, size);
952 ASSERT_EQ(dst, memmove(dst, src, size));
953 ASSERT_EQ(0, memcmp(dst, src_copy, size));
954}
955
956#define MEMMOVE_DATA_SIZE (1024*1024*3)
957
958TEST(string, memmove_check) {
959 char* buffer = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE));
960 ASSERT_TRUE(buffer != NULL);
961
962 char* src_data = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE));
963 ASSERT_TRUE(src_data != NULL);
964 // Initialize to a known pattern to copy into src for each test and
965 // to compare dst against.
966 for (size_t i = 0; i < MEMMOVE_DATA_SIZE; i++) {
967 src_data[i] = (i + 1) % 255;
968 }
969
970 // Check all different dst offsets between 0 and 127 inclusive.
971 char* src = buffer;
972 for (size_t i = 0; i < 127; i++) {
973 char* dst = buffer + 256 + i;
974 // Small copy.
975 verify_memmove(src_data, dst, src, 1024);
976
977 // Medium copy.
978 verify_memmove(src_data, dst, src, 64 * 1024);
979
980 // Medium copy.
981 verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024);
982 }
983
984 // Check all leftover size offsets between 1 and 127 inclusive.
985 char* dst = buffer + 256;
986 src = buffer;
987 for (size_t size = 1; size < 127; size++) {
988 // Small copy.
989 verify_memmove(src_data, dst, src, 1024);
990
991 // Medium copy.
992 verify_memmove(src_data, dst, src, 64 * 1024);
993
994 // Large copy.
995 verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024);
996 }
997}
998
Anna Tikhonova036154b2012-10-05 15:21:11 +0400999TEST(string, bcopy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +04001000 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +04001001 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -07001002 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
1003 memset(state.ptr1, '4', state.MAX_LEN);
1004 memset(state.ptr1 + state.MAX_LEN, 'a', state.MAX_LEN);
Anna Tikhonova036154b2012-10-05 15:21:11 +04001005 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
1006
1007 size_t start = random() % (2 * state.MAX_LEN - state.len[i]);
1008 memcpy(state.ptr2 + start, state.ptr1, state.len[i]);
1009
1010 bcopy(state.ptr1, state.ptr1 + start, state.len[i]);
1011 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, 2 * state.MAX_LEN));
1012 }
1013 }
1014}
1015
1016TEST(string, bzero) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +04001017 StringTestState<char> state(LARGE);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -07001018 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
1019 memset(state.ptr1, 'R', state.MAX_LEN);
Anna Tikhonova036154b2012-10-05 15:21:11 +04001020
1021 size_t start = random() % state.MAX_LEN;
1022 size_t end = start + random() % (state.MAX_LEN - start);
1023
1024 memcpy(state.ptr2, state.ptr1, start);
1025 memset(state.ptr2 + start, '\0', end - start);
1026 memcpy(state.ptr2 + end, state.ptr1 + end, state.MAX_LEN - end);
1027
1028 bzero(state.ptr1 + start, end - start);
1029
1030 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
1031 }
1032}
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001033
1034static void DoMemcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1035 memset(src, (len % 255) + 1, len);
1036 memset(dst, 0, len);
1037
1038 ASSERT_EQ(dst, memcpy(dst, src, len));
1039 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1040}
1041
1042TEST(string, memcpy_align) {
1043 RunSrcDstBufferAlignTest(LARGE, DoMemcpyTest);
1044}
1045
1046TEST(string, memcpy_overread) {
1047 RunSrcDstBufferOverreadTest(DoMemcpyTest);
1048}
1049
Shu Zhang6c80ccd2014-05-12 18:12:15 +08001050static void DoMemmoveTest(uint8_t* src, uint8_t* dst, size_t len) {
1051 memset(src, (len % 255) + 1, len);
1052 memset(dst, 0, len);
1053
1054 ASSERT_EQ(dst, memmove(dst, src, len));
1055 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1056}
1057
1058TEST(string, memmove_align) {
1059 RunSrcDstBufferAlignTest(LARGE, DoMemmoveTest);
1060}
1061
1062TEST(string, memmove_overread) {
1063 RunSrcDstBufferOverreadTest(DoMemmoveTest);
1064}
1065
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001066static void DoMemsetTest(uint8_t* buf, size_t len) {
1067 for (size_t i = 0; i < len; i++) {
1068 buf[i] = 0;
1069 }
1070 int value = (len % 255) + 1;
1071 ASSERT_EQ(buf, memset(buf, value, len));
1072 for (size_t i = 0; i < len; i++) {
1073 ASSERT_EQ(value, buf[i]);
1074 }
1075}
1076
1077TEST(string, memset_align) {
1078 RunSingleBufferAlignTest(LARGE, DoMemsetTest);
1079}
1080
1081static void DoStrlenTest(uint8_t* buf, size_t len) {
1082 if (len >= 1) {
1083 memset(buf, (32 + (len % 96)), len - 1);
1084 buf[len-1] = '\0';
1085 ASSERT_EQ(len-1, strlen(reinterpret_cast<char*>(buf)));
1086 }
1087}
1088
1089TEST(string, strlen_align) {
1090 RunSingleBufferAlignTest(LARGE, DoStrlenTest);
1091}
1092
1093TEST(string, strlen_overread) {
1094 RunSingleBufferOverreadTest(DoStrlenTest);
1095}
1096
1097static void DoStrcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1098 if (len >= 1) {
1099 memset(src, (32 + (len % 96)), len - 1);
1100 src[len-1] = '\0';
1101 memset(dst, 0, len);
1102 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcpy(reinterpret_cast<char*>(dst),
1103 reinterpret_cast<char*>(src))));
1104 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1105 }
1106}
1107
1108TEST(string, strcpy_align) {
1109 RunSrcDstBufferAlignTest(LARGE, DoStrcpyTest);
1110}
1111
1112TEST(string, strcpy_overread) {
1113 RunSrcDstBufferOverreadTest(DoStrcpyTest);
1114}
1115
Christopher Ferris14687652014-11-10 13:58:17 -08001116#if defined(STRLCPY_SUPPORTED)
1117static void DoStrlcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1118 if (len >= 1) {
1119 memset(src, (32 + (len % 96)), len - 1);
1120 src[len-1] = '\0';
1121 memset(dst, 0, len);
1122 ASSERT_EQ(len-1, strlcpy(reinterpret_cast<char*>(dst),
1123 reinterpret_cast<char*>(src), len));
1124 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1125 }
1126}
1127#endif
1128
1129TEST(string, strlcpy_align) {
1130#if defined(STRLCPY_SUPPORTED)
1131 RunSrcDstBufferAlignTest(LARGE, DoStrlcpyTest);
1132#else
1133 GTEST_LOG_(INFO) << "Skipping test, strlcpy not supported on this platform.";
1134#endif
1135}
1136
1137TEST(string, strlcpy_overread) {
1138#if defined(STRLCPY_SUPPORTED)
1139 RunSrcDstBufferOverreadTest(DoStrlcpyTest);
1140#else
1141 GTEST_LOG_(INFO) << "Skipping test, strlcpy not supported on this platform.";
1142#endif
1143}
1144
1145
Christopher Ferris950a58e2014-04-04 14:38:18 -07001146static void DoStpcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1147 if (len >= 1) {
1148 memset(src, (32 + (len % 96)), len - 1);
1149 src[len-1] = '\0';
1150 memset(dst, 0, len);
1151 ASSERT_EQ(dst+len-1, reinterpret_cast<uint8_t*>(stpcpy(reinterpret_cast<char*>(dst),
1152 reinterpret_cast<char*>(src))));
1153 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1154 }
1155}
1156
1157TEST(string, stpcpy_align) {
1158 RunSrcDstBufferAlignTest(LARGE, DoStpcpyTest);
1159}
1160
1161TEST(string, stpcpy_overread) {
1162 RunSrcDstBufferOverreadTest(DoStpcpyTest);
1163}
1164
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001165// Use our own incrementer to cut down on the total number of calls.
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001166static size_t LargeSetIncrement(size_t len) {
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001167 if (len >= 4096) {
1168 return 4096;
1169 } else if (len >= 1024) {
1170 return 1024;
1171 } else if (len >= 256) {
1172 return 256;
1173 }
1174 return 1;
1175}
1176
1177#define STRCAT_DST_LEN 128
1178
1179static void DoStrcatTest(uint8_t* src, uint8_t* dst, size_t len) {
1180 if (len >= 1) {
1181 int value = 32 + (len % 96);
1182 memset(src, value, len - 1);
1183 src[len-1] = '\0';
1184
1185 if (len >= STRCAT_DST_LEN) {
1186 // Create a small buffer for doing quick compares in each loop.
1187 uint8_t cmp_buf[STRCAT_DST_LEN];
1188 // Make sure dst string contains a different value then the src string.
1189 int value2 = 32 + (value + 2) % 96;
1190 memset(cmp_buf, value2, sizeof(cmp_buf));
1191
1192 for (size_t i = 1; i <= STRCAT_DST_LEN; i++) {
1193 memset(dst, value2, i-1);
1194 memset(dst+i-1, 0, len-i);
1195 src[len-i] = '\0';
1196 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1197 reinterpret_cast<char*>(src))));
1198 ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0);
1199 ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0);
1200 }
1201 } else {
1202 dst[0] = '\0';
1203 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1204 reinterpret_cast<char*>(src))));
1205 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1206 }
1207 }
1208}
1209
1210TEST(string, strcat_align) {
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001211 RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, LargeSetIncrement);
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001212}
1213
1214TEST(string, strcat_overread) {
1215 RunSrcDstBufferOverreadTest(DoStrcatTest);
1216}
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001217
Christopher Ferris14687652014-11-10 13:58:17 -08001218#if defined(STRLCAT_SUPPORTED)
1219static void DoStrlcatTest(uint8_t* src, uint8_t* dst, size_t len) {
1220 if (len >= 1) {
1221 int value = 32 + (len % 96);
1222 memset(src, value, len - 1);
1223 src[len-1] = '\0';
1224
1225 if (len >= STRCAT_DST_LEN) {
1226 // Create a small buffer for doing quick compares in each loop.
1227 uint8_t cmp_buf[STRCAT_DST_LEN];
1228 // Make sure dst string contains a different value then the src string.
1229 int value2 = 32 + (value + 2) % 96;
1230 memset(cmp_buf, value2, sizeof(cmp_buf));
1231
1232 for (size_t i = 1; i <= STRCAT_DST_LEN; i++) {
1233 memset(dst, value2, i-1);
1234 memset(dst+i-1, 0, len-i);
1235 src[len-i] = '\0';
1236 ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst),
1237 reinterpret_cast<char*>(src), len));
1238 ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0);
1239 ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0);
1240 }
1241 } else {
1242 dst[0] = '\0';
1243 ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst),
1244 reinterpret_cast<char*>(src), len));
1245 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1246 }
1247 }
1248}
1249#endif
1250
1251TEST(string, strlcat_align) {
1252#if defined(STRLCAT_SUPPORTED)
1253 RunSrcDstBufferAlignTest(MEDIUM, DoStrlcatTest, LargeSetIncrement);
1254#else
1255 GTEST_LOG_(INFO) << "Skipping test, strlcat not supported on this platform.";
1256#endif
1257}
1258
1259TEST(string, strlcat_overread) {
1260#if defined(STRLCAT_SUPPORTED)
1261 RunSrcDstBufferOverreadTest(DoStrlcatTest);
1262#else
1263 GTEST_LOG_(INFO) << "Skipping test, strlcat not supported on this platform.";
1264#endif
1265}
1266
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001267static void DoStrcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1268 if (len >= 1) {
1269 memset(buf1, (32 + (len % 96)), len - 1);
1270 buf1[len-1] = '\0';
1271 memset(buf2, (32 + (len % 96)), len - 1);
1272 buf2[len-1] = '\0';
1273 ASSERT_EQ(0, strcmp(reinterpret_cast<char*>(buf1),
1274 reinterpret_cast<char*>(buf2)));
1275 }
1276}
1277
1278static void DoStrcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1279 // Do string length differences.
1280 int c = (32 + (len1 % 96));
1281 memset(buf1, c, len1 - 1);
1282 buf1[len1-1] = '\0';
1283 memset(buf2, c, len2 - 1);
1284 buf2[len2-1] = '\0';
1285 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1286 reinterpret_cast<char*>(buf2)));
1287
1288 // Do single character differences.
1289 size_t len;
1290 if (len1 > len2) {
1291 len = len2;
1292 } else {
1293 len = len1;
1294 }
1295 // Need at least a two character buffer to do this test.
1296 if (len > 1) {
1297 buf1[len-1] = '\0';
1298 buf2[len-1] = '\0';
1299 int diff_c = (c + 1) % 96;
1300
1301 buf1[len-2] = diff_c;
1302 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1303 reinterpret_cast<char*>(buf2)));
1304
1305 buf1[len-2] = c;
1306 buf2[len-2] = diff_c;
1307 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1308 reinterpret_cast<char*>(buf2)));
1309 }
1310}
1311
1312TEST(string, strcmp_align) {
1313 RunCmpBufferAlignTest(MEDIUM, DoStrcmpTest, DoStrcmpFailTest, LargeSetIncrement);
1314}
1315
1316TEST(string, strcmp_overread) {
1317 RunCmpBufferOverreadTest(DoStrcmpTest, DoStrcmpFailTest);
1318}
1319
1320static void DoMemcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1321 memset(buf1, len+1, len);
1322 memset(buf2, len+1, len);
1323 ASSERT_EQ(0, memcmp(buf1, buf2, len));
1324}
1325
1326static void DoMemcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1327 size_t len;
1328 if (len1 > len2) {
1329 len = len2;
1330 } else {
1331 len = len1;
1332 }
1333
1334 memset(buf1, len2+1, len);
1335 buf1[len-1] = len2;
1336 memset(buf2, len2+1, len);
1337 ASSERT_NE(0, memcmp(buf1, buf2, len));
1338
1339 buf1[len-1] = len2+1;
1340 buf2[len-1] = len2;
1341 ASSERT_NE(0, memcmp(buf1, buf2, len));
1342}
1343
1344TEST(string, memcmp_align) {
1345 RunCmpBufferAlignTest(MEDIUM, DoMemcmpTest, DoMemcmpFailTest, LargeSetIncrement);
1346}
1347
1348TEST(string, memcmp_overread) {
1349 RunCmpBufferOverreadTest(DoMemcmpTest, DoMemcmpFailTest);
1350}
Christopher Ferris3a657d02014-06-27 12:33:22 -07001351
1352static void DoStrchrTest(uint8_t* buf, size_t len) {
1353 if (len >= 1) {
1354 char value = 32 + (len % 96);
1355 char search_value = 33 + (len % 96);
1356 memset(buf, value, len - 1);
1357 buf[len-1] = '\0';
1358 ASSERT_EQ(NULL, strchr(reinterpret_cast<char*>(buf), search_value));
1359 ASSERT_EQ(reinterpret_cast<char*>(&buf[len-1]), strchr(reinterpret_cast<char*>(buf), '\0'));
1360 if (len >= 2) {
1361 buf[0] = search_value;
1362 ASSERT_EQ(reinterpret_cast<char*>(&buf[0]), strchr(reinterpret_cast<char*>(buf), search_value));
1363 buf[0] = value;
1364 buf[len-2] = search_value;
1365 ASSERT_EQ(reinterpret_cast<char*>(&buf[len-2]), strchr(reinterpret_cast<char*>(buf), search_value));
1366 }
1367 }
1368}
1369
1370TEST(string, strchr_align) {
1371 RunSingleBufferAlignTest(MEDIUM, DoStrchrTest);
1372}
1373
1374TEST(string, strchr_overread) {
1375 RunSingleBufferOverreadTest(DoStrchrTest);
1376}
Elliott Hughes09c39d62014-08-19 14:30:30 -07001377
1378static void TestBasename(const char* in, const char* expected_out) {
1379 errno = 0;
1380 const char* out = basename(in);
1381 ASSERT_STREQ(expected_out, out) << in;
1382 ASSERT_EQ(0, errno) << in;
1383}
1384
1385TEST(string, __gnu_basename) {
1386 TestBasename("", "");
1387 TestBasename("/usr/lib", "lib");
1388 TestBasename("/usr/", "");
1389 TestBasename("usr", "usr");
1390 TestBasename("/", "");
1391 TestBasename(".", ".");
1392 TestBasename("..", "..");
1393 TestBasename("///", "");
1394 TestBasename("//usr//lib//", "");
1395}
Elliott Hughes41ef9022015-02-14 13:21:22 -08001396
1397TEST(string, strnlen_147048) {
1398 // https://code.google.com/p/android/issues/detail?id=147048
1399 char stack_src[64] = {0};
1400 EXPECT_EQ(0U, strnlen(stack_src, 1024*1024*1024));
1401 char* heap_src = new char[1];
1402 *heap_src = '\0';
1403 EXPECT_EQ(0U, strnlen(heap_src, 1024*1024*1024));
1404 delete[] heap_src;
1405}
Elliott Hughes3cfb52a2015-02-18 21:29:13 -08001406
Elliott Hughesd2a9fb32015-07-27 20:55:03 -07001407TEST(string, strnlen_74741) {
1408 ASSERT_EQ(4U, strnlen("test", SIZE_MAX));
1409}
1410
Elliott Hughes3cfb52a2015-02-18 21:29:13 -08001411TEST(string, mempcpy) {
1412 char dst[6];
1413 ASSERT_EQ(&dst[4], reinterpret_cast<char*>(mempcpy(dst, "hello", 4)));
1414}