Chris@1528: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@1528: Chris@1528: /* Chris@1528: Sonic Visualiser Chris@1528: An audio file viewer and annotation editor. Chris@1528: Centre for Digital Music, Queen Mary, University of London. Chris@1528: Chris@1528: This program is free software; you can redistribute it and/or Chris@1528: modify it under the terms of the GNU General Public License as Chris@1528: published by the Free Software Foundation; either version 2 of the Chris@1528: License, or (at your option) any later version. See the file Chris@1528: COPYING included with this distribution for more information. Chris@1528: */ Chris@1528: Chris@1528: #ifndef TEST_ZOOM_CONSTRAINTS_H Chris@1528: #define TEST_ZOOM_CONSTRAINTS_H Chris@1528: Chris@1528: #include "../PowerOfTwoZoomConstraint.h" Chris@1528: #include "../PowerOfSqrtTwoZoomConstraint.h" Chris@1528: Chris@1528: #include Chris@1528: #include Chris@1528: #include Chris@1528: Chris@1528: #include Chris@1528: Chris@1528: using namespace std; Chris@1528: Chris@1528: class TestZoomConstraints : public QObject Chris@1528: { Chris@1528: Q_OBJECT Chris@1528: Chris@1528: private slots: Chris@1528: void unconstrainedNearest() { Chris@1528: ZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3), 3); Chris@1528: QCOMPARE(c.getNearestBlockSize(4), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(20), 20); Chris@1528: QCOMPARE(c.getNearestBlockSize(23), 23); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1), max); Chris@1528: } Chris@1528: Chris@1528: void unconstrainedUp() { Chris@1528: ZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 3); Chris@1528: QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundUp), 20); Chris@1528: QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundUp), 32); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); Chris@1528: } Chris@1528: Chris@1528: void unconstrainedDown() { Chris@1528: ZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 3); Chris@1528: QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundDown), 20); Chris@1528: QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundDown), 32); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); Chris@1528: } Chris@1528: Chris@1528: void powerOfTwoNearest() { Chris@1528: PowerOfTwoZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(4), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(20), 16); Chris@1528: QCOMPARE(c.getNearestBlockSize(23), 16); Chris@1528: QCOMPARE(c.getNearestBlockSize(24), 16); Chris@1528: QCOMPARE(c.getNearestBlockSize(25), 32); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1), max); Chris@1528: } Chris@1528: Chris@1528: void powerOfTwoUp() { Chris@1528: PowerOfTwoZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundUp), 32); Chris@1528: QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundUp), 32); Chris@1528: QCOMPARE(c.getNearestBlockSize(33, ZoomConstraint::RoundUp), 64); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); Chris@1528: } Chris@1528: Chris@1528: void powerOfTwoDown() { Chris@1528: PowerOfTwoZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(20, ZoomConstraint::RoundDown), 16); Chris@1528: QCOMPARE(c.getNearestBlockSize(32, ZoomConstraint::RoundDown), 32); Chris@1528: QCOMPARE(c.getNearestBlockSize(33, ZoomConstraint::RoundDown), 32); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); Chris@1528: } Chris@1528: Chris@1528: void powerOfSqrtTwoNearest() { Chris@1528: PowerOfSqrtTwoZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(4), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(18), 16); Chris@1528: QCOMPARE(c.getNearestBlockSize(19), 16); Chris@1528: QCOMPARE(c.getNearestBlockSize(20), 22); Chris@1528: QCOMPARE(c.getNearestBlockSize(23), 22); Chris@1528: QCOMPARE(c.getNearestBlockSize(28), 32); Chris@1528: // PowerOfSqrtTwoZoomConstraint makes an effort to ensure Chris@1528: // bigger numbers get rounded to a multiple of something Chris@1528: // simple (64 or 90 depending on whether they are power-of-two Chris@1528: // or power-of-sqrt-two types) Chris@1528: QCOMPARE(c.getNearestBlockSize(800), 720); Chris@1528: QCOMPARE(c.getNearestBlockSize(1023), 1024); Chris@1528: QCOMPARE(c.getNearestBlockSize(1024), 1024); Chris@1528: QCOMPARE(c.getNearestBlockSize(1025), 1024); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1), max); Chris@1528: } Chris@1528: Chris@1528: void powerOfSqrtTwoUp() { Chris@1528: PowerOfSqrtTwoZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundUp), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundUp), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundUp), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundUp), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(18, ZoomConstraint::RoundUp), 22); Chris@1528: QCOMPARE(c.getNearestBlockSize(22, ZoomConstraint::RoundUp), 22); Chris@1528: QCOMPARE(c.getNearestBlockSize(23, ZoomConstraint::RoundUp), 32); Chris@1528: QCOMPARE(c.getNearestBlockSize(800, ZoomConstraint::RoundUp), 1024); Chris@1528: QCOMPARE(c.getNearestBlockSize(1023, ZoomConstraint::RoundUp), 1024); Chris@1528: QCOMPARE(c.getNearestBlockSize(1024, ZoomConstraint::RoundUp), 1024); Chris@1528: // see comment above Chris@1528: QCOMPARE(c.getNearestBlockSize(1025, ZoomConstraint::RoundUp), 1440); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundUp), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundUp), max); Chris@1528: } Chris@1528: Chris@1528: void powerOfSqrtTwoDown() { Chris@1528: PowerOfSqrtTwoZoomConstraint c; Chris@1528: QCOMPARE(c.getNearestBlockSize(1, ZoomConstraint::RoundDown), 1); Chris@1528: QCOMPARE(c.getNearestBlockSize(2, ZoomConstraint::RoundDown), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(3, ZoomConstraint::RoundDown), 2); Chris@1528: QCOMPARE(c.getNearestBlockSize(4, ZoomConstraint::RoundDown), 4); Chris@1528: QCOMPARE(c.getNearestBlockSize(18, ZoomConstraint::RoundDown), 16); Chris@1528: QCOMPARE(c.getNearestBlockSize(22, ZoomConstraint::RoundDown), 22); Chris@1528: QCOMPARE(c.getNearestBlockSize(23, ZoomConstraint::RoundDown), 22); Chris@1528: // see comment above Chris@1528: QCOMPARE(c.getNearestBlockSize(800, ZoomConstraint::RoundDown), 720); Chris@1528: QCOMPARE(c.getNearestBlockSize(1023, ZoomConstraint::RoundDown), 720); Chris@1528: QCOMPARE(c.getNearestBlockSize(1024, ZoomConstraint::RoundDown), 1024); Chris@1528: QCOMPARE(c.getNearestBlockSize(1025, ZoomConstraint::RoundDown), 1024); Chris@1528: int max = c.getMaxZoomLevel(); Chris@1528: QCOMPARE(c.getNearestBlockSize(max, ZoomConstraint::RoundDown), max); Chris@1528: QCOMPARE(c.getNearestBlockSize(max+1, ZoomConstraint::RoundDown), max); Chris@1528: } Chris@1528: }; Chris@1528: Chris@1528: #endif Chris@1528: