Fix last commit
[carla.git] / source / tests.old / CarlaRingBuffer.cpp
blob5b5f8f210e0af1ed6f6d02da794906a23e64d517
1 /*
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 // -----------------------------------------------------------------------
21 // simple types
23 template <class BufferStruct>
24 static void test_CarlaRingBuffer1(CarlaRingBuffer<BufferStruct>& b) noexcept
26 // start empty
27 assert(b.isEmpty());
29 // write all types
30 b.writeBool(true);
31 b.writeByte(123);
32 b.writeShort(1234);
33 b.writeInt(99999123);
34 b.writeLong(0xffffffff);
35 b.writeFloat(0.0123f);
36 b.writeDouble(0.0123);
38 // should be considered empty until a commitWrite
39 assert(b.isEmpty());
41 // commit
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);
55 // still has data
56 assert(! b.isEmpty());
58 assert(b.readDouble() == 0.0123);
60 // now empty
61 assert(b.isEmpty());
64 // -----------------------------------------------------------------------
65 // custom type
67 struct BufferTestStruct {
68 BufferTestStruct()
69 : b(false), i(255), l(9999) {}
71 bool b;
72 int32_t i;
73 char _pad[999];
74 int64_t l;
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
85 // start empty
86 assert(b.isEmpty());
88 // write unmodified
89 BufferTestStruct t1, t2;
90 assert(t1 == t2);
91 b.writeCustomType(t1);
92 assert(b.commitWrite());
94 // test read
95 b.readCustomType(t1);
96 assert(t1 == t2);
98 // modify t1
99 b.writeCustomType(t1);
100 assert(b.commitWrite());
101 carla_zeroStruct(t1);
103 // test read
104 b.readCustomType(t1);
105 assert(t1 == t2);
107 // now empty
108 assert(b.isEmpty());
111 // -----------------------------------------------------------------------
112 // custom data
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"
122 "\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"
127 "\n"
128 "For a full copy of the GNU General Public License see the doc/GPL.txt file.\n"
129 "\n";
131 static const std::size_t kLicenseSize = std::strlen(kLicense);
133 // start empty
134 assert(b.isEmpty());
136 // write big chunk
137 b.writeCustomData(kLicense, kLicenseSize);
139 // still empty
140 assert(b.isEmpty());
142 // commit
143 assert(b.commitWrite());
145 // not empty
146 assert(! b.isEmpty());
148 // read data
149 char license[kLicenseSize+1];
150 carla_zeroChars(license, kLicenseSize+1);
151 b.readCustomData(license, kLicenseSize);
153 // now empty again
154 assert(b.isEmpty());
156 // test data integrity
157 assert(std::strcmp(license, kLicense) == 0);
160 // -----------------------------------------------------------------------
162 int main()
164 CarlaHeapRingBuffer heap;
165 CarlaStackRingBuffer stack;
167 // small test first
168 heap.createBuffer(4096);
169 heap.deleteBuffer();
170 heap.createBuffer(1);
171 heap.deleteBuffer();
173 // ok
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);
188 return 0;
191 // -----------------------------------------------------------------------