diff data/model/test/TestZoomConstraints.h @ 1552:05c3fbaec8ea

Introduce RelativelyFineZoomConstraint, which encodes more-or-less the scheme that was already used for the horizontal thumbwheel in the pane (which overrode the layers' own zoom constraints unless they said they couldn't support any other)
author Chris Cannam
date Wed, 10 Oct 2018 14:32:34 +0100
parents 2f3a77472c8c
children 616d3e8a250b
line wrap: on
line diff
--- a/data/model/test/TestZoomConstraints.h	Wed Oct 10 08:44:15 2018 +0100
+++ b/data/model/test/TestZoomConstraints.h	Wed Oct 10 14:32:34 2018 +0100
@@ -17,6 +17,7 @@
 
 #include "../PowerOfTwoZoomConstraint.h"
 #include "../PowerOfSqrtTwoZoomConstraint.h"
+#include "../RelativelyFineZoomConstraint.h"
 
 #include <QObject>
 #include <QtTest>
@@ -30,24 +31,90 @@
 {
     Q_OBJECT
 
+    string roundingName(ZoomConstraint::RoundingDirection dir) {
+        switch (dir) {
+        case ZoomConstraint::RoundDown: return "RoundDown";
+        case ZoomConstraint::RoundUp: return "RoundUp";
+        case ZoomConstraint::RoundNearest: return "RoundNearest";
+        }
+        return "<?>";
+    }
+    
+    void compare(ZoomLevel zin,
+                 ZoomConstraint::RoundingDirection dir,
+                 ZoomLevel zobt,
+                 ZoomLevel zexp) {
+        if (zexp.level == 1) {
+            // A zoom level of "1 pixel per frame" is not considered
+            // canonical - it should be "1 frame per pixel"
+            zexp.zone = ZoomLevel::FramesPerPixel;
+        }
+        if (zobt == zexp) {
+            return;
+        } else {
+            cerr << "For input " << zin << " and rounding direction "
+                 << roundingName(dir)
+                 << ", expected output " << zexp << " but obtained " << zobt
+                 << endl;
+            QCOMPARE(zobt, zexp);
+        }
+    }
+
     void checkFpp(const ZoomConstraint &c,
                   ZoomConstraint::RoundingDirection dir,
                   int n,
                   int expected) {
-        QCOMPARE(c.getNearestZoomLevel(ZoomLevel(ZoomLevel::FramesPerPixel, n),
-                                       dir),
-                 ZoomLevel(ZoomLevel::FramesPerPixel, expected));
+        ZoomLevel zin(ZoomLevel::FramesPerPixel, n);
+        ZoomLevel zexp(ZoomLevel::FramesPerPixel, expected);
+        ZoomLevel zobt(c.getNearestZoomLevel(zin, dir));
+        compare(zin, dir, zobt, zexp);
     }
-    
+
+    void checkPpf(const ZoomConstraint &c,
+                  ZoomConstraint::RoundingDirection dir,
+                  int n,
+                  int expected) {
+        ZoomLevel zin(ZoomLevel::PixelsPerFrame, n);
+        ZoomLevel zexp(ZoomLevel::PixelsPerFrame, expected);
+        ZoomLevel zobt(c.getNearestZoomLevel(zin, dir));
+        compare(zin, dir, zobt, zexp);
+    }
+
+    void checkBoth(const ZoomConstraint &c,
+                   ZoomConstraint::RoundingDirection dir,
+                   int n,
+                   int expected) {
+        checkFpp(c, dir, n, expected);
+        checkPpf(c, dir, n, expected);
+    }
+
+    void checkMaxMin(const ZoomConstraint &c,
+                     ZoomConstraint::RoundingDirection dir) {
+        auto max = c.getMaxZoomLevel();
+        compare(max, dir,
+                c.getNearestZoomLevel(max, dir), max);
+        compare(max.incremented(), dir,
+                c.getNearestZoomLevel(max.incremented(), dir), max);
+        auto min = c.getMinZoomLevel();
+        compare(min, dir,
+                c.getNearestZoomLevel(min, dir), min);
+        compare(min.decremented(), dir,
+                c.getNearestZoomLevel(min.decremented(), dir), min);
+    }
+
+    const static ZoomConstraint::RoundingDirection up = ZoomConstraint::RoundUp;
+    const static ZoomConstraint::RoundingDirection down = ZoomConstraint::RoundDown;
+    const static ZoomConstraint::RoundingDirection nearest = ZoomConstraint::RoundNearest;
+                                                                         
 private slots:
     void unconstrainedNearest() {
         ZoomConstraint c;
-        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);
+        checkBoth(c, nearest, 1, 1);
+        checkBoth(c, nearest, 2, 2);
+        checkBoth(c, nearest, 3, 3);
+        checkBoth(c, nearest, 4, 4);
+        checkBoth(c, nearest, 20, 20);
+        checkBoth(c, nearest, 32, 32);
         auto max = c.getMaxZoomLevel();
         QCOMPARE(c.getNearestZoomLevel(max), max);
         QCOMPARE(c.getNearestZoomLevel(max.incremented()), max);
@@ -55,44 +122,40 @@
     
     void unconstrainedUp() {
         ZoomConstraint c;
-        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);
+        checkBoth(c, up, 1, 1);
+        checkBoth(c, up, 2, 2);
+        checkBoth(c, up, 3, 3);
+        checkBoth(c, up, 4, 4);
+        checkBoth(c, up, 20, 20);
+        checkBoth(c, up, 32, 32);
         auto max = c.getMaxZoomLevel();
-        QCOMPARE(c.getNearestZoomLevel(max,
-                                       ZoomConstraint::RoundUp), max);
-        QCOMPARE(c.getNearestZoomLevel(max.incremented(),
-                                       ZoomConstraint::RoundUp), max);
+        QCOMPARE(c.getNearestZoomLevel(max, up), max);
+        QCOMPARE(c.getNearestZoomLevel(max.incremented(), up), max);
     }
     
     void unconstrainedDown() {
         ZoomConstraint c;
-        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);
+        checkBoth(c, down, 1, 1);
+        checkBoth(c, down, 2, 2);
+        checkBoth(c, down, 3, 3);
+        checkBoth(c, down, 4, 4);
+        checkBoth(c, down, 20, 20);
+        checkBoth(c, down, 32, 32);
         auto max = c.getMaxZoomLevel();
