2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Portability.h>
19 #include <gtest/gtest.h>
23 constexpr size_t kSize = 4096 * 4;
28 for (size_t i = 0; i < kSize; ++i) {
29 src[i] = static_cast<char>(i);
30 dst[i] = static_cast<char>(255 - i);
35 TEST(memcpy, zero_len) UBSAN_DISABLE("nonnull-attribute") {
36 // If length is 0, we shouldn't touch any memory. So this should
38 char* srcNull = nullptr;
39 char* dstNull = nullptr;
40 memcpy(dstNull, srcNull, 0);
43 // Test copy `len' bytes and verify that exactly `len' bytes are copied.
44 void testLen(size_t len) {
49 memcpy(dst, src, len);
50 for (size_t i = 0; i < len; ++i) {
51 EXPECT_EQ(src[i], static_cast<char>(i));
52 EXPECT_EQ(src[i], dst[i]);
55 EXPECT_EQ(src[len], static_cast<char>(len));
56 EXPECT_EQ(dst[len], static_cast<char>(255 - len));
61 for (size_t len = 1; len < 8; ++len) {
67 for (size_t len = 8; len < 128; ++len) {
71 for (size_t len = 128; len < kSize; len += 128) {
75 for (size_t len = 128; len < kSize; len += 73) {