Mercurial > hg > svcore
diff data/model/test/TestZoomConstraints.h @ 1551:4de4284d0596
Merge from branch zoom
author | Chris Cannam |
---|---|
date | Wed, 10 Oct 2018 08:44:15 +0100 |
parents | 2f3a77472c8c |
children | 05c3fbaec8ea |
line wrap: on
line diff
--- a/data/model/test/TestZoomConstraints.h Wed Oct 03 15:45:57 2018 +0100 +++ b/data/model/test/TestZoomConstraints.h Wed Oct 10 08:44:15 2018 +0100 @@ -1,15 +1,15 @@ /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ /* - Sonic Visualiser - An audio file viewer and annotation editor. - Centre for Digital Music, Queen Mary, University of London. + Sonic Visualiser + An audio file viewer and annotation editor. + Centre for Digital Music, Queen Mary, University of London. - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. See the file - COPYING included with this distribution for more information. + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. See the file + COPYING included with this distribution for more information. */ #ifndef TEST_ZOOM_CONSTRAINTS_H @@ -30,149 +30,171 @@ { Q_OBJECT + void checkFpp(const ZoomConstraint &c, + ZoomConstraint::RoundingDirection dir, + int n, + int expected) { + QCOMPARE(c.getNearestZoomLevel(ZoomLevel(ZoomLevel::FramesPerPixel, n), + dir), + ZoomLevel(ZoomLevel::FramesPerPixel, expected)); + } + private slots: void unconstrainedNearest() { ZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1), 1); - QCOMPARE(c.getNearestBlockSize(2), 2); - QCOMPARE(c.getNearestBlockSize(3), 3); - QCOMPARE(c.getNearestBlockSize(4), 4); - QCOMPARE(c.getNearestBlockSize(20), 20); - QCOMPARE(c.getNearestBlockSize(23), 23); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max), max); - QCOMPARE(c.getNearestBlockSize(max+1), max); + checkFpp(c, ZoomConstraint::RoundNearest, 1, 1); + checkFpp(c, ZoomConstraint::RoundNearest, 2, 2); + checkFpp(c, ZoomConstraint::RoundNearest, 3, 3); + checkFpp(c, ZoomConstraint::RoundNearest, 4, 4); + checkFpp(c, ZoomConstraint::RoundNearest, 20, 20); + checkFpp(c, ZoomConstraint::RoundNearest, 32, 32); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented()), max); } void unconstrainedUp() { ZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); - QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); - QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 3); - QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); - QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundUp), 20); - QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundUp), 32); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); - QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); + checkFpp(c, ZoomConstraint::RoundUp, 1, 1); + checkFpp(c, ZoomConstraint::RoundUp, 2, 2); + checkFpp(c, ZoomConstraint::RoundUp, 3, 3); + checkFpp(c, ZoomConstraint::RoundUp, 4, 4); + checkFpp(c, ZoomConstraint::RoundUp, 20, 20); + checkFpp(c, ZoomConstraint::RoundUp, 32, 32); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max, + ZoomConstraint::RoundUp), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented(), + ZoomConstraint::RoundUp), max); } void unconstrainedDown() { ZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); - QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); - QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 3); - QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); - QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundDown), 20); - QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundDown), 32); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); - QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); + checkFpp(c, ZoomConstraint::RoundDown, 1, 1); + checkFpp(c, ZoomConstraint::RoundDown, 2, 2); + checkFpp(c, ZoomConstraint::RoundDown, 3, 3); + checkFpp(c, ZoomConstraint::RoundDown, 4, 4); + checkFpp(c, ZoomConstraint::RoundDown, 20, 20); + checkFpp(c, ZoomConstraint::RoundDown, 32, 32); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max, + ZoomConstraint::RoundDown), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented(), + ZoomConstraint::RoundDown), max); } void powerOfTwoNearest() { PowerOfTwoZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1), 1); - QCOMPARE(c.getNearestBlockSize(2), 2); - QCOMPARE(c.getNearestBlockSize(3), 2); - QCOMPARE(c.getNearestBlockSize(4), 4); - QCOMPARE(c.getNearestBlockSize(20), 16); - QCOMPARE(c.getNearestBlockSize(23), 16); - QCOMPARE(c.getNearestBlockSize(24), 16); - QCOMPARE(c.getNearestBlockSize(25), 32); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max), max); - QCOMPARE(c.getNearestBlockSize(max+1), max); + checkFpp(c, ZoomConstraint::RoundNearest, 1, 1); + checkFpp(c, ZoomConstraint::RoundNearest, 2, 2); + checkFpp(c, ZoomConstraint::RoundNearest, 3, 2); + checkFpp(c, ZoomConstraint::RoundNearest, 4, 4); + checkFpp(c, ZoomConstraint::RoundNearest, 20, 16); + checkFpp(c, ZoomConstraint::RoundNearest, 23, 16); + checkFpp(c, ZoomConstraint::RoundNearest, 24, 16); + checkFpp(c, ZoomConstraint::RoundNearest, 25, 32); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented()), max); } void powerOfTwoUp() { PowerOfTwoZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); - QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); - QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 4); - QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); - QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundUp), 32); - QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundUp), 32); - QCOMPARE(c.getNearestBlockSize(33, ZoomConstraint::RoundUp), 64); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); - QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); + checkFpp(c, ZoomConstraint::RoundUp, 1, 1); + checkFpp(c, ZoomConstraint::RoundUp, 2, 2); + checkFpp(c, ZoomConstraint::RoundUp, 3, 4); + checkFpp(c, ZoomConstraint::RoundUp, 4, 4); + checkFpp(c, ZoomConstraint::RoundUp, 20, 32); + checkFpp(c, ZoomConstraint::RoundUp, 32, 32); + checkFpp(c, ZoomConstraint::RoundUp, 33, 64); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max, + ZoomConstraint::RoundUp), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented(), + ZoomConstraint::RoundUp), max); } void powerOfTwoDown() { PowerOfTwoZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); - QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); - QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 2); - QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); - QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundDown), 16); - QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundDown), 32); - QCOMPARE(c.getNearestBlockSize(33, ZoomConstraint::RoundDown), 32); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); - QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); + checkFpp(c, ZoomConstraint::RoundDown, 1, 1); + checkFpp(c, ZoomConstraint::RoundDown, 2, 2); + checkFpp(c, ZoomConstraint::RoundDown, 3, 2); + checkFpp(c, ZoomConstraint::RoundDown, 4, 4); + checkFpp(c, ZoomConstraint::RoundDown, 20, 16); + checkFpp(c, ZoomConstraint::RoundDown, 32, 32); + checkFpp(c, ZoomConstraint::RoundDown, 33, 32); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max, + ZoomConstraint::RoundDown), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented(), + ZoomConstraint::RoundDown), max); } void powerOfSqrtTwoNearest() { PowerOfSqrtTwoZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1), 1); - QCOMPARE(c.getNearestBlockSize(2), 2); - QCOMPARE(c.getNearestBlockSize(3), 2); - QCOMPARE(c.getNearestBlockSize(4), 4); - QCOMPARE(c.getNearestBlockSize(18), 16); - QCOMPARE(c.getNearestBlockSize(19), 16); - QCOMPARE(c.getNearestBlockSize(20), 22); - QCOMPARE(c.getNearestBlockSize(23), 22); - QCOMPARE(c.getNearestBlockSize(28), 32); + checkFpp(c, ZoomConstraint::RoundNearest, 1, 1); + checkFpp(c, ZoomConstraint::RoundNearest, 2, 2); + checkFpp(c, ZoomConstraint::RoundNearest, 3, 2); + checkFpp(c, ZoomConstraint::RoundNearest, 4, 4); + checkFpp(c, ZoomConstraint::RoundNearest, 18, 16); + checkFpp(c, ZoomConstraint::RoundNearest, 19, 16); + checkFpp(c, ZoomConstraint::RoundNearest, 20, 22); + checkFpp(c, ZoomConstraint::RoundNearest, 23, 22); + checkFpp(c, ZoomConstraint::RoundNearest, 28, 32); // PowerOfSqrtTwoZoomConstraint makes an effort to ensure // bigger numbers get rounded to a multiple of something // simple (64 or 90 depending on whether they are power-of-two // or power-of-sqrt-two types) - QCOMPARE(c.getNearestBlockSize(800), 720); - QCOMPARE(c.getNearestBlockSize(1023), 1024); - QCOMPARE(c.getNearestBlockSize(1024), 1024); - QCOMPARE(c.getNearestBlockSize(1025), 1024); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max), max); - QCOMPARE(c.getNearestBlockSize(max+1), max); + checkFpp(c, ZoomConstraint::RoundNearest, 800, 720); + checkFpp(c, ZoomConstraint::RoundNearest, 1023, 1024); + checkFpp(c, ZoomConstraint::RoundNearest, 1024, 1024); + checkFpp(c, ZoomConstraint::RoundNearest, 1024, 1024); + checkFpp(c, ZoomConstraint::RoundNearest, 1025, 1024); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented()), max); } void powerOfSqrtTwoUp() { PowerOfSqrtTwoZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); - QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); - QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 4); - QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); - QCOMPARE(c.getNearestBlockSize(18, ZoomConstraint::RoundUp), 22); - QCOMPARE(c.getNearestBlockSize(22, ZoomConstraint::RoundUp), 22); - QCOMPARE(c.getNearestBlockSize(23, ZoomConstraint::RoundUp), 32); - QCOMPARE(c.getNearestBlockSize(800, ZoomConstraint::RoundUp), 1024); - QCOMPARE(c.getNearestBlockSize(1023, ZoomConstraint::RoundUp), 1024); - QCOMPARE(c.getNearestBlockSize(1024, ZoomConstraint::RoundUp), 1024); + checkFpp(c, ZoomConstraint::RoundUp, 1, 1); + checkFpp(c, ZoomConstraint::RoundUp, 2, 2); + checkFpp(c, ZoomConstraint::RoundUp, 3, 4); + checkFpp(c, ZoomConstraint::RoundUp, 4, 4); + checkFpp(c, ZoomConstraint::RoundUp, 18, 22); + checkFpp(c, ZoomConstraint::RoundUp, 22, 22); + checkFpp(c, ZoomConstraint::RoundUp, 23, 32); + checkFpp(c, ZoomConstraint::RoundUp, 800, 1024); + checkFpp(c, ZoomConstraint::RoundUp, 1023, 1024); + checkFpp(c, ZoomConstraint::RoundUp, 1024, 1024); // see comment above - QCOMPARE(c.getNearestBlockSize(1025, ZoomConstraint::RoundUp), 1440); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); - QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); + checkFpp(c, ZoomConstraint::RoundUp, 1025, 1440); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max, + ZoomConstraint::RoundUp), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented(), + ZoomConstraint::RoundUp), max); } void powerOfSqrtTwoDown() { PowerOfSqrtTwoZoomConstraint c; - QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); - QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); - QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 2); - QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); - QCOMPARE(c.getNearestBlockSize(18, ZoomConstraint::RoundDown), 16); - QCOMPARE(c.getNearestBlockSize(22, ZoomConstraint::RoundDown), 22); - QCOMPARE(c.getNearestBlockSize(23, ZoomConstraint::RoundDown), 22); + checkFpp(c, ZoomConstraint::RoundDown, 1, 1); + checkFpp(c, ZoomConstraint::RoundDown, 2, 2); + checkFpp(c, ZoomConstraint::RoundDown, 3, 2); + checkFpp(c, ZoomConstraint::RoundDown, 4, 4); + checkFpp(c, ZoomConstraint::RoundDown, 18, 16); + checkFpp(c, ZoomConstraint::RoundDown, 22, 22); + checkFpp(c, ZoomConstraint::RoundDown, 23, 22); // see comment above - QCOMPARE(c.getNearestBlockSize(800, ZoomConstraint::RoundDown), 720); - QCOMPARE(c.getNearestBlockSize(1023, ZoomConstraint::RoundDown), 720); - QCOMPARE(c.getNearestBlockSize(1024, ZoomConstraint::RoundDown), 1024); - QCOMPARE(c.getNearestBlockSize(1025, ZoomConstraint::RoundDown), 1024); - int max = c.getMaxZoomLevel(); - QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); - QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); + checkFpp(c, ZoomConstraint::RoundDown, 800, 720); + checkFpp(c, ZoomConstraint::RoundDown, 1023, 720); + checkFpp(c, ZoomConstraint::RoundDown, 1024, 1024); + checkFpp(c, ZoomConstraint::RoundDown, 1025, 1024); + auto max = c.getMaxZoomLevel(); + QCOMPARE(c.getNearestZoomLevel(max, + ZoomConstraint::RoundDown), max); + QCOMPARE(c.getNearestZoomLevel(max.incremented(), + ZoomConstraint::RoundDown), max); } };