Libpolycomp  1.0
A compression/decompression library that implements the polynomial compression and other simple compression schemes
test_polycomp_high_level.c
1 /* test_polycomp_high_level.c - Tests for high-level polynomial
2  * compression functions
3  *
4  * Copyright (c) 2015 Maurizio Tomasi
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  */
26 
27 #include <libpolycomp.h>
28 #include <assert.h>
29 #include <math.h>
30 #include <stdlib.h>
31 
32 #include <stdio.h>
33 
34 #define MAX_ERROR 0.1
35 
36 void test_compression(void)
37 {
38  double input[]
39  = { 1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0, 2.0, 6.0, 7.0, 9.0 };
40  size_t input_size = sizeof(input) / sizeof(input[0]);
41  double* decompr;
42  size_t decompr_size;
43  pcomp_polycomp_chunk_t** chunks;
44  size_t num_of_chunks;
45  pcomp_polycomp_t* params
46  = pcomp_init_polycomp(4, 2, MAX_ERROR, PCOMP_ALG_USE_CHEBYSHEV);
47  size_t idx;
48 
49  pcomp_compress_polycomp(&chunks, &num_of_chunks, input, input_size,
50  params);
51  assert(num_of_chunks == 3);
52 
53  decompr_size = pcomp_total_num_of_samples(chunks, num_of_chunks);
54  assert(decompr_size == input_size);
55 
56  decompr = malloc(decompr_size * sizeof(double));
57  pcomp_decompress_polycomp(decompr, chunks, num_of_chunks);
58 
59  for (idx = 0; idx < decompr_size; ++idx) {
60  assert(fabs(input[idx] - decompr[idx]) <= MAX_ERROR);
61  }
62 
63  pcomp_free_polycomp(params);
64  pcomp_free_chunks(chunks, num_of_chunks);
65 }
66 
67 void test_encoding(void)
68 {
69  double input[]
70  = { 1.0, 2.0, 3.0, 4.0, 3.0, 2.0, 1.0, 2.0, 6.0, 7.0, 9.0 };
71  size_t input_size = sizeof(input) / sizeof(input[0]);
72  double* decompr;
73  size_t decompr_size;
74  pcomp_polycomp_chunk_t** chunks;
75  void* buf;
76  size_t buf_size;
77  size_t num_of_chunks;
78  pcomp_polycomp_t* params
79  = pcomp_init_polycomp(4, 2, MAX_ERROR, PCOMP_ALG_USE_CHEBYSHEV);
80  size_t idx;
81 
82  pcomp_compress_polycomp(&chunks, &num_of_chunks, input, input_size,
83  params);
84 
85  buf = malloc(pcomp_chunks_num_of_bytes(chunks, num_of_chunks));
86  assert(pcomp_encode_chunks(buf, &buf_size, chunks, num_of_chunks)
87  == PCOMP_STAT_SUCCESS);
88  pcomp_free_chunks(chunks, num_of_chunks);
89 
90  assert(pcomp_decode_chunks(&chunks, &num_of_chunks, buf)
91  == PCOMP_STAT_SUCCESS);
92  free(buf);
93 
94  decompr_size = pcomp_total_num_of_samples(chunks, num_of_chunks);
95  assert(decompr_size == input_size);
96 
97  decompr = malloc(decompr_size * sizeof(double));
98  pcomp_decompress_polycomp(decompr, chunks, num_of_chunks);
99 
100  for (idx = 0; idx < decompr_size; ++idx) {
101  assert(fabs(input[idx] - decompr[idx]) <= MAX_ERROR);
102  }
103 
104  pcomp_free_polycomp(params);
105  pcomp_free_chunks(chunks, num_of_chunks);
106 }
107 
108 int main(void)
109 {
110  test_compression();
111  test_encoding();
112  return 0;
113 }
int pcomp_encode_chunks(void *buf, size_t *buf_size, pcomp_polycomp_chunk_t *const chunk_array[], size_t num_of_chunks)
Encode a list of chunks into a sequence of raw bytes.
Definition: poly.c:2193
int pcomp_decode_chunks(pcomp_polycomp_chunk_t **chunk_array[], size_t *num_of_chunks, const void *buf)
Decode a byte sequence created by pcomp_encode_chunks into an array of chunks.
Definition: poly.c:2287
void pcomp_free_polycomp(pcomp_polycomp_t *params)
Free the memory allocated by pcomp_init_polycomp for a pcomp_polycomp_t structure.
Definition: poly.c:628
When needed, apply the Chebyshev transform to the residuals of the polynomial fit.
Definition: libpolycomp.h:358
size_t pcomp_total_num_of_samples(pcomp_polycomp_chunk_t *const chunk_array[], size_t num_of_chunks)
Compute the sum of the number of samples encoded in chunk_array.
Definition: poly.c:2022
pcomp_polycomp_t * pcomp_init_polycomp(pcomp_chunk_size_t samples_per_chunk, pcomp_poly_size_t num_of_coeffs, double max_allowable_error, pcomp_polycomp_algorithm_t algorithm)
Allocate space for a pcomp_polycomp_t structure.
Definition: poly.c:598
int pcomp_decompress_polycomp(double *output_buf, pcomp_polycomp_chunk_t *const chunk_array[], size_t num_of_chunks)
Decompress a sequence of chunks.
Definition: poly.c:2059
int pcomp_compress_polycomp(pcomp_polycomp_chunk_t **chunk_array[], size_t *num_of_chunks, const double *input_buf, size_t input_size, const pcomp_polycomp_t *params)
Compress the array input_buf using polynomial compression.
Definition: poly.c:1951
size_t pcomp_chunks_num_of_bytes(pcomp_polycomp_chunk_t *const chunks[], size_t num_of_chunks)
Number of bytes required by pcomp_encode_chunks.
Definition: poly.c:2138
void pcomp_free_chunks(pcomp_polycomp_chunk_t *chunk_array[], size_t num_of_chunks)
Free an array of chunks.
Definition: poly.c:2106
Header file for Libpolycomp.