51 unsigned char encoded[12];
54 assert(12 == encodedBytes);
55 assert(0x80 == encoded[8]);
56 assert(0x96 == encoded[9]);
57 assert(0x98 == encoded[10]);
58 assert(0x00 == encoded[11]);
60 cout <<
"+ pass encodeLinear1 " << endl << endl;
72 unsigned char encoded[20];
75 assert(18 == encodedBytes);
76 assert(0x80 == encoded[8]);
77 assert(0x96 == encoded[9]);
78 assert(0x98 == encoded[10]);
79 assert(0x00 == encoded[11]);
80 assert(0x75 == encoded[16]);
81 assert(0x80 == encoded[17]);
83 cout <<
"+ pass encodeLinear " << endl << endl;
96 unsigned char encoded[28];
102 assert(4 == numDecoded);
103 assert(abs(100.0 - decoded[0]) < 0.000005);
104 assert(abs(200.0 - decoded[1]) < 0.000005);
105 assert(abs(300.00005 - decoded[2]) < 0.000005);
106 assert(abs(400.00010 - decoded[3]) < 0.000005);
108 cout <<
"+ pass decodeLinearNice " << endl << endl;
124 unsigned char encoded[33];
130 assert( abs(5 - fixedPoint) < 0.000005);
133 assert( abs(500 - fixedPoint) < 0.000005);
136 assert( abs(50000 - fixedPoint) < 0.000005);
139 assert( abs(5000000 - fixedPoint) < 0.000005);
143 assert( abs(-1 - fixedPoint) < 0.000005);
152 assert(25 == encodedBytes);
153 assert(7 == numDecoded);
155 assert(abs(100.0 - decoded[0]) < 0.001);
156 assert(abs(200.0 - decoded[1]) < 0.001);
157 assert(abs(300.00005 - decoded[2]) < 0.001);
158 assert(abs(400.00010 - decoded[3]) < 0.001);
162 double encodedLength[5];
165 mz_err[0] = 0.1; encodedLength[0] = 22;
166 mz_err[1] = 1e-3; encodedLength[1] = 25;
167 mz_err[2] = 1e-5; encodedLength[2] = 29;
168 mz_err[3] = 1e-6; encodedLength[3] = 30;
169 mz_err[4] = 1e-7; encodedLength[4] = 31;
171 for (
int k = 0; k < 4; k++)
178 assert( encodedLength[k] == encodedBytes);
179 assert(7 == numDecoded);
180 assert(abs(100.0 - decoded[0]) < mz_err[k]);
181 assert(abs(200.0 - decoded[1]) < mz_err[k]);
182 assert(abs(300.00005 - decoded[2]) < mz_err[k]);
183 assert(abs(400.00010 - decoded[3]) < mz_err[k]);
184 assert(abs(450.00010 - decoded[4]) < mz_err[k]);
185 assert(abs(455.00010 - decoded[5]) < mz_err[k]);
186 assert(abs(700.00010 - decoded[6]) < mz_err[k]);
189 cout <<
"+ pass decodeLinearNiceFP " << endl << endl;
201 unsigned char encoded[28];
207 assert(4 == numDecoded);
208 assert(abs(100.0 - decoded[0]) < 0.000005);
209 assert(abs(200.0 - decoded[1]) < 0.000005);
210 assert(abs(300.00005 - decoded[2]) < 0.000005);
211 assert(abs(0.00010 - decoded[3]) < 0.000005);
213 cout <<
"+ pass decodeLinearWierd " << endl << endl;
221 mzs[2] = 30000000.00005;
225 unsigned char encoded[28];
229 cout <<
"- fail test decodeLinearWierd_llong_overflow: didn't throw exception for corrupt input " << endl << endl;
231 }
catch (
const char *err) {
232 assert( std::string(err) == std::string(
"[MSNumpress::encodeLinear] Next number overflows LLONG_MAX."));
234 cout <<
"+ pass decodeLinearWierd_llong_overflow " << endl << endl;
242 mzs[2] = 30000000.00005;
246 unsigned char encoded[28];
250 cout <<
"- fail test decodeLinearWierd_int_overflow: didn't throw exception for corrupt input " << endl << endl;
252 }
catch (
const char *err) {
253 assert( std::string(err) == std::string(
"[MSNumpress::encodeLinear] Cannot encode a number that exceeds the bounds of [-INT_MAX, INT_MAX]."));
255 cout <<
"+ pass decodeLinearWierd3 " << endl << endl;
261 mzs[0] = 30000000.00005;
262 mzs[1] = 60000000.00005;
263 mzs[2] = 30000000.00005;
267 unsigned char encoded[28];
271 cout <<
"- fail test decodeLinearWierd_int_underflow: didn't throw exception for corrupt input " << endl << endl;
273 }
catch (
const char *err) {
274 assert( std::string(err) == std::string(
"[MSNumpress::encodeLinear] Cannot encode a number that exceeds the bounds of [-INT_MAX, INT_MAX]."));
276 cout <<
"+ pass decodeLinearWierd3 " << endl << endl;
280 unsigned char encoded[20];
285 cout <<
"- fail decodeLinearCorrupt1: didn't throw exception for corrupt input " << endl << endl;
287 }
catch (
const char *err) {
293 cout <<
"- fail decodeLinearCorrupt1: didn't throw exception for corrupt input " << endl << endl;
295 }
catch (
const char *err) {
299 cout <<
"+ pass decodeLinearCorrupt 1 " << endl << endl;
313 unsigned char encoded[28];
320 cout <<
"- fail decodeLinearCorrupt2: didn't throw exception for corrupt input " << endl << endl;
322 }
catch (
const char *err) {
326 cout <<
"+ pass decodeLinearCorrupt 2 " << endl << endl;
336 mzs[0] = 300 + (rand() % 1000) / 1000.0;
337 for (
size_t i=1; i<n; i++)
338 mzs[i] = mzs[i-1] + (rand() % 1000) / 1000.0;
340 cout <<
"+ max val: " << mzs[n-1] << endl;
342 cout <<
"+ pass optimalLinearFixedPoint " << endl << endl;
351 mzs[0] = 300 + (rand() % 1000) / 1000.0;
352 for (
size_t i=1; i<n; i++)
353 mzs[i] = mzs[i-1] + (rand() % 1000) / 1000.0;
358 assert(abs(5.0 - fixedP) < 0.000005);
361 assert(abs(50.0 - fixedP) < 0.000005);
364 assert(abs(500.0 - fixedP) < 0.000005);
366 cout <<
"+ optimal linear fp: " << fixedP << endl;
367 cout <<
"+ pass optimalLinearFixedPointMass " << endl << endl;
373 for (
size_t i=0; i<n; i++)
378 unsigned char encoded[75];
384 assert(n == numDecoded);
388 double mLim = 0.000005;
390 for (
size_t i=0; i<n; i++) {
391 error = abs(mzs[i] - decoded[i]);
394 cout <<
"error " << error <<
" above limit " << mLim << endl;
395 assert(error < mLim);
398 cout <<
"+ size compressed: " << encodedBytes / double(n*8) * 100 <<
"% " << endl;
399 cout <<
"+ max error: " << m <<
" limit: " << mLim << endl;
400 cout <<
"+ pass encodeDecodeLinearStraight " << endl << endl;
407 double eLim = 1.0e-300;
410 for (
size_t i=0; i<n; i++)
413 unsigned char encoded[8000];
427 double decoded[1000];
430 assert(numDecoded == n);
431 for (
size_t i=0; i<n; i++) {
432 error = abs(mzs[i] - decoded[i]);
434 cout <<
"error " << error <<
" is non-zero ( >= " << eLim <<
" )" << endl;
438 cout <<
"+ pass encodeDecodeSafeStraight " << endl << endl;
447 double eLim = 1.0e-300;
450 mzs[0] = 300 + rand() / double(RAND_MAX);
451 for (
size_t i=1; i<n; i++)
452 mzs[i] = mzs[i-1] + rand() / double(RAND_MAX);
454 unsigned char encoded[8000];
457 assert(encodedBytes == n*8);
459 double decoded[1000];
462 assert(numDecoded == n);
463 for (
size_t i=0; i<n; i++) {
464 error = abs(mzs[i] - decoded[i]);
466 cout <<
"error " << error <<
" is non-zero ( >= " << eLim <<
" )" << endl;
470 cout <<
"+ pass encodeDecodeSafe " << endl << endl;
480 mzs[0] = 300 + rand() / double(RAND_MAX);
481 for (
size_t i=1; i<n; i++)
482 mzs[i] = mzs[i-1] + rand() / double(RAND_MAX);
486 unsigned char encoded[5000];
489 double decoded[1000];
492 assert(n == numDecoded);
496 double mLim = 0.000005;
498 for (
size_t i=0; i<n; i++) {
499 error = abs(mzs[i] - decoded[i]);
502 cout <<
"error " << error <<
" above limit " << mLim << endl;
503 assert(error < mLim);
506 cout <<
"+ size compressed: " << encodedBytes / double(n*8) * 100 <<
"% " << endl;
507 cout <<
"+ max error: " << m <<
" limit: " << mLim << endl;
508 cout <<
"+ pass encodeDecodeLinear " << endl << endl;
518 mzs[0] = 100 + (rand() % 1000) / 1000.0;
519 for (
size_t i=1; i<n; i++)
520 mzs[i] = mzs[i-1] + (rand() % 1000) / 1000.0;
524 unsigned char encoded[5000];
525 double firstDecoded[1000];
526 double decoded[1000];
533 for (
size_t i=0; i<n; i++)
534 firstDecoded[i] = decoded[i];
536 for (
size_t i=0; i<5; i++) {
543 assert(fixedPoint == afterFixedPoint);
544 assert(n == numDecoded);
546 for (
size_t i=0; i<n; i++)
547 if (firstDecoded[i] != decoded[i]) {
548 cout << endl << firstDecoded[i] <<
" " << decoded[i] << endl;
549 assert(firstDecoded[i] == decoded[i]);
552 cout <<
"+ pass encodeDecodeLinear5 " << endl << endl;
562 for (
size_t i=0; i<n; i++)
563 ics[i] = rand() % 1000000;
565 unsigned char encoded[5000];
568 double decoded[1000];
571 assert(n == numDecoded);
573 for (
size_t i=0; i<n; i++)
574 assert(abs(ics[i] - decoded[i]) < 0.5);
576 cout <<
"+ pass encodeDecodePic " << endl << endl;
586 for (
size_t i=0; i<n; i++)
587 ics[i] = rand() % 1000000;
595 unsigned char encoded[5000];
596 double firstDecoded[1000];
597 double decoded[1000];
602 for (
size_t i=0; i<n; i++)
603 firstDecoded[i] = decoded[i];
605 for (
size_t i=0; i<5; i++) {
610 assert(n == numDecoded);
612 for (
size_t i=0; i<n; i++)
613 if (firstDecoded[i] != decoded[i]) {
614 cout << endl << firstDecoded[i] <<
" " << decoded[i] << endl;
615 assert(firstDecoded[i] == decoded[i]);
618 cout <<
"+ pass encodeDecodePic5 " << endl << endl;
629 for (
size_t i=0; i<n; i++)
630 ics[i] = rand() % 1000000;
633 cout <<
"+ pass optimalSlofFixedPoint " << endl << endl;
643 for (
size_t i=0; i<n; i++)
644 ics[i] = rand() % 1000000;
652 unsigned char encoded[5000];
655 double decoded[1000];
658 assert(n == numDecoded);
662 double mLim = 0.0005;
663 double rmLim = 0.0005;
666 for (
size_t i=0; i<n; i++)
668 error = abs(ics[i] - decoded[i]);
671 cout << endl << ics[i] <<
" " << decoded[i] << endl;
672 assert(error < mLim);
675 error = abs((ics[i] - decoded[i]) / ((ics[i] + decoded[i])/2));
677 if (error >= rmLim) {
678 cout << endl << ics[i] <<
" " << decoded[i] << endl;
679 assert(error < rmLim);
682 cout <<
"+ max error: " << m <<
" limit: " << mLim << endl;
683 cout <<
"+ max rel error: " << rm <<
" limit: " << rmLim << endl;
684 cout <<
"+ pass encodeDecodeSlof " << endl << endl;
694 for (
size_t i=0; i<n; i++)
695 ics[i] = rand() % 1000000;
705 unsigned char encoded[5000];
706 double firstDecoded[1000];
707 double decoded[1000];
712 for (
size_t i=0; i<n; i++)
713 firstDecoded[i] = decoded[i];
715 for (
size_t i=0; i<5; i++) {
722 assert(n == numDecoded);
723 assert(fixedPoint == afterFixedPoint);
725 for (
size_t i=0; i<n; i++)
726 if (firstDecoded[i] != decoded[i]) {
727 cout << endl << firstDecoded[i] <<
" " << decoded[i] << endl;
728 assert(firstDecoded[i] == decoded[i]);
731 cout <<
"+ pass encodeDecodeSlof5 " << endl << endl;
737 std::vector<double> result;
740 std::vector<unsigned char> data;
752 cout <<
"- fail testErroneousDecodePic: didn't throw exception for corrupt input " << endl << endl;
754 }
catch (
const char *err) {
758 cout <<
"+ pass testErroneousDecodePic " << endl << endl;
762int main(
int argc,
const char* argv[]) {
784 cout <<
"=== all tests succeeded! ===" << endl;
void encodeDecodeLinearStraight()
double ENC_TWO_BYTE_FIXED_POINT
void decodeLinearCorrupt1()
void testErroneousDecodePic()
void decodeLinearWierd_int_overflow()
void optimalLinearFixedPointMass()
void decodeLinearWierd_int_underflow()
void encodeDecodeLinear5()
void decodeLinearWierd_llong_overflow()
void encodeDecodeLinear()
void optimalSlofFixedPoint()
void decodeLinearCorrupt2()
void optimalLinearFixedPoint()
int main(int argc, const char *argv[])
void decodeLinearNiceLowFP()
void encodeDecodeSafeStraight()
size_t encodePic(const double *data, size_t dataSize, unsigned char *result)
double optimalLinearFixedPoint(const double *data, size_t dataSize)
size_t decodeLinear(const unsigned char *data, const size_t dataSize, double *result)
double optimalSlofFixedPoint(const double *data, size_t dataSize)
size_t decodeSlof(const unsigned char *data, const size_t dataSize, double *result)
size_t encodeSlof(const double *data, size_t dataSize, unsigned char *result, double fixedPoint)
size_t encodeSafe(const double *data, const size_t dataSize, unsigned char *result)
size_t decodeSafe(const unsigned char *data, const size_t dataSize, double *result)
size_t decodePic(const unsigned char *data, const size_t dataSize, double *result)
size_t encodeLinear(const double *data, size_t dataSize, unsigned char *result, double fixedPoint)
double optimalLinearFixedPointMass(const double *data, size_t dataSize, double mass_acc)