| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/common_audio/ring_buffer.h" | 11 #include "webrtc/common_audio/ring_buffer.h" |
| 12 | 12 |
| 13 #include <stdlib.h> | 13 #include <stdlib.h> |
| 14 #include <time.h> | 14 #include <time.h> |
| 15 |
| 15 #include <algorithm> | 16 #include <algorithm> |
| 17 #include <memory> |
| 16 | 18 |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "webrtc/base/scoped_ptr.h" | |
| 19 | 20 |
| 20 namespace webrtc { | 21 namespace webrtc { |
| 21 | 22 |
| 22 struct FreeBufferDeleter { | 23 struct FreeBufferDeleter { |
| 23 inline void operator()(void* ptr) const { | 24 inline void operator()(void* ptr) const { |
| 24 WebRtc_FreeBuffer(ptr); | 25 WebRtc_FreeBuffer(ptr); |
| 25 } | 26 } |
| 26 }; | 27 }; |
| 27 typedef rtc::scoped_ptr<RingBuffer, FreeBufferDeleter> scoped_ring_buffer; | 28 typedef std::unique_ptr<RingBuffer, FreeBufferDeleter> scoped_ring_buffer; |
| 28 | 29 |
| 29 static void AssertElementEq(int expected, int actual) { | 30 static void AssertElementEq(int expected, int actual) { |
| 30 ASSERT_EQ(expected, actual); | 31 ASSERT_EQ(expected, actual); |
| 31 } | 32 } |
| 32 | 33 |
| 33 static int SetIncrementingData(int* data, int num_elements, | 34 static int SetIncrementingData(int* data, int num_elements, |
| 34 int starting_value) { | 35 int starting_value) { |
| 35 for (int i = 0; i < num_elements; i++) { | 36 for (int i = 0; i < num_elements; i++) { |
| 36 data[i] = starting_value++; | 37 data[i] = starting_value++; |
| 37 } | 38 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 51 static void RandomStressTest(int** data_ptr) { | 52 static void RandomStressTest(int** data_ptr) { |
| 52 const int kNumTests = 10; | 53 const int kNumTests = 10; |
| 53 const int kNumOps = 1000; | 54 const int kNumOps = 1000; |
| 54 const int kMaxBufferSize = 1000; | 55 const int kMaxBufferSize = 1000; |
| 55 | 56 |
| 56 unsigned int seed = time(NULL); | 57 unsigned int seed = time(NULL); |
| 57 printf("seed=%u\n", seed); | 58 printf("seed=%u\n", seed); |
| 58 srand(seed); | 59 srand(seed); |
| 59 for (int i = 0; i < kNumTests; i++) { | 60 for (int i = 0; i < kNumTests; i++) { |
| 60 const int buffer_size = std::max(rand() % kMaxBufferSize, 1); | 61 const int buffer_size = std::max(rand() % kMaxBufferSize, 1); |
| 61 rtc::scoped_ptr<int[]> write_data(new int[buffer_size]); | 62 std::unique_ptr<int[]> write_data(new int[buffer_size]); |
| 62 rtc::scoped_ptr<int[]> read_data(new int[buffer_size]); | 63 std::unique_ptr<int[]> read_data(new int[buffer_size]); |
| 63 scoped_ring_buffer buffer(WebRtc_CreateBuffer(buffer_size, sizeof(int))); | 64 scoped_ring_buffer buffer(WebRtc_CreateBuffer(buffer_size, sizeof(int))); |
| 64 ASSERT_TRUE(buffer.get() != NULL); | 65 ASSERT_TRUE(buffer.get() != NULL); |
| 65 WebRtc_InitBuffer(buffer.get()); | 66 WebRtc_InitBuffer(buffer.get()); |
| 66 int buffer_consumed = 0; | 67 int buffer_consumed = 0; |
| 67 int write_element = 0; | 68 int write_element = 0; |
| 68 int read_element = 0; | 69 int read_element = 0; |
| 69 for (int j = 0; j < kNumOps; j++) { | 70 for (int j = 0; j < kNumOps; j++) { |
| 70 const bool write = rand() % 2 == 0 ? true : false; | 71 const bool write = rand() % 2 == 0 ? true : false; |
| 71 const int num_elements = rand() % buffer_size; | 72 const int num_elements = rand() % buffer_size; |
| 72 if (write) { | 73 if (write) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 141 |
| 141 TEST(RingBufferTest, CreateHandlesErrors) { | 142 TEST(RingBufferTest, CreateHandlesErrors) { |
| 142 EXPECT_TRUE(WebRtc_CreateBuffer(0, 1) == NULL); | 143 EXPECT_TRUE(WebRtc_CreateBuffer(0, 1) == NULL); |
| 143 EXPECT_TRUE(WebRtc_CreateBuffer(1, 0) == NULL); | 144 EXPECT_TRUE(WebRtc_CreateBuffer(1, 0) == NULL); |
| 144 RingBuffer* buffer = WebRtc_CreateBuffer(1, 1); | 145 RingBuffer* buffer = WebRtc_CreateBuffer(1, 1); |
| 145 EXPECT_TRUE(buffer != NULL); | 146 EXPECT_TRUE(buffer != NULL); |
| 146 WebRtc_FreeBuffer(buffer); | 147 WebRtc_FreeBuffer(buffer); |
| 147 } | 148 } |
| 148 | 149 |
| 149 } // namespace webrtc | 150 } // namespace webrtc |
| OLD | NEW |