diff options
author | David Robillard <d@drobilla.net> | 2019-04-22 11:50:34 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-04-22 11:50:34 +0200 |
commit | 40d2067dc44c9d46fc1f83c41fe4187a70f23bce (patch) | |
tree | ffcd296efa4832e7127013e60e36ece31389fe26 | |
parent | be6c2693ec741650a0684fa8a827ab2a5cd7b8f7 (diff) | |
download | raul-40d2067dc44c9d46fc1f83c41fe4187a70f23bce.tar.gz raul-40d2067dc44c9d46fc1f83c41fe4187a70f23bce.tar.bz2 raul-40d2067dc44c9d46fc1f83c41fe4187a70f23bce.zip |
Simplify unit tests and improve coverage by using assert
-rw-r--r-- | test/array_test.cpp | 50 | ||||
-rw-r--r-- | test/double_buffer_test.cpp | 18 | ||||
-rw-r--r-- | test/path_test.cpp | 103 | ||||
-rw-r--r-- | test/ringbuffer_test.cpp | 88 | ||||
-rw-r--r-- | test/socket_test.cpp | 42 | ||||
-rw-r--r-- | test/symbol_test.cpp | 19 | ||||
-rw-r--r-- | test/thread_test.cpp | 15 |
7 files changed, 119 insertions, 216 deletions
diff --git a/test/array_test.cpp b/test/array_test.cpp index e401fc8..014b062 100644 --- a/test/array_test.cpp +++ b/test/array_test.cpp @@ -1,6 +1,6 @@ /* This file is part of Raul. - Copyright 2007-2016 David Robillard <http://drobilla.net> + Copyright 2007-2019 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -14,8 +14,11 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ +#undef NDEBUG + #include "raul/Array.hpp" +#include <cassert> #include <cstdio> int @@ -24,51 +27,26 @@ main() Raul::Array<int> array1(32, 2); array1[0] = 42; - if (array1[0] != 42) { - fprintf(stderr, "array1[0] != 42\n"); - return 1; - } else if (array1[1] != 2) { - fprintf(stderr, "array1[1] != 2\n"); - return 1; - } else if (array1.size() != 32) { - fprintf(stderr, "array1.size() != 1\n"); - return 1; - } + assert(array1[0] == 42); + assert(array1[1] == 2); + assert(array1.size() == 32); array1.alloc(16, 0); - if (array1[0] != 0) { - fprintf(stderr, "array1[0] != 0\n"); - return 1; - } else if (array1.at(0) != 0) { - fprintf(stderr, "array1.at(0) != 0\n"); - return 1; - } else if (array1.size() != 16) { - fprintf(stderr, "array1.size() != 16\n"); - return 1; - } + assert(array1[0] == 0); + assert(array1.at(0) == 0); + assert(array1.size() == 16); array1.alloc(8, 0); - if (array1.size() != 8) { - fprintf(stderr, "array1.size() != 8\n"); - return 1; - } + assert(array1.size() == 8); Raul::Array<int> array2(array1); for (size_t i = 0; i < array1.size(); ++i) { - if (array2[i] != array1[i]) { - fprintf(stderr, "Mismatch at %zu\n", i); - return 1; - } + assert(array2[i] == array1[i]); } Raul::Array<int> array3(8, 47); - if (array3[0] != 47) { - fprintf(stderr, "array3[0] != 47\n"); - return 1; - } else if (array3.size() != 8) { - fprintf(stderr, "array3.size() != 8\n"); - return 1; - } + assert(array3[0] == 47); + assert(array3.size() == 8); return 0; } diff --git a/test/double_buffer_test.cpp b/test/double_buffer_test.cpp index 1717d0e..f75f0ae 100644 --- a/test/double_buffer_test.cpp +++ b/test/double_buffer_test.cpp @@ -1,6 +1,6 @@ /* This file is part of Raul. - Copyright 2013 David Robillard <http://drobilla.net> + Copyright 2013-2019 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -14,26 +14,24 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ +#undef NDEBUG + #include "raul/DoubleBuffer.hpp" +#include <cassert> + int main() { Raul::DoubleBuffer<int> db(0); - if (db.get() != 0) { - return 1; - } + assert(db.get() == 0); db.set(42); - if (db.get() != 42) { - return 1; - } + assert(db.get() == 42); db.set(43); - if (db.get() != 43) { - return 1; - } + assert(db.get() == 43); return 0; } diff --git a/test/path_test.cpp b/test/path_test.cpp index 538a262..6b95c4f 100644 --- a/test/path_test.cpp +++ b/test/path_test.cpp @@ -1,6 +1,6 @@ /* This file is part of Raul. - Copyright 2007-2012 David Robillard <http://drobilla.net> + Copyright 2007-2019 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -14,9 +14,12 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ +#undef NDEBUG + #include "raul/Path.hpp" #include "raul/Symbol.hpp" +#include <cassert> #include <cstring> #include <iostream> #include <string> @@ -27,58 +30,52 @@ main() using Path = Raul::Path; using Symbol = Raul::Symbol; -#define CHECK(cond) \ - do { if (!(cond)) { \ - std::cerr << "Test failed: " << (cond) << std::endl; \ - return 1; \ - } } while (0) - - CHECK(Path("/foo/bar").parent() == Path("/foo")); - CHECK(Path("/foo").parent() == Path("/")); - CHECK(Path("/").parent() == Path("/")); - - CHECK(Path("/").is_parent_of(Path("/foo"))); - CHECK(Path("/foo").is_parent_of(Path("/foo/bar"))); - CHECK(!(Path("/foo").is_parent_of(Path("/foo2")))); - CHECK(!(Path("/foo").is_parent_of(Path("/foo")))); - - CHECK(Path::lca(Path("/foo"), Path("/foo/bar/baz")) == Path("/")); - CHECK(Path::lca(Path("/foo/bar"), Path("/foo/bar/baz")) == Path("/foo")); - CHECK(Path::lca(Path("/foo/bar/quux"), Path("/foo/bar/baz")) == Path("/foo/bar")); - - CHECK(!Path::is_valid("")); - CHECK(!Path::is_valid("hello")); - CHECK(!Path::is_valid("/foo/bar/")); - CHECK(!Path::is_valid("/foo//bar")); - CHECK(!Path::is_valid("/foo/bar/d*s")); - CHECK(Path::is_valid("/")); - CHECK(!Path::is_valid("/foo/3foo/bar")); - - CHECK(Path::descendant_comparator(Path("/"), Path("/"))); - CHECK(Path::descendant_comparator(Path("/"), Path("/foo"))); - CHECK(Path::descendant_comparator(Path("/foo"), Path("/foo/bar"))); - CHECK(Path::descendant_comparator(Path("/foo"), Path("/foo"))); - CHECK(Path::descendant_comparator(Path("/"), Path("/"))); - CHECK(!Path::descendant_comparator(Path("/baz"), Path("/"))); - CHECK(!Path::descendant_comparator(Path("/foo"), Path("/bar"))); - CHECK(!Path::descendant_comparator(Path("/foo/bar"), Path("/foo"))); - - CHECK(!Symbol::is_valid("")); - CHECK(!Symbol::is_valid("/I/have/slashes")); - CHECK(!Symbol::is_valid("!illegalchar")); - CHECK(!Symbol::is_valid("0illegalleadingdigit")); - CHECK(strcmp(Symbol::symbolify("").c_str(), "")); - - CHECK(Path("/foo").child(Symbol("bar")) == "/foo/bar"); - CHECK(Path("/foo").child(Path("/bar/baz")) == "/foo/bar/baz"); - CHECK(Path("/foo").child(Path("/")) == "/foo"); - - CHECK(!strcmp(Path("/foo").symbol(), "foo")); - CHECK(!strcmp(Path("/foo/bar").symbol(), "bar")); - CHECK(!strcmp(Path("/").symbol(), "")); + assert(Path("/foo/bar").parent() == Path("/foo")); + assert(Path("/foo").parent() == Path("/")); + assert(Path("/").parent() == Path("/")); + + assert(Path("/").is_parent_of(Path("/foo"))); + assert(Path("/foo").is_parent_of(Path("/foo/bar"))); + assert(!(Path("/foo").is_parent_of(Path("/foo2")))); + assert(!(Path("/foo").is_parent_of(Path("/foo")))); + + assert(Path::lca(Path("/foo"), Path("/foo/bar/baz")) == Path("/")); + assert(Path::lca(Path("/foo/bar"), Path("/foo/bar/baz")) == Path("/foo")); + assert(Path::lca(Path("/foo/bar/quux"), Path("/foo/bar/baz")) == Path("/foo/bar")); + + assert(!Path::is_valid("")); + assert(!Path::is_valid("hello")); + assert(!Path::is_valid("/foo/bar/")); + assert(!Path::is_valid("/foo//bar")); + assert(!Path::is_valid("/foo/bar/d*s")); + assert(Path::is_valid("/")); + assert(!Path::is_valid("/foo/3foo/bar")); + + assert(Path::descendant_comparator(Path("/"), Path("/"))); + assert(Path::descendant_comparator(Path("/"), Path("/foo"))); + assert(Path::descendant_comparator(Path("/foo"), Path("/foo/bar"))); + assert(Path::descendant_comparator(Path("/foo"), Path("/foo"))); + assert(Path::descendant_comparator(Path("/"), Path("/"))); + assert(!Path::descendant_comparator(Path("/baz"), Path("/"))); + assert(!Path::descendant_comparator(Path("/foo"), Path("/bar"))); + assert(!Path::descendant_comparator(Path("/foo/bar"), Path("/foo"))); + + assert(!Symbol::is_valid("")); + assert(!Symbol::is_valid("/I/have/slashes")); + assert(!Symbol::is_valid("!illegalchar")); + assert(!Symbol::is_valid("0illegalleadingdigit")); + assert(strcmp(Symbol::symbolify("").c_str(), "")); + + assert(Path("/foo").child(Symbol("bar")) == "/foo/bar"); + assert(Path("/foo").child(Path("/bar/baz")) == "/foo/bar/baz"); + assert(Path("/foo").child(Path("/")) == "/foo"); + + assert(!strcmp(Path("/foo").symbol(), "foo")); + assert(!strcmp(Path("/foo/bar").symbol(), "bar")); + assert(!strcmp(Path("/").symbol(), "")); Path original(std::string("/foo/bar")); - CHECK(original == Path(original)); + assert(original == Path(original)); bool valid = true; try { @@ -87,7 +84,7 @@ main() std::cerr << "Caught exception: " << e.what() << std::endl; valid = false; } - CHECK(!valid); + assert(!valid); valid = true; try { @@ -96,7 +93,7 @@ main() std::cerr << "Caught exception: " << e.what() << std::endl; valid = false; } - CHECK(!valid); + assert(!valid); return 0; } diff --git a/test/ringbuffer_test.cpp b/test/ringbuffer_test.cpp index 8713be3..4527902 100644 --- a/test/ringbuffer_test.cpp +++ b/test/ringbuffer_test.cpp @@ -1,6 +1,6 @@ /* This file is part of Raul. - Copyright 2007-2012 David Robillard <http://drobilla.net> + Copyright 2007-2019 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -14,8 +14,11 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ +#undef NDEBUG + #include "raul/RingBuffer.hpp" +#include <cassert> #include <climits> #include <cstdint> #include <cstdio> @@ -33,7 +36,6 @@ using RingBuffer = Raul::RingBuffer; struct Context { std::unique_ptr<RingBuffer> ring; size_t n_writes{0}; - size_t ring_errors{0}; }; int @@ -46,17 +48,12 @@ gen_msg(int* msg, int start) return start; } -int -cmp_msg(int* msg1, int* msg2) +void +check_msg(int* msg1, int* msg2) { for (int i = 0; i < MSG_SIZE; ++i) { - if (msg1[i] != msg2[i]) { - fprintf(stderr, "ERROR: %d != %d @ %d\n", msg1[i], msg2[i], i); - return 0; - } + assert(msg1[i] == msg2[i]); } - - return 1; } void @@ -71,16 +68,8 @@ reader(Context& ctx) 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); - if (n_read != MSG_SIZE * sizeof(int)) { - fprintf(stderr, "FAIL: Read size incorrect\n"); - ++ctx.ring_errors; - return; - } - if (!cmp_msg(ref_msg, read_msg)) { - fprintf(stderr, "FAIL: Message %zu is corrupt\n", count); - ++ctx.ring_errors; - return; - } + assert(n_read == MSG_SIZE * sizeof(int)); + check_msg(ref_msg, read_msg); start = gen_msg(ref_msg, start); ++count; } @@ -99,11 +88,7 @@ writer(Context& ctx) 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); - if (n_write != MSG_SIZE * sizeof(int)) { - fprintf(stderr, "FAIL: Write size incorrect\n"); - ++ctx.ring_errors; - return; - } + assert(n_write == MSG_SIZE * sizeof(int)); start = gen_msg(write_msg, start); } } @@ -139,59 +124,31 @@ main(int argc, char** argv) ctx.ring = std::unique_ptr<RingBuffer>(new RingBuffer(uint32_t(size))); auto& ring = ctx.ring; - if (ring->capacity() < size - 1) { - fprintf(stderr, "Ring capacity is smaller than expected\n"); - return 1; - } + assert(ring->capacity() >= size - 1); - if (ring->skip(1)) { - fprintf(stderr, "Successfully skipped in empty RingBuffer\n"); - return 1; - } + assert(!ring->skip(1)); char buf[6] = { 'h', 'e', 'l', 'l', '0', '\0' }; - if (ring->read(1, buf)) { - fprintf(stderr, "Successfully read from empty RingBuffer\n"); - return 1; - } + assert(!ring->read(1, buf)); ring->write(sizeof(buf), buf); ring->skip(1); char buf2[sizeof(buf) - 1]; ring->read(sizeof(buf2), buf2); - if (strcmp(buf2, buf + 1)) { - fprintf(stderr, "Skip failed\n"); - return 1; - } + assert(!strcmp(buf2, buf + 1)); ring->reset(); - if (ring->read_space() != 0) { - fprintf(stderr, "Reset RingBuffer is not empty\n"); - return 1; - } + assert(ring->read_space() == 0); for (uint32_t i = 0; i < ring->capacity(); ++i) { const char c = 'X'; - if (ring->write(1, &c) != 1) { - fprintf(stderr, "Write failed\n"); - return 1; - } + assert(ring->write(1, &c) == 1); } - if (ring->write_space() != 0) { - fprintf(stderr, "Ring is not full as expected\n"); - return 1; - } - - if (ring->write(1, buf) != 0) { - fprintf(stderr, "Successfully wrote to full RingBuffer\n"); - return 1; - } - - if (ring->peek(1, buf2) != 1 || buf2[0] != 'X') { - fprintf(stderr, "Failed to read from full RingBuffer\n"); - return 1; - } + assert(ring->write_space() == 0); + assert(ring->write(1, buf) == 0); + assert(ring->peek(1, buf2) == 1); + assert(buf2[0] == 'X'); ring->reset(); @@ -201,10 +158,5 @@ main(int argc, char** argv) reader_thread.join(); writer_thread.join(); - if (ctx.ring_errors) { - fprintf(stderr, "FAIL: Error occurred\n"); - return 1; - } - return 0; } diff --git a/test/socket_test.cpp b/test/socket_test.cpp index 9208dc3..4e99e93 100644 --- a/test/socket_test.cpp +++ b/test/socket_test.cpp @@ -1,6 +1,6 @@ /* This file is part of Raul. - Copyright 2007-2014 David Robillard <http://drobilla.net> + Copyright 2007-2019 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -14,12 +14,15 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ +#undef NDEBUG + #include "raul/Socket.hpp" #include <poll.h> #include <sys/wait.h> #include <unistd.h> +#include <cassert> #include <cerrno> #include <cstdio> #include <cstring> @@ -37,23 +40,10 @@ main() Socket unix_server_sock(Socket::Type::UNIX); Socket tcp_server_sock(Socket::Type::TCP); - if (!unix_server_sock.bind(unix_uri)) { - fprintf(stderr, "Failed to bind UNIX server socket (%s)\n", - strerror(errno)); - return 1; - } else if (!unix_server_sock.listen()) { - fprintf(stderr, "Failed to listen on UNIX server socket (%s)\n", - strerror(errno)); - return 1; - } else if (!tcp_server_sock.bind(tcp_uri)) { - fprintf(stderr, "Failed to bind TCP server socket (%s)\n", - strerror(errno)); - return 1; - } else if (!tcp_server_sock.listen()) { - fprintf(stderr, "Failed to listen on TCP server socket (%s)\n", - strerror(errno)); - return 1; - } + assert(unix_server_sock.bind(unix_uri)); + assert(unix_server_sock.listen()); + assert(tcp_server_sock.bind(tcp_uri)); + assert(tcp_server_sock.listen()); const pid_t child_pid = fork(); if (child_pid) { @@ -72,10 +62,9 @@ main() unsigned n_received = 0; while (n_received < 2) { const int ret = poll(pfds, 2, -1); - if (ret == -1) { - fprintf(stderr, "poll error (%s)\n", strerror(errno)); - break; - } else if ((pfds[0].revents & POLLHUP) || pfds[1].revents & POLLHUP) { + assert(ret != -1); + + if ((pfds[0].revents & POLLHUP) || pfds[1].revents & POLLHUP) { break; } else if (ret == 0) { fprintf(stderr, "poll returned with no data\n"); @@ -104,13 +93,8 @@ main() Raul::Socket unix_sock(Socket::Type::UNIX); Raul::Socket tcp_sock(Socket::Type::TCP); - if (!unix_sock.connect(unix_uri)) { - fprintf(stderr, "Failed to connect to UNIX socket\n"); - return 1; - } else if (!tcp_sock.connect(tcp_uri)) { - fprintf(stderr, "Failed to connect to TCP socket\n"); - return 1; - } + assert(unix_sock.connect(unix_uri)); + assert(tcp_sock.connect(tcp_uri)); return 0; } diff --git a/test/symbol_test.cpp b/test/symbol_test.cpp index 4af598b..7cb6666 100644 --- a/test/symbol_test.cpp +++ b/test/symbol_test.cpp @@ -1,6 +1,6 @@ /* This file is part of Raul. - Copyright 2007-2012 David Robillard <http://drobilla.net> + Copyright 2007-2019 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -14,8 +14,11 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ +#undef NDEBUG + #include "raul/Symbol.hpp" +#include <cassert> #include <iostream> #include <list> #include <string> @@ -25,12 +28,6 @@ main() { using Symbol = Raul::Symbol; -#define CHECK(cond) \ - do { if (!(cond)) { \ - std::cerr << "Test failed: " << (cond) << std::endl; \ - return 1; \ - } } while (0) - std::list<std::string> names; names.emplace_back("Dry/Wet Balance"); names.emplace_back("foo+1bar(baz)"); @@ -47,11 +44,11 @@ main() names.emplace_back(""); for (const auto& name : names) { - CHECK(!Symbol::symbolify(name).empty()); + assert(!Symbol::symbolify(name).empty()); } Symbol original("sym"); - CHECK(original == Symbol(original)); + assert(original == Symbol(original)); bool valid = true; try { @@ -60,7 +57,7 @@ main() std::cerr << "Caught exception: " << e.what() << std::endl; valid = false; } - CHECK(!valid); + assert(!valid); valid = true; try { @@ -69,7 +66,7 @@ main() std::cerr << "Caught exception: " << e.what() << std::endl; valid = false; } - CHECK(!valid); + assert(!valid); return 0; } diff --git a/test/thread_test.cpp b/test/thread_test.cpp index 72ba93f..cf65050 100644 --- a/test/thread_test.cpp +++ b/test/thread_test.cpp @@ -1,6 +1,6 @@ /* This file is part of Raul. - Copyright 2007-2017 David Robillard <http://drobilla.net> + Copyright 2007-2019 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -14,9 +14,12 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ +#undef NDEBUG + #include "raul/Semaphore.hpp" #include <atomic> +#include <cassert> #include <iostream> #include <thread> @@ -35,10 +38,7 @@ wait_for_sem(Semaphore* sem) sem->wait(); std::cout << "[Waiter] Received signal, exiting" << std::endl; var = 42; - if (var != 42) { - std::cerr << "[Waiter] var != 42" << std::endl; - ++n_errors; - } + assert(var == 42); } } // namespace @@ -59,10 +59,7 @@ main() std::cout << "[Main] Exiting" << std::endl; - if (var != 24) { - std::cerr << "[Main] var != 24" << std::endl; - ++n_errors; - } + assert(var == 24); return n_errors.load(); } |