2 * CarlaRingBuffer Tests
3 * Copyright (C) 2014 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaRingBuffer.hpp"
20 // -----------------------------------------------------------------------
23 template <class BufferStruct
>
24 static void test_CarlaRingBuffer1(CarlaRingBuffer
<BufferStruct
>& b
) noexcept
34 b
.writeLong(0xffffffff);
35 b
.writeFloat(0.0123f
);
36 b
.writeDouble(0.0123);
38 // should be considered empty until a commitWrite
42 assert(b
.commitWrite());
44 // should have data now
45 assert(! b
.isEmpty());
47 // read all types back
48 assert(b
.readBool() == true);
49 assert(b
.readByte() == 123);
50 assert(b
.readShort() == 1234);
51 assert(b
.readInt() == 99999123);
52 assert(b
.readLong() == 0xffffffff);
53 assert(b
.readFloat() == 0.0123f
);
56 assert(! b
.isEmpty());
58 assert(b
.readDouble() == 0.0123);
64 // -----------------------------------------------------------------------
67 struct BufferTestStruct
{
69 : b(false), i(255), l(9999) {}
76 bool operator==(const BufferTestStruct
& s
) const noexcept
78 return (b
== s
.b
&& i
== s
.i
&& l
== s
.l
);
82 template <class BufferStruct
>
83 static void test_CarlaRingBuffer2(CarlaRingBuffer
<BufferStruct
>& b
) noexcept
89 BufferTestStruct t1
, t2
;
91 b
.writeCustomType(t1
);
92 assert(b
.commitWrite());
99 b
.writeCustomType(t1
);
100 assert(b
.commitWrite());
101 carla_zeroStruct(t1
);
104 b
.readCustomType(t1
);
111 // -----------------------------------------------------------------------
114 template <class BufferStruct
>
115 static void test_CarlaRingBuffer3(CarlaRingBuffer
<BufferStruct
>& b
) noexcept
117 static const char* const kLicense
= ""
118 "This program is free software; you can redistribute it and/or\n"
119 "modify it under the terms of the GNU General Public License as\n"
120 "published by the Free Software Foundation; either version 2 of\n"
121 "the License, or any later version.\n"
123 "This program is distributed in the hope that it will be useful,\n"
124 "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
125 "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
126 "GNU General Public License for more details.\n"
128 "For a full copy of the GNU General Public License see the doc/GPL.txt file.\n"
131 static const std::size_t kLicenseSize
= std::strlen(kLicense
);
137 b
.writeCustomData(kLicense
, kLicenseSize
);
143 assert(b
.commitWrite());
146 assert(! b
.isEmpty());
149 char license
[kLicenseSize
+1];
150 carla_zeroChars(license
, kLicenseSize
+1);
151 b
.readCustomData(license
, kLicenseSize
);
156 // test data integrity
157 assert(std::strcmp(license
, kLicense
) == 0);
160 // -----------------------------------------------------------------------
164 CarlaHeapRingBuffer heap
;
165 CarlaStackRingBuffer stack
;
168 heap
.createBuffer(4096);
170 heap
.createBuffer(1);
174 heap
.createBuffer(1024);
176 test_CarlaRingBuffer1(heap
);
177 test_CarlaRingBuffer1(stack
);
178 test_CarlaRingBuffer2(heap
);
179 test_CarlaRingBuffer2(stack
);
181 // test the big chunk a couple of times to ensure circular writing
182 for (int i
=0; i
<20; ++i
)
184 test_CarlaRingBuffer3(heap
);
185 test_CarlaRingBuffer3(stack
);
191 // -----------------------------------------------------------------------