annotate Test.cpp @ 52:4bd0cd3c60f3

Fix erroneous timestamp printing in verbose feature output (it was printing (none) if there was a timestamp instead of if there wasn't); print only diffs; update SDK subrepo
author Chris Cannam
date Fri, 12 Sep 2014 18:05:52 +0100
parents f1e8e14e9c96
children 86d8a699dfbe
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
cannam@3 178 void
Chris@52 179 Test::dumpFeature(const Plugin::Feature &f, bool showValues)
Chris@52 180 {
Chris@52 181 cout << " Timestamp: " << (!f.hasTimestamp ? "(none)" : f.timestamp.toText()) << endl;
Chris@52 182 cout << " Duration: " << (!f.hasDuration ? "(none)" : f.duration.toText()) << endl;
Chris@52 183 cout << " Label: " << (f.label == "" ? "(none)" : f.label) << endl;
Chris@52 184 if (showValues) {
Chris@52 185 cout << " Values (" << f.values.size() << "): " << (f.values.empty() ? "(none)" : "");
Chris@52 186 for (int j = 0; j < (int)f.values.size(); ++j) {
Chris@52 187 cout << f.values[j] << " ";
Chris@52 188 }
Chris@52 189 cout << endl;
Chris@52 190 } else {
Chris@52 191 cout << " Values (" << f.values.size() << "): (elided)" << endl;
Chris@52 192 }
Chris@52 193 }
Chris@52 194
Chris@52 195 void
Chris@52 196 Test::dump(const Plugin::FeatureSet &fs, bool showValues)
cannam@3 197 {
cannam@3 198 for (Plugin::FeatureSet::const_iterator fsi = fs.begin();
cannam@3 199 fsi != fs.end(); ++fsi) {
cannam@3 200 int output = fsi->first;
Chris@52 201 cout << "Output " << output << ":" << endl;
cannam@3 202 const Plugin::FeatureList &fl = fsi->second;
cannam@3 203 for (int i = 0; i < (int)fl.size(); ++i) {
Chris@52 204 cout << " Feature " << i << ":" << endl;
cannam@3 205 const Plugin::Feature &f = fl[i];
Chris@52 206 dumpFeature(f, showValues);
cannam@3 207 }
cannam@3 208 }
cannam@3 209 }
cannam@3 210
cannam@3 211 void
Chris@52 212 Test::dumpTwo(const Result &r,
Chris@52 213 const Plugin::FeatureSet &a,
Chris@52 214 const Plugin::FeatureSet &b)
cannam@3 215 {
cannam@8 216 std::cout << r.message() << std::endl;
cannam@8 217 std::cout << "\nFirst result set:" << std::endl;
Chris@52 218 dump(a, false);
cannam@8 219 std::cout << "\nSecond result set:" << std::endl;
Chris@52 220 dump(b, false);
cannam@8 221 std::cout << std::endl;
cannam@3 222 }
cannam@3 223
Chris@52 224 void
Chris@52 225 Test::dumpDiff(const Result &r,
Chris@52 226 const Plugin::FeatureSet &a,
Chris@52 227 const Plugin::FeatureSet &b)
Chris@52 228 {
Chris@52 229 cout << r.message() << endl;
Chris@52 230 cout << "\nDifferences follow:" << endl;
Chris@52 231 if (a.size() != b.size()) {
Chris@52 232 cout << "*** First result set has features on " << a.size()
Chris@52 233 << " output(s), second has features on " << b.size()
Chris@52 234 << endl;
Chris@52 235 return;
Chris@52 236 }
Chris@52 237 Plugin::FeatureSet::const_iterator ai = a.begin();
Chris@52 238 Plugin::FeatureSet::const_iterator bi = b.begin();
Chris@52 239 while (ai != a.end()) {
Chris@52 240 if (ai->first != bi->first) {
Chris@52 241 cout << "\n*** Output number mismatch: first result set says "
Chris@52 242 << ai->first << " where second says " << bi->first
Chris@52 243 << endl;
Chris@52 244 } else {
Chris@52 245 cout << "\nOutput " << ai->first << ":" << endl;
Chris@52 246 if (ai->second.size() != bi->second.size()) {
Chris@52 247 cout << "*** First result set has " << ai->second.size()
Chris@52 248 << " feature(s) on this output, second has "
Chris@52 249 << bi->second.size() << endl;
Chris@52 250 } else {
Chris@52 251 int fno = 0;
Chris@52 252 int diffcount = 0;
Chris@52 253 Plugin::FeatureList::const_iterator afi = ai->second.begin();
Chris@52 254 Plugin::FeatureList::const_iterator bfi = bi->second.begin();
Chris@52 255 while (afi != ai->second.end()) {
Chris@52 256 if (!(*afi == *bfi)) {
Chris@52 257 if (diffcount == 0) {
Chris@52 258 bool differInValues = (afi->values != bfi->values);
Chris@52 259 if (afi->hasTimestamp != bfi->hasTimestamp) {
Chris@52 260 cout << "*** Feature " << fno << " differs in presence of timestamp (" << afi->hasTimestamp << " vs " << bfi->hasTimestamp << ")" << endl;
Chris@52 261 }
Chris@52 262 if (afi->hasTimestamp && (afi->timestamp != bfi->timestamp)) {
Chris@52 263 cout << "*** Feature " << fno << " differs in timestamp (" << afi->timestamp << " vs " << bfi->timestamp << " )" << endl;
Chris@52 264 }
Chris@52 265 if (afi->hasDuration != bfi->hasDuration) {
Chris@52 266 cout << "*** Feature " << fno << " differs in presence of duration (" << afi->hasDuration << " vs " << bfi->hasDuration << ")" << endl;
Chris@52 267 }
Chris@52 268 if (afi->hasDuration && (afi->duration != bfi->duration)) {
Chris@52 269 cout << "*** Feature " << fno << " differs in duration (" << afi->duration << " vs " << bfi->duration << " )" << endl;
Chris@52 270 }
Chris@52 271 if (afi->label != bfi->label) {
Chris@52 272 cout << "*** Feature " << fno << " differs in label" << endl;
Chris@52 273 }
Chris@52 274 if (differInValues) {
Chris@52 275 cout << "*** Feature " << fno << " differs in values" << endl;
Chris@52 276 }
Chris@52 277 cout << " First output:" << endl;
Chris@52 278 dumpFeature(*afi, differInValues);
Chris@52 279 cout << " Second output:" << endl;
Chris@52 280 dumpFeature(*bfi, differInValues);
Chris@52 281 }
Chris@52 282 ++diffcount;
Chris@52 283 }
Chris@52 284 ++fno;
Chris@52 285 ++afi;
Chris@52 286 ++bfi;
Chris@52 287 }
Chris@52 288 if (diffcount > 1) {
Chris@52 289 cout << diffcount-1 << " subsequent differing feature(s) elided" << endl;
Chris@52 290 }
Chris@52 291 }
Chris@52 292 }
Chris@52 293 ++ai;
Chris@52 294 ++bi;
Chris@52 295 }
Chris@52 296 cout << endl;
Chris@52 297 }
Chris@52 298
cannam@1 299 bool
cannam@0 300 operator==(const Plugin::FeatureSet &a, const Plugin::FeatureSet &b)
cannam@0 301 {
cannam@0 302 if (a.size() != b.size()) return false;
cannam@0 303 for (Plugin::FeatureSet::const_iterator ai = a.begin();
cannam@0 304 ai != a.end(); ++ai) {
cannam@0 305 int output = ai->first;
cannam@0 306 Plugin::FeatureSet::const_iterator bi = b.find(output);
cannam@0 307 if (bi == b.end()) return false;
cannam@0 308 if (!(ai->second == bi->second)) return false;
cannam@0 309 }
cannam@0 310 return true;
cannam@0 311 }
cannam@0 312
cannam@0 313 bool
cannam@0 314 operator==(const Plugin::FeatureList &a, const Plugin::FeatureList &b)
cannam@0 315 {
cannam@0 316 if (a.size() != b.size()) return false;
cannam@0 317 for (int i = 0; i < (int)a.size(); ++i) {
cannam@0 318 if (!(a[i] == b[i])) return false;
cannam@0 319 }
cannam@0 320 return true;
cannam@0 321 }
cannam@0 322
cannam@0 323 bool
cannam@0 324 operator==(const Plugin::Feature &a, const Plugin::Feature &b)
cannam@0 325 {
cannam@0 326 if (a.hasTimestamp != b.hasTimestamp) return false;
cannam@0 327 if (a.hasTimestamp && (a.timestamp != b.timestamp)) return false;
cannam@0 328 if (a.hasDuration != b.hasDuration) return false;
cannam@0 329 if (a.hasDuration && (a.duration != b.duration)) return false;
cannam@0 330 if (a.values != b.values) return false;
cannam@0 331 if (a.label != b.label) return false;
cannam@0 332 return true;
cannam@0 333 }
cannam@0 334