~ubuntu-branches/ubuntu/vivid/soundscaperenderer/vivid

« back to all changes in this revision

Viewing changes to apf/unit_tests/test_misc.cpp

  • Committer: Package Import Robot
  • Author(s): IOhannes m zmölnig (Debian/GNU)
  • Date: 2014-05-08 16:58:09 UTC
  • Revision ID: package-import@ubuntu.com-20140508165809-7tz9dhu5pvo5wy25
Tags: upstream-0.4.1~dfsg
Import upstream version 0.4.1~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * Copyright © 2012-2014 Institut für Nachrichtentechnik, Universität Rostock *
 
3
 * Copyright © 2006-2012 Quality & Usability Lab,                             *
 
4
 *                       Telekom Innovation Laboratories, TU Berlin           *
 
5
 *                                                                            *
 
6
 * This file is part of the Audio Processing Framework (APF).                 *
 
7
 *                                                                            *
 
8
 * The APF is free software:  you can redistribute it and/or modify it  under *
 
9
 * the terms of the  GNU  General  Public  License  as published by the  Free *
 
10
 * Software Foundation, either version 3 of the License,  or (at your option) *
 
11
 * any later version.                                                         *
 
12
 *                                                                            *
 
13
 * The APF is distributed in the hope that it will be useful, but WITHOUT ANY *
 
14
 * WARRANTY;  without even the implied warranty of MERCHANTABILITY or FITNESS *
 
15
 * FOR A PARTICULAR PURPOSE.                                                  *
 
16
 * See the GNU General Public License for more details.                       *
 
17
 *                                                                            *
 
18
 * You should  have received a copy  of the GNU General Public License  along *
 
19
 * with this program.  If not, see <http://www.gnu.org/licenses/>.            *
 
20
 *                                                                            *
 
21
 *                                 http://AudioProcessingFramework.github.com *
 
22
 ******************************************************************************/
 
23
 
 
24
// Tests for misc.h.
 
25
 
 
26
#include "apf/misc.h"
 
27
 
 
28
#include <cassert>
 
29
#include "catch/catch.hpp"
 
30
 
 
31
template<typename Derived>
 
32
struct B : apf::CRTP<Derived>
 
33
{
 
34
  int value() { return this->derived().i; }
 
35
};
 
36
 
 
37
struct D : B<D>
 
38
{
 
39
  int i = 42;
 
40
};
 
41
 
 
42
TEST_CASE("CRTP", "")
 
43
{
 
44
auto d = D();
 
45
CHECK(d.value() == 42);
 
46
}
 
47
 
 
48
TEST_CASE("BlockParameter", "")
 
