aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/test_utils.cpp
diff options
context:
space:
mode:
authorPaul Adenot <[email protected]>2016-02-02 18:01:18 +0100
committerPaul Adenot <[email protected]>2016-02-06 06:29:22 +0100
commit1f13325871742e406e8b15deaff10d256fa1cd29 (patch)
tree97a106b94ce49786a63bce79ed6dedb402e2a13d /test/test_utils.cpp
parentc438f775a69cdc8ba6d7d543073c3ccf982050b8 (diff)
downloadcubeb-1f13325871742e406e8b15deaff10d256fa1cd29.tar.gz
cubeb-1f13325871742e406e8b15deaff10d256fa1cd29.zip
Add an auto pointer and an auto array class
The auto pointer frees in the dtor. The auto array has a number of feature that are useful to work with real-time streaming audio: push back and pop front (because we're dealing with temporal data), insert silence, auto resize, no compaction when it's resized down, bound check, direct internal access to work nicely with other APIs.
Diffstat (limited to 'test/test_utils.cpp')
-rw-r--r--test/test_utils.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/test/test_utils.cpp b/test/test_utils.cpp
new file mode 100644
index 0000000..1a35ce0
--- /dev/null
+++ b/test/test_utils.cpp
@@ -0,0 +1,80 @@
+#include <cassert>
+#include "cubeb_utils.h"
+
+int test_auto_array()
+{
+ auto_array<uint32_t> array;
+ auto_array<uint32_t> array2(10);
+ uint32_t a[10];
+
+ assert(array2.length() == 0);
+ assert(array2.capacity() == 10);
+
+
+ for (uint32_t i = 0; i < 10; i++) {
+ a[i] = i;
+ }
+
+ assert(array.capacity() == 0);
+ assert(array.length() == 0);
+
+ array.push(a, 10);
+
+ assert(!array.resize(9));
+
+ for (uint32_t i = 0; i < 10; i++) {
+ assert(array.data()[i] == i);
+ }
+
+ assert(array.capacity() == 10);
+ assert(array.length() == 10);
+
+ uint32_t b[10];
+
+ array.pop(b, 5);
+
+ assert(array.capacity() == 10);
+ assert(array.length() == 5);
+ for (uint32_t i = 0; i < 5; i++) {
+ assert(b[i] == i);
+ assert(array.data()[i] == 5 + i);
+ }
+ uint32_t* bb = b + 5;
+ array.pop(bb, 5);
+
+ assert(array.capacity() == 10);
+ assert(array.length() == 0);
+ for (uint32_t i = 0; i < 5; i++) {
+ assert(bb[i] == 5 + i);
+ }
+
+ assert(!array.pop(nullptr, 1));
+
+ array.push(a, 10);
+ array.push(a, 10);
+
+ for (uint32_t j = 0; j < 2; j++) {
+ for (uint32_t i = 0; i < 10; i++) {
+ assert(array.data()[10 * j + i] == i);
+ }
+ }
+ assert(array.length() == 20);
+ assert(array.capacity() == 20);
+ array.pop(nullptr, 5);
+
+ for (uint32_t i = 0; i < 5; i++) {
+ assert(array.data()[i] == 5 + i);
+ }
+
+ assert(array.length() == 15);
+ assert(array.capacity() == 20);
+
+ return 0;
+}
+
+
+int main()
+{
+ test_auto_array();
+ return 0;
+}