tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_ring_array.cpp (2297B)


      1 #include "gtest/gtest.h"
      2 #ifdef __APPLE__
      3 #include "cubeb/cubeb.h"
      4 #include "cubeb_ring_array.h"
      5 #include <CoreAudio/CoreAudioTypes.h>
      6 #include <iostream>
      7 #include <string.h>
      8 
      9 TEST(cubeb, ring_array)
     10 {
     11  ring_array ra;
     12 
     13  ASSERT_EQ(ring_array_init(&ra, 0, 0, 1, 1), CUBEB_ERROR_INVALID_PARAMETER);
     14  ASSERT_EQ(ring_array_init(&ra, 1, 0, 0, 1), CUBEB_ERROR_INVALID_PARAMETER);
     15 
     16  unsigned int capacity = 8;
     17  ring_array_init(&ra, capacity, sizeof(int), 1, 1);
     18  int verify_data[capacity]; // {1,2,3,4,5,6,7,8};
     19  AudioBuffer * p_data = NULL;
     20 
     21  for (unsigned int i = 0; i < capacity; ++i) {
     22    verify_data[i] = i; // in case capacity change value
     23    *(int *)ra.buffer_array[i].mData = i;
     24    ASSERT_EQ(ra.buffer_array[i].mDataByteSize, sizeof(int));
     25    ASSERT_EQ(ra.buffer_array[i].mNumberChannels, 1u);
     26  }
     27 
     28  /* Get store buffers*/
     29  for (unsigned int i = 0; i < capacity; ++i) {
     30    p_data = ring_array_get_free_buffer(&ra);
     31    ASSERT_NE(p_data, nullptr);
     32    ASSERT_EQ(*(int *)p_data->mData, verify_data[i]);
     33  }
     34  /*Now array is full extra store should give NULL*/
     35  ASSERT_EQ(ring_array_get_free_buffer(&ra), nullptr);
     36  /* Get fetch buffers*/
     37  for (unsigned int i = 0; i < capacity; ++i) {
     38    p_data = ring_array_get_data_buffer(&ra);
     39    ASSERT_NE(p_data, nullptr);
     40    ASSERT_EQ(*(int *)p_data->mData, verify_data[i]);
     41  }
     42  /*Now array is empty extra fetch should give NULL*/
     43  ASSERT_EQ(ring_array_get_data_buffer(&ra), nullptr);
     44 
     45  p_data = NULL;
     46  /* Repeated store fetch should can go for ever*/
     47  for (unsigned int i = 0; i < 2 * capacity; ++i) {
     48    p_data = ring_array_get_free_buffer(&ra);
     49    ASSERT_NE(p_data, nullptr);
     50    ASSERT_EQ(ring_array_get_data_buffer(&ra), p_data);
     51  }
     52 
     53  p_data = NULL;
     54  /* Verify/modify buffer data*/
     55  for (unsigned int i = 0; i < capacity; ++i) {
     56    p_data = ring_array_get_free_buffer(&ra);
     57    ASSERT_NE(p_data, nullptr);
     58    ASSERT_EQ(*((int *)p_data->mData), verify_data[i]);
     59    (*((int *)p_data->mData))++; // Modify data
     60  }
     61  for (unsigned int i = 0; i < capacity; ++i) {
     62    p_data = ring_array_get_data_buffer(&ra);
     63    ASSERT_NE(p_data, nullptr);
     64    ASSERT_EQ(*((int *)p_data->mData),
     65              verify_data[i] + 1); // Verify modified data
     66  }
     67 
     68  ring_array_destroy(&ra);
     69 }
     70 #else
     71 TEST(cubeb, DISABLED_ring_array) {}
     72 #endif