1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 //
15 // This tests the system installed C standard library version of memset.
16 //
17 // Note: We have caught real production bugs with these tests. Do not assume
18 // your vendor's C library is correct! For standard C functions like memset and
19 // memcpy, there are compiler intrisics which assume that the C standard is
20 // followed. If the implemention of memset or memcpy does not exactly follow
21 // the standard, subtle and hard to track down bugs can be the result.
22
23 #include <array>
24 #include <cstring>
25 #include <numeric>
26
27 #include "pw_containers/algorithm.h"
28 #include "pw_unit_test/framework.h"
29
30 namespace pw {
31 namespace {
32
33 // From the ISO C standard:
34 // http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf
35 //
36 // Section 7.21.6.1: memset(void *s, int c, size_t n)
37 //
38 // void* memset(void* buffer,
39 // int character,
40 // size_t num_bytes);
41 //
42 // Copy c into the first n bytes of s.
43 // Returns buffer, a copy of the destination pointer.
44 //
45
TEST(Memset,EmptyCase)46 TEST(Memset, EmptyCase) {
47 std::array<char, 5> arr{'h', 'e', 'l', 'l', 'o'};
48 void* ret = memset(arr.data(), 0, 0);
49
50 // Destination buffer returned.
51 EXPECT_EQ(ret, arr.data());
52
53 // Destination buffer untouched.
54 constexpr std::array<char, 5> kExpected{'h', 'e', 'l', 'l', 'o'};
55 EXPECT_TRUE(pw::containers::Equal(arr, kExpected));
56 }
57
TEST(Memset,OneCharacter)58 TEST(Memset, OneCharacter) {
59 std::array<char, 5> arr{'h', 'e', 'l', 'l', 'o'};
60 void* ret = memset(arr.data(), 0, 1);
61
62 // Ensure the destination buffer is returned.
63 EXPECT_EQ(ret, arr.data());
64
65 // Ensure the destination buffer is untouched.
66 constexpr std::array<char, 5> kExpected{0, 'e', 'l', 'l', 'o'};
67 EXPECT_TRUE(pw::containers::Equal(arr, kExpected));
68 }
69
70 // Now do a detailed case with more values. Span both word sizes and alignments
71 // to ensure we hit some edge cases.
TEST(Memset,MultipleSizesMultipleAlignments)72 TEST(Memset, MultipleSizesMultipleAlignments) {
73 constexpr int kMaxBytes = 64;
74 std::array<char, kMaxBytes> arr;
75
76 constexpr int kMaxAlignment = 16;
77
78 // Avoid 0 sentinel to prevent interaction with uninitialized memory.
79 constexpr char kSentinel = 3;
80 constexpr char kIotaStart = kSentinel + 7;
81
82 // Try different alignments.
83 for (int alignment = 0; alignment < kMaxAlignment; ++alignment) {
84 // Try different memset sizes.
85 for (int write_size = 0; write_size < (kMaxBytes - kMaxAlignment);
86 ++write_size) {
87 // Fill entire array with incrementing integers; starting above sentinel.
88 std::iota(arr.begin(), arr.end(), kIotaStart);
89
90 // Memset the first write_size bytes, with our sentinel
91 void* write_head = &arr[alignment];
92 const void* ret = memset(write_head, kSentinel, write_size);
93
94 // Check destination buffer returned.
95 EXPECT_EQ(ret, write_head);
96
97 for (int j = 0; j < kMaxBytes; ++j) {
98 if (j < alignment) {
99 // First part of destination buffer untouched; should match iota.
100 EXPECT_EQ(arr[j], kIotaStart + j);
101 } else if (j < alignment + write_size) {
102 // Second part is set to the sentinel value.
103 EXPECT_EQ(arr[j], kSentinel);
104 } else {
105 // Third part is back to the iota content.
106 EXPECT_EQ(arr[j], kIotaStart + j);
107 }
108 }
109 }
110 }
111 }
112
113 } // namespace
114 } // namespace pw
115