blob: cf9004567a14d22fad2a4fa31bd0817c5e03239e [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
457TEST(string, strcmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400458 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400459 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700460 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400461 memset(state.ptr1, 'v', state.MAX_LEN);
462 memset(state.ptr2, 'n', state.MAX_LEN);
463 state.ptr1[state.len[i] - 1] = '\0';
464 state.ptr2[state.len[i] - 1] = '\0';
465
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700466 size_t pos = 1 + (random() % (state.MAX_LEN - 1));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400467 int actual;
468 int expected;
469 if (pos >= state.len[i] - 1) {
470 memcpy(state.ptr1, state.ptr2, state.len[i]);
471 expected = 0;
472 actual = strcmp(state.ptr1, state.ptr2);
473 } else {
474 memcpy(state.ptr1, state.ptr2, pos);
475 if (state.ptr1[pos] > state.ptr2[pos]) {
476 expected = 1;
477 } else if (state.ptr1[pos] == state.ptr2[pos]) {
478 state.ptr1[pos + 1] = '\0';
479 state.ptr2[pos + 1] = '\0';
480 expected = 0;
481 } else {
482 expected = -1;
483 }
484 actual = strcmp(state.ptr1, state.ptr2);
485 }
486
487 ASSERT_EQ(expected, signum(actual));
488 }
489 }
490}
491
Christopher Ferris950a58e2014-04-04 14:38:18 -0700492TEST(string, stpcpy) {
493 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700494 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700495 size_t pos = random() % state.MAX_LEN;
496
497 memset(state.ptr1, '\2', pos);
498 state.ptr1[pos] = '\0';
499 state.ptr1[state.MAX_LEN - 1] = '\0';
500
501 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
502
503 memset(state.ptr2, '\1', state.MAX_LEN);
504 state.ptr2[state.MAX_LEN - 1] = '\0';
505
506 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
507 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
508 state.ptr[2 * state.MAX_LEN - 1] = '\0';
509
510 ASSERT_TRUE(stpcpy(state.ptr2, state.ptr1) == state.ptr2 + strlen(state.ptr1));
511 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
512 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
513 }
514}
515
Anna Tikhonova036154b2012-10-05 15:21:11 +0400516TEST(string, strcpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400517 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700518 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400519 size_t pos = random() % state.MAX_LEN;
520
521 memset(state.ptr1, '\2', pos);
522 state.ptr1[pos] = '\0';
523 state.ptr1[state.MAX_LEN - 1] = '\0';
524
525 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
526
527 memset(state.ptr2, '\1', state.MAX_LEN);
528 state.ptr2[state.MAX_LEN - 1] = '\0';
529
530 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
531 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
532 state.ptr[2 * state.MAX_LEN - 1] = '\0';
533
534 ASSERT_TRUE(strcpy(state.ptr2, state.ptr1) == state.ptr2);
535 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN)) != 0 ||
536 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
537 }
538}
539
Anna Tikhonova036154b2012-10-05 15:21:11 +0400540TEST(string, strlcat) {
Christopher Ferris14687652014-11-10 13:58:17 -0800541#if defined(STRLCAT_SUPPORTED)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400542 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400543 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700544 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400545 memset(state.ptr2, '\2', state.MAX_LEN + state.len[i]);
546 state.ptr2[state.MAX_LEN - 1] = '\0';
547 memcpy(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]);
548
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700549 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400550 memset(state.ptr1, '\3', pos);
551 state.ptr1[pos] = '\0';
552 if (pos < state.len[i]) {
553 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, pos + 1);
554 } else {
555 memcpy(state.ptr + state.MAX_LEN - 1, state.ptr1, state.len[i]);
556 state.ptr[state.MAX_LEN + state.len[i] - 1] = '\0';
557 }
558
559 strlcat(state.ptr2, state.ptr1, state.MAX_LEN + state.len[i]);
560
561 ASSERT_TRUE(memcmp(state.ptr, state.ptr2, state.MAX_LEN + state.len[i]) == 0);
562 }
563 }
Christopher Ferris14687652014-11-10 13:58:17 -0800564#else
565 GTEST_LOG_(INFO) << "Skipping test, strlcat not supported on this platform.";
566#endif
Anna Tikhonova036154b2012-10-05 15:21:11 +0400567}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400568
Anna Tikhonova036154b2012-10-05 15:21:11 +0400569TEST(string, strlcpy) {
Christopher Ferris14687652014-11-10 13:58:17 -0800570#if defined(STRLCPY_SUPPORTED)
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400571 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700572 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
573 int rand = 'O';
Anna Tikhonova036154b2012-10-05 15:21:11 +0400574 memset(state.ptr1, rand, state.MAX_LEN);
575
576 size_t pos = random() % state.MAX_LEN;
577 if (pos < state.MAX_LEN) {
578 state.ptr1[pos] = '\0';
579 }
580 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
581
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700582 memset(state.ptr2, 'I', state.MAX_LEN);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400583 memcpy(state.ptr + state.MAX_LEN, state.ptr2, state.MAX_LEN);
584
585 if (pos > state.MAX_LEN - 1) {
586 memcpy(state.ptr + state.MAX_LEN, state.ptr1, state.MAX_LEN);
587 state.ptr[2 * state.MAX_LEN - 1] = '\0';
588 } else {
589 memcpy(state.ptr + state.MAX_LEN, state.ptr1, pos + 1);
590 }
591
592 ASSERT_EQ(strlcpy(state.ptr2, state.ptr1, state.MAX_LEN), strlen(state.ptr1));
593 ASSERT_FALSE((memcmp(state.ptr1, state.ptr, state.MAX_LEN) != 0) ||
594 (memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN) != 0));
595 }
Christopher Ferris14687652014-11-10 13:58:17 -0800596#else
597 GTEST_LOG_(INFO) << "Skipping test, strlcpy not supported on this platform.";
598#endif
Anna Tikhonova036154b2012-10-05 15:21:11 +0400599}
Anna Tikhonova036154b2012-10-05 15:21:11 +0400600
601TEST(string, strncat) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400602 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400603 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700604 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400605 memset(state.ptr2, '\2', state.MAX_LEN);
606 state.ptr2[state.MAX_LEN - 1] = '\0';
607 memcpy(state.ptr, state.ptr2, 2 * state.MAX_LEN);
608
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700609 memset(state.ptr1, 'I', state.len[i]);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400610 state.ptr1[random() % state.len[i]] = '\0';
611 state.ptr1[state.len[i] - 1] = '\0';
612
613 size_t pos = strlen(state.ptr1);
614
615 size_t actual = random() % state.len[i];
616 strncpy(state.ptr + state.MAX_LEN - 1, state.ptr1, std::min(actual, pos));
617 state.ptr[state.MAX_LEN + std::min(actual, pos) - 1] = '\0';
618
619 ASSERT_TRUE(strncat(state.ptr2, state.ptr1, actual) == state.ptr2);
620 ASSERT_EQ(memcmp(state.ptr, state.ptr2, 2 * state.MAX_LEN), 0);
621 }
622 }
623}
624
625TEST(string, strncmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400626 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400627 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700628 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400629 memset(state.ptr1, 'v', state.MAX_LEN);
630 memset(state.ptr2, 'n', state.MAX_LEN);
631 state.ptr1[state.len[i] - 1] = '\0';
632 state.ptr2[state.len[i] - 1] = '\0';
633
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700634 size_t pos = 1 + (random() % (state.MAX_LEN - 1));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400635 int actual;
636 int expected;
637 if (pos >= state.len[i] - 1) {
638 memcpy(state.ptr1, state.ptr2, state.len[i]);
639 expected = 0;
640 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
641 } else {
642 memcpy(state.ptr1, state.ptr2, pos);
643 if (state.ptr1[pos] > state.ptr2[pos]) {
644 expected = 1;
645 } else if (state.ptr1[pos] == state.ptr2[pos]) {
646 state.ptr1[pos + 1] = '\0';
647 state.ptr2[pos + 1] = '\0';
648 expected = 0;
649 } else {
650 expected = -1;
651 }
652 actual = strncmp(state.ptr1, state.ptr2, state.len[i]);
653 }
654
655 ASSERT_EQ(expected, signum(actual));
656 }
657 }
658}
659
Christopher Ferris950a58e2014-04-04 14:38:18 -0700660TEST(string, stpncpy) {
661 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700662 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
663 memset(state.ptr1, 'J', state.MAX_LEN);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700664 // Choose a random size for our src buffer.
665 size_t ptr1_len = random() % state.MAX_LEN;
666 state.ptr1[ptr1_len] = '\0';
667 // Copy ptr1 into ptr, used to verify that ptr1 does not get modified.
668 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
669 // Init ptr2 to a set value.
670 memset(state.ptr2, '\1', state.MAX_LEN);
671
672 // Choose a random amount of data to copy.
673 size_t copy_len = random() % state.MAX_LEN;
674
675 // Set the second half of ptr to the expected pattern in ptr2.
676 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
677 memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len);
678 size_t expected_end;
679 if (copy_len > ptr1_len) {
680 memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len);
681 expected_end = ptr1_len;
682 } else {
683 expected_end = copy_len;
684 }
685
686 ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len));
687
688 // Verify ptr1 was not modified.
689 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN));
690 // Verify ptr2 contains the expected data.
691 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN));
692 }
693}
694
Anna Tikhonova036154b2012-10-05 15:21:11 +0400695TEST(string, strncpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400696 StringTestState<char> state(SMALL);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700697 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700698 // Choose a random value to fill the string, except \0 (string terminator),
699 // or \1 (guarantees it's different from anything in ptr2).
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700700 memset(state.ptr1, 'K', state.MAX_LEN);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700701 // Choose a random size for our src buffer.
702 size_t ptr1_len = random() % state.MAX_LEN;
703 state.ptr1[ptr1_len] = '\0';
704 // Copy ptr1 into ptr, used to verify that ptr1 does not get modified.
Anna Tikhonova036154b2012-10-05 15:21:11 +0400705 memcpy(state.ptr, state.ptr1, state.MAX_LEN);
Christopher Ferris950a58e2014-04-04 14:38:18 -0700706 // Init ptr2 to a set value.
Anna Tikhonova036154b2012-10-05 15:21:11 +0400707 memset(state.ptr2, '\1', state.MAX_LEN);
708
Christopher Ferris950a58e2014-04-04 14:38:18 -0700709 // Choose a random amount of data to copy.
710 size_t copy_len = random() % state.MAX_LEN;
711
712 // Set the second half of ptr to the expected pattern in ptr2.
713 memset(state.ptr + state.MAX_LEN, '\1', state.MAX_LEN);
714 memcpy(state.ptr + state.MAX_LEN, state.ptr1, copy_len);
715 size_t expected_end;
716 if (copy_len > ptr1_len) {
717 memset(state.ptr + state.MAX_LEN + ptr1_len, '\0', copy_len - ptr1_len);
718 expected_end = ptr1_len;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400719 } else {
Christopher Ferris950a58e2014-04-04 14:38:18 -0700720 expected_end = copy_len;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400721 }
722
Christopher Ferris950a58e2014-04-04 14:38:18 -0700723 ASSERT_EQ(state.ptr2 + expected_end, stpncpy(state.ptr2, state.ptr1, copy_len));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400724
Christopher Ferris950a58e2014-04-04 14:38:18 -0700725 // Verify ptr1 was not modified.
726 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr, state.MAX_LEN));
727 // Verify ptr2 contains the expected data.
728 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr + state.MAX_LEN, state.MAX_LEN));
Anna Tikhonova036154b2012-10-05 15:21:11 +0400729 }
730}
731
732TEST(string, strrchr) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700733 int seek_char = 'M';
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400734 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400735 for (size_t i = 1; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700736 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400737 if (~seek_char > 0) {
738 memset(state.ptr1, ~seek_char, state.len[i]);
739 } else {
740 memset(state.ptr1, '\1', state.len[i]);
741 }
742 state.ptr1[state.len[i] - 1] = '\0';
743
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700744 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400745 char* expected;
746 if (pos >= state.len[i] - 1) {
747 if (seek_char == 0) {
748 expected = state.ptr1 + state.len[i] - 1;
749 } else {
750 expected = NULL;
751 }
752 } else {
753 state.ptr1[pos] = seek_char;
754 expected = state.ptr1 + pos;
755 }
756
757 ASSERT_TRUE(strrchr(state.ptr1, seek_char) == expected);
758 }
759 }
760}
761
762TEST(string, memchr) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700763 int seek_char = 'N';
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400764 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400765 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700766 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400767 memset(state.ptr1, ~seek_char, state.len[i]);
768
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700769 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400770 char* expected;
771 if (pos >= state.len[i]) {
772 expected = NULL;
773 } else {
774 state.ptr1[pos] = seek_char;
775 expected = state.ptr1 + pos;
776 }
777
778 ASSERT_TRUE(memchr(state.ptr1, seek_char, state.len[i]) == expected);
779 }
780 }
781}
782
Christopher Ferrise03e1ea2014-07-30 16:06:56 -0700783TEST(string, memchr_zero) {
784 uint8_t* buffer;
785 ASSERT_EQ(0, posix_memalign(reinterpret_cast<void**>(&buffer), 64, 64));
786 memset(buffer, 10, 64);
787 ASSERT_TRUE(NULL == memchr(buffer, 5, 0));
788 ASSERT_TRUE(NULL == memchr(buffer, 10, 0));
789}
790
Anna Tikhonova036154b2012-10-05 15:21:11 +0400791TEST(string, memrchr) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700792 int seek_char = 'P';
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400793 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400794 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700795 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400796 memset(state.ptr1, ~seek_char, state.len[i]);
797
Dmitriy Ivanov7b956ed2014-09-04 12:47:07 -0700798 size_t pos = random() % state.MAX_LEN;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400799 char* expected;
800 if (pos >= state.len[i]) {
801 expected = NULL;
802 } else {
803 state.ptr1[pos] = seek_char;
804 expected = state.ptr1 + pos;
805 }
806
807 ASSERT_TRUE(memrchr(state.ptr1, seek_char, state.len[i]) == expected);
808 }
809 }
810}
811
812TEST(string, memcmp) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400813 StringTestState<char> state(SMALL);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400814 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700815 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
816 int c1 = 'A';
817 int c2 = 'N';
Anna Tikhonova036154b2012-10-05 15:21:11 +0400818 memset(state.ptr1, c1, state.MAX_LEN);
819 memset(state.ptr2, c1, state.MAX_LEN);
820
821 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
822 state.ptr2[pos] = c2;
823
824 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
825 int actual = memcmp(state.ptr1, state.ptr2, state.MAX_LEN);
826
827 ASSERT_EQ(signum(expected), signum(actual));
828 }
829 }
830}
831
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400832TEST(string, wmemcmp) {
833 StringTestState<wchar_t> state(SMALL);
834
835 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700836 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400837 long long mask = ((long long) 1 << 8 * sizeof(wchar_t)) - 1;
838 int c1 = rand() & mask;
839 int c2 = rand() & mask;
840 wmemset(state.ptr1, c1, state.MAX_LEN);
841 wmemset(state.ptr2, c1, state.MAX_LEN);
842
843 int pos = (state.len[i] == 0) ? 0 : (random() % state.len[i]);
844 state.ptr2[pos] = c2;
845
846 int expected = (static_cast<int>(c1) - static_cast<int>(c2));
847 int actual = wmemcmp(state.ptr1, state.ptr2, (size_t) state.MAX_LEN);
848
849 ASSERT_EQ(signum(expected), signum(actual));
850 }
851 }
852}
853
Anna Tikhonova036154b2012-10-05 15:21:11 +0400854TEST(string, memcpy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400855 StringTestState<char> state(LARGE);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700856 int rand = 4;
Anna Tikhonova036154b2012-10-05 15:21:11 +0400857 for (size_t i = 0; i < state.n - 1; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700858 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400859 size_t pos = random() % (state.MAX_LEN - state.len[i]);
860
861 memset(state.ptr1, rand, state.len[i]);
862 memset(state.ptr1 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
863
864 memset(state.ptr2, rand, state.len[i]);
865 memset(state.ptr2 + state.len[i], ~rand, state.MAX_LEN - state.len[i]);
866 memset(state.ptr2 + pos, '\0', state.len[i]);
867
868 ASSERT_FALSE(memcpy(state.ptr2 + pos, state.ptr1 + pos, state.len[i]) != state.ptr2 + pos);
869 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
870 }
871 }
872}
873
874TEST(string, memset) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400875 StringTestState<char> state(LARGE);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700876 char ch = 'P';
Anna Tikhonova036154b2012-10-05 15:21:11 +0400877 for (size_t i = 0; i < state.n - 1; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700878 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
Anna Tikhonova036154b2012-10-05 15:21:11 +0400879 memset(state.ptr1, ~ch, state.MAX_LEN);
880 memcpy(state.ptr2, state.ptr1, state.MAX_LEN);
881
882 size_t pos = random () % (state.MAX_LEN - state.len[i]);
883 for (size_t k = pos; k < pos + state.len[i]; k++) {
884 state.ptr1[k] = ch;
885 }
886
887 ASSERT_TRUE(memset(state.ptr2 + pos, ch, state.len[i]) == state.ptr2 + pos);
888
889 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
890 }
891 }
892}
893
894TEST(string, memmove) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400895 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400896 for (size_t i = 0; i < state.n - 1; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700897 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
898 memset(state.ptr1, 'Q', 2 * state.MAX_LEN);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400899
900 size_t pos = random() % (state.MAX_LEN - state.len[i]);
901
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700902 memset(state.ptr1, 'R', state.len[i]);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400903 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
904 memcpy(state.ptr, state.ptr1, state.len[i]);
905 memcpy(state.ptr1 + pos, state.ptr, state.len[i]);
906
907 ASSERT_TRUE(memmove(state.ptr2 + pos, state.ptr2, state.len[i]) == state.ptr2 + pos);
908 ASSERT_EQ(0, memcmp(state.ptr2, state.ptr1, 2 * state.MAX_LEN));
909 }
910 }
911}
912
Varvara Rainchikfce86142014-05-27 12:41:55 +0400913TEST(string, memmove_cache_size) {
914 size_t len = 600000;
915 int max_alignment = 31;
916 int alignments[] = {0, 5, 11, 29, 30};
917 char* ptr = reinterpret_cast<char*>(malloc(sizeof(char) * len));
918 char* ptr1 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len));
919 char* glob_ptr2 = reinterpret_cast<char*>(malloc(2 * sizeof(char) * len + max_alignment));
920 size_t pos = 64;
921
922 ASSERT_TRUE(ptr != NULL);
923 ASSERT_TRUE(ptr1 != NULL);
924 ASSERT_TRUE(glob_ptr2 != NULL);
925
926 for (int i = 0; i < 5; i++) {
927 char* ptr2 = glob_ptr2 + alignments[i];
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700928 memset(ptr1, 'S', 2 * len);
929 memset(ptr1, 'T', len);
Varvara Rainchikfce86142014-05-27 12:41:55 +0400930 memcpy(ptr2, ptr1, 2 * len);
931 memcpy(ptr, ptr1, len);
932 memcpy(ptr1 + pos, ptr, len);
933
934 ASSERT_TRUE(memmove(ptr2 + pos, ptr, len) == ptr2 + pos);
935 ASSERT_EQ(0, memcmp(ptr2, ptr1, 2 * len));
936 }
937 free(ptr);
938 free(ptr1);
939 free(glob_ptr2);
940}
941
Shu Zhang6c80ccd2014-05-12 18:12:15 +0800942static void verify_memmove(char* src_copy, char* dst, char* src, size_t size) {
943 memset(dst, 0, size);
944 memcpy(src, src_copy, size);
945 ASSERT_EQ(dst, memmove(dst, src, size));
946 ASSERT_EQ(0, memcmp(dst, src_copy, size));
947}
948
949#define MEMMOVE_DATA_SIZE (1024*1024*3)
950
951TEST(string, memmove_check) {
952 char* buffer = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE));
953 ASSERT_TRUE(buffer != NULL);
954
955 char* src_data = reinterpret_cast<char*>(malloc(MEMMOVE_DATA_SIZE));
956 ASSERT_TRUE(src_data != NULL);
957 // Initialize to a known pattern to copy into src for each test and
958 // to compare dst against.
959 for (size_t i = 0; i < MEMMOVE_DATA_SIZE; i++) {
960 src_data[i] = (i + 1) % 255;
961 }
962
963 // Check all different dst offsets between 0 and 127 inclusive.
964 char* src = buffer;
965 for (size_t i = 0; i < 127; i++) {
966 char* dst = buffer + 256 + i;
967 // Small copy.
968 verify_memmove(src_data, dst, src, 1024);
969
970 // Medium copy.
971 verify_memmove(src_data, dst, src, 64 * 1024);
972
973 // Medium copy.
974 verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024);
975 }
976
977 // Check all leftover size offsets between 1 and 127 inclusive.
978 char* dst = buffer + 256;
979 src = buffer;
980 for (size_t size = 1; size < 127; size++) {
981 // Small copy.
982 verify_memmove(src_data, dst, src, 1024);
983
984 // Medium copy.
985 verify_memmove(src_data, dst, src, 64 * 1024);
986
987 // Large copy.
988 verify_memmove(src_data, dst, src, 1024 * 1024 + 128 * 1024);
989 }
990}
991
Anna Tikhonova036154b2012-10-05 15:21:11 +0400992TEST(string, bcopy) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +0400993 StringTestState<char> state(LARGE);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400994 for (size_t i = 0; i < state.n; i++) {
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -0700995 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
996 memset(state.ptr1, '4', state.MAX_LEN);
997 memset(state.ptr1 + state.MAX_LEN, 'a', state.MAX_LEN);
Anna Tikhonova036154b2012-10-05 15:21:11 +0400998 memcpy(state.ptr2, state.ptr1, 2 * state.MAX_LEN);
999
1000 size_t start = random() % (2 * state.MAX_LEN - state.len[i]);
1001 memcpy(state.ptr2 + start, state.ptr1, state.len[i]);
1002
1003 bcopy(state.ptr1, state.ptr1 + start, state.len[i]);
1004 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, 2 * state.MAX_LEN));
1005 }
1006 }
1007}
1008
1009TEST(string, bzero) {
Alexander Ivchenkobaa91f42013-06-27 12:55:46 +04001010 StringTestState<char> state(LARGE);
Dmitriy Ivanov1467dfe2014-08-13 11:24:37 -07001011 for (state.BeginIterations(); state.HasNextIteration(); state.NextIteration()) {
1012 memset(state.ptr1, 'R', state.MAX_LEN);
Anna Tikhonova036154b2012-10-05 15:21:11 +04001013
1014 size_t start = random() % state.MAX_LEN;
1015 size_t end = start + random() % (state.MAX_LEN - start);
1016
1017 memcpy(state.ptr2, state.ptr1, start);
1018 memset(state.ptr2 + start, '\0', end - start);
1019 memcpy(state.ptr2 + end, state.ptr1 + end, state.MAX_LEN - end);
1020
1021 bzero(state.ptr1 + start, end - start);
1022
1023 ASSERT_EQ(0, memcmp(state.ptr1, state.ptr2, state.MAX_LEN));
1024 }
1025}
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001026
1027static void DoMemcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1028 memset(src, (len % 255) + 1, len);
1029 memset(dst, 0, len);
1030
1031 ASSERT_EQ(dst, memcpy(dst, src, len));
1032 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1033}
1034
1035TEST(string, memcpy_align) {
1036 RunSrcDstBufferAlignTest(LARGE, DoMemcpyTest);
1037}
1038
1039TEST(string, memcpy_overread) {
1040 RunSrcDstBufferOverreadTest(DoMemcpyTest);
1041}
1042
Shu Zhang6c80ccd2014-05-12 18:12:15 +08001043static void DoMemmoveTest(uint8_t* src, uint8_t* dst, size_t len) {
1044 memset(src, (len % 255) + 1, len);
1045 memset(dst, 0, len);
1046
1047 ASSERT_EQ(dst, memmove(dst, src, len));
1048 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1049}
1050
1051TEST(string, memmove_align) {
1052 RunSrcDstBufferAlignTest(LARGE, DoMemmoveTest);
1053}
1054
1055TEST(string, memmove_overread) {
1056 RunSrcDstBufferOverreadTest(DoMemmoveTest);
1057}
1058
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001059static void DoMemsetTest(uint8_t* buf, size_t len) {
1060 for (size_t i = 0; i < len; i++) {
1061 buf[i] = 0;
1062 }
1063 int value = (len % 255) + 1;
1064 ASSERT_EQ(buf, memset(buf, value, len));
1065 for (size_t i = 0; i < len; i++) {
1066 ASSERT_EQ(value, buf[i]);
1067 }
1068}
1069
1070TEST(string, memset_align) {
1071 RunSingleBufferAlignTest(LARGE, DoMemsetTest);
1072}
1073
1074static void DoStrlenTest(uint8_t* buf, size_t len) {
1075 if (len >= 1) {
1076 memset(buf, (32 + (len % 96)), len - 1);
1077 buf[len-1] = '\0';
1078 ASSERT_EQ(len-1, strlen(reinterpret_cast<char*>(buf)));
1079 }
1080}
1081
1082TEST(string, strlen_align) {
1083 RunSingleBufferAlignTest(LARGE, DoStrlenTest);
1084}
1085
1086TEST(string, strlen_overread) {
1087 RunSingleBufferOverreadTest(DoStrlenTest);
1088}
1089
1090static void DoStrcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1091 if (len >= 1) {
1092 memset(src, (32 + (len % 96)), len - 1);
1093 src[len-1] = '\0';
1094 memset(dst, 0, len);
1095 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcpy(reinterpret_cast<char*>(dst),
1096 reinterpret_cast<char*>(src))));
1097 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1098 }
1099}
1100
1101TEST(string, strcpy_align) {
1102 RunSrcDstBufferAlignTest(LARGE, DoStrcpyTest);
1103}
1104
1105TEST(string, strcpy_overread) {
1106 RunSrcDstBufferOverreadTest(DoStrcpyTest);
1107}
1108
Christopher Ferris14687652014-11-10 13:58:17 -08001109#if defined(STRLCPY_SUPPORTED)
1110static void DoStrlcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1111 if (len >= 1) {
1112 memset(src, (32 + (len % 96)), len - 1);
1113 src[len-1] = '\0';
1114 memset(dst, 0, len);
1115 ASSERT_EQ(len-1, strlcpy(reinterpret_cast<char*>(dst),
1116 reinterpret_cast<char*>(src), len));
1117 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1118 }
1119}
1120#endif
1121
1122TEST(string, strlcpy_align) {
1123#if defined(STRLCPY_SUPPORTED)
1124 RunSrcDstBufferAlignTest(LARGE, DoStrlcpyTest);
1125#else
1126 GTEST_LOG_(INFO) << "Skipping test, strlcpy not supported on this platform.";
1127#endif
1128}
1129
1130TEST(string, strlcpy_overread) {
1131#if defined(STRLCPY_SUPPORTED)
1132 RunSrcDstBufferOverreadTest(DoStrlcpyTest);
1133#else
1134 GTEST_LOG_(INFO) << "Skipping test, strlcpy not supported on this platform.";
1135#endif
1136}
1137
1138
Christopher Ferris950a58e2014-04-04 14:38:18 -07001139static void DoStpcpyTest(uint8_t* src, uint8_t* dst, size_t len) {
1140 if (len >= 1) {
1141 memset(src, (32 + (len % 96)), len - 1);
1142 src[len-1] = '\0';
1143 memset(dst, 0, len);
1144 ASSERT_EQ(dst+len-1, reinterpret_cast<uint8_t*>(stpcpy(reinterpret_cast<char*>(dst),
1145 reinterpret_cast<char*>(src))));
1146 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1147 }
1148}
1149
1150TEST(string, stpcpy_align) {
1151 RunSrcDstBufferAlignTest(LARGE, DoStpcpyTest);
1152}
1153
1154TEST(string, stpcpy_overread) {
1155 RunSrcDstBufferOverreadTest(DoStpcpyTest);
1156}
1157
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001158// Use our own incrementer to cut down on the total number of calls.
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001159static size_t LargeSetIncrement(size_t len) {
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001160 if (len >= 4096) {
1161 return 4096;
1162 } else if (len >= 1024) {
1163 return 1024;
1164 } else if (len >= 256) {
1165 return 256;
1166 }
1167 return 1;
1168}
1169
1170#define STRCAT_DST_LEN 128
1171
1172static void DoStrcatTest(uint8_t* src, uint8_t* dst, size_t len) {
1173 if (len >= 1) {
1174 int value = 32 + (len % 96);
1175 memset(src, value, len - 1);
1176 src[len-1] = '\0';
1177
1178 if (len >= STRCAT_DST_LEN) {
1179 // Create a small buffer for doing quick compares in each loop.
1180 uint8_t cmp_buf[STRCAT_DST_LEN];
1181 // Make sure dst string contains a different value then the src string.
1182 int value2 = 32 + (value + 2) % 96;
1183 memset(cmp_buf, value2, sizeof(cmp_buf));
1184
1185 for (size_t i = 1; i <= STRCAT_DST_LEN; i++) {
1186 memset(dst, value2, i-1);
1187 memset(dst+i-1, 0, len-i);
1188 src[len-i] = '\0';
1189 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1190 reinterpret_cast<char*>(src))));
1191 ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0);
1192 ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0);
1193 }
1194 } else {
1195 dst[0] = '\0';
1196 ASSERT_EQ(dst, reinterpret_cast<uint8_t*>(strcat(reinterpret_cast<char*>(dst),
1197 reinterpret_cast<char*>(src))));
1198 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1199 }
1200 }
1201}
1202
1203TEST(string, strcat_align) {
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001204 RunSrcDstBufferAlignTest(MEDIUM, DoStrcatTest, LargeSetIncrement);
Christopher Ferrisb687ad32013-11-06 17:32:11 -08001205}
1206
1207TEST(string, strcat_overread) {
1208 RunSrcDstBufferOverreadTest(DoStrcatTest);
1209}
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001210
Christopher Ferris14687652014-11-10 13:58:17 -08001211#if defined(STRLCAT_SUPPORTED)
1212static void DoStrlcatTest(uint8_t* src, uint8_t* dst, size_t len) {
1213 if (len >= 1) {
1214 int value = 32 + (len % 96);
1215 memset(src, value, len - 1);
1216 src[len-1] = '\0';
1217
1218 if (len >= STRCAT_DST_LEN) {
1219 // Create a small buffer for doing quick compares in each loop.
1220 uint8_t cmp_buf[STRCAT_DST_LEN];
1221 // Make sure dst string contains a different value then the src string.
1222 int value2 = 32 + (value + 2) % 96;
1223 memset(cmp_buf, value2, sizeof(cmp_buf));
1224
1225 for (size_t i = 1; i <= STRCAT_DST_LEN; i++) {
1226 memset(dst, value2, i-1);
1227 memset(dst+i-1, 0, len-i);
1228 src[len-i] = '\0';
1229 ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst),
1230 reinterpret_cast<char*>(src), len));
1231 ASSERT_TRUE(memcmp(dst, cmp_buf, i-1) == 0);
1232 ASSERT_TRUE(memcmp(src, dst+i-1, len-i+1) == 0);
1233 }
1234 } else {
1235 dst[0] = '\0';
1236 ASSERT_EQ(len-1, strlcat(reinterpret_cast<char*>(dst),
1237 reinterpret_cast<char*>(src), len));
1238 ASSERT_TRUE(memcmp(src, dst, len) == 0);
1239 }
1240 }
1241}
1242#endif
1243
1244TEST(string, strlcat_align) {
1245#if defined(STRLCAT_SUPPORTED)
1246 RunSrcDstBufferAlignTest(MEDIUM, DoStrlcatTest, LargeSetIncrement);
1247#else
1248 GTEST_LOG_(INFO) << "Skipping test, strlcat not supported on this platform.";
1249#endif
1250}
1251
1252TEST(string, strlcat_overread) {
1253#if defined(STRLCAT_SUPPORTED)
1254 RunSrcDstBufferOverreadTest(DoStrlcatTest);
1255#else
1256 GTEST_LOG_(INFO) << "Skipping test, strlcat not supported on this platform.";
1257#endif
1258}
1259
Christopher Ferrise5bbb6b2013-12-03 18:39:10 -08001260static void DoStrcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1261 if (len >= 1) {
1262 memset(buf1, (32 + (len % 96)), len - 1);
1263 buf1[len-1] = '\0';
1264 memset(buf2, (32 + (len % 96)), len - 1);
1265 buf2[len-1] = '\0';
1266 ASSERT_EQ(0, strcmp(reinterpret_cast<char*>(buf1),
1267 reinterpret_cast<char*>(buf2)));
1268 }
1269}
1270
1271static void DoStrcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1272 // Do string length differences.
1273 int c = (32 + (len1 % 96));
1274 memset(buf1, c, len1 - 1);
1275 buf1[len1-1] = '\0';
1276 memset(buf2, c, len2 - 1);
1277 buf2[len2-1] = '\0';
1278 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1279 reinterpret_cast<char*>(buf2)));
1280
1281 // Do single character differences.
1282 size_t len;
1283 if (len1 > len2) {
1284 len = len2;
1285 } else {
1286 len = len1;
1287 }
1288 // Need at least a two character buffer to do this test.
1289 if (len > 1) {
1290 buf1[len-1] = '\0';
1291 buf2[len-1] = '\0';
1292 int diff_c = (c + 1) % 96;
1293
1294 buf1[len-2] = diff_c;
1295 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1296 reinterpret_cast<char*>(buf2)));
1297
1298 buf1[len-2] = c;
1299 buf2[len-2] = diff_c;
1300 ASSERT_NE(0, strcmp(reinterpret_cast<char*>(buf1),
1301 reinterpret_cast<char*>(buf2)));
1302 }
1303}
1304
1305TEST(string, strcmp_align) {
1306 RunCmpBufferAlignTest(MEDIUM, DoStrcmpTest, DoStrcmpFailTest, LargeSetIncrement);
1307}
1308
1309TEST(string, strcmp_overread) {
1310 RunCmpBufferOverreadTest(DoStrcmpTest, DoStrcmpFailTest);
1311}
1312
1313static void DoMemcmpTest(uint8_t* buf1, uint8_t* buf2, size_t len) {
1314 memset(buf1, len+1, len);
1315 memset(buf2, len+1, len);
1316 ASSERT_EQ(0, memcmp(buf1, buf2, len));
1317}
1318
1319static void DoMemcmpFailTest(uint8_t* buf1, uint8_t* buf2, size_t len1, size_t len2) {
1320 size_t len;
1321 if (len1 > len2) {
1322 len = len2;
1323 } else {
1324 len = len1;
1325 }
1326
1327 memset(buf1, len2+1, len);
1328 buf1[len-1] = len2;
1329 memset(buf2, len2+1, len);
1330 ASSERT_NE(0, memcmp(buf1, buf2, len));
1331
1332 buf1[len-1] = len2+1;
1333 buf2[len-1] = len2;
1334 ASSERT_NE(0, memcmp(buf1, buf2, len));
1335}
1336
1337TEST(string, memcmp_align) {
1338 RunCmpBufferAlignTest(MEDIUM, DoMemcmpTest, DoMemcmpFailTest, LargeSetIncrement);
1339}
1340
1341TEST(string, memcmp_overread) {
1342 RunCmpBufferOverreadTest(DoMemcmpTest, DoMemcmpFailTest);
1343}
Christopher Ferris3a657d02014-06-27 12:33:22 -07001344
1345static void DoStrchrTest(uint8_t* buf, size_t len) {
1346 if (len >= 1) {
1347 char value = 32 + (len % 96);
1348 char search_value = 33 + (len % 96);
1349 memset(buf, value, len - 1);
1350 buf[len-1] = '\0';
1351 ASSERT_EQ(NULL, strchr(reinterpret_cast<char*>(buf), search_value));
1352 ASSERT_EQ(reinterpret_cast<char*>(&buf[len-1]), strchr(reinterpret_cast<char*>(buf), '\0'));
1353 if (len >= 2) {
1354 buf[0] = search_value;
1355 ASSERT_EQ(reinterpret_cast<char*>(&buf[0]), strchr(reinterpret_cast<char*>(buf), search_value));
1356 buf[0] = value;
1357 buf[len-2] = search_value;
1358 ASSERT_EQ(reinterpret_cast<char*>(&buf[len-2]), strchr(reinterpret_cast<char*>(buf), search_value));
1359 }
1360 }
1361}
1362
1363TEST(string, strchr_align) {
1364 RunSingleBufferAlignTest(MEDIUM, DoStrchrTest);
1365}
1366
1367TEST(string, strchr_overread) {
1368 RunSingleBufferOverreadTest(DoStrchrTest);
1369}
Elliott Hughes09c39d62014-08-19 14:30:30 -07001370
1371static void TestBasename(const char* in, const char* expected_out) {
1372 errno = 0;
1373 const char* out = basename(in);
1374 ASSERT_STREQ(expected_out, out) << in;
1375 ASSERT_EQ(0, errno) << in;
1376}
1377
1378TEST(string, __gnu_basename) {
1379 TestBasename("", "");
1380 TestBasename("/usr/lib", "lib");
1381 TestBasename("/usr/", "");
1382 TestBasename("usr", "usr");
1383 TestBasename("/", "");
1384 TestBasename(".", ".");
1385 TestBasename("..", "..");
1386 TestBasename("///", "");
1387 TestBasename("//usr//lib//", "");
1388}
Elliott Hughes41ef9022015-02-14 13:21:22 -08001389
1390TEST(string, strnlen_147048) {
1391 // https://code.google.com/p/android/issues/detail?id=147048
1392 char stack_src[64] = {0};
1393 EXPECT_EQ(0U, strnlen(stack_src, 1024*1024*1024));
1394 char* heap_src = new char[1];
1395 *heap_src = '\0';
1396 EXPECT_EQ(0U, strnlen(heap_src, 1024*1024*1024));
1397 delete[] heap_src;
1398}
Elliott Hughes3cfb52a2015-02-18 21:29:13 -08001399
Elliott Hughesd2a9fb32015-07-27 20:55:03 -07001400TEST(string, strnlen_74741) {
1401 ASSERT_EQ(4U, strnlen("test", SIZE_MAX));
1402}
1403
Elliott Hughes3cfb52a2015-02-18 21:29:13 -08001404TEST(string, mempcpy) {
1405 char dst[6];
1406 ASSERT_EQ(&dst[4], reinterpret_cast<char*>(mempcpy(dst, "hello", 4)));
1407}