annotate Test.cpp @ 53:86d8a699dfbe

Don't object if two consecutive runs produce the same output, if neither has timestamps anyway
author Chris Cannam
date Fri, 12 Sep 2014 21:58:45 +0100
parents 4bd0cd3c60f3
children c7fd03f5ae02
rev   line source
cannam@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@0 2
cannam@0 3 /*
cannam@0 4 Vamp Plugin Fuzz Tester
cannam@0 5 Chris Cannam, cannam@all-day-breakfast.com
cannam@0 6 Centre for Digital Music, Queen Mary, University of London.
Chris@42 7 Copyright 2009-2014 QMUL.
cannam@0 8
cannam@0 9 This program loads a Vamp plugin and tests its susceptibility to a
cannam@0 10 number of common pitfalls, including handling of extremes of input
cannam@0 11 data. If you can think of any additional useful tests that are
cannam@0 12 easily added, please send them to me.
cannam@0 13
cannam@0 14 Permission is hereby granted, free of charge, to any person
cannam@0 15 obtaining a copy of this software and associated documentation
cannam@0 16 files (the "Software"), to deal in the Software without
cannam@0 17 restriction, including without limitation the rights to use, copy,
cannam@0 18 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@0 19 of the Software, and to permit persons to whom the Software is
cannam@0 20 furnished to do so, subject to the following conditions:
cannam@0 21
cannam@0 22 The above copyright notice and this permission notice shall be
cannam@0 23 included in all copies or substantial portions of the Software.
cannam@0 24
cannam@0 25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@0 26 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@0 27 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@0 28 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@0 29 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@0 30 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@0 31 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@0 32
cannam@0 33 Except as contained in this notice, the names of the Centre for
cannam@0 34 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@0 35 shall not be used in advertising or otherwise to promote the sale,
cannam@0 36 use or other dealings in this Software without prior written
cannam@0 37 authorization.
cannam@0 38 */
cannam@0 39
cannam@0 40 #include "Test.h"
cannam@0 41
cannam@0 42 #include <vamp-hostsdk/PluginLoader.h>
cannam@0 43
cannam@0 44 using namespace Vamp;
cannam@0 45 using namespace Vamp::HostExt;
cannam@0 46
cannam@20 47 #include <math.h>
cannam@1 48
cannam@28 49 #ifdef __SUNPRO_CC
cannam@28 50 #include <ieeefp.h>
cannam@28 51 #define isinf(x) (!finite(x))
cannam@28 52 #endif
cannam@28 53
cannam@0 54 Test::Test() { }
cannam@0 55 Test::~Test() { }
cannam@0 56
Chris@52 57 using std::cerr;
Chris@52 58 using std::cout;
Chris@52 59 using std::endl;
Chris@52 60 using std::string;
Chris@52 61
cannam@0 62 Plugin *
Chris@52 63 Test::load(string key, float rate)
cannam@0 64 {
cannam@23 65 Plugin *p = PluginLoader::getInstance()->loadPlugin
cannam@0 66 (key, rate, PluginLoader::ADAPT_ALL);
cannam@23 67 if (!p) throw FailedToLoadPlugin();
cannam@23 68 return p;
cannam@0 69 }
cannam@0 70
cannam@1 71 float **
cannam@1 72 Test::createBlock(size_t channels, size_t blocksize)
cannam@1 73 {
cannam@1 74 float **b = new float *[channels];
cannam@1 75 for (size_t c = 0; c < channels; ++c) {
cannam@1 76 b[c] = new float[blocksize];
cannam@1 77 }
cannam@1 78 return b;
cannam@1 79 }
cannam@1 80
cannam@1 81 void
cannam@1 82 Test::destroyBlock(float **blocks, size_t channels)
cannam@1 83 {
cannam@1 84 for (size_t c = 0; c < channels; ++c) {
cannam@1 85 delete[] blocks[c];
cannam@1 86 }
cannam@1 87 delete[] blocks;
cannam@1 88 }
cannam@1 89
cannam@3 90 float **
cannam@3 91 Test::createTestAudio(size_t channels, size_t blocksize, size_t blocks)
cannam@3 92 {
cannam@3 93 float **b = new float *[channels];
cannam@3 94 for (size_t c = 0; c < channels; ++c) {
cannam@3 95 b[c] = new float[blocksize * blocks];
cannam@3 96 for (int i = 0; i < int(blocksize * blocks); ++i) {
cannam@3 97 b[c][i] = sinf(float(i) / 10.f);
cannam@3 98 if (i == 5005 || i == 20002) {
cannam@3 99 b[c][i-2] = 0;
cannam@3 100 b[c][i-1] = -1;
cannam@3 101 b[c][i] = 1;
cannam@3 102 }
cannam@3 103 }
cannam@3 104 }
cannam@3 105 return b;
cannam@3 106 }
cannam@3 107
cannam@3 108 void
cannam@3 109 Test::destroyTestAudio(float **b, size_t channels)
cannam@3 110 {
cannam@3 111 for (size_t c = 0; c < channels; ++c) {
cannam@3 112 delete[] b[c];
cannam@3 113 }
cannam@3 114 delete[] b;
cannam@3 115 }
cannam@3 116
cannam@1 117 bool
cannam@1 118 Test::initDefaults(Plugin *p, size_t &channels, size_t &step, size_t &block,
cannam@1 119 Results &r)
cannam@1 120 {
cannam@1 121 channels = p->getMinChannelCount();
cannam@1 122 block = p->getPreferredBlockSize();
cannam@1 123 step = p->getPreferredStepSize();
cannam@1 124 if (block == 0) block = 1024;
cannam@1 125 if (step == 0) {
cannam@1 126 if (p->getInputDomain() == Plugin::FrequencyDomain) step = block/2;
cannam@1 127 else step = block;
cannam@1 128 }
cannam@1 129 if (!p->initialise(channels, step, block)) {
cannam@1 130 r.push_back(error("initialisation with default values failed"));
cannam@1 131 return false;
cannam@1 132 }
cannam@1 133 return true;
cannam@1 134 }
cannam@1 135
cannam@3 136 bool
cannam@3 137 Test::initAdapted(Plugin *p, size_t &channels, size_t step, size_t block,
cannam@3 138 Results &r)
cannam@3 139 {
cannam@3 140 channels = p->getMinChannelCount();
cannam@3 141 if (!p->initialise(channels, step, block)) {
cannam@3 142 r.push_back(error("initialisation failed"));
cannam@3 143 return false;
cannam@3 144 }
cannam@3 145 return true;
cannam@3 146 }
cannam@3 147
cannam@0 148 void
cannam@0 149 Test::appendFeatures(Plugin::FeatureSet &a, const Plugin::FeatureSet &b)
cannam@0 150 {
cannam@0 151 for (Plugin::FeatureSet::const_iterator i = b.begin(); i != b.end(); ++i) {
cannam@0 152 int output = i->first;
cannam@0 153 const Plugin::FeatureList &fl = i->second;
cannam@0 154 Plugin::FeatureList &target = a[output];
cannam@0 155 for (Plugin::FeatureList::const_iterator j = fl.begin(); j != fl.end(); ++j) {
cannam@0 156 target.push_back(*j);
cannam@0 157 }
cannam@0 158 }
cannam@0 159 }
cannam@0 160
cannam@0 161 bool
cannam@1 162 Test::allFeaturesValid(const Plugin::FeatureSet &b)
cannam@1 163 {
cannam@1 164 for (Plugin::FeatureSet::const_iterator i = b.begin(); i != b.end(); ++i) {
cannam@1 165 for (int j = 0; j < (int)i->second.size(); ++j) {
cannam@1 166 if (i->second[j].values.empty()) continue;
cannam@1 167 for (int k = 0; k < (int)i->second[j].values.size(); ++k) {
cannam@1 168 if (isnan(i->second[j].values[k]) ||
cannam@1 169 isinf(i->second[j].values[k])) {
cannam@1 170 return false;
cannam@1 171 }
cannam@1 172 }
cannam@1 173 }
cannam@1 174 }
cannam@1 175 return true;
cannam@1 176 }
cannam@1 177
Chris@53 178 bool
Chris@53 179 Test::containsTimestamps(const Plugin::FeatureSet &b)
Chris@53 180 {
Chris@53 181 for (Plugin::FeatureSet::const_iterator i = b.begin(); i != b.end(); ++i) {
Chris@53 182 for (int j = 0; j < (int)i->second.size(); ++j) {
Chris@53 183 if (i->second[j].values.empty()) continue;
Chris@53 184 for (int k = 0; k < (int)i->second[j].values.size(); ++k) {
Chris@53 185 if (i->second[j].hasTimestamp) {
Chris@53 186 return true;
Chris@53 187 }
Chris@53 188 }
Chris@53 189 }
Chris@53 190 }
Chris@53 191 return false;
Chris@53 192 }
Chris@53 193
cannam@3 194 void
Chris@52 195 Test::dumpFeature(const Plugin::Feature &f, bool showValues)
Chris@52 196 {
Chris@52 197 cout << " Timestamp: " << (!f.hasTimestamp ? "(none)" : f.timestamp.toText()) << endl;
Chris@52 198 cout << " Duration: " << (!f.hasDuration ? "(none)" : f.duration.toText()) << endl;
Chris@52 199 cout << " Label: " << (f.label == "" ? "(none)" : f.label) << endl;
Chris@52 200 if (showValues) {
Chris@52 201 cout << " Values (" << f.values.size() << "): " << (f.values.empty() ? "(none)" : "");
Chris@52 202 for (int j = 0; j < (int)f.values.size(); ++j) {
Chris@52 203 cout << f.values[j] << " ";
Chris@52 204 }
Chris@52 205 cout << endl;
Chris@52 206 } else {
Chris@52 207 cout << " Values (" << f.values.size() << "): (elided)" << endl;
Chris@52 208 }
Chris@52 209 }
Chris@52 210
Chris@52 211 void
Chris@52 212 Test::dump(const Plugin::FeatureSet &fs, bool showValues)
cannam@3 213 {
cannam@3 214 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
cannam@3 215 fsi != fs.end(); ++fsi) {
cannam@3 216 int output = fsi->first;
Chris@52 217 cout << "Output " << output << ":" << endl;
cannam@3 218 const Plugin::FeatureList &fl = fsi->second;
cannam@3 219 for (int i = 0; i < (int)fl.size(); ++i) {
Chris@52 220 cout << " Feature " << i << ":" << endl;
cannam@3 221 const Plugin::Feature &f = fl[i];
Chris@52 222 dumpFeature(f, showValues);
cannam@3 223 }
cannam@3 224 }
cannam@3 225 }
cannam@3 226
cannam@3 227 void
Chris@52 228 Test::dumpTwo(const Result &r,
Chris@52 229 const Plugin::FeatureSet &a,
Chris@52 230 const Plugin::FeatureSet &b)
cannam@3 231 {
cannam@8 232 std::cout << r.message() << std::endl;
cannam@8 233 std::cout << "\nFirst result set:" << std::endl;
Chris@52 234 dump(a, false);
cannam@8 235 std::cout << "\nSecond result set:" << std::endl;
Chris@52 236 dump(b, false);
cannam@8 237 std::cout << std::endl;
cannam@3 238 }
cannam@3 239
Chris@52 240 void
Chris@52 241 Test::dumpDiff(const Result &r,
Chris@52 242 const Plugin::FeatureSet &a,
Chris@52 243 const Plugin::FeatureSet &b)
Chris@52 244 {
Chris@52 245 cout << r.message() << endl;
Chris@52 246 cout << "\nDifferences follow:" << endl;
Chris@52 247 if (a.size() != b.size()) {
Chris@52 248 cout << "*** First result set has features on " << a.size()
Chris@52 249 << " output(s), second has features on " << b.size()
Chris@52 250 << endl;
Chris@52 251 return;
Chris@52 252 }
Chris@52 253 Plugin::FeatureSet::const_iterator ai = a.begin();
Chris@52 254 Plugin::FeatureSet::const_iterator bi = b.begin();
Chris@52 255 while (ai != a.end()) {
Chris@52 256 if (ai->first != bi->first) {
Chris@52 257 cout << "\n*** Output number mismatch: first result set says "
Chris@52 258 << ai->first << " where second says " << bi->first
Chris@52 259 << endl;
Chris@52 260 } else {
Chris@52 261 cout << "\nOutput " << ai->first << ":" << endl;
Chris@52 262 if (ai->second.size() != bi->second.size()) {
Chris@52 263 cout << "*** First result set has " << ai->second.size()
Chris@52 264 << " feature(s) on this output, second has "
Chris@52 265 << bi->second.size() << endl;
Chris@52 266 } else {
Chris@52 267 int fno = 0;
Chris@52 268 int diffcount = 0;
Chris@52 269 Plugin::FeatureList::const_iterator afi = ai->second.begin();
Chris@52 270 Plugin::FeatureList::const_iterator bfi = bi->second.begin();
Chris@52 271 while (afi != ai->second.end()) {
Chris@52 272 if (!(*afi == *bfi)) {
Chris@52 273 if (diffcount == 0) {
Chris@52 274 bool differInValues = (afi->values != bfi->values);
Chris@52 275 if (afi->hasTimestamp != bfi->hasTimestamp) {
Chris@52 276 cout << "*** Feature " << fno << " differs in presence of timestamp (" << afi->hasTimestamp << " vs " << bfi->hasTimestamp << ")" << endl;
Chris@52 277 }
Chris@52 278 if (afi->hasTimestamp && (afi->timestamp != bfi->timestamp)) {
Chris@52 279 cout << "*** Feature " << fno << " differs in timestamp (" << afi->timestamp << " vs " << bfi->timestamp << " )" << endl;
Chris@52 280 }
Chris@52 281 if (afi->hasDuration != bfi->hasDuration) {
Chris@52 282 cout << "*** Feature " << fno << " differs in presence of duration (" << afi->hasDuration << " vs " << bfi->hasDuration << ")" << endl;
Chris@52 283 }
Chris@52 284 if (afi->hasDuration && (afi->duration != bfi->duration)) {
Chris@52 285 cout << "*** Feature " << fno << " differs in duration (" << afi->duration << " vs " << bfi->duration << " )" << endl;
Chris@52 286 }
Chris@52 287 if (afi->label != bfi->label) {
Chris@52 288 cout << "*** Feature " << fno << " differs in label" << endl;
Chris@52 289 }
Chris@52 290 if (differInValues) {
Chris@52 291 cout << "*** Feature " << fno << " differs in values" << endl;
Chris@52 292 }
Chris@52 293 cout << " First output:" << endl;
Chris@52 294 dumpFeature(*afi, differInValues);
Chris@52 295 cout << " Second output:" << endl;
Chris@52 296 dumpFeature(*bfi, differInValues);
Chris@52 297 }
Chris@52 298 ++diffcount;
Chris@52 299 }
Chris@52 300 ++fno;
Chris@52 301 ++afi;
Chris@52 302 ++bfi;
Chris@52 303 }
Chris@52 304 if (diffcount > 1) {
Chris@52 305 cout << diffcount-1 << " subsequent differing feature(s) elided" << endl;
Chris@52 306 }
Chris@52 307 }
Chris@52 308 }
Chris@52 309 ++ai;
Chris@52 310 ++bi;
Chris@52 311 }
Chris@52 312 cout << endl;
Chris@52 313 }
Chris@52 314
cannam@1 315 bool
cannam@0 316 operator==(const Plugin::FeatureSet &a, const Plugin::FeatureSet &b)
cannam@0 317 {
cannam@0 318 if (a.size() != b.size()) return false;
cannam@0 319 for (Plugin::FeatureSet::const_iterator ai = a.begin();
cannam@0 320 ai != a.end(); ++ai) {
cannam@0 321 int output = ai->first;
cannam@0 322 Plugin::FeatureSet::const_iterator bi = b.find(output);
cannam@0 323 if (bi == b.end()) return false;
cannam@0 324 if (!(ai->second == bi->second)) return false;
cannam@0 325 }
cannam@0 326 return true;
cannam@0 327 }
cannam@0 328
cannam@0 329 bool
cannam@0 330 operator==(const Plugin::FeatureList &a, const Plugin::FeatureList &b)
cannam@0 331 {
cannam@0 332 if (a.size() != b.size()) return false;
cannam@0 333 for (int i = 0; i < (int)a.size(); ++i) {
cannam@0 334 if (!(a[i] == b[i])) return false;
cannam@0 335 }
cannam@0 336 return true;
cannam@0 337 }
cannam@0 338
cannam@0 339 bool
cannam@0 340 operator==(const Plugin::Feature &a, const Plugin::Feature &b)
cannam@0 341 {
cannam@0 342 if (a.hasTimestamp != b.hasTimestamp) return false;
cannam@0 343 if (a.hasTimestamp && (a.timestamp != b.timestamp)) return false;
cannam@0 344 if (a.hasDuration != b.hasDuration) return false;
cannam@0 345 if (a.hasDuration && (a.duration != b.duration)) return false;
cannam@0 346 if (a.values != b.values) return false;
cannam@0 347 if (a.label != b.label) return false;
cannam@0 348 return true;
cannam@0 349 }
cannam@0 350