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 | |
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')
-rw-r--r-- | test/array_test.cpp | 38 | ||||
-rw-r--r-- | test/build_test.cpp | 72 | ||||
-rw-r--r-- | test/double_buffer_test.cpp | 14 | ||||
-rw-r--r-- | test/maid_test.cpp | 163 | ||||
-rw-r--r-- | test/path_test.cpp | 139 | ||||
-rw-r--r-- | test/ringbuffer_test.cpp | 167 | ||||
-rw-r--r-- | test/sem_test.cpp | 57 | ||||
-rw-r--r-- | test/socket_test.cpp | 132 | ||||
-rw-r--r-- | test/symbol_test.cpp | 74 | ||||
-rw-r--r-- | test/thread_test.cpp | 32 | ||||
-rw-r--r-- | test/time_test.cpp | 32 |
11 files changed, 464 insertions, 456 deletions
diff --git a/test/array_test.cpp b/test/array_test.cpp index fcbc810..8053732 100644 --- a/test/array_test.cpp +++ b/test/array_test.cpp @@ -23,29 +23,29 @@ int main() { - Raul::Array<int> array1(32, 2); + Raul::Array<int> array1(32, 2); - array1[0] = 42; - assert(array1[0] == 42); - assert(array1[1] == 2); - assert(array1.size() == 32); + array1[0] = 42; + assert(array1[0] == 42); + assert(array1[1] == 2); + assert(array1.size() == 32); - array1.alloc(16, 0); - assert(array1[0] == 0); - assert(array1.at(0) == 0); - assert(array1.size() == 16); + array1.alloc(16, 0); + assert(array1[0] == 0); + assert(array1.at(0) == 0); + assert(array1.size() == 16); - array1.alloc(8, 0); - assert(array1.size() == 8); + array1.alloc(8, 0); + assert(array1.size() == 8); - Raul::Array<int> array2(array1); - for (size_t i = 0; i < array1.size(); ++i) { - assert(array2[i] == array1[i]); - } + Raul::Array<int> array2(array1); + for (size_t i = 0; i < array1.size(); ++i) { + assert(array2[i] == array1[i]); + } - Raul::Array<int> array3(8, 47); - assert(array3[0] == 47); - assert(array3.size() == 8); + Raul::Array<int> array3(8, 47); + assert(array3[0] == 47); + assert(array3.size() == 8); - return 0; + return 0; } diff --git a/test/build_test.cpp b/test/build_test.cpp index a03f776..69ddf36 100644 --- a/test/build_test.cpp +++ b/test/build_test.cpp @@ -27,59 +27,57 @@ #include "raul/TimeStamp.hpp" #ifndef _WIN32 -# include "raul/Process.hpp" -# include "raul/Socket.hpp" +# include "raul/Process.hpp" +# include "raul/Socket.hpp" #endif class DeletableThing : public Raul::Deletable -{ -}; +{}; class NonCopyableThing : public Raul::Noncopyable -{ -}; +{}; int main() { - Raul::Array<int> array; - DeletableThing deletable; - Raul::DoubleBuffer<int> double_buffer(0); - Raul::Maid maid; - NonCopyableThing non_copyable; - Raul::Path path; - Raul::RingBuffer ring_buffer(64u); - Raul::Semaphore semaphore(0u); - Raul::Symbol symbol("foo"); - Raul::TimeSlice time_slice(48000u, 960u, 120.0); + Raul::Array<int> array; + DeletableThing deletable; + Raul::DoubleBuffer<int> double_buffer(0); + Raul::Maid maid; + NonCopyableThing non_copyable; + Raul::Path path; + Raul::RingBuffer ring_buffer(64u); + Raul::Semaphore semaphore(0u); + Raul::Symbol symbol("foo"); + Raul::TimeSlice time_slice(48000u, 960u, 120.0); - Raul::TimeStamp time_stamp(Raul::TimeUnit(Raul::TimeUnit::BEATS, 960u)); + Raul::TimeStamp time_stamp(Raul::TimeUnit(Raul::TimeUnit::BEATS, 960u)); - try { - Raul::Symbol bad_symbol("not a valid symbol!"); - (void)bad_symbol; - } catch (const Raul::Exception&) { - } + try { + Raul::Symbol bad_symbol("not a valid symbol!"); + (void)bad_symbol; + } catch (const Raul::Exception&) { + } #ifndef _WIN32 - const char* cmd[] = {"echo"}; - Raul::Process::launch(cmd); + const char* cmd[] = {"echo"}; + Raul::Process::launch(cmd); - Raul::Socket socket(Raul::Socket::Type::UNIX); + Raul::Socket socket(Raul::Socket::Type::UNIX); - (void)socket; + (void)socket; #endif - (void)array; - (void)deletable; - (void)double_buffer; - (void)maid; - (void)non_copyable; - (void)path; - (void)ring_buffer; - (void)symbol; - (void)time_slice; - (void)time_stamp; + (void)array; + (void)deletable; + (void)double_buffer; + (void)maid; + (void)non_copyable; + (void)path; + (void)ring_buffer; + (void)symbol; + (void)time_slice; + (void)time_stamp; - return 0; + return 0; } diff --git a/test/double_buffer_test.cpp b/test/double_buffer_test.cpp index e1a4112..0f58ea2 100644 --- a/test/double_buffer_test.cpp +++ b/test/double_buffer_test.cpp @@ -22,15 +22,15 @@ int main() { - Raul::DoubleBuffer<int> db(0); + Raul::DoubleBuffer<int> db(0); - assert(db.get() == 0); + assert(db.get() == 0); - db.set(42); - assert(db.get() == 42); + db.set(42); + assert(db.get() == 42); - db.set(43); - assert(db.get() == 43); + db.set(43); + assert(db.get() == 43); - return 0; + return 0; } diff --git a/test/maid_test.cpp b/test/maid_test.cpp index 192960c..ee2ee7f 100644 --- a/test/maid_test.cpp +++ b/test/maid_test.cpp @@ -33,106 +33,111 @@ static const size_t n_junk_per_thread = 1U << 16U; static std::atomic<size_t> n_junk(0); static std::atomic<size_t> n_finished_threads(0); -class Junk : public Maid::Disposable { +class Junk : public Maid::Disposable +{ public: - explicit Junk(size_t v) : _val(v) { ++n_junk; } + explicit Junk(size_t v) + : _val(v) + { + ++n_junk; + } - Junk(const Junk&) = delete; - Junk& operator=(const Junk&) = delete; + Junk(const Junk&) = delete; + Junk& operator=(const Junk&) = delete; - Junk(Junk&&) = delete; - Junk& operator=(Junk&&) = delete; + Junk(Junk&&) = delete; + Junk& operator=(Junk&&) = delete; - ~Junk() override { --n_junk; } + ~Junk() override { --n_junk; } - size_t value() const { return _val; } + size_t value() const { return _val; } private: - size_t _val; + size_t _val; }; static void litter(Maid* maid) { - for (size_t i = 0; i < n_junk_per_thread; ++i) { - Maid::managed_ptr<Junk> a = maid->make_managed<Junk>(i); - assert(a->value() == i); - } + for (size_t i = 0; i < n_junk_per_thread; ++i) { + Maid::managed_ptr<Junk> a = maid->make_managed<Junk>(i); + assert(a->value() == i); + } - ++n_finished_threads; + ++n_finished_threads; } static void test() { - Maid maid; - - // Check basic single-threaded correctness - { - assert(n_junk == 0); - Maid::managed_ptr<Junk> a = maid.make_managed<Junk>(1U); - assert(n_junk == 1); - Maid::managed_ptr<Junk> b = maid.make_managed<Junk>(2U); - assert(n_junk == 2); - } - - maid.dispose(nullptr); // Mustn't crash - - // All referenes dropped, but deletion deferred - assert(n_junk == 2); - - // Trigger actual deletion - maid.cleanup(); - assert(n_junk == 0); - assert(maid.empty()); - - // Create some threads to produce garbage - std::vector<std::thread> litterers; - for (size_t i = 0; i < n_threads; ++i) { - litterers.emplace_back(litter, &maid); - } - - // Wait for some garbage to show up if necessary (unlikely) - size_t initial_n_junk = n_junk; - while (maid.empty()) { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - initial_n_junk = n_junk; - } - - printf("Starting with %zu initial bits of junk\n", initial_n_junk); - - // Ensure we're actually cleaning things up concurrently - maid.cleanup(); - assert(n_junk != initial_n_junk); - - // Continue cleaning up as long as threads are running - size_t n_cleanup_calls = 1; - while (n_finished_threads < n_threads) { - maid.cleanup(); - ++n_cleanup_calls; - } - - printf("Called cleanup %zu times\n", n_cleanup_calls); - - // Join litterer threads - for (auto& t : litterers) { - t.join(); - } - - // Clean up any leftover garbage (unlikely/impossible?) - maid.cleanup(); - assert(n_junk == 0); - - // Allocate a new object, then let it and the Maid go out of scope - Maid::managed_ptr<Junk> c = maid.make_managed<Junk>(5U); - assert(n_junk == 1); + Maid maid; + + // Check basic single-threaded correctness + { + assert(n_junk == 0); + Maid::managed_ptr<Junk> a = maid.make_managed<Junk>(1U); + assert(n_junk == 1); + Maid::managed_ptr<Junk> b = maid.make_managed<Junk>(2U); + assert(n_junk == 2); + } + + maid.dispose(nullptr); // Mustn't crash + + // All referenes dropped, but deletion deferred + assert(n_junk == 2); + + // Trigger actual deletion + maid.cleanup(); + assert(n_junk == 0); + assert(maid.empty()); + + // Create some threads to produce garbage + std::vector<std::thread> litterers; + for (size_t i = 0; i < n_threads; ++i) { + litterers.emplace_back(litter, &maid); + } + + // Wait for some garbage to show up if necessary (unlikely) + size_t initial_n_junk = n_junk; + while (maid.empty()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + initial_n_junk = n_junk; + } + + printf("Starting with %zu initial bits of junk\n", initial_n_junk); + + // Ensure we're actually cleaning things up concurrently + maid.cleanup(); + assert(n_junk != initial_n_junk); + + // Continue cleaning up as long as threads are running + size_t n_cleanup_calls = 1; + while (n_finished_threads < n_threads) { + maid.cleanup(); + ++n_cleanup_calls; + } + + printf("Called cleanup %zu times\n", n_cleanup_calls); + + // Join litterer threads + for (auto& t : litterers) { + t.join(); + } + + // Clean up any leftover garbage (unlikely/impossible?) + maid.cleanup(); + assert(n_junk == 0); + + // Allocate a new object, then let it and the Maid go out of scope + Maid::managed_ptr<Junk> c = maid.make_managed<Junk>(5U); + assert(n_junk == 1); } int main() { - assert(n_junk == 0); - test(); - assert(n_junk == 0); - return 0; + assert(n_junk == 0); + test(); + assert(n_junk == 0); + return 0; } diff --git a/test/path_test.cpp b/test/path_test.cpp index deda557..5681ec1 100644 --- a/test/path_test.cpp +++ b/test/path_test.cpp @@ -26,73 +26,74 @@ int main() { - using Path = Raul::Path; - using Symbol = Raul::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(), "")); - - const Path original(std::string("/foo/bar")); - assert(original == original); - - bool valid = true; - try { - Path path("/ends/in/slash/"); - } catch (const Path::BadPath& e) { - std::cerr << "Caught exception: " << e.what() << std::endl; - valid = false; - } - assert(!valid); - - valid = true; - try { - Path path(std::string("/has//double/slash")); - } catch (const Path::BadPath& e) { - std::cerr << "Caught exception: " << e.what() << std::endl; - valid = false; - } - assert(!valid); - - return 0; + using Path = Raul::Path; + using Symbol = Raul::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(), "")); + + const Path original(std::string("/foo/bar")); + assert(original == original); + + bool valid = true; + try { + Path path("/ends/in/slash/"); + } catch (const Path::BadPath& e) { + std::cerr << "Caught exception: " << e.what() << std::endl; + valid = false; + } + assert(!valid); + + valid = true; + try { + Path path(std::string("/has//double/slash")); + } catch (const Path::BadPath& e) { + std::cerr << "Caught exception: " << e.what() << std::endl; + valid = false; + } + assert(!valid); + + return 0; } 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; } diff --git a/test/sem_test.cpp b/test/sem_test.cpp index 58bc56c..2e8f8f8 100644 --- a/test/sem_test.cpp +++ b/test/sem_test.cpp @@ -24,48 +24,49 @@ static void wait_for_sem(Raul::Semaphore* sem) { - sem->wait(); + sem->wait(); } static void timed_wait_for_sem(Raul::Semaphore* sem) { - while (!sem->timed_wait(std::chrono::milliseconds(100))) {} + while (!sem->timed_wait(std::chrono::milliseconds(100))) { + } } int main() { - Raul::Semaphore sem(0); - assert(!sem.try_wait()); + Raul::Semaphore sem(0); + assert(!sem.try_wait()); - // Check that semaphore wakes up strict waiter - std::thread waiter(wait_for_sem, &sem); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - sem.post(); - waiter.join(); + // Check that semaphore wakes up strict waiter + std::thread waiter(wait_for_sem, &sem); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sem.post(); + waiter.join(); - // Check that semaphore wakes up timed waiter - std::thread timed_waiter(timed_wait_for_sem, &sem); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - sem.post(); - timed_waiter.join(); + // Check that semaphore wakes up timed waiter + std::thread timed_waiter(timed_wait_for_sem, &sem); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + sem.post(); + timed_waiter.join(); - // Check that timed_wait actually waits - const auto start = std::chrono::steady_clock::now(); - sem.timed_wait(std::chrono::milliseconds(100)); - const auto end = std::chrono::steady_clock::now(); - assert(end - start > std::chrono::milliseconds(80)); - assert(end - start < std::chrono::milliseconds(400)); + // Check that timed_wait actually waits + const auto start = std::chrono::steady_clock::now(); + sem.timed_wait(std::chrono::milliseconds(100)); + const auto end = std::chrono::steady_clock::now(); + assert(end - start > std::chrono::milliseconds(80)); + assert(end - start < std::chrono::milliseconds(400)); - // Check that we can't successfully wait on a zero semaphore - assert(!sem.timed_wait(std::chrono::milliseconds(100))); + // Check that we can't successfully wait on a zero semaphore + assert(!sem.timed_wait(std::chrono::milliseconds(100))); - // Check that initial value works correctly - Raul::Semaphore sem2(2); - assert(sem2.wait()); - assert(sem2.wait()); - assert(!sem2.try_wait()); + // Check that initial value works correctly + Raul::Semaphore sem2(2); + assert(sem2.wait()); + assert(sem2.wait()); + assert(!sem2.try_wait()); - return 0; + return 0; } diff --git a/test/socket_test.cpp b/test/socket_test.cpp index f2f04fc..e1638d6 100644 --- a/test/socket_test.cpp +++ b/test/socket_test.cpp @@ -29,70 +29,70 @@ int main() { - using Socket = Raul::Socket; - - std::string unix_uri("unix:///tmp/raul_test_sock"); - std::string tcp_uri("tcp://127.0.0.1:12345"); - - Socket unix_server_sock(Socket::Type::UNIX); - Socket tcp_server_sock(Socket::Type::TCP); - 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) { - // This is the parent (server) process - int status = 0; - waitpid(child_pid, &status, 0); - - struct pollfd pfds[2]; - pfds[0].fd = unix_server_sock.fd(); - pfds[0].events = POLLIN; - pfds[0].revents = 0; - pfds[1].fd = tcp_server_sock.fd(); - pfds[1].events = POLLIN; - pfds[1].revents = 0; - - unsigned n_received = 0; - while (n_received < 2) { - const int ret = poll(pfds, 2, -1); - assert(ret != -1); - - if ((pfds[0].revents & POLLHUP) || pfds[1].revents & POLLHUP) { - break; - } - - if (ret == 0) { - fprintf(stderr, "poll returned with no data\n"); - continue; - } - - if (pfds[0].revents & POLLIN) { - std::shared_ptr<Socket> conn = unix_server_sock.accept(); - ++n_received; - } - - if (pfds[1].revents & POLLIN) { - std::shared_ptr<Socket> conn = tcp_server_sock.accept(); - ++n_received; - } - } - - unix_server_sock.shutdown(); - tcp_server_sock.shutdown(); - unlink("/tmp/raul_test_sock"); - fprintf(stderr, "n received: %u\n", n_received); - return n_received != 2; - } - - // This is the child (client) process - Raul::Socket unix_sock(Socket::Type::UNIX); - Raul::Socket tcp_sock(Socket::Type::TCP); - - assert(unix_sock.connect(unix_uri)); - assert(tcp_sock.connect(tcp_uri)); - - return 0; + using Socket = Raul::Socket; + + std::string unix_uri("unix:///tmp/raul_test_sock"); + std::string tcp_uri("tcp://127.0.0.1:12345"); + + Socket unix_server_sock(Socket::Type::UNIX); + Socket tcp_server_sock(Socket::Type::TCP); + 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) { + // This is the parent (server) process + int status = 0; + waitpid(child_pid, &status, 0); + + struct pollfd pfds[2]; + pfds[0].fd = unix_server_sock.fd(); + pfds[0].events = POLLIN; + pfds[0].revents = 0; + pfds[1].fd = tcp_server_sock.fd(); + pfds[1].events = POLLIN; + pfds[1].revents = 0; + + unsigned n_received = 0; + while (n_received < 2) { + const int ret = poll(pfds, 2, -1); + assert(ret != -1); + + if ((pfds[0].revents & POLLHUP) || pfds[1].revents & POLLHUP) { + break; + } + + if (ret == 0) { + fprintf(stderr, "poll returned with no data\n"); + continue; + } + + if (pfds[0].revents & POLLIN) { + std::shared_ptr<Socket> conn = unix_server_sock.accept(); + ++n_received; + } + + if (pfds[1].revents & POLLIN) { + std::shared_ptr<Socket> conn = tcp_server_sock.accept(); + ++n_received; + } + } + + unix_server_sock.shutdown(); + tcp_server_sock.shutdown(); + unlink("/tmp/raul_test_sock"); + fprintf(stderr, "n received: %u\n", n_received); + return n_received != 2; + } + + // This is the child (client) process + Raul::Socket unix_sock(Socket::Type::UNIX); + Raul::Socket tcp_sock(Socket::Type::TCP); + + 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 16d9476..e52a294 100644 --- a/test/symbol_test.cpp +++ b/test/symbol_test.cpp @@ -25,47 +25,47 @@ int main() { - using Symbol = Raul::Symbol; + using Symbol = Raul::Symbol; - std::list<std::string> names; - names.emplace_back("Dry/Wet Balance"); - names.emplace_back("foo+1bar(baz)"); - names.emplace_back("ThisCRAR"); - names.emplace_back("NAME"); - names.emplace_back("thing with a bunch of spaces"); - names.emplace_back("thing-with-a-bunch-of-dashes"); - names.emplace_back("CamelCaseABC"); - names.emplace_back("Signal Level [dB]"); - names.emplace_back("Gain dB"); - names.emplace_back("Dry/Wet Balance"); - names.emplace_back("Phaser1 - Similar to CSound's phaser1 by Sean Costello"); - names.emplace_back("1"); - names.emplace_back(""); + std::list<std::string> names; + names.emplace_back("Dry/Wet Balance"); + names.emplace_back("foo+1bar(baz)"); + names.emplace_back("ThisCRAR"); + names.emplace_back("NAME"); + names.emplace_back("thing with a bunch of spaces"); + names.emplace_back("thing-with-a-bunch-of-dashes"); + names.emplace_back("CamelCaseABC"); + names.emplace_back("Signal Level [dB]"); + names.emplace_back("Gain dB"); + names.emplace_back("Dry/Wet Balance"); + names.emplace_back("Phaser1 - Similar to CSound's phaser1 by Sean Costello"); + names.emplace_back("1"); + names.emplace_back(""); - for (const auto& name : names) { - assert(!Symbol::symbolify(name).empty()); - } + for (const auto& name : names) { + assert(!Symbol::symbolify(name).empty()); + } - const Symbol original("sym"); - assert(original == original); + const Symbol original("sym"); + assert(original == original); - bool valid = true; - try { - Symbol symbol("0startswithdigit"); - } catch (const Symbol::BadSymbol& e) { - std::cerr << "Caught exception: " << e.what() << std::endl; - valid = false; - } - assert(!valid); + bool valid = true; + try { + Symbol symbol("0startswithdigit"); + } catch (const Symbol::BadSymbol& e) { + std::cerr << "Caught exception: " << e.what() << std::endl; + valid = false; + } + assert(!valid); - valid = true; - try { - Symbol symbol(std::string("invalid/symbol")); - } catch (const Symbol::BadSymbol& e) { - std::cerr << "Caught exception: " << e.what() << std::endl; - valid = false; - } - assert(!valid); + valid = true; + try { + Symbol symbol(std::string("invalid/symbol")); + } catch (const Symbol::BadSymbol& e) { + std::cerr << "Caught exception: " << e.what() << std::endl; + valid = false; + } + assert(!valid); - return 0; + return 0; } diff --git a/test/thread_test.cpp b/test/thread_test.cpp index b2a8144..8c2d13a 100644 --- a/test/thread_test.cpp +++ b/test/thread_test.cpp @@ -32,12 +32,12 @@ std::atomic<int> n_errors(0); void wait_for_sem(Semaphore* sem) { - var = 41; - std::cout << "[Waiter] Waiting for signal..." << std::endl; - sem->wait(); - std::cout << "[Waiter] Received signal, exiting" << std::endl; - var = 42; - assert(var == 42); + var = 41; + std::cout << "[Waiter] Waiting for signal..." << std::endl; + sem->wait(); + std::cout << "[Waiter] Received signal, exiting" << std::endl; + var = 42; + assert(var == 42); } } // namespace @@ -45,20 +45,20 @@ wait_for_sem(Semaphore* sem) int main() { - Semaphore sem(0); - std::thread waiter(wait_for_sem, &sem); + Semaphore sem(0); + std::thread waiter(wait_for_sem, &sem); - var = 24; + var = 24; - std::cout << "[Main] Signalling..." << std::endl; - sem.post(); + std::cout << "[Main] Signalling..." << std::endl; + sem.post(); - std::cout << "[Main] Waiting for waiter..." << std::endl; - waiter.join(); + std::cout << "[Main] Waiting for waiter..." << std::endl; + waiter.join(); - std::cout << "[Main] Exiting" << std::endl; + std::cout << "[Main] Exiting" << std::endl; - assert(var == 24); + assert(var == 24); - return n_errors.load(); + return n_errors.load(); } diff --git a/test/time_test.cpp b/test/time_test.cpp index 6bd1e38..1746a7e 100644 --- a/test/time_test.cpp +++ b/test/time_test.cpp @@ -13,8 +13,8 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ -#include "raul/TimeStamp.hpp" #include "raul/TimeSlice.hpp" +#include "raul/TimeStamp.hpp" #include <cstdint> #include <iostream> @@ -22,24 +22,24 @@ int main() { - Raul::TimeUnit unit(Raul::TimeUnit::BEATS, 19200); - Raul::TimeSlice ts(48000, 19200, 120.0); + Raul::TimeUnit unit(Raul::TimeUnit::BEATS, 19200); + Raul::TimeSlice ts(48000, 19200, 120.0); - double in_double = 2.5; + double in_double = 2.5; - Raul::TimeStamp t(unit, - static_cast<uint32_t>(in_double), - static_cast<uint32_t>( - (in_double - static_cast<uint32_t>(in_double)) * - unit.ppt())); + Raul::TimeStamp t( + unit, + static_cast<uint32_t>(in_double), + static_cast<uint32_t>((in_double - static_cast<uint32_t>(in_double)) * + unit.ppt())); - std::cout << "\tSeconds: "; - std::cout << ts.beats_to_seconds(t); - std::cout << std::endl; + std::cout << "\tSeconds: "; + std::cout << ts.beats_to_seconds(t); + std::cout << std::endl; - std::cout << "\tTicks: "; - std::cout << ts.beats_to_ticks(t); - std::cout << std::endl; + std::cout << "\tTicks: "; + std::cout << ts.beats_to_ticks(t); + std::cout << std::endl; - return 0; + return 0; } |