diff options
author | David Robillard <d@drobilla.net> | 2021-01-02 14:46:29 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2021-01-02 14:46:29 +0100 |
commit | bd0214b1da66225f410641692e89e492f668472a (patch) | |
tree | 80956f373070734dc14a7f103e5fa21a5baa0b6e /test/ringbuffer_test.cpp | |
parent | 5c263125aeae87dcd4694a4d3a4781bda7247a00 (diff) | |
download | raul-bd0214b1da66225f410641692e89e492f668472a.tar.gz raul-bd0214b1da66225f410641692e89e492f668472a.tar.bz2 raul-bd0214b1da66225f410641692e89e492f668472a.zip |
Format all code with clang-format
Diffstat (limited to 'test/ringbuffer_test.cpp')
-rw-r--r-- | test/ringbuffer_test.cpp | 167 |
1 files changed, 85 insertions, 82 deletions
diff --git a/test/ringbuffer_test.cpp b/test/ringbuffer_test.cpp index 2a83912..6bf32ce 100644 --- a/test/ringbuffer_test.cpp +++ b/test/ringbuffer_test.cpp @@ -34,66 +34,67 @@ namespace { using RingBuffer = Raul::RingBuffer; struct Context { - std::unique_ptr<RingBuffer> ring{}; - size_t n_writes{0}; + std::unique_ptr<RingBuffer> ring{}; + size_t n_writes{0}; }; int gen_msg(int* msg, int start) { - for (unsigned i = 0u; i < MSG_SIZE; ++i) { - msg[i] = start; - start = (start + 1) % INT_MAX; - } - return start; + for (unsigned i = 0u; i < MSG_SIZE; ++i) { + msg[i] = start; + start = (start + 1) % INT_MAX; + } + return start; } void check_msg(const int* msg1, const int* msg2) { - for (unsigned i = 0u; i < MSG_SIZE; ++i) { - assert(msg1[i] == msg2[i]); - } + for (unsigned i = 0u; i < MSG_SIZE; ++i) { + assert(msg1[i] == msg2[i]); + } } void reader(Context& ctx) { - printf("Reader starting\n"); - - int ref_msg[MSG_SIZE]; // Reference generated for comparison - int read_msg[MSG_SIZE]; // Read from ring - size_t count = 0; - int start = gen_msg(ref_msg, 0); - for (size_t i = 0; i < ctx.n_writes; ++i) { - if (ctx.ring->read_space() >= MSG_SIZE * sizeof(int)) { - const uint32_t n_read = ctx.ring->read(MSG_SIZE * sizeof(int), read_msg); - assert(n_read == MSG_SIZE * sizeof(int)); - check_msg(ref_msg, read_msg); - start = gen_msg(ref_msg, start); - ++count; - } - } - - printf("Reader finished\n"); + printf("Reader starting\n"); + + int ref_msg[MSG_SIZE]; // Reference generated for comparison + int read_msg[MSG_SIZE]; // Read from ring + size_t count = 0; + int start = gen_msg(ref_msg, 0); + for (size_t i = 0; i < ctx.n_writes; ++i) { + if (ctx.ring->read_space() >= MSG_SIZE * sizeof(int)) { + const uint32_t n_read = ctx.ring->read(MSG_SIZE * sizeof(int), read_msg); + assert(n_read == MSG_SIZE * sizeof(int)); + check_msg(ref_msg, read_msg); + start = gen_msg(ref_msg, start); + ++count; + } + } + + printf("Reader finished\n"); } void writer(Context& ctx) { - printf("Writer starting\n"); - - int write_msg[MSG_SIZE]; // Written to ring - int start = gen_msg(write_msg, 0); - for (size_t i = 0; i < ctx.n_writes; ++i) { - if (ctx.ring->write_space() >= MSG_SIZE * sizeof(int)) { - const uint32_t n_write = ctx.ring->write(MSG_SIZE * sizeof(int), write_msg); - assert(n_write == MSG_SIZE * sizeof(int)); - start = gen_msg(write_msg, start); - } - } - - printf("Writer finished\n"); + printf("Writer starting\n"); + + int write_msg[MSG_SIZE]; // Written to ring + int start = gen_msg(write_msg, 0); + for (size_t i = 0; i < ctx.n_writes; ++i) { + if (ctx.ring->write_space() >= MSG_SIZE * sizeof(int)) { + const uint32_t n_write = + ctx.ring->write(MSG_SIZE * sizeof(int), write_msg); + assert(n_write == MSG_SIZE * sizeof(int)); + start = gen_msg(write_msg, start); + } + } + + printf("Writer finished\n"); } } // namespace @@ -101,62 +102,64 @@ writer(Context& ctx) int main(int argc, char** argv) { - if (argc > 1 && argv[1][0] == '-') { - printf("Usage: %s SIZE N_WRITES\n", argv[0]); - return 1; - } + if (argc > 1 && argv[1][0] == '-') { + printf("Usage: %s SIZE N_WRITES\n", argv[0]); + return 1; + } - Context ctx; + Context ctx; - uint32_t size = 512u; - if (argc > 1) { - size = static_cast<uint32_t>(std::stoi(argv[1])); - } + uint32_t size = 512u; + if (argc > 1) { + size = static_cast<uint32_t>(std::stoi(argv[1])); + } - ctx.n_writes = size * 1024u; - if (argc > 2) { - ctx.n_writes = std::stoul(argv[2]); - } + ctx.n_writes = size * 1024u; + if (argc > 2) { + ctx.n_writes = std::stoul(argv[2]); + } - printf("Testing %zu writes of %u ints to a %u int ring...\n", - ctx.n_writes, MSG_SIZE, size); + printf("Testing %zu writes of %u ints to a %u int ring...\n", + ctx.n_writes, + MSG_SIZE, + size); - ctx.ring = std::unique_ptr<RingBuffer>(new RingBuffer(size)); + ctx.ring = std::unique_ptr<RingBuffer>(new RingBuffer(size)); - auto& ring = ctx.ring; - assert(ring->capacity() >= size - 1); + auto& ring = ctx.ring; + assert(ring->capacity() >= size - 1); - assert(!ring->skip(1)); + assert(!ring->skip(1)); - char buf[6] = { 'h', 'e', 'l', 'l', '0', '\0' }; - assert(!ring->read(1, buf)); + char buf[6] = {'h', 'e', 'l', 'l', '0', '\0'}; + assert(!ring->read(1, buf)); - ring->write(sizeof(buf), buf); - ring->skip(1); - char buf2[sizeof(buf) - 1]; - ring->read(sizeof(buf2), buf2); - assert(!strcmp(buf2, buf + 1)); + ring->write(sizeof(buf), buf); + ring->skip(1); + char buf2[sizeof(buf) - 1]; + ring->read(sizeof(buf2), buf2); + assert(!strcmp(buf2, buf + 1)); - ring->reset(); - assert(ring->read_space() == 0); + ring->reset(); + assert(ring->read_space() == 0); - for (uint32_t i = 0; i < ring->capacity(); ++i) { - const char c = 'X'; - assert(ring->write(1, &c) == 1); - } + for (uint32_t i = 0; i < ring->capacity(); ++i) { + const char c = 'X'; + assert(ring->write(1, &c) == 1); + } - assert(ring->write_space() == 0); - assert(ring->write(1, buf) == 0); - assert(ring->peek(1, buf2) == 1); - assert(buf2[0] == 'X'); + assert(ring->write_space() == 0); + assert(ring->write(1, buf) == 0); + assert(ring->peek(1, buf2) == 1); + assert(buf2[0] == 'X'); - ring->reset(); + ring->reset(); - std::thread reader_thread(reader, std::ref(ctx)); - std::thread writer_thread(writer, std::ref(ctx)); + std::thread reader_thread(reader, std::ref(ctx)); + std::thread writer_thread(writer, std::ref(ctx)); - reader_thread.join(); - writer_thread.join(); + reader_thread.join(); + writer_thread.join(); - return 0; + return 0; } |