Warning: session_start(): open(/var/lib/php/sessions/sess_5dcsmcc1249apr5qdp998vrkef, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
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 {
simpleTests();
repeat nTests {
actualVectorTest();
}
printResults();
}
void simpleTests {
for (double slope = -1; slope <= 1; slope += 1/8.0)
singleSlopeTest(slope);
printResults();
}
void printResults {
print();
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) {
printStruct(data);
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);
}
}