-        QCOMPARE(c.getNearestZoomLevel(max,
-                                       ZoomConstraint::RoundDown), max);
-        QCOMPARE(c.getNearestZoomLevel(max.incremented(),
-                                       ZoomConstraint::RoundDown), max);
+        QCOMPARE(c.getNearestZoomLevel(max, down), max);
+        QCOMPARE(c.getNearestZoomLevel(max.incremented(), down), max);
     }
 
     void powerOfTwoNearest() {
         PowerOfTwoZoomConstraint c;
-        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);
+        checkBoth(c, nearest, 1, 1);
+        checkBoth(c, nearest, 2, 2);
+        checkBoth(c, nearest, 3, 2);
+        checkBoth(c, nearest, 4, 4);
+        checkBoth(c, nearest, 20, 16);
+        checkBoth(c, nearest, 23, 16);
+        checkBoth(c, nearest, 24, 16);
+        checkBoth(c, nearest, 25, 32);
         auto max = c.getMaxZoomLevel();
         QCOMPARE(c.getNearestZoomLevel(max), max);
         QCOMPARE(c.getNearestZoomLevel(max.incremented()), max);
@@ -100,101 +163,157 @@
     
     void powerOfTwoUp() {
         PowerOfTwoZoomConstraint c;
-        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);
+        checkBoth(c, up, 1, 1);
+        checkBoth(c, up, 2, 2);
+        checkFpp(c, up, 3, 4);
+        checkPpf(c, up, 3, 2);
+        checkBoth(c, up, 4, 4);
+        checkFpp(c, up, 20, 32);
+        checkPpf(c, up, 20, 16);
+        checkBoth(c, up, 32, 32);
+        checkFpp(c, up, 33, 64);
+        checkPpf(c, up, 33, 32);
+        checkMaxMin(c, up);
     }
     
     void powerOfTwoDown() {
         PowerOfTwoZoomConstraint c;
-        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);
+        checkBoth(c, down, 1, 1);
+        checkBoth(c, down, 2, 2);
+        checkFpp(c, down, 3, 2);
+        checkPpf(c, down, 3, 4);
+        checkBoth(c, down, 4, 4);
+        checkFpp(c, down, 20, 16);
+        checkPpf(c, down, 20, 32);
+        checkBoth(c, down, 32, 32);
+        checkFpp(c, down, 33, 32);
+        checkPpf(c, down, 33, 64);
+        checkMaxMin(c, down);
     }
 
     void powerOfSqrtTwoNearest() {
         PowerOfSqrtTwoZoomConstraint c;
-        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);
+        checkBoth(c, nearest, 1, 1);
+        checkBoth(c, nearest, 2, 2);
+        checkBoth(c, nearest, 3, 2);
+        checkBoth(c, nearest, 4, 4);
+        checkBoth(c, nearest, 18, 16);
+        checkBoth(c, nearest, 19, 16);
+        checkBoth(c, nearest, 20, 22);
+        checkBoth(c, nearest, 23, 22);
+        checkBoth(c, nearest, 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)
-        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);
+        checkBoth(c, nearest, 350, 360);
+        // The most extreme level available in ppf mode
+        // (getMinZoomLevel()) is currently 512, so these bigger
+        // numbers will only happen in fpp mode
+        checkFpp(c, nearest, 800, 720);
+        checkFpp(c, nearest, 1023, 1024);
+        checkFpp(c, nearest, 1024, 1024);
+        checkFpp(c, nearest, 1024, 1024);
+        checkFpp(c, nearest, 1025, 1024);
+        checkPpf(c, nearest, 800, 512);
+        checkPpf(c, nearest, 1025, 512);
+        checkMaxMin(c, nearest);
     }
     
     void powerOfSqrtTwoUp() {
         PowerOfSqrtTwoZoomConstraint c;
-        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
-        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);
+        checkBoth(c, up, 1, 1);
+        checkBoth(c, up, 2, 2);
+        checkFpp(c, up, 3, 4);
+        checkPpf(c, up, 3, 2);
+        checkBoth(c, up, 4, 4);
+        checkFpp(c, up, 18, 22);
+        checkPpf(c, up, 18, 16);
+        checkBoth(c, up, 22, 22);
+        checkFpp(c, up, 23, 32);
+        checkPpf(c, up, 23, 22);
+        // see comments above
+        checkFpp(c, up, 800, 1024);
+        checkFpp(c, up, 1023, 1024);
+        checkFpp(c, up, 1024, 1024);
+        checkFpp(c, up, 1025, 1440);
+        checkPpf(c, up, 300, 256);
+        checkPpf(c, up, 800, 512);
+        checkPpf(c, up, 1600, 512);
+        checkMaxMin(c, up);
     }
     
     void powerOfSqrtTwoDown() {
         PowerOfSqrtTwoZoomConstraint c;
-        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
-        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);
+        checkBoth(c, down, 1, 1);
+        checkBoth(c, down, 2, 2);
+        checkFpp(c, down, 3, 2);
+        checkPpf(c, down, 3, 4);
+        checkBoth(c, down, 4, 4);
+        checkFpp(c, down, 18, 16);
+        checkPpf(c, down, 18, 22);
+        checkBoth(c, down, 22, 22);
+        checkFpp(c, down, 23, 22);
+        checkPpf(c, down, 23, 32);
+        // see comments above
+        checkFpp(c, down, 800, 720);
+        checkFpp(c, down, 1023, 720);
+        checkFpp(c, down, 1024, 1024);
+        checkFpp(c, down, 1025, 1024);
+        checkPpf(c, down, 300, 360);
+        checkPpf(c, down, 800, 512);
+        checkPpf(c, down, 1600, 512);
+        checkMaxMin(c, down);
+    }
+
+    void relativelyFineNearest() {
+        RelativelyFineZoomConstraint c;
+        checkBoth(c, nearest, 1, 1);
+        checkBoth(c, nearest, 2, 2);
+        checkBoth(c, nearest, 3, 3);
+        checkBoth(c, nearest, 4, 4);
+        checkBoth(c, nearest, 20, 20);
+        checkBoth(c, nearest, 33, 32);
+        checkBoth(c, nearest, 59, 56);
+        checkBoth(c, nearest, 69, 72);
+        checkBoth(c, nearest, 121, 128);
+        checkMaxMin(c, nearest);
+    }
+    
+    void relativelyFineUp() {
+        RelativelyFineZoomConstraint c;
+        checkBoth(c, up, 1, 1);
+        checkBoth(c, up, 2, 2);
+        checkBoth(c, up, 3, 3);
+        checkBoth(c, up, 4, 4);
+        checkBoth(c, up, 20, 20);
+        checkFpp(c, up, 33, 36);
+        checkPpf(c, up, 33, 32);
+        checkFpp(c, up, 59, 64);
+        checkPpf(c, up, 59, 56);
+        checkFpp(c, up, 69, 72);
+        checkPpf(c, up, 69, 64);
+        checkFpp(c, up, 121, 128);
+        checkPpf(c, up, 121, 112);
+        checkMaxMin(c, up);
+    }
+    
+    void relativelyFineDown() {
+        RelativelyFineZoomConstraint c;
+        checkBoth(c, down, 1, 1);
+        checkBoth(c, down, 2, 2);
+        checkBoth(c, down, 3, 3);
+        checkBoth(c, down, 4, 4);
+        checkBoth(c, down, 20, 20);
+        checkFpp(c, down, 33, 32);
+        checkPpf(c, down, 33, 36);
+        checkFpp(c, down, 59, 56);
+        checkPpf(c, down, 59, 64);
+        checkFpp(c, down, 69, 64);
+        checkPpf(c, down, 69, 72);
+        checkFpp(c, down, 121, 112);
+        checkPpf(c, down, 121, 128);
+        checkMaxMin(c, down);
     }
 };