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 
11 #include "webrtc/common_audio/ring_buffer.h"
12 
13 #include <stdlib.h>
14 #include <time.h>
15 #include <algorithm>
16 
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webrtc/base/scoped_ptr.h"
19 
20 namespace webrtc {
21 
22 struct FreeBufferDeleter {
operator ()webrtc::FreeBufferDeleter23   inline void operator()(void* ptr) const {
24     WebRtc_FreeBuffer(ptr);
25   }
26 };
27 typedef rtc::scoped_ptr<RingBuffer, FreeBufferDeleter> scoped_ring_buffer;
28 
AssertElementEq(int expected,int actual)29 static void AssertElementEq(int expected, int actual) {
30   ASSERT_EQ(expected, actual);
31 }
32 
SetIncrementingData(int * data,int num_elements,int starting_value)33 static int SetIncrementingData(int* data, int num_elements,
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 
CheckIncrementingData(int * data,int num_elements,int starting_value)41 static int CheckIncrementingData(int* data, int num_elements,
42                                  int starting_value) {
43   for (int i = 0; i < num_elements; i++) {
44     AssertElementEq(starting_value++, data[i]);
45   }
46   return starting_value;
47 }
48 
49 // We use ASSERTs in this test to avoid obscuring the seed in the case of a
50 // failure.
RandomStressTest(int ** data_ptr)51 static void RandomStressTest(int** data_ptr) {
52   const int kNumTests = 10;
53   const int kNumOps = 1000;
54   const int kMaxBufferSize = 1000;
55 
56   unsigned int seed = time(NULL);
57   printf("seed=%u\n", seed);
58   srand(seed);
59   for (int i = 0; i < kNumTests; i++) {
60     const int buffer_size = std::max(rand() % kMaxBufferSize, 1);
61     rtc::scoped_ptr<int[]> write_data(new int[buffer_size]);
62     rtc::scoped_ptr<int[]> read_data(new int[buffer_size]);
63     scoped_ring_buffer buffer(WebRtc_CreateBuffer(buffer_size, sizeof(int)));
64     ASSERT_TRUE(buffer.get() != NULL);
65     WebRtc_InitBuffer(buffer.get());
66     int buffer_consumed = 0;
67     int write_element = 0;
68     int read_element = 0;
69     for (int j = 0; j < kNumOps; j++) {
70       const bool write = rand() % 2 == 0 ? true : false;
71       const int num_elements = rand() % buffer_size;
72       if (write) {
73         const int buffer_available = buffer_size - buffer_consumed;
74         ASSERT_EQ(static_cast<size_t>(buffer_available),
75                   WebRtc_available_write(buffer.get()));
76         const int expected_elements = std::min(num_elements, buffer_available);
77         write_element = SetIncrementingData(write_data.get(), expected_elements,
78                                      write_element);
79         ASSERT_EQ(static_cast<size_t>(expected_elements),
80                   WebRtc_WriteBuffer(buffer.get(), write_data.get(),
81                                      num_elements));
82         buffer_consumed = std::min(buffer_consumed + expected_elements,
83                                    buffer_size);
84       } else {
85         const int expected_elements = std::min(num_elements,
86                                                buffer_consumed);
87         ASSERT_EQ(static_cast<size_t>(buffer_consumed),
88                   WebRtc_available_read(buffer.get()));
89         ASSERT_EQ(static_cast<size_t>(expected_elements),
90                   WebRtc_ReadBuffer(buffer.get(),
91                                     reinterpret_cast<void**>(data_ptr),
92                                     read_data.get(),
93                                     num_elements));
94         int* check_ptr = read_data.get();
95         if (data_ptr) {
96           check_ptr = *data_ptr;
97         }
98         read_element = CheckIncrementingData(check_ptr, expected_elements,
99                                              read_element);
100         buffer_consumed = std::max(buffer_consumed - expected_elements, 0);
101       }
102     }
103   }
104 }
105 
TEST(RingBufferTest,RandomStressTest)106 TEST(RingBufferTest, RandomStressTest) {
107   int* data_ptr = NULL;
108   RandomStressTest(&data_ptr);
109 }
110 
TEST(RingBufferTest,RandomStressTestWithNullPtr)111 TEST(RingBufferTest, RandomStressTestWithNullPtr) {
112   RandomStressTest(NULL);
113 }
114 
TEST(RingBufferTest,PassingNulltoReadBufferForcesMemcpy)115 TEST(RingBufferTest, PassingNulltoReadBufferForcesMemcpy) {
116   const size_t kDataSize = 2;
117   int write_data[kDataSize];
118   int read_data[kDataSize];
119   int* data_ptr;
120 
121   scoped_ring_buffer buffer(WebRtc_CreateBuffer(kDataSize, sizeof(int)));
122   ASSERT_TRUE(buffer.get() != NULL);
123   WebRtc_InitBuffer(buffer.get());
124 
125   SetIncrementingData(write_data, kDataSize, 0);
126   EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize));
127   SetIncrementingData(read_data, kDataSize, kDataSize);
128   EXPECT_EQ(kDataSize, WebRtc_ReadBuffer(buffer.get(),
129       reinterpret_cast<void**>(&data_ptr), read_data, kDataSize));
130   // Copying was not necessary, so |read_data| has not been updated.
131   CheckIncrementingData(data_ptr, kDataSize, 0);
132   CheckIncrementingData(read_data, kDataSize, kDataSize);
133 
134   EXPECT_EQ(kDataSize, WebRtc_WriteBuffer(buffer.get(), write_data, kDataSize));
135   EXPECT_EQ(kDataSize, WebRtc_ReadBuffer(buffer.get(), NULL, read_data,
136                                          kDataSize));
137   // Passing NULL forces a memcpy, so |read_data| is now updated.
138   CheckIncrementingData(read_data, kDataSize, 0);
139 }
140 
TEST(RingBufferTest,CreateHandlesErrors)141 TEST(RingBufferTest, CreateHandlesErrors) {
142   EXPECT_TRUE(WebRtc_CreateBuffer(0, 1) == NULL);
143   EXPECT_TRUE(WebRtc_CreateBuffer(1, 0) == NULL);
144   RingBuffer* buffer = WebRtc_CreateBuffer(1, 1);
145   EXPECT_TRUE(buffer != NULL);
146   WebRtc_FreeBuffer(buffer);
147 }
148 
149 }  // namespace webrtc
150