Merge remote-tracking branch 'origin/master' into export-dialog
[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         void process (AudioGrapher::ProcessContext<T> const & c)
90         {
91                 std::vector<T> & data (VectorSink<T>::data);
92                 data.resize (total_frames + c.frames());
93                 memcpy (&data[total_frames], c.data(), c.frames() * sizeof(T));
94                 total_frames += c.frames();
95         }
96         using AudioGrapher::Sink<T>::process;
97
98         void reset ()
99         {
100                 total_frames = 0;
101                 VectorSink<T>::reset();
102         }
103
104   private:
105         framecnt_t total_frames;
106 };
107
108
109 template<typename T>
110 class ThrowingSink : public AudioGrapher::Sink<T>
111 {
112   public:
113         void process (AudioGrapher::ProcessContext<T> const &)
114         {
115                 throw AudioGrapher::Exception(*this, "ThrowingSink threw!");
116         }
117         using AudioGrapher::Sink<T>::process;
118 };
119
120 template<typename T>
121 class ProcessContextGrabber : public AudioGrapher::Sink<T>
122 {
123   public:
124         void process (AudioGrapher::ProcessContext<T> const & c)
125         {
126                 contexts.push_back (c);
127         }
128         using AudioGrapher::Sink<T>::process;
129         
130         typedef std::list<AudioGrapher::ProcessContext<T> > ContextList;
131         ContextList contexts;
132         
133 };
134
135 #endif // AUDIOGRAPHER_TESTS_UTILS_H