| 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 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 return starting_value; | 47 return starting_value; |
| 48 } | 48 } |
| 49 | 49 |
| 50 // We use ASSERTs in this test to avoid obscuring the seed in the case of a | 50 // We use ASSERTs in this test to avoid obscuring the seed in the case of a |
| 51 // failure. | 51 // failure. |
| 52 static void RandomStressTest(int** data_ptr) { | 52 static void RandomStressTest(int** data_ptr) { |
| 53 const int kNumTests = 10; | 53 const int kNumTests = 10; |
| 54 const int kNumOps = 1000; | 54 const int kNumOps = 1000; |
| 55 const int kMaxBufferSize = 1000; | 55 const int kMaxBufferSize = 1000; |
| 56 | 56 |
| 57 unsigned int seed = time(NULL); | 57 unsigned int seed = time(nullptr); |
| 58 printf("seed=%u\n", seed); | 58 printf("seed=%u\n", seed); |
| 59 srand(seed); | 59 srand(seed); |
| 60 for (int i = 0; i < kNumTests; i++) { | 60 for (int i = 0; i < kNumTests; i++) { |
| 61 const int buffer_size = std::max(rand() % kMaxBufferSize, 1); | 61 const int buffer_size = std::max(rand() % kMaxBufferSize, 1); |
| 62 std::unique_ptr<int[]> write_data(new int[buffer_size]); | 62 std::unique_ptr<int[]> write_data(new int[buffer_size]); |
| 63 std::unique_ptr<int[]> read_data(new int[buffer_size]); | 63 std::unique_ptr<int[]> read_data(new int[buffer_size]); |
| 64 scoped_ring_buffer buffer(WebRtc_CreateBuffer(buffer_size, sizeof(int))); | 64 scoped_ring_buffer buffer(WebRtc_CreateBuffer(buffer_size, sizeof(int))); |
| 65 ASSERT_TRUE(buffer.get() != NULL); | 65 ASSERT_TRUE(buffer.get() != nullptr); |
| 66 WebRtc_InitBuffer(buffer.get()); | 66 WebRtc_InitBuffer(buffer.get()); |
| 67 int buffer_consumed = 0; | 67 int buffer_consumed = 0; |
| 68 int write_element = 0; | 68 int write_element = 0; |
| 69 int read_element = 0; | 69 int read_element = 0; |
| 70 for (int j = 0; j < kNumOps; j++) { | 70 for (int j = 0; j < kNumOps; j++) { |
| 71 const bool write = rand() % 2 == 0 ? true : false; | 71 const bool write = rand() % 2 == 0 ? true : false; |
| 72 const int num_elements = rand() % buffer_size; | 72 const int num_elements = rand() % buffer_size; |
| 73 if (write) { | 73 if (write) { |
| 74 const int buffer_available = buffer_size - buffer_consumed; | 74 const int buffer_available = buffer_size - buffer_consumed; |
| 75 ASSERT_EQ(static_cast<size_t>(buffer_available), | 75 ASSERT_EQ(static_cast<size_t>(buffer_available), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 98 } | 98 } |
| 99 read_element = CheckIncrementingData(check_ptr, expected_elements, | 99 read_element = CheckIncrementingData(check_ptr, expected_elements, |
| 100 read_element); | 100 read_element); |
| 101 buffer_consumed = std::max(buffer_consumed - expected_elements, 0); | 101 buffer_consumed = std::max(buffer_consumed - expected_elements, 0); |
| 102 } | 102 } |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 } | 105 } |
| 106 | 106 |
| 107 TEST(RingBufferTest, RandomStressTest) { | 107 TEST(RingBufferTest, RandomStressTest) { |
| 108 int* data_ptr = NULL; | 108 int* data_ptr = nullptr; |
| 109 RandomStressTest(&data_ptr); | 109 RandomStressTest(&data_ptr); |
| 110 } | 110 } |
| 111 | 111 |
| 112 TEST(RingBufferTest, RandomStressTestWithNullPtr) { | 112 TEST(RingBufferTest, RandomStressTestWithNullPtr) { |
| 113 RandomStressTest(NULL); | 113 RandomStressTest(nullptr); |
| 114 } | 114 } |
| 115 | 115 |
| 116 TEST(RingBufferTest, PassingNulltoReadBufferForcesMemcpy) { | 116 TEST(RingBufferTest, PassingNulltoReadBufferForcesMemcpy) { |
| 117 const size_t kDataSize = 2; | 117 const size_t kDataSize = 2; |
| 118 int write_data[kDataSize]; | 118 int write_data[kDataSize]; |
| 119 int read_data[kDataSize]; | 119 int read_data[kDataSize]; |
| 120 int* data_ptr; | 120 int* data_ptr; |
| 121 | 121 |
| 122 scoped_ring_buffer buffer(WebRtc_CreateBuffer(kDataSize, sizeof(int))); | 122 scoped_ring_buffer buffer(WebRtc_CreateBuffer(kDataSize, sizeof(int))); |
| 123 ASSERT_TRUE(buffer.get() != NULL); | 123 ASSERT_TRUE(buffer.get() != nullptr); |
| 124 WebRtc_InitBuffer(buffer.get()); | 124 WebRtc_InitBuffer(buffer.get()); |
| 125 | 125 |
| 126 SetIncrementingData(write_data, kDataSize, 0); | 126 SetIncrementingData(write_data, kDataSize, 0); |
| 127 EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize)); | 127 EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize)); |
| 128 SetIncrementingData(read_data, kDataSize, kDataSize); | 128 SetIncrementingData(read_data, kDataSize, kDataSize); |
| 129 EXPECT_EQ(kDataSize, WebRtc_ReadBuffer(buffer.get(), | 129 EXPECT_EQ(kDataSize, WebRtc_ReadBuffer(buffer.get(), |
| 130 reinterpret_cast<void**>(&data_ptr), read_data, kDataSize)); | 130 reinterpret_cast<void**>(&data_ptr), read_data, kDataSize)); |
| 131 // Copying was not necessary, so |read_data| has not been updated. | 131 // Copying was not necessary, so |read_data| has not been updated. |
| 132 CheckIncrementingData(data_ptr, kDataSize, 0); | 132 CheckIncrementingData(data_ptr, kDataSize, 0); |
| 133 CheckIncrementingData(read_data, kDataSize, kDataSize); | 133 CheckIncrementingData(read_data, kDataSize, kDataSize); |
| 134 | 134 |
| 135 EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize)); | 135 EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize)); |
| 136 EXPECT_EQ(kDataSize, WebRtc_ReadBuffer(buffer.get(), NULL, read_data, | 136 EXPECT_EQ(kDataSize, |
| 137 kDataSize)); | 137 WebRtc_ReadBuffer(buffer.get(), nullptr, read_data, kDataSize)); |
| 138 // Passing NULL forces a memcpy, so |read_data| is now updated. | 138 // Passing null forces a memcpy, so |read_data| is now updated. |
| 139 CheckIncrementingData(read_data, kDataSize, 0); | 139 CheckIncrementingData(read_data, kDataSize, 0); |
| 140 } | 140 } |
| 141 | 141 |
| 142 TEST(RingBufferTest, CreateHandlesErrors) { | 142 TEST(RingBufferTest, CreateHandlesErrors) { |
| 143 EXPECT_TRUE(WebRtc_CreateBuffer(0, 1) == NULL); | 143 EXPECT_TRUE(WebRtc_CreateBuffer(0, 1) == nullptr); |
| 144 EXPECT_TRUE(WebRtc_CreateBuffer(1, 0) == NULL); | 144 EXPECT_TRUE(WebRtc_CreateBuffer(1, 0) == nullptr); |
| 145 RingBuffer* buffer = WebRtc_CreateBuffer(1, 1); | 145 RingBuffer* buffer = WebRtc_CreateBuffer(1, 1); |
| 146 EXPECT_TRUE(buffer != NULL); | 146 EXPECT_TRUE(buffer != nullptr); |
| 147 WebRtc_FreeBuffer(buffer); | 147 WebRtc_FreeBuffer(buffer); |
| 148 } | 148 } |
| 149 | 149 |
| 150 } // namespace webrtc | 150 } // namespace webrtc |
| OLD | NEW |