andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "common_audio/ring_buffer.h" |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
| 14 | #include <time.h> |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 15 | |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 16 | #include <algorithm> |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 17 | #include <memory> |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 18 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "test/gtest.h" |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | |
andrew@webrtc.org | d617a44 | 2014-02-20 21:08:36 +0000 | [diff] [blame] | 23 | struct FreeBufferDeleter { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | inline void operator()(void* ptr) const { WebRtc_FreeBuffer(ptr); } |
andrew@webrtc.org | d617a44 | 2014-02-20 21:08:36 +0000 | [diff] [blame] | 25 | }; |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 26 | typedef std::unique_ptr<RingBuffer, FreeBufferDeleter> scoped_ring_buffer; |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 27 | |
| 28 | static void AssertElementEq(int expected, int actual) { |
| 29 | ASSERT_EQ(expected, actual); |
| 30 | } |
| 31 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 32 | static int SetIncrementingData(int* data, |
| 33 | int num_elements, |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 34 | int starting_value) { |
| 35 | for (int i = 0; i < num_elements; i++) { |
| 36 | data[i] = starting_value++; |
| 37 | } |
| 38 | return starting_value; |
| 39 | } |
| 40 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 41 | static int CheckIncrementingData(int* data, |
| 42 | int num_elements, |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 43 | int starting_value) { |
| 44 | for (int i = 0; i < num_elements; i++) { |
| 45 | AssertElementEq(starting_value++, data[i]); |
| 46 | } |
| 47 | return starting_value; |
| 48 | } |
| 49 | |
| 50 | // We use ASSERTs in this test to avoid obscuring the seed in the case of a |
| 51 | // failure. |
| 52 | static void RandomStressTest(int** data_ptr) { |
andrew@webrtc.org | c145668 | 2014-07-17 23:16:44 +0000 | [diff] [blame] | 53 | const int kNumTests = 10; |
| 54 | const int kNumOps = 1000; |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 55 | const int kMaxBufferSize = 1000; |
| 56 | |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 57 | unsigned int seed = time(nullptr); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 58 | printf("seed=%u\n", seed); |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 59 | srand(seed); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 60 | for (int i = 0; i < kNumTests; i++) { |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 61 | // rand_r is not supported on many platforms, so rand is used. |
| 62 | const int buffer_size = std::max(rand() % kMaxBufferSize, 1); // NOLINT |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 63 | std::unique_ptr<int[]> write_data(new int[buffer_size]); |
| 64 | std::unique_ptr<int[]> read_data(new int[buffer_size]); |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 65 | scoped_ring_buffer buffer(WebRtc_CreateBuffer(buffer_size, sizeof(int))); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 66 | ASSERT_TRUE(buffer.get() != nullptr); |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 67 | WebRtc_InitBuffer(buffer.get()); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 68 | int buffer_consumed = 0; |
| 69 | int write_element = 0; |
| 70 | int read_element = 0; |
| 71 | for (int j = 0; j < kNumOps; j++) { |
oprypin | 67fdb80 | 2017-03-09 06:25:06 -0800 | [diff] [blame] | 72 | const bool write = rand() % 2 == 0 ? true : false; // NOLINT |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | const int num_elements = rand() % buffer_size; // NOLINT |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 74 | if (write) { |
| 75 | const int buffer_available = buffer_size - buffer_consumed; |
| 76 | ASSERT_EQ(static_cast<size_t>(buffer_available), |
| 77 | WebRtc_available_write(buffer.get())); |
| 78 | const int expected_elements = std::min(num_elements, buffer_available); |
| 79 | write_element = SetIncrementingData(write_data.get(), expected_elements, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 80 | write_element); |
| 81 | ASSERT_EQ( |
| 82 | static_cast<size_t>(expected_elements), |
| 83 | WebRtc_WriteBuffer(buffer.get(), write_data.get(), num_elements)); |
| 84 | buffer_consumed = |
| 85 | std::min(buffer_consumed + expected_elements, buffer_size); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 86 | } else { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 87 | const int expected_elements = std::min(num_elements, buffer_consumed); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 88 | ASSERT_EQ(static_cast<size_t>(buffer_consumed), |
| 89 | WebRtc_available_read(buffer.get())); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 90 | ASSERT_EQ( |
| 91 | static_cast<size_t>(expected_elements), |
| 92 | WebRtc_ReadBuffer(buffer.get(), reinterpret_cast<void**>(data_ptr), |
| 93 | read_data.get(), num_elements)); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 94 | int* check_ptr = read_data.get(); |
| 95 | if (data_ptr) { |
| 96 | check_ptr = *data_ptr; |
| 97 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 98 | read_element = |
| 99 | CheckIncrementingData(check_ptr, expected_elements, read_element); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 100 | buffer_consumed = std::max(buffer_consumed - expected_elements, 0); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | TEST(RingBufferTest, RandomStressTest) { |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 107 | int* data_ptr = nullptr; |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 108 | RandomStressTest(&data_ptr); |
| 109 | } |
| 110 | |
| 111 | TEST(RingBufferTest, RandomStressTestWithNullPtr) { |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 112 | RandomStressTest(nullptr); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST(RingBufferTest, PassingNulltoReadBufferForcesMemcpy) { |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 116 | const size_t kDataSize = 2; |
| 117 | int write_data[kDataSize]; |
| 118 | int read_data[kDataSize]; |
| 119 | int* data_ptr; |
| 120 | |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 121 | scoped_ring_buffer buffer(WebRtc_CreateBuffer(kDataSize, sizeof(int))); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 122 | ASSERT_TRUE(buffer.get() != nullptr); |
andrew@webrtc.org | 6b63015 | 2015-01-15 00:09:53 +0000 | [diff] [blame] | 123 | WebRtc_InitBuffer(buffer.get()); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 124 | |
| 125 | SetIncrementingData(write_data, kDataSize, 0); |
| 126 | EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize)); |
| 127 | SetIncrementingData(read_data, kDataSize, kDataSize); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | EXPECT_EQ(kDataSize, |
| 129 | WebRtc_ReadBuffer(buffer.get(), reinterpret_cast<void**>(&data_ptr), |
| 130 | read_data, kDataSize)); |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 131 | // Copying was not necessary, so |read_data| has not been updated. |
| 132 | CheckIncrementingData(data_ptr, kDataSize, 0); |
| 133 | CheckIncrementingData(read_data, kDataSize, kDataSize); |
| 134 | |
| 135 | EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize)); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 136 | EXPECT_EQ(kDataSize, |
| 137 | WebRtc_ReadBuffer(buffer.get(), nullptr, read_data, kDataSize)); |
| 138 | // Passing null forces a memcpy, so |read_data| is now updated. |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 139 | CheckIncrementingData(read_data, kDataSize, 0); |
| 140 | } |
| 141 | |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 142 | TEST(RingBufferTest, CreateHandlesErrors) { |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 143 | EXPECT_TRUE(WebRtc_CreateBuffer(0, 1) == nullptr); |
| 144 | EXPECT_TRUE(WebRtc_CreateBuffer(1, 0) == nullptr); |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 145 | RingBuffer* buffer = WebRtc_CreateBuffer(1, 1); |
deadbeef | 922246a | 2017-02-26 04:18:12 -0800 | [diff] [blame] | 146 | EXPECT_TRUE(buffer != nullptr); |
andrew@webrtc.org | 91f3255 | 2013-02-27 00:35:06 +0000 | [diff] [blame] | 147 | WebRtc_FreeBuffer(buffer); |
| 148 | } |
| 149 | |
andrew@webrtc.org | 9ae1354 | 2013-02-25 17:07:35 +0000 | [diff] [blame] | 150 | } // namespace webrtc |