proxygen
MemcpyTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <folly/Portability.h>
18 
20 
21 namespace {
22 
23 constexpr size_t kSize = 4096 * 4;
24 char src[kSize];
25 char dst[kSize];
26 
27 void init() {
28  for (size_t i = 0; i < kSize; ++i) {
29  src[i] = static_cast<char>(i);
30  dst[i] = static_cast<char>(255 - i);
31  }
32 }
33 } // namespace
34 
35 TEST(memcpy, zero_len)
37  // If length is 0, we shouldn't touch any memory. So this should
38  // not crash.
39  char* srcNull = nullptr;
40  char* dstNull = nullptr;
41  memcpy(dstNull, srcNull, 0);
42 }
43 
44 // Test copy `len' bytes and verify that exactly `len' bytes are copied.
45 void testLen(size_t len) {
46  if (len > kSize) {
47  return;
48  }
49  init();
50  memcpy(dst, src, len);
51  for (size_t i = 0; i < len; ++i) {
52  EXPECT_EQ(src[i], static_cast<char>(i));
53  EXPECT_EQ(src[i], dst[i]);
54  }
55  if (len < kSize) {
56  EXPECT_EQ(src[len], static_cast<char>(len));
57  EXPECT_EQ(dst[len], static_cast<char>(255 - len));
58  }
59 }
60 
61 TEST(memcpy, small) {
62  for (size_t len = 1; len < 8; ++len) {
63  testLen(len);
64  }
65 }
66 
67 TEST(memcpy, main) {
68  for (size_t len = 8; len < 128; ++len) {
69  testLen(len);
70  }
71 
72  for (size_t len = 128; len < kSize; len += 128) {
73  testLen(len);
74  }
75 
76  for (size_t len = 128; len < kSize; len += 73) {
77  testLen(len);
78  }
79 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
void init()
int main(void)
Definition: test.c:2834
auto small
TEST(memcpy, small)
Definition: MemcpyTest.cpp:61
FOLLY_DISABLE_UNDEFINED_BEHAVIOR_SANITIZER("nonnull-attribute")
Definition: MemcpyTest.cpp:36
void testLen(size_t len)
Definition: MemcpyTest.cpp:45