49
{
 
50
 
 
51
SECTION("default ctor", "")
 
52
{
 
53
  auto bp = apf::BlockParameter<int>();
 
54
  CHECK(0 == bp.get());
 
55
  CHECK(0 == bp.old());
 
56
}
 
57
 
 
58
SECTION("int", "")
 
59
{
 
60
  auto bp = apf::BlockParameter<int>(111);
 
61
  CHECK(111 == bp.get());
 
62
  CHECK(111 == bp.old());
 
63
  CHECK(111 == bp.both());
 
64
  CHECK(bp.both() == 111);
 
65
  CHECK_FALSE(bp.changed());
 
66
 
 
67
  // TODO: once Catch supports checking for asserts, enable this:
 
68
  //assert(bp.exactly_one_assignment());
 
69
 
 
70
  bp = 222;
 
71
  CHECK(222 == bp.get());
 
72
  CHECK(111 == bp.old());
 
73
  CHECK(bp.changed());
 
74
 
 
75
  CHECK_FALSE(111 == bp.both());
 
76
  CHECK_FALSE(bp.both() == 111);
 
77
  CHECK(bp.both() != 0);
 
78
  CHECK(0 != bp.both());
 
79
  CHECK(bp.both() > 0);
 
80
  CHECK(bp.both() >= 0);
 
81
  CHECK_FALSE(0 > bp.both());
 
82
  CHECK_FALSE(0 >= bp.both());
 
83
  CHECK_FALSE(bp.both() > 333);
 
84
  CHECK_FALSE(bp.both() >= 333);
 
85
  CHECK(333 > bp.both());
 
86
  CHECK(333 >= bp.both());
 
87
  CHECK(bp.both() < 333);
 
88
  CHECK(bp.both() <= 333);
 
89
  CHECK_FALSE(333 < bp.both());
 
90
  CHECK_FALSE(333 <= bp.both());
 
91
  CHECK_FALSE(bp.both() < 0);
 
92
  CHECK_FALSE(bp.both() <= 0);
 
93
  CHECK(0 < bp.both());
 
94
  CHECK(0 <= bp.both());
 
95
 
 
96
  assert(bp.exactly_one_assignment());
 
97
 
 
98
  bp = 333;
 
99
  CHECK(333 == bp.get());
 
100
  CHECK(222 == bp.old());
 
101
  CHECK(bp.changed());
 
102
 
 
103
  bp -= 111;
 
104
  CHECK_FALSE(bp.changed());
 
105
 
 
106
  ++bp;
 
107
  CHECK(222 == bp.old());
 
108
  CHECK(223 == bp.get());
 
109
 
 
110
  assert(bp.exactly_one_assignment());
 
111
}
 
112
 
 
113
SECTION("conversion operator", "")
 
114
{
 
115
  auto bp = apf::BlockParameter<int>(42);
 
116
  int i = 0;
 
117
  CHECK(0 == i);
 
118
  i = bp;
 
119
  CHECK(42 == i);
 
120
  CHECK((i - bp) == 0);
 
121
}
 
122
 
 
123
SECTION("conversion operator from const object", "")
 
124
{
 
125
  const auto bp = apf::BlockParameter<int>(42);
 
126
  int i = 0;
 
127
  CHECK(0 == i);
 
128
  i = bp;
 
129
  CHECK(42 == i);
 
130
  CHECK((i - bp) == 0);
 
131
}
 
132
 
 
133
struct NonCopyable
 
134
{
 
135
  NonCopyable(int) {};  // hypothetical constructor
 
136
  NonCopyable(const NonCopyable&) = delete;
 
137
  NonCopyable(NonCopyable&&) = default;
 
138
  NonCopyable& operator=(NonCopyable&& other) = default;
 
139
};
 
140
 
 
141
SECTION("non-copyable T", "")
 
142
{
 
143
  // These are just compile-time checks:
 
144
 
 
145
  auto bp = apf::BlockParameter<NonCopyable>{42};
 
146
  bp = NonCopyable(43);
 
147
};
 
148
 
 
149
// TODO: move CountCtors in a separate file?
 
150
 
 
151
struct CountCtors
 
152
{
 
153
  CountCtors() { ++ default_constructor; }
 
154
  CountCtors(const CountCtors&) { ++ copy_constructor; }
 
155
  CountCtors(CountCtors&&) { ++move_constructor; }
 
156
 
 
157
  CountCtors& operator=(const CountCtors&) { ++copy_assignment; return *this; };
 
158
  CountCtors& operator=(CountCtors&&) { ++move_assignment; return *this; };
 
159
 
 
160
  int default_constructor = 0;
 
161
  int copy_constructor = 0;
 
162
  int move_constructor = 0;
 
163
 
 
164
  int copy_assignment = 0;
 
165
  int move_assignment = 0;
 
166
};
 
167
 
 
168
SECTION("check if move ctor and move assignment is used", "")
 
169
{
 
170
  auto bp = apf::BlockParameter<CountCtors>{CountCtors()};
 
171
  CHECK(bp.get().copy_constructor == 1);
 
172
  CHECK(bp.old().move_constructor == 1);
 
173
 
 
174
  bp = CountCtors();
 
175
 
 
176
  CHECK(bp.get().move_assignment == 1);
 
177
  CHECK(bp.get().copy_assignment == 0);
 
178
  CHECK(bp.old().move_assignment == 1);
 
179
  CHECK(bp.old().copy_assignment == 0);
 
180
}
 
181
 
 
182
} // TEST_CASE
 
183
 
 
184
// Settings for Vim (http://www.vim.org/), please do not remove:
 
185
// vim:softtabstop=2:shiftwidth=2:expandtab:textwidth=80:cindent