-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_polycomp_low_level.c
215 lines (179 loc) · 7.3 KB
/
test_polycomp_low_level.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* test_polycomp_low_level.c - Tests for low-level polynomial
* compression functions
*
* Copyright (c) 2015 Maurizio Tomasi
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <libpolycomp.h>
#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#define EPSILON 1.0e-7
#define MAX_ALLOWABLE_ERROR 0.3
int test_chunk_creation(void)
{
const double samples[] = { 1.0, 2.0, 3.0, 4.0 };
const size_t num_of_samples = sizeof(samples) / sizeof(samples[0]);
const double poly[] = { 3.0, 2.0 };
const size_t num_of_poly = sizeof(poly) / sizeof(poly[0]);
const uint8_t cheby_mask[] = { 0xFF };
const double cheby[] = { -1.0, -2.0, -3.0, -4.0 };
const size_t num_of_cheby = sizeof(cheby) / sizeof(cheby[0]);
const double* values;
size_t idx;
pcomp_polycomp_chunk_t* chunk = NULL;
chunk = pcomp_init_uncompressed_chunk(num_of_samples, &samples[0]);
assert(chunk != NULL);
assert(!pcomp_chunk_is_compressed(chunk));
assert(pcomp_chunk_num_of_samples(chunk) == num_of_samples);
pcomp_free_chunk(chunk);
assert(pcomp_chunk_cheby_mask_size(num_of_samples) == 1);
chunk = pcomp_init_compressed_chunk(num_of_samples, num_of_poly,
&poly[0], num_of_cheby,
&cheby_mask[0], &cheby[0]);
assert(chunk != NULL);
assert(pcomp_chunk_is_compressed(chunk));
assert(pcomp_chunk_num_of_poly_coeffs(chunk) == num_of_poly);
values = pcomp_chunk_poly_coeffs(chunk);
for (idx = 0; idx < pcomp_chunk_num_of_poly_coeffs(chunk); ++idx) {
assert(values[idx] == poly[idx]);
}
assert(pcomp_chunk_num_of_cheby_coeffs(chunk) == num_of_cheby);
values = pcomp_chunk_cheby_coeffs(chunk);
for (idx = 0; idx < pcomp_chunk_num_of_cheby_coeffs(chunk); ++idx) {
assert(values[idx] == cheby[idx]);
}
pcomp_free_chunk(chunk);
return 0;
}
int test_no_compression(void)
{
/* It is impossible to compress these data using the polynomial
* compression algorithm, as they contain too many jumps */
double input[]
= { 1.0, -2.0, 3.0, -4.0, 5.0, 6.0, 7.0, -8.0, 9.0, -10.0 };
size_t input_size = sizeof(input) / sizeof(input[0]);
double* decompr = malloc(sizeof(double) * input_size);
pcomp_polycomp_chunk_t* chunk = pcomp_init_chunk(input_size);
double max_error;
pcomp_chebyshev_t* inv_chebyshev
= pcomp_init_chebyshev(input_size, PCOMP_TD_INVERSE);
pcomp_polycomp_t* polycomp = pcomp_init_polycomp(
input_size, 2, MAX_ALLOWABLE_ERROR, PCOMP_ALG_USE_CHEBYSHEV);
size_t idx;
pcomp_run_polycomp_on_chunk(polycomp, input, input_size, chunk,
&max_error);
assert(pcomp_chunk_num_of_samples(chunk) == 10);
assert(!pcomp_chunk_is_compressed(chunk));
assert(pcomp_chunk_poly_coeffs(chunk) == NULL);
assert(pcomp_chunk_cheby_coeffs(chunk) == NULL);
pcomp_decompress_polycomp_chunk(decompr, chunk, inv_chebyshev);
for (idx = 0; idx < input_size; ++idx) {
assert(input[idx] == decompr[idx]);
}
free(decompr);
pcomp_free_chunk(chunk);
pcomp_free_chebyshev(inv_chebyshev);
pcomp_free_polycomp(polycomp);
return 0;
}
int test_no_chebyshev(void)
{
/* These data are fitted perfectly by a straight line */
double input[]
= { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
size_t input_size = sizeof(input) / sizeof(input[0]);
double* decompr = malloc(sizeof(double) * input_size);
pcomp_polycomp_chunk_t* chunk = pcomp_init_chunk(input_size);
double max_error;
pcomp_chebyshev_t* inv_chebyshev
= pcomp_init_chebyshev(input_size, PCOMP_TD_INVERSE);
pcomp_polycomp_t* polycomp = pcomp_init_polycomp(
input_size, 2, MAX_ALLOWABLE_ERROR, PCOMP_ALG_USE_CHEBYSHEV);
size_t idx;
pcomp_run_polycomp_on_chunk(polycomp, input, input_size, chunk,
&max_error);
assert(pcomp_chunk_num_of_samples(chunk) == 10);
assert(pcomp_chunk_is_compressed(chunk));
assert(pcomp_chunk_num_of_poly_coeffs(chunk) == 2);
assert(pcomp_chunk_poly_coeffs(chunk) != NULL);
assert(fabs(pcomp_chunk_poly_coeffs(chunk)[0] - 0.0) < EPSILON);
assert(fabs(pcomp_chunk_poly_coeffs(chunk)[1] - 1.0) < EPSILON);
assert(pcomp_chunk_cheby_coeffs(chunk) == NULL);
pcomp_decompress_polycomp_chunk(decompr, chunk, inv_chebyshev);
for (idx = 0; idx < input_size; ++idx) {
assert(fabs(input[idx] - decompr[idx]) < EPSILON);
}
free(decompr);
pcomp_free_chunk(chunk);
pcomp_free_chebyshev(inv_chebyshev);
pcomp_free_polycomp(polycomp);
return 0;
}
int test_complete_compression_and_decompression(void)
{
double input[]
= { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 11.0 };
size_t input_size = sizeof(input) / sizeof(input[0]);
double* decompr = malloc(sizeof(double) * input_size);
pcomp_polycomp_chunk_t* chunk = pcomp_init_chunk(input_size);
double max_error;
pcomp_chebyshev_t* inv_chebyshev
= pcomp_init_chebyshev(input_size, PCOMP_TD_INVERSE);
pcomp_polycomp_t* polycomp = pcomp_init_polycomp(
input_size, 2, MAX_ALLOWABLE_ERROR, PCOMP_ALG_USE_CHEBYSHEV);
size_t idx;
pcomp_run_polycomp_on_chunk(polycomp, input, input_size, chunk,
&max_error);
assert(pcomp_chunk_num_of_samples(chunk) == 10);
assert(pcomp_chunk_is_compressed(chunk));
assert(pcomp_chunk_num_of_poly_coeffs(chunk) == 2);
assert(pcomp_chunk_poly_coeffs(chunk) != NULL);
assert(pcomp_chunk_num_of_cheby_coeffs(chunk) < 10);
pcomp_decompress_polycomp_chunk(decompr, chunk, inv_chebyshev);
for (idx = 0; idx < input_size; ++idx) {
assert(fabs(input[idx] - decompr[idx]) < MAX_ALLOWABLE_ERROR);
}
free(decompr);
pcomp_free_chunk(chunk);
pcomp_free_chebyshev(inv_chebyshev);
pcomp_free_polycomp(polycomp);
return 0;
}
int main(void)
{
int result;
result = test_chunk_creation();
if (result != 0)
return result;
result = test_no_compression();
if (result != 0)
return result;
result = test_no_chebyshev();
if (result != 0)
return result;
result = test_complete_compression_and_decompression();
if (result != 0)
return result;
return 0;
}