use quotes for in-tree pbd/glib wrapper include
[ardour.git] / libs / audiographer / tests / utils.h
1 #ifndef AUDIOGRAPHER_TESTS_UTILS_H
2 #define AUDIOGRAPHER_TESTS_UTILS_H
3
4 // Includes we want almost always
5
6 #include <cppunit/extensions/HelperMacros.h>
7 #include <boost/shared_ptr.hpp>
8
9 // includes used in this file
10
11 #include "audiographer/sink.h"
12 #include "audiographer/exception.h"
13
14 #include <vector>
15 #include <list>
16 #include <cstring>
17 #include <cstdlib>
18 #include <ctime>
19
20 using AudioGrapher::framecnt_t;
21
22 struct TestUtils
23 {
24         template<typename T>
25         static bool array_equals (T const * a, T const * b, framecnt_t frames)
26         {
27                 for (framecnt_t i = 0; i < frames; ++i) {
28                         if (a[i] != b[i]) {
29                                 return false;
30                         }
31                 }
32                 return true;
33         }
34
35         template<typename T>
36         static bool array_filled (T const * array, framecnt_t frames)
37         {
38                 for (framecnt_t i = 0; i < frames; ++i) {
39                         if (array[i] == static_cast<T> (0.0)) {
40                                 return false;
41                         }
42                 }
43                 return true;
44         }
45
46         /// Generate random data, all samples guaranteed not to be 0.0, 1.0 or -1.0
47         static float * init_random_data (framecnt_t frames, float range = 1.0)
48         {
49                 unsigned int const granularity = 4096;
50                 float * data = new float[frames];
51                 srand (std::time (NULL));
52
53                 for (framecnt_t i = 0; i < frames; ++i) {
54                         do {
55                                 int biased_int = (rand() % granularity) - (granularity / 2);
56                                 data[i] = (range * biased_int) / granularity;
57                         } while (data[i] == 0.0 || data[i] == 1.0 || data[i] == -1.0);
58                 }
59                 return data;
60         }
61 };
62
63 template<typename T>
64 class VectorSink : public AudioGrapher::Sink<T>
65 {
66   public:
67         virtual void process (AudioGrapher::ProcessContext<T> const & c)
68         {
69                 data.resize (c.frames());
70                 memcpy (&data[0], c.data(), c.frames() * sizeof(T));
71         }
72
73         void process (AudioGrapher::ProcessContext<T> & c) { AudioGrapher::Sink<T>::process (c); }
74         using AudioGrapher::Sink<T>::process;
75
76         std::vector<T> const & get_data() const { return data; }
77         T const * get_array() const { return &data[0]; }
78         void reset() { data.clear(); }
79
80   protected:
81         std::vector<T> data;
82
83 };
84
85 template<typename T>
86 class AppendingVectorSink : public VectorSink<T>
87 {
88   public:
89         AppendingVectorSink() { reset (); }
90         void process (AudioGrapher::ProcessContext<T> const & c)
91         {
92                 std::vector<T> & data (VectorSink<T>::data);
93                 data.resize (total_frames + c.frames());
94                 memcpy (&data[total_frames], c.data(), c.frames() * sizeof(T));
95                 total_frames += c.frames();
96         }
97         using AudioGrapher::Sink<T>::process;
98
99         void reset ()
100         {
101                 total_frames = 0;
102                 VectorSink<T>::reset();
103         }
104
105   private:
106         framecnt_t total_frames;
107 };
108
109
110 template<typename T>
111 class ThrowingSink : public AudioGrapher::Sink<T>
112 {
113   public:
114         void process (AudioGrapher::ProcessContext<T> const &)
115         {
116                 throw AudioGrapher::Exception(*this, "ThrowingSink threw!");
117         }
118         using AudioGrapher::Sink<T>::process;
119 };
120
121 template<typename T>
122 class ProcessContextGrabber : public AudioGrapher::Sink<T>
123 {
124   public:
125         void process (AudioGrapher::ProcessContext<T> const & c)
126         {
127                 contexts.push_back (c);
128         }
129         using AudioGrapher::Sink<T>::process;
130
131         typedef std::list<AudioGrapher::ProcessContext<T> > ContextList;
132         ContextList contexts;
133
134 };
135
136 #endif // AUDIOGRAPHER_TESTS_UTILS_H