sclass Test_CompressToInterpolatedDoubleArray { settable int maxPillars = 5; settable int minX =-10, maxX = 10; settable int maxRunlength = 10; settable int nTests = oneMillion(); // result gettable int maxPixelError; gettable int pixelErrors; run { for (double slope = -1; slope <= 1; slope += 1/8.0) singleSlopeTest(slope); repeat nTests { actualVectorTest(); } printResults(); } void printResults { printVars(+pixelErrors, +maxPixelError); if (pixelErrors == 0) print("PERFECT!"); else if (maxPixelError == 1) print("A bit less than optimal, but only by 1 pixel"); else print("This is not good."); } void singleSlopeTest(double slope) { int n = 16; var data = intArrayFromFunction(n, i -> iround(slope*i)); testCompression(data, 2); } void testCompression(int[] data, int maxPillarsAllowed) { var compressed = CompressToInterpolatedDoubleArray(data)!; printStruct(compressed); var reconstructed = compressed!; var reconstructed2 = iroundDoubleArray(reconstructed); // Test reconstructed array assertEquals(l(data), l(reconstructed2)); for i over data: { int diff = absDiff(data[i], reconstructed2[i]); maxPixelError = max(maxPixelError, diff); if (diff != 0) ++pixelErrors; } // Test that we have achieved sufficient compression assertLessThanOrEqualTo(maxPillarsAllowed, compressed.nPillars()); } int randomX() { ret random_incl(minX, maxX); } void actualVectorTest() { int nPillars = random_incl(2, maxPillars); new IntBuffer values; // first pillar values.add(randomX()); repeat nPillars { int value = randomX(); int runlength = random_incl(1, maxRunlength); if (runlength > 1) { int lastValue = last(values); double slope = (value-lastValue)/(double) runlength; double x = lastValue; for (int i = 1; i < runlength; i++) values.add(iround(x += slope)); } values.add(value); } int[] data = values.toArrayNonNull(); testCompression(data, nPillars); } }