Lerp precision.
[string-lerp.git] / tests / string-lerp.js
index 5728e7f..f2fb88d 100644 (file)
@@ -114,6 +114,22 @@ JS.Test.describe('diff lerp', function () { with (this) {
             assert(r === "a" || r === UNUSUAL_Q);
         }
     }});
+
+    it("prefers ins/del to sub/sub", function () { with (this) {
+        // When the cost is uniform this string can be transformed by
+        // rewriting the whole thing for the same cost as deleting the
+        // front and adding to the back. But visually, we'd rather do
+        // the latter.
+        assertEqual("core", lerp("hard core", "core dump", 0.50));
+    }});
+
+    it("weights ins/del cheaper than sub", function () { with (this) {
+        // When the cost is uniform it is cheaper to rewrite the
+        // former into the latter. But we'd rather keep the "core" for
+        // visual reasons, so we need to make sure we have unequal
+        // costs.
+        assertEqual("core", lerp("apple core", "core dump", 0.51));
+    }});
 }});
 
 JS.Test.describe('numeric lerp', function () { with (this) {
@@ -136,11 +152,16 @@ JS.Test.describe('numeric lerp', function () { with (this) {
 
     it("rounds integers", function () { with (this) {
         assertEqual("12", lerp("0", "100", 0.123));
-        assertEqual("12.3", lerp("0", "100.0", 0.123));
+        assertEqual("12.3", lerp("0.0", "100.0", 0.123));
         assertEqual("12.3", lerp("0.0", "100", 0.123));
         assertEqual("12.3", lerp("0.0", "100.0", 0.123));
     }});
 
+    it("thinks about precision", function () { with (this) {
+        assertEqual("12.30", lerp("0.00", "100.00", 0.123));
+        assertEqual("12.300", lerp("0.000", "100.000", 0.123));
+    }});
+
     it("computes parameters outside [0, 1]", function () { with (this) {
         assertEqual("Giving 110%", lerp("Giving 0%", "Giving 100%", 1.1));
     }});
@@ -153,3 +174,36 @@ JS.Test.describe('numeric lerp', function () { with (this) {
         assertEqual("Chapter 5. The sky was rgb(0, 0, 128).", lerp(A, B, 0.5));
     }});
 }});
+
+JS.Test.describe('fast lerp', function () { with (this) {
+    var lerp = m.lerp;
+    var A = "Do you like green eggs and ham?";
+    var B = "I do not like them, Sam-I-am.";
+
+    it("handles empty strings", function () { with (this) {
+        assertEqual("", lerp("", "", -1));
+        assertEqual("", lerp("", "", 0));
+        assertEqual("", lerp("", "", 0.5));
+        assertEqual("", lerp("", "", 1));
+        assertEqual("", lerp("", "", 2));
+    }});
+
+    it("maintains identity", function () { with (this) {
+        for (var i = -1; i < 2; i += 1/1024) {
+            assertEqual(A, lerp(A, A, i));
+            assertEqual(B, lerp(B, B, i));
+        }
+    }});
+
+    it("handles lows", function () { with (this) {
+        assertEqual(A, lerp(A, B, -Infinity));
+        assertEqual(A, lerp(A, B, -1));
+        assertEqual(A, lerp(A, B, 0));
+    }});
+
+    it("handles highs", function () { with (this) {
+        assertEqual(B, lerp(A, B, 1));
+        assertEqual(B, lerp(A, B, 2));
+        assertEqual(B, lerp(A, B, Infinity));
+    }});
+}});