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

« « « Anklang Documentation
Loading...
Searching...
No Matches
eventlist.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 "eventlist.hh"
3#include "utils.hh"
4#include "testing.hh"
5#include "internal.hh"
6
7TEST_INTEGRITY (event_list_tests);
8static void
9event_list_tests()
10{
11 struct Control {
12 uint tick = 0;
13 Control (uint t = 0) : tick (t) {}
14 static int
15 compare_order (const Control &a, const Control &b)
16 {
17 return Ase::Aux::compare_lesser (a.tick, b.tick);
18 }
19 };
20 struct Note : Control {
21 uint16_t key = 0;
22 Note (uint t = 0, uint k = 0) : Control (t), key (k) {}
23 static int
24 compare_order (const Note &a, const Note &b)
25 {
26 int cmp = Control::compare_order (a, b);
27 if (!cmp)
28 cmp = Ase::Aux::compare_lesser (a.key, b.key);
29 return cmp;
30 }
31 };
32 const Note *cnote;
33 int ret;
34
35 struct CompareKey { int operator() (const Note &a, const Note &b) { return int (a.key) - int (b.key); } };
36 struct CompareTick { int operator() (const Note &a, const Note &b) { return Note::compare_order (a, b); } };
37 using OrderedNoteList = Ase::OrderedEventList<Note,CompareTick>;
38 OrderedNoteList::ConstP notesp;
39 {
40 int modified = -99;
41 Ase::EventList<Note,CompareKey> note_events ([&] (const Note&, int mod) { modified = mod; });
42 note_events.insert (Note()); TASSERT (modified == +1); // inserted
43 note_events.insert (Note()); TASSERT (modified == 0); // replaced
44 cnote = note_events.lookup (Note()); TASSERT (cnote != nullptr);
45 ret = note_events.remove (Note()); TASSERT (ret && modified == -1); // removed
46 modified = -99;
47 ret = note_events.remove (Note()); TASSERT (!ret && modified == -99); // unknown
48 note_events.insert (Note (33, 3)); TASSERT (modified == +1); // inserted
49 cnote = note_events.first(); TASSERT (cnote && cnote->key == 3);
50 note_events.insert (Note (15, 5)); TASSERT (modified == +1); // inserted
51 note_events.insert (Note (21, 1)); TASSERT (modified == +1); // inserted
52 cnote = note_events.first(); TASSERT (cnote && cnote->key == 1);
53 ret = note_events.last() - cnote + 1; TASSERT (ret == 3);
54 cnote = note_events.last(); TASSERT (cnote && cnote->key == 5);
55 cnote = note_events.lookup_after (Note (0, 2)); TASSERT (cnote && cnote->key == 3);
56 cnote += 1; TASSERT (cnote && cnote->key == 5);
57 notesp = note_events.ordered_events<OrderedNoteList>();
58 note_events.clear_silently();
59 ret = note_events.size(); TASSERT (ret == 0);
60 }
61
62 const auto &notes = *notesp;
63 TASSERT (notes.size() == 3);
64 TASSERT (notes[0].tick == 15);
65 TASSERT (notes[1].tick == 21);
66 TASSERT (notes[2].tick == 33);
67 cnote = notes.lookup (Note (33, 3)); TASSERT (cnote && cnote == &notes.back());
68 cnote = notes.lookup_after (Note (17, 0)); TASSERT (cnote && cnote->key == 1);
69 cnote = notes.lookup_after (Note (0, 0)); TASSERT (cnote && cnote == &notes.front());
70}
Maintain an array of unique Event structures with change notification.
Definition eventlist.hh:25
#define TEST_INTEGRITY(FUNC)
Register func as an integrity test.
Definition internal.hh:77
typedef int
uint32_t uint
Provide 'uint' as convenience type.
Definition cxxaux.hh:18
typedef uint16_t
Container for a sorted array of opaque Event structures with binary lookup.
Definition eventlist.hh:13
#define TASSERT(cond)
Unconditional test assertion, enters breakpoint if not fullfilled.
Definition testing.hh:24