blob: 74d95f49e90aa6d606e7a2a5ee84d02f5854ae95 [file] [log] [blame]
Joachim Bauch58daf402017-12-21 21:00:341/*
2 * Copyright 2017 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "rtc_base/zero_memory.h"
12
Yves Gerey3e707812018-11-28 15:47:4913#include <stdint.h>
14
Joachim Bauch58daf402017-12-21 21:00:3415#include "api/array_view.h"
Yves Gerey3e707812018-11-28 15:47:4916#include "test/gtest.h"
Joachim Bauch58daf402017-12-21 21:00:3417
18namespace rtc {
19
20TEST(ZeroMemoryTest, TestZeroMemory) {
21 static const size_t kBufferSize = 32;
22 uint8_t buffer[kBufferSize];
23 for (size_t i = 0; i < kBufferSize; i++) {
24 buffer[i] = static_cast<uint8_t>(i + 1);
25 }
26 ExplicitZeroMemory(buffer, sizeof(buffer));
27 for (size_t i = 0; i < kBufferSize; i++) {
28 EXPECT_EQ(buffer[i], 0);
29 }
30}
31
32TEST(ZeroMemoryTest, TestZeroArrayView) {
33 static const size_t kBufferSize = 32;
34 uint8_t buffer[kBufferSize];
35 for (size_t i = 0; i < kBufferSize; i++) {
36 buffer[i] = static_cast<uint8_t>(i + 1);
37 }
38 ExplicitZeroMemory(rtc::ArrayView<uint8_t>(buffer, sizeof(buffer)));
39 for (size_t i = 0; i < kBufferSize; i++) {
40 EXPECT_EQ(buffer[i], 0);
41 }
42}
43
44// While this test doesn't actually test anything, it can be used to check
45// the compiler output to make sure the call to "ExplicitZeroMemory" is not
46// optimized away.
47TEST(ZeroMemoryTest, TestZeroMemoryUnused) {
48 static const size_t kBufferSize = 32;
49 uint8_t buffer[kBufferSize];
50 ExplicitZeroMemory(buffer, sizeof(buffer));
51}
52
53} // namespace rtc