HPCToolkit
Compression_test.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #include "../DataCompressionLayer.hpp"
61 #include "../ByteUtilities.hpp"
62 
63 #include <cstdlib>
64 #include <cstdio>
65 #include <cassert>
66 #include <iostream>
67 using namespace std;
68 
69 using namespace TraceviewerServer;
70 
71 int inf(FILE *source, FILE *dest);
72 
74  srand(3321);
76  int size = BUFFER_SIZE/2 + 1000;
77  for (int i = 0; i < size;i++) {
78  compr.writeLong(rand());
79  }
80  compr.flush();
81  cout << "Compressed " << size << " longs ("<< size*8<<" bytes). The compressed size is " << compr.getOutputLength() << endl;
82 
83  //Now check it using the official zlib sample code:
84  FILE* tmp1 = tmpfile();
85  fwrite(compr.getOutputBuffer(), sizeof(unsigned char), compr.getOutputLength(), tmp1);
86  rewind(tmp1);
87  FILE* tmp2 = tmpfile();
88  inf(tmp1, tmp2);
89  rewind(tmp2);
90  srand(3321);
91  for (int i = 0; i < size;i++) {
92  long checkval = rand();
93  char b[8];
94  fread(&b[0], sizeof(char), 8, tmp2);
95  long readval = ByteUtilities::readLong(b);
96  assert(checkval == readval);
97  }
98  cout << "Compression correctness verified."<<endl;
99 }
100 /* Decompress from file source to file dest until stream ends or EOF.
101  * inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be
102  * allocated for processing, Z_DATA_ERROR if the deflate data is
103  * invalid or incomplete, Z_VERSION_ERROR if the version of zlib.h and
104  * the version of the library linked do not match, or Z_ERRNO if there
105  * is an error reading or writing the files. */
106 int inf(FILE *source, FILE *dest)
107 {
108 #define CHUNK 16384
109  int ret;
110  unsigned have;
111  z_stream strm;
112  unsigned char in[CHUNK];
113  unsigned char out[CHUNK];
114 
115  /* allocate inflate state */
116  strm.zalloc = Z_NULL;
117  strm.zfree = Z_NULL;
118  strm.opaque = Z_NULL;
119  strm.avail_in = 0;
120  strm.next_in = Z_NULL;
121  ret = inflateInit(&strm);
122  if (ret != Z_OK)
123  return ret;
124 
125  /* decompress until deflate stream ends or end of file */
126  do {
127  strm.avail_in = fread(in, 1, CHUNK, source);
128  if (ferror(source)) {
129  (void)inflateEnd(&strm);
130  return Z_ERRNO;
131  }
132  if (strm.avail_in == 0)
133  break;
134  strm.next_in = in;
135 
136  /* run inflate() on input until output buffer not full */
137  do {
138  strm.avail_out = CHUNK;
139  strm.next_out = out;
140  ret = inflate(&strm, Z_NO_FLUSH);
141  assert(ret != Z_STREAM_ERROR); /* state not clobbered */
142  switch (ret) {
143  case Z_NEED_DICT:
144  ret = Z_DATA_ERROR; /* and fall through */
145  case Z_DATA_ERROR:
146  case Z_MEM_ERROR:
147  (void)inflateEnd(&strm);
148  return ret;
149  }
150  have = CHUNK - strm.avail_out;
151  if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
152  (void)inflateEnd(&strm);
153  return Z_ERRNO;
154  }
155  } while (strm.avail_out == 0);
156 
157  /* done when inflate() says it's done */
158  } while (ret != Z_STREAM_END);
159 
160  /* clean up and return */
161  (void)inflateEnd(&strm);
162  return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
163 }
#define BUFFER_SIZE
Definition: perf_mmap.h:66
#define CHUNK
size_t MONITOR_EXT_WRAP_NAME() fread(void *ptr, size_t size, size_t count, FILE *stream)
Definition: io-over.c:226
int inf(FILE *source, FILE *dest)
size_t MONITOR_EXT_WRAP_NAME() fwrite(const void *ptr, size_t size, size_t count, FILE *stream)
Definition: io-over.c:260
void compressionTest()