a2b2c96f1186e993043906a611e023a2a73a9ce0
[ardour.git] / libs / audiographer / tests / type_utils_test.cc
1 #include "tests/utils.h"
2
3 #include "audiographer/type_utils.h"
4
5 using namespace AudioGrapher;
6
7 class TypeUtilsTest : public CppUnit::TestFixture
8 {
9   CPPUNIT_TEST_SUITE (TypeUtilsTest);
10   CPPUNIT_TEST (testZeroFillPod);
11   CPPUNIT_TEST (testZeroFillNonPod);
12   CPPUNIT_TEST (testCopy);
13   CPPUNIT_TEST (testMoveBackward);
14   CPPUNIT_TEST (testMoveForward);
15   CPPUNIT_TEST_SUITE_END ();
16
17   public:
18         void setUp()
19         {
20                 
21         }
22
23         void tearDown()
24         {
25                 
26         }
27
28         void testZeroFillPod()
29         {
30                 unsigned frames = 10;
31                 float buf[frames];
32                 TypeUtils<float>::zero_fill (buf, frames);
33                 float zero = 0.0;
34                 for (unsigned i = 0; i < frames; ++i) {
35                         CPPUNIT_ASSERT_EQUAL (zero, buf[i]);
36                 }
37         }
38         
39         void testZeroFillNonPod()
40         {
41                 /* does not compile on OS X Lion
42                 unsigned frames = 10;
43                 NonPodType buf[frames];
44                 TypeUtils<NonPodType>::zero_fill (buf, frames);
45                 NonPodType zero;
46                 for (unsigned i = 0; i < frames; ++i) {
47                         CPPUNIT_ASSERT (zero == buf[i]);
48                 }
49                 */
50         }
51         
52         void testMoveBackward()
53         {
54                 int seq[8] = { 0, 1, 2, 3,
55                                4, 5, 6, 7 };
56                 
57                 TypeUtils<int>::move (&seq[4], &seq[2], 4);
58                 
59                 for (int i = 2; i < 2 + 4; ++i) {
60                         CPPUNIT_ASSERT_EQUAL (i + 2, seq[i]);
61                 }
62         }
63         
64         void testMoveForward()
65         {
66                 int seq[8] = { 0, 1, 2, 3,
67                                4, 5, 6, 7 };
68                 
69                 TypeUtils<int>::move (&seq[2], &seq[4], 4);
70                 
71                 for (int i = 4; i < 4 + 4; ++i) {
72                         CPPUNIT_ASSERT_EQUAL (i - 2, seq[i]);
73                 }
74         }
75
76         void testCopy()
77         {
78                 int const seq1[4] = { 1, 2, 3, 4 };
79                 int const seq2[4] = { 5, 6, 7, 8 };
80                 int seq3[8] = { 0, 0, 0, 0,
81                                   0, 0, 0, 0 };
82                 
83                 TypeUtils<int>::copy (seq1, seq3, 4);
84                 for (int i = 0; i < 4; ++i) {
85                         CPPUNIT_ASSERT_EQUAL (seq1[i], seq3[i]);
86                 }
87                 
88                 for (int i = 4; i < 8; ++i) {
89                         CPPUNIT_ASSERT_EQUAL (0, seq3[i]);
90                 }
91                 
92                 TypeUtils<int>::copy (seq2, &seq3[4], 4);
93                 for (int i = 0; i < 4; ++i) {
94                         CPPUNIT_ASSERT_EQUAL (seq1[i], seq3[i]);
95                 }
96                 for (int i = 0; i < 4; ++i) {
97                         CPPUNIT_ASSERT_EQUAL (seq2[i], seq3[4 + i]);
98                 }
99         }
100
101   private:
102         
103         struct NonPodType {
104                 NonPodType() : data (42) {}
105                 bool operator== (NonPodType const & other) const
106                         { return data == other.data; }
107                 int data;
108         };
109         
110
111 };
112
113 CPPUNIT_TEST_SUITE_REGISTRATION (TypeUtilsTest);
114