test: compress: add mixed compressed/uncompressed data test
This commit is contained in:
87
compress-test/mix-compress.c
Normal file
87
compress-test/mix-compress.c
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <blue/compress/compressor.h>
|
||||||
|
#include <blue/compress/cstream.h>
|
||||||
|
#include <blue/compress/function.h>
|
||||||
|
#include <blue/core/ringbuffer.h>
|
||||||
|
#include <blue/core/stream.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define NR_ITERATIONS 10
|
||||||
|
|
||||||
|
static const char source[]
|
||||||
|
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit. In sodales "
|
||||||
|
"scelerisque lacus. In ullamcorper sem a nibh venenatis efficitur at "
|
||||||
|
"id quam. Nam quis urna nulla. In in consectetur ligula. Morbi "
|
||||||
|
"ligula neque, imperdiet quis erat et, mollis consequat sapien. "
|
||||||
|
"Vivamus quis bibendum nulla. Vivamus tincidunt mauris sed nisl "
|
||||||
|
"vulputate pellentesque. Nam blandit ac risus ut auctor. Fusce "
|
||||||
|
"sagittis quam ut sapien accumsan, pellentesque finibus elit "
|
||||||
|
"eleifend. Duis sodales ex urna, a porttitor ex bibendum quis quam.";
|
||||||
|
|
||||||
|
int main(int argc, const char **argv)
|
||||||
|
{
|
||||||
|
if (argc < 2) {
|
||||||
|
fprintf(stderr, "usage: %s <outpath>\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const b_compression_function *zstd
|
||||||
|
= b_compression_function_get_by_id(B_COMPRESSOR_FUNCTION_ZSTD);
|
||||||
|
if (!zstd) {
|
||||||
|
fprintf(stderr, "zstd support not enabled\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_compression_mode mode = B_COMPRESSION_MODE_COMPRESS;
|
||||||
|
|
||||||
|
FILE *out_fp = fopen(argv[1], "wb");
|
||||||
|
if (!out_fp) {
|
||||||
|
fprintf(stderr, "cannot open output file %s\n", argv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_stream *out_stream = b_stream_open_fp(out_fp);
|
||||||
|
b_cstream *cstream;
|
||||||
|
b_cstream_open(out_stream, zstd, mode, &cstream);
|
||||||
|
|
||||||
|
const size_t source_len = strlen(source);
|
||||||
|
bool compressed = false;
|
||||||
|
|
||||||
|
for (int i = 0; i < NR_ITERATIONS; i++) {
|
||||||
|
if (compressed) {
|
||||||
|
b_cstream_begin_compressed_section(cstream, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nr_written = 0;
|
||||||
|
b_status status = b_cstream_write(
|
||||||
|
cstream, source, source_len, &nr_written);
|
||||||
|
if (!B_OK(status)) {
|
||||||
|
fprintf(stderr, "write error: %s\n",
|
||||||
|
b_status_description(status));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nr_written_compressed = 0;
|
||||||
|
|
||||||
|
if (compressed) {
|
||||||
|
b_cstream_end_compressed_section(
|
||||||
|
cstream, &nr_written_compressed, &nr_written);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("iteration %d: wrote %zu (compressed) / %zu "
|
||||||
|
"(uncompressed) bytes (%s)\n",
|
||||||
|
i, nr_written_compressed, nr_written,
|
||||||
|
compressed ? "compressed" : "uncompressed");
|
||||||
|
|
||||||
|
compressed = !compressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Done\n");
|
||||||
|
|
||||||
|
b_cstream_close(cstream);
|
||||||
|
b_stream_close(out_stream);
|
||||||
|
fclose(out_fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
81
compress-test/mix-decompress.c
Normal file
81
compress-test/mix-decompress.c
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
#include <assert.h>
|
||||||
|
#include <blue/compress/compressor.h>
|
||||||
|
#include <blue/compress/cstream.h>
|
||||||
|
#include <blue/compress/function.h>
|
||||||
|
#include <blue/core/ringbuffer.h>
|
||||||
|
#include <blue/core/stream.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int main(int argc, const char **argv)
|
||||||
|
{
|
||||||
|
if (argc < 2) {
|
||||||
|
fprintf(stderr, "usage: %s <inpath>\n", argv[0]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const b_compression_function *zstd
|
||||||
|
= b_compression_function_get_by_id(B_COMPRESSOR_FUNCTION_ZSTD);
|
||||||
|
if (!zstd) {
|
||||||
|
fprintf(stderr, "zstd support not enabled\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_compression_mode mode = B_COMPRESSION_MODE_DECOMPRESS;
|
||||||
|
|
||||||
|
FILE *in_fp = fopen(argv[1], "rb");
|
||||||
|
if (!in_fp) {
|
||||||
|
fprintf(stderr, "cannot open input file %s\n", argv[1]);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
b_stream *in_stream = b_stream_open_fp(in_fp);
|
||||||
|
b_cstream *cstream;
|
||||||
|
b_cstream_open(in_stream, zstd, mode, &cstream);
|
||||||
|
|
||||||
|
bool compressed = false;
|
||||||
|
char buf[513];
|
||||||
|
|
||||||
|
for (int i = 0;; i++) {
|
||||||
|
if (compressed) {
|
||||||
|
b_cstream_begin_compressed_section(cstream, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(buf, 0x0, sizeof buf);
|
||||||
|
|
||||||
|
size_t nr_read = 0;
|
||||||
|
b_status status
|
||||||
|
= b_cstream_read(cstream, buf, sizeof buf - 1, &nr_read);
|
||||||
|
if (!B_OK(status)) {
|
||||||
|
fprintf(stderr, "write error: %s\n",
|
||||||
|
b_status_description(status));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nr_read == 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t nr_read_compressed = 0;
|
||||||
|
if (compressed) {
|
||||||
|
b_cstream_end_compressed_section(
|
||||||
|
cstream, &nr_read_compressed, &nr_read);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(" * iteration %d: read %zu (compressed) / %zu "
|
||||||
|
"(uncompressed) bytes (%s)\n",
|
||||||
|
i, nr_read_compressed, nr_read,
|
||||||
|
compressed ? "compressed" : "uncompressed");
|
||||||
|
printf("%s\n", buf);
|
||||||
|
|
||||||
|
compressed = !compressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("Done\n");
|
||||||
|
|
||||||
|
b_cstream_close(cstream);
|
||||||
|
b_stream_close(in_stream);
|
||||||
|
fclose(in_fp);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user