Libpolycomp  1.0
A compression/decompression library that implements the polynomial compression and other simple compression schemes
test_diff_rle.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 <stdlib.h>
29 
30 /***********************************************************************
31  * Check that the RLE routines encode the output sequence in the
32  * required format */
33 
34 #define DEFINE_DIFFRLE_BINARY_FORMAT_TEST(fn_name, pcomp_fn, \
35  datatype_t) \
36  void fn_name(void) \
37  { \
38  datatype_t input_buf[] = { 10, 12, 14, 16, 17, 18 }; \
39  size_t input_size = sizeof(input_buf) / sizeof(input_buf[0]); \
40  size_t output_size = pcomp_diffrle_bufsize(input_size) \
41  * sizeof(datatype_t); \
42  datatype_t* output_buf = malloc(output_size); \
43  \
44  assert(pcomp_fn(output_buf, &output_size, input_buf, \
45  input_size) == PCOMP_STAT_SUCCESS); \
46  \
47  assert(output_size == 5); \
48  assert(output_buf[0] == 10); /* Starting value */ \
49  assert(output_buf[1] == 3); /* Count */ \
50  assert(output_buf[2] == 2); /* Increment */ \
51  assert(output_buf[3] == 2); /* Count */ \
52  assert(output_buf[4] == 1); /* Increment */ \
53  \
54  free(output_buf); \
55  }
56 
57 DEFINE_DIFFRLE_BINARY_FORMAT_TEST(test_diffrle_binary_format_int8,
59 DEFINE_DIFFRLE_BINARY_FORMAT_TEST(test_diffrle_binary_format_int16,
61 DEFINE_DIFFRLE_BINARY_FORMAT_TEST(test_diffrle_binary_format_int32,
63 DEFINE_DIFFRLE_BINARY_FORMAT_TEST(test_diffrle_binary_format_int64,
65 
66 DEFINE_DIFFRLE_BINARY_FORMAT_TEST(test_diffrle_binary_format_uint8,
68 DEFINE_DIFFRLE_BINARY_FORMAT_TEST(test_diffrle_binary_format_uint16,
70  uint16_t)
71 DEFINE_DIFFRLE_BINARY_FORMAT_TEST(test_diffrle_binary_format_uint32,
73  uint32_t)
74 DEFINE_DIFFRLE_BINARY_FORMAT_TEST(test_diffrle_binary_format_uint64,
76  uint64_t)
77 
78 void test_diffrle_binary_format(void)
79 {
80  test_diffrle_binary_format_int8();
81  test_diffrle_binary_format_int16();
82  test_diffrle_binary_format_int32();
83  test_diffrle_binary_format_int64();
84 
85  test_diffrle_binary_format_uint8();
86  test_diffrle_binary_format_uint16();
87  test_diffrle_binary_format_uint32();
88  test_diffrle_binary_format_uint64();
89 }
90 
91 /***********************************************************************
92  * Check that the RLE routines are able to properly decompress a
93  * (long) sequence of data. */
94 
95 #define DEFINE_DIFFRLE_DECOMPRESS_TEST(fn_name, pcomp_compr_fn, \
96  pcomp_decompr_fn, datatype_t) \
97  void fn_name(void) \
98  { \
99  const size_t input_size = 10000; \
100  datatype_t* input_buf \
101  = malloc(input_size * sizeof(datatype_t)); \
102  size_t compr_size = pcomp_diffrle_bufsize(input_size) \
103  * sizeof(datatype_t); \
104  datatype_t* compr_buf = malloc(compr_size); \
105  datatype_t* decompr_buf \
106  = malloc(input_size * sizeof(datatype_t)); \
107  size_t decompr_size = input_size; \
108  size_t idx; \
109  \
110  for (idx = 0; idx < input_size; ++idx) { \
111  /* Pick a number between 0 and 9 */ \
112  input_buf[idx] = random() % 10; \
113  } \
114  \
115  assert(pcomp_compr_fn(compr_buf, &compr_size, input_buf, \
116  input_size) == PCOMP_STAT_SUCCESS); \
117  assert(pcomp_decompr_fn(decompr_buf, decompr_size, compr_buf, \
118  compr_size) == PCOMP_STAT_SUCCESS); \
119  \
120  assert(decompr_size == input_size); \
121  for (idx = 0; idx < input_size; ++idx) { \
122  assert(decompr_buf[idx] == input_buf[idx]); \
123  } \
124  \
125  free(input_buf); \
126  free(compr_buf); \
127  free(decompr_buf); \
128  }
129 
130 DEFINE_DIFFRLE_DECOMPRESS_TEST(test_diffrle_decompress_int8,
133 DEFINE_DIFFRLE_DECOMPRESS_TEST(test_diffrle_decompress_int16,
136 DEFINE_DIFFRLE_DECOMPRESS_TEST(test_diffrle_decompress_int32,
139 DEFINE_DIFFRLE_DECOMPRESS_TEST(test_diffrle_decompress_int64,
142 
143 DEFINE_DIFFRLE_DECOMPRESS_TEST(test_diffrle_decompress_uint8,
146 DEFINE_DIFFRLE_DECOMPRESS_TEST(test_diffrle_decompress_uint16,
149  uint16_t)
150 DEFINE_DIFFRLE_DECOMPRESS_TEST(test_diffrle_decompress_uint32,
153  uint32_t)
154 DEFINE_DIFFRLE_DECOMPRESS_TEST(test_diffrle_decompress_uint64,
157  uint64_t)
158 
159 void test_diffrle_decompression(void)
160 {
161  test_diffrle_decompress_int8();
162  test_diffrle_decompress_int16();
163  test_diffrle_decompress_int32();
164  test_diffrle_decompress_int64();
165 
166  test_diffrle_decompress_uint8();
167  test_diffrle_decompress_uint16();
168  test_diffrle_decompress_uint32();
169  test_diffrle_decompress_uint64();
170 }
171 
172 int main(void)
173 {
174  test_diffrle_binary_format();
175  test_diffrle_decompression();
176 
177  return 0;
178 }
int pcomp_decompress_diffrle_uint32(uint32_t *output_buf, size_t output_size, const uint32_t *input_buf, size_t input_size)
Compress an array of uint32_t values using the diffRLE compression.
int pcomp_compress_diffrle_uint8(uint8_t *output_buf, size_t *output_size, const uint8_t *input_buf, size_t input_size)
Compress an array of uint8_t values using the diffRLE compression.
int pcomp_decompress_diffrle_int16(int16_t *output_buf, size_t output_size, const int16_t *input_buf, size_t input_size)
Compress an array of int16_t values using the diffRLE compression.
int pcomp_compress_diffrle_int32(int32_t *output_buf, size_t *output_size, const int32_t *input_buf, size_t input_size)
Compress an array of int32_t values using the diffRLE compression.
int pcomp_decompress_diffrle_uint8(uint8_t *output_buf, size_t output_size, const uint8_t *input_buf, size_t input_size)
Compress an array of uint8_t values using the diffRLE compression.
int pcomp_compress_diffrle_uint16(uint16_t *output_buf, size_t *output_size, const uint16_t *input_buf, size_t input_size)
Compress an array of uint16_t values using the diffRLE compression.
int pcomp_decompress_diffrle_int32(int32_t *output_buf, size_t output_size, const int32_t *input_buf, size_t input_size)
Compress an array of int32_t values using the diffRLE compression.
int pcomp_decompress_diffrle_uint16(uint16_t *output_buf, size_t output_size, const uint16_t *input_buf, size_t input_size)
Compress an array of uint16_t values using the diffRLE compression.
int pcomp_compress_diffrle_uint32(uint32_t *output_buf, size_t *output_size, const uint32_t *input_buf, size_t input_size)
Compress an array of uint32_t values using the diffRLE compression.
int pcomp_decompress_diffrle_int8(int8_t *output_buf, size_t output_size, const int8_t *input_buf, size_t input_size)
Decompress an array of int8_t values encoded using the diffRLE compression.
int pcomp_compress_diffrle_int16(int16_t *output_buf, size_t *output_size, const int16_t *input_buf, size_t input_size)
Compress an array of int16_t values using the diffRLE compression.
int pcomp_decompress_diffrle_uint64(uint64_t *output_buf, size_t output_size, const uint64_t *input_buf, size_t input_size)
Compress an array of uint64_t values using the diffRLE compression.
int pcomp_decompress_diffrle_int64(int64_t *output_buf, size_t output_size, const int64_t *input_buf, size_t input_size)
Compress an array of int64_t values using the diffRLE compression.
int pcomp_compress_diffrle_int8(int8_t *output_buf, size_t *output_size, const int8_t *input_buf, size_t input_size)
Compress an array of int8_t values using the diffRLE compression.
int pcomp_compress_diffrle_int64(int64_t *output_buf, size_t *output_size, const int64_t *input_buf, size_t input_size)
Compress an array of int64_t values using the diffRLE compression.
Header file for Libpolycomp.
int pcomp_compress_diffrle_uint64(uint64_t *output_buf, size_t *output_size, const uint64_t *input_buf, size_t input_size)
Compress an array of uint64_t values using the diffRLE compression.