Anklang 0.3.0-460-gc4ef46ba
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
queuemux.cc
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#include "queuemux.hh"
3#include "randomhash.hh"
4#include "internal.hh"
5#include "testing.hh"
6
7#define QDEBUG(...) Ase::debug ("queuemux", __VA_ARGS__)
8
9namespace {
10using namespace Ase;
11
12struct SomeValue {
13 int i;
14};
15
16static __attribute__ ((always_inline)) inline long
17QueueMultiplexer_priority (const SomeValue &o)
18{
19 return o.i;
20}
21
22TEST_INTEGRITY (queuemux_test);
23static void
24queuemux_test()
25{
26 // generate ascending (sorted) sample values
27 int ascending_counter = -17;
28 auto ascending_values = [&ascending_counter] () {
29 if (random_int64() & 1)
30 ascending_counter++;
31 return ascending_counter;
32 };
33 const size_t TOTAL = 39;
34 std::vector<int> samples;
35 for (size_t i = 0; i < TOTAL; i++)
36 samples.push_back (ascending_values());
37
38 // setting: N queues contain ascending (sorted) values
39 constexpr size_t N = 4;
40 using Queue = std::vector<SomeValue>;
41 std::vector<Queue> queues;
42 queues.resize (N);
43 for (int v : samples)
44 queues[random_int64() % N].push_back (SomeValue { v });
45
46 // task: fetch values from all queues in sorted order
47 std::array<const Queue*,N> queue_ptrs{};
48 for (size_t i = 0; i < queues.size(); i++)
49 queue_ptrs[i] = &queues[i];
51 mux.assign (queue_ptrs);
52 TASSERT (mux.count_pending() == TOTAL);
53 int last = -2147483648;
54 size_t sc = 0;
55 while (mux.more())
56 {
57 const SomeValue &current = mux.pop();
58 QDEBUG ("QueueMultiplexer: %d\n", current.i);
59 TASSERT (current.i >= last);
60 last = current.i;
61 TASSERT (sc < samples.size() && samples[sc++] == current.i);
62 }
63 TASSERT (sc == samples.size());
64 TASSERT (mux.count_pending() == 0);
65}
66
67} // Anon
#define TEST_INTEGRITY(FUNC)
Register func as an integrity test.
Definition internal.hh:77
The Anklang C++ API namespace.
Definition api.hh:9
uint64_t random_int64()
#define TASSERT(cond)
Unconditional test assertion, enters breakpoint if not fullfilled.
Definition testing.hh:24