- Timestamp:
- Jun 8, 2015, 11:01:33 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ohana.20150429/src/libfits/test/zlib.c
r38404 r38427 1 # include <stdio.h> 2 # include <stdlib.h> 3 # include <string.h> 4 # include <assert.h> 1 # include <ohana.h> 2 # include <gfitsio.h> 5 3 # include <zlib.h> 6 4 # include "tap_ohana.h" 5 6 # define VERBOSE 0 7 7 # define NSRC 10 8 8 # define NTGT 20 … … 18 18 int main (int argc, char **argv) { 19 19 20 plan_tests (7); 21 22 diag ("libfits zlib.c tests"); 23 24 diag ("zlib version: %s", ZLIB_VERSION); 25 diag ("zlib vernum: %x", ZLIB_VERNUM); 26 20 27 int i, err; 21 28 double srcbuf[NSRC], tgtbuf[NTGT], outbuf[NOUT]; … … 41 48 } 42 49 43 # ifdef VERBOSE44 50 char *rawdata = (char *) srcbuf; 45 fprintf (stderr, "inp: ");46 for (i = 0; i < NSRC*8; i++) {51 if (VERBOSE) fprintf (stderr, "inp: "); 52 for (i = 0; VERBOSE && (i < NSRC*8); i++) { 47 53 fprintf (stderr, "0x%02hhx ", rawdata[i]); 48 54 } 49 fprintf (stderr, "\n"); 50 # endif 55 if (VERBOSE) fprintf (stderr, "\n"); 51 56 52 57 // the '5' is the compression level: make this a user argument 53 58 err = deflateInit(&zdn, 5); 54 //fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", zdn.avail_in, zdn.avail_out, (int) zdn.total_out);55 if (err != Z_OK) { fprintf (stdout, "not ok: error 1: %d vs %d\n", err, Z_OK); exit (1); }59 if (VERBOSE) fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", zdn.avail_in, zdn.avail_out, (int) zdn.total_out); 60 ok (err == Z_OK, "result is Z_OK: %d vs %d", err, Z_OK); 56 61 57 62 err = deflate(&zdn, Z_FINISH); 58 // fprintf (stderr, "out buffers cmp 1: %d => %d => %d\n", zdn.avail_in, zdn.avail_out, (int) zdn.total_out); 59 if (err != Z_STREAM_END) { fprintf (stdout, "not ok: error 2: %d vs %d\n", err, Z_STREAM_END); exit (2); } 60 // fprintf (stderr, "out buffers cmp 2: %d => %d => %d\n", zdn.avail_in, zdn.avail_out, (int) zdn.total_out); 63 if (VERBOSE) fprintf (stderr, "out buffers cmp 1: %d => %d => %d\n", zdn.avail_in, zdn.avail_out, (int) zdn.total_out); 64 ok (err == Z_STREAM_END, "result is Z_STREAM_END: %d vs %d", err, Z_STREAM_END); 61 65 62 66 // dump out the data which failed to uncompress (and the input data) 63 # ifdef VERBOSE64 67 char *cmpdata = (char *) tgtbuf; 65 fprintf (stderr, "out: ");66 for (i = 0; i < zdn.total_out; i++) {68 if (VERBOSE) fprintf (stderr, "out: "); 69 for (i = 0; VERBOSE && (i < zdn.total_out); i++) { 67 70 fprintf (stderr, "0x%02hhx ", cmpdata[i]); 68 71 } 69 fprintf (stderr, "\n"); 70 # endif 71 72 assert (zdn.total_out <= NTGT*sizeof(double)); 73 74 // fprintf (stderr, "result: %d bytes\n", (int) zdn.total_out); 75 // for (i = 0; i < zdn.total_out / sizeof(double); i++) { 76 // fprintf (stderr, "%d : 0x%02hhx\n", i, tgtbuf[i]); 77 // } 72 if (VERBOSE) fprintf (stderr, "\n"); 73 74 ok (zdn.total_out <= NTGT*sizeof(double), "output compressed size is good"); 75 76 if (VERBOSE) fprintf (stderr, "result: %d bytes\n", (int) zdn.total_out); 77 for (i = 0; VERBOSE && (i < zdn.total_out / sizeof(double)); i++) { 78 fprintf (stderr, "%d : 0x%02hhx\n", i, tgtbuf[i]); 79 } 78 80 } 79 81 … … 91 93 // the '5' is the compression level: make this a user argument 92 94 err = inflateInit(&zup); 93 //fprintf (stderr, "out buffers unc 0: %d => %d => %d\n", zup.avail_in, zup.avail_out, (int) zup.total_out);94 if (err != Z_OK) { fprintf (stdout, "not ok: error 1: %d vs %d\n", err, Z_OK); exit (1); }95 if (VERBOSE) fprintf (stderr, "out buffers unc 0: %d => %d => %d\n", zup.avail_in, zup.avail_out, (int) zup.total_out); 96 ok (err == Z_OK, "result is Z_OK: %d vs %d", err, Z_OK); 95 97 96 98 err = inflate(&zup, Z_FINISH); 97 // fprintf (stderr, "out buffers unc 1: %d => %d => %d\n", zup.avail_in, zup.avail_out, (int) zup.total_out); 98 if (err != Z_STREAM_END) { fprintf (stderr, "error 2: %d vs %d\n", err, Z_STREAM_END); exit (2); } 99 // fprintf (stderr, "out buffers unc 2: %d => %d => %d\n", zup.avail_in, zup.avail_out, (int) zup.total_out); 100 101 # ifdef VERBOSE 99 if (VERBOSE) fprintf (stderr, "out buffers unc 1: %d => %d => %d\n", zup.avail_in, zup.avail_out, (int) zup.total_out); 100 ok (err == Z_STREAM_END, "result is Z_STREAM_END: %d vs %d", err, Z_STREAM_END); 101 102 102 char *outdata = (char *) outbuf; 103 fprintf (stderr, "unc: ");104 for (i = 0; i < zup.total_out; i++) {103 if (VERBOSE) fprintf (stderr, "unc: "); 104 for (i = 0; VERBOSE && (i < zup.total_out); i++) { 105 105 fprintf (stderr, "0x%02hhx ", outdata[i]); 106 106 } 107 fprintf (stderr, "\n"); 108 # endif 109 110 assert (zup.total_out <= NTGT*sizeof(double)); 111 112 // fprintf (stderr, "result: %d bytes\n", (int) zup.total_out); 107 if (VERBOSE) fprintf (stderr, "\n"); 108 109 ok (zup.total_out <= NTGT*sizeof(double), "uncompressed size is good"); 110 111 int Nbad = 0; 113 112 for (i = 0; 0 && i < zup.total_out / sizeof(double); i++) { 114 // fprintf (stderr, "%d : %f : %f\n", i, outbuf[i], srcbuf[i]); 115 assert (outbuf[i] == srcbuf[i]); 116 } 117 } 118 119 return exit_status(); 120 } 121 122 int main_old (int argc, char **argv) { 123 124 int i, err; 125 char srcbuf[NSRC], tgtbuf[NTGT], outbuf[NTGT]; 126 113 if (outbuf[i] != srcbuf[i]) Nbad++; 114 } 115 ok (Nbad == 0, "no mismatched bytes"); 116 } 117 118 /**** TEST 2 : use gzip headers ****/ 127 119 { 128 120 z_stream zdn; 129 zdn.next_in = srcbuf;130 zdn.avail_in = NSRC ;131 zdn.next_out = tgtbuf;132 zdn.avail_out = NTGT ;121 zdn.next_in = (char *) srcbuf; 122 zdn.avail_in = NSRC * sizeof(double); 123 zdn.next_out = (char *) tgtbuf; 124 zdn.avail_out = NTGT * sizeof(double); 133 125 134 126 zdn.zalloc = Z_NULL; … … 136 128 zdn.opaque = Z_NULL; 137 129 138 for (i = 0; i < NSRC; i++) srcbuf[i] = i; 139 memset (tgtbuf, 0, NTGT); 140 memset (outbuf, 0, NTGT); 130 for (i = 0; i < NSRC; i++) srcbuf[i] = 1.001*(i + 10); 131 memset (tgtbuf, 0, NTGT * sizeof(double)); 132 memset (outbuf, 0, NOUT * sizeof(double)); 133 134 for (i = 0; i < NSRC; i++) { 135 char *Pout = (char *) &srcbuf[i]; 136 SWAP_DBLE; 137 } 138 139 char *rawdata = (char *) srcbuf; 140 if (VERBOSE) fprintf (stderr, "inp: "); 141 for (i = 0; VERBOSE && (i < NSRC*8); i++) { 142 fprintf (stderr, "0x%02hhx ", rawdata[i]); 143 } 144 if (VERBOSE) fprintf (stderr, "\n"); 141 145 142 146 // the '5' is the compression level: make this a user argument 143 err = deflateInit(&zdn, 5); 144 if (err != Z_OK) { fprintf (stderr, "error 1: %d vs %d\n", err, Z_OK); exit (1); } 147 // windowBits = 31 = (15 + 16) = (2^15 window bits) + (create a gzip stream) 148 err = deflateInit2(&zdn, 1, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY); 149 if (VERBOSE) fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", zdn.avail_in, zdn.avail_out, (int) zdn.total_out); 150 ok (err == Z_OK, "result is Z_OK: %d vs %d", err, Z_OK); 145 151 146 152 err = deflate(&zdn, Z_FINISH); 147 if (err != Z_STREAM_END) { fprintf (stderr, "error 2: %d vs %d\n", err, Z_STREAM_END); exit (2); } 148 149 assert (zdn.total_out <= NTGT); 150 151 fprintf (stderr, "result: %d bytes\n", (int) zdn.total_out); 152 for (i = 0; i < zdn.total_out; i++) { 153 if (VERBOSE) fprintf (stderr, "out buffers cmp 1: %d => %d => %d\n", zdn.avail_in, zdn.avail_out, (int) zdn.total_out); 154 ok (err == Z_STREAM_END, "result is Z_STREAM_END: %d vs %d", err, Z_STREAM_END); 155 156 // dump out the data which failed to uncompress (and the input data) 157 char *cmpdata = (char *) tgtbuf; 158 if (VERBOSE) fprintf (stderr, "out: "); 159 for (i = 0; VERBOSE && (i < zdn.total_out); i++) { 160 fprintf (stderr, "0x%02hhx ", cmpdata[i]); 161 } 162 if (VERBOSE) fprintf (stderr, "\n"); 163 164 ok (zdn.total_out <= NTGT*sizeof(double), "output compressed size is good"); 165 166 if (VERBOSE) fprintf (stderr, "result: %d bytes\n", (int) zdn.total_out); 167 for (i = 0; VERBOSE && (i < zdn.total_out / sizeof(double)); i++) { 153 168 fprintf (stderr, "%d : 0x%02hhx\n", i, tgtbuf[i]); 154 169 } … … 157 172 { 158 173 z_stream zup; 159 zup.next_in = tgtbuf;160 zup.avail_in = NTGT ;161 zup.next_out = outbuf;162 zup.avail_out = N TGT;174 zup.next_in = (char *) tgtbuf; 175 zup.avail_in = NTGT * sizeof(double); 176 zup.next_out = (char *) outbuf; 177 zup.avail_out = NOUT * sizeof(double); 163 178 164 179 zup.zalloc = Z_NULL; … … 166 181 zup.opaque = Z_NULL; 167 182 168 // the '5' is the compression level: make this a user argument 169 err = inflateInit(&zup); 170 if (err != Z_OK) { fprintf (stderr, "error 1: %d vs %d\n", err, Z_OK); exit (1); } 183 // a value of 32 tells inflate to use the window size used in the compression AND to look for both zlib and gzip headers 184 // windowBits = 47 = (15 + 32) = (2^15 window bits) + (test for either gzip or zlib streams) 185 err = inflateInit2(&zup, 47); 186 if (VERBOSE) fprintf (stderr, "out buffers unc 0: %d => %d => %d\n", zup.avail_in, zup.avail_out, (int) zup.total_out); 187 ok (err == Z_OK, "inflateInit2 result is Z_OK: %d vs %d", err, Z_OK); 171 188 172 189 err = inflate(&zup, Z_FINISH); 173 if (err != Z_STREAM_END) { fprintf (stderr, "error 2: %d vs %d\n", err, Z_STREAM_END); exit (2); } 174 175 assert (zup.total_out <= NTGT); 176 177 fprintf (stderr, "result: %d bytes\n", (int) zup.total_out); 178 for (i = 0; i < zup.total_out; i++) { 179 fprintf (stderr, "%d : 0x%02hhx : 0x%02hhx\n", i, outbuf[i], srcbuf[i]); 180 assert (outbuf[i] == srcbuf[i]); 181 } 182 } 190 if (VERBOSE) fprintf (stderr, "out buffers unc 1: %d => %d => %d\n", zup.avail_in, zup.avail_out, (int) zup.total_out); 191 ok (err == Z_STREAM_END, "inflate result is Z_STREAM_END: %d vs %d", err, Z_STREAM_END); 192 193 char *outdata = (char *) outbuf; 194 if (VERBOSE) fprintf (stderr, "unc: "); 195 for (i = 0; VERBOSE && (i < zup.total_out); i++) { 196 fprintf (stderr, "0x%02hhx ", outdata[i]); 197 } 198 if (VERBOSE) fprintf (stderr, "\n"); 199 200 ok (zup.total_out <= NTGT*sizeof(double), "uncompressed size is good"); 201 202 int Nbad = 0; 203 for (i = 0; 0 && i < zup.total_out / sizeof(double); i++) { 204 if (outbuf[i] != srcbuf[i]) Nbad++; 205 } 206 ok (Nbad == 0, "no mismatched bytes"); 207 } 208 183 209 return exit_status(); 184 210 }
Note:
See TracChangeset
for help on using the changeset viewer.
