Libpolycomp  1.0
A compression/decompression library that implements the polynomial compression and other simple compression schemes
test_quant.c
1 /* test_rle.c - Tests for RLE functions
2  *
3  * Copyright (c) 2015 Maurizio Tomasi
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include <libpolycomp.h>
27 #include <assert.h>
28 #include <math.h>
29 #include <stdlib.h>
30 
31 /***********************************************************************
32  * Check that the quantization routines encode the output sequence in
33  * the required format (see the "polycomp" paper, Fig. 3).
34  */
35 
36 #define DEFINE_QUANT_TEST(fn_name, pcomp_fn, datatype_t) \
37  void fn_name(void) \
38  { \
39  datatype_t input_buf[] = { 3.06, 5.31, 2.25, 7.92, 4.86 }; \
40  size_t input_size = sizeof(input_buf) / sizeof(input_buf[0]); \
41  size_t output_size = 4; /* Known by manual calculation */ \
42  uint8_t* output_buf = malloc(output_size); \
43  \
44  pcomp_quant_params_t* params \
45  = pcomp_init_quant_params(sizeof(datatype_t), 5); \
46  \
47  assert(pcomp_fn(output_buf, &output_size, input_buf, \
48  input_size, params) == PCOMP_STAT_SUCCESS); \
49  \
50  assert(output_buf[0] == 36); /* Count */ \
51  assert(output_buf[1] == 65); /* Value */ \
52  assert(output_buf[2] == 247); /* Count */ \
53  assert(output_buf[3] == 0); \
54  \
55  free(output_buf); \
56  pcomp_free_quant_params(params); \
57  }
58 
59 DEFINE_QUANT_TEST(test_quant_compression_float,
61 DEFINE_QUANT_TEST(test_quant_compression_double,
63 
64 /***********************************************************************
65  * Check that the decompression of quantized data works as expected
66  * (see the "polycomp" paper, Fig. 3).
67  */
68 
69 #define DEFINE_QUANT_DECOMPR_TEST(fn_name, compr_fn, decompr_fn, \
70  datatype_t) \
71  void fn_name(void) \
72  { \
73  datatype_t input_buf[] = { 3.06, 5.31, 2.25, 7.92, 4.86 }; \
74  size_t input_size = sizeof(input_buf) / sizeof(input_buf[0]); \
75  size_t compr_size = 4; /* Known by manual calculation */ \
76  uint8_t* compr_buf = malloc(compr_size); \
77  datatype_t decompr_buf[5]; \
78  size_t idx; \
79  \
80  pcomp_quant_params_t* params \
81  = pcomp_init_quant_params(sizeof(datatype_t), 5); \
82  \
83  assert(compr_fn(compr_buf, &compr_size, input_buf, input_size, \
84  params) == PCOMP_STAT_SUCCESS); \
85  \
86  assert(decompr_fn(&decompr_buf[0], input_size, compr_buf, \
87  compr_size, params) == PCOMP_STAT_SUCCESS); \
88  \
89  for (idx = 0; idx < input_size; ++idx) { \
90  assert(fabs(input_buf[idx] - decompr_buf[idx]) < 0.186); \
91  } \
92  \
93  free(compr_buf); \
94  }
95 
96 DEFINE_QUANT_DECOMPR_TEST(test_quant_decompression_float,
98  pcomp_decompress_quant_float, float)
99 DEFINE_QUANT_DECOMPR_TEST(test_quant_decompression_double,
100  pcomp_compress_quant_double,
101  pcomp_decompress_quant_double, double)
102 
103 int main(void)
104 {
105  test_quant_compression_float();
106  test_quant_compression_double();
107 
108  test_quant_decompression_float();
109  test_quant_decompression_double();
110 
111  return 0;
112 }
int pcomp_compress_quant_float(void *output_buf, size_t *output_size, const float *input_buf, size_t input_size, pcomp_quant_params_t *params)
Quantize a stream of 32-bit floating point numbers.
int pcomp_compress_quant_double(void *output_buf, size_t *output_size, const double *input_buf, size_t input_size, pcomp_quant_params_t *params)
Quantize a stream of 64-bit floating point numbers.
Header file for Libpolycomp.