test: move all module tests to the test/ directory
This commit is contained in:
82
test/compress/mix-compress.c
Normal file
82
test/compress/mix-compress.c
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <assert.h>
|
||||
#include <blue/compress/compressor.h>
|
||||
#include <blue/compress/cstream.h>
|
||||
#include <blue/compress/zstd.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;
|
||||
}
|
||||
|
||||
b_compressor_mode mode = B_COMPRESSOR_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, B_TYPE_ZSTD_COMPRESSOR, 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);
|
||||
}
|
||||
|
||||
size_t tx_total = 0;
|
||||
b_cstream_tx_bytes(cstream, &tx_total);
|
||||
printf("iteration %d: wrote %zu (compressed) / %zu "
|
||||
"(uncompressed) / %zu (total) bytes (%s)\n",
|
||||
i, nr_written_compressed, nr_written, tx_total,
|
||||
compressed ? "compressed" : "uncompressed");
|
||||
|
||||
compressed = !compressed;
|
||||
}
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
b_cstream_unref(cstream);
|
||||
b_stream_unref(out_stream);
|
||||
fclose(out_fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
76
test/compress/mix-decompress.c
Normal file
76
test/compress/mix-decompress.c
Normal file
@@ -0,0 +1,76 @@
|
||||
#include <assert.h>
|
||||
#include <blue/compress/compressor.h>
|
||||
#include <blue/compress/cstream.h>
|
||||
#include <blue/compress/zstd.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;
|
||||
}
|
||||
|
||||
b_compressor_mode mode = B_COMPRESSOR_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, B_TYPE_ZSTD_COMPRESSOR, 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);
|
||||
}
|
||||
|
||||
size_t tx_total = 0;
|
||||
b_cstream_tx_bytes(cstream, &tx_total);
|
||||
printf(" * iteration %d: read %zu (compressed) / %zu "
|
||||
"(uncompressed) / %zu (total) bytes (%s)\n",
|
||||
i, nr_read_compressed, nr_read, tx_total,
|
||||
compressed ? "compressed" : "uncompressed");
|
||||
printf("%s\n", buf);
|
||||
|
||||
compressed = !compressed;
|
||||
}
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
b_cstream_unref(cstream);
|
||||
b_stream_unref(in_stream);
|
||||
fclose(in_fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
183
test/compress/simple1.c
Normal file
183
test/compress/simple1.c
Normal file
@@ -0,0 +1,183 @@
|
||||
#include <assert.h>
|
||||
#include <blue/compress/compressor.h>
|
||||
#include <blue/compress/zstd.h>
|
||||
#include <blue/core/ringbuffer.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUF_SIZE 32
|
||||
|
||||
int refill_input_buffer(FILE *fp, b_ringbuffer *dest)
|
||||
{
|
||||
while (1) {
|
||||
void *buf;
|
||||
size_t capacity;
|
||||
b_status status
|
||||
= b_ringbuffer_open_write_buffer(dest, &buf, &capacity);
|
||||
if (status == B_ERR_NO_SPACE) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t r = fread(buf, 1, capacity, fp);
|
||||
|
||||
b_ringbuffer_close_write_buffer(dest, &buf, r);
|
||||
|
||||
if (r == 0) {
|
||||
return ferror(fp) ? -1 : 0;
|
||||
}
|
||||
|
||||
if (r < capacity) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int flush_output_buffer(FILE *fp, b_ringbuffer *src)
|
||||
{
|
||||
while (1) {
|
||||
const void *buf;
|
||||
size_t capacity;
|
||||
b_status status
|
||||
= b_ringbuffer_open_read_buffer(src, &buf, &capacity);
|
||||
if (status == B_ERR_NO_DATA) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!B_OK(status)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t w = fwrite(buf, 1, capacity, fp);
|
||||
|
||||
b_ringbuffer_close_read_buffer(src, &buf, w);
|
||||
|
||||
if (w < capacity) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
if (argc < 4) {
|
||||
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_compressor_mode mode;
|
||||
if (!strcmp(argv[1], "C")) {
|
||||
mode = B_COMPRESSOR_MODE_COMPRESS;
|
||||
} else if (!strcmp(argv[1], "D")) {
|
||||
mode = B_COMPRESSOR_MODE_DECOMPRESS;
|
||||
} else {
|
||||
fprintf(stderr, "invalid mode %s\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *in_fp = fopen(argv[2], "rb");
|
||||
if (!in_fp) {
|
||||
fprintf(stderr, "cannot open input file %s\n", argv[2]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *out_fp = fopen(argv[3], "wb");
|
||||
if (!out_fp) {
|
||||
fclose(in_fp);
|
||||
fprintf(stderr, "cannot open output file %s\n", argv[3]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_status status = B_SUCCESS;
|
||||
b_type compressor_type = B_TYPE_ZSTD_COMPRESSOR;
|
||||
b_compressor *compressor = b_object_create(compressor_type);
|
||||
|
||||
size_t inbuf_size, outbuf_size;
|
||||
b_compressor_get_buffer_size(
|
||||
compressor_type, mode, &inbuf_size, &outbuf_size);
|
||||
|
||||
b_ringbuffer *in = b_ringbuffer_create(inbuf_size);
|
||||
b_ringbuffer *out = b_ringbuffer_create(outbuf_size);
|
||||
|
||||
if (!in || !out) {
|
||||
fprintf(stderr, "memory allocation failure");
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_compressor_set_buffer(compressor, in, out);
|
||||
b_compressor_set_mode(compressor, mode);
|
||||
|
||||
int ret = 0;
|
||||
while (1) {
|
||||
ret = refill_input_buffer(in_fp, in);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "read error\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (!b_ringbuffer_available_data_remaining(in)) {
|
||||
break;
|
||||
}
|
||||
|
||||
status = b_compressor_step(compressor);
|
||||
|
||||
if (status == B_ERR_NO_DATA) {
|
||||
break;
|
||||
} else if (status == B_ERR_NO_SPACE) {
|
||||
ret = flush_output_buffer(out_fp, out);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "write error\n");
|
||||
break;
|
||||
}
|
||||
} else if (!B_OK(status)) {
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ret = flush_output_buffer(out_fp, out);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "write error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (mode == B_COMPRESSOR_MODE_COMPRESS) {
|
||||
while (!b_compressor_eof(compressor)) {
|
||||
status = b_compressor_end(compressor);
|
||||
if (!B_OK(status)) {
|
||||
fprintf(stderr,
|
||||
"compression finalisation error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!b_ringbuffer_available_data_remaining(out)) {
|
||||
break;
|
||||
}
|
||||
|
||||
ret = flush_output_buffer(out_fp, out);
|
||||
if (ret != 0) {
|
||||
fprintf(stderr, "write error\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
b_compressor_unref(compressor);
|
||||
|
||||
fclose(in_fp);
|
||||
fclose(out_fp);
|
||||
|
||||
b_ringbuffer_unref(in);
|
||||
b_ringbuffer_unref(out);
|
||||
|
||||
return ret;
|
||||
}
|
||||
143
test/compress/stream.c
Normal file
143
test/compress/stream.c
Normal file
@@ -0,0 +1,143 @@
|
||||
#include <assert.h>
|
||||
#include <blue/compress/compressor.h>
|
||||
#include <blue/compress/cstream.h>
|
||||
#include <blue/compress/zstd.h>
|
||||
#include <blue/core/ringbuffer.h>
|
||||
#include <blue/core/stream.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUF_SIZE 32
|
||||
|
||||
static int compress(b_type compressor_type, FILE *in, FILE *out)
|
||||
{
|
||||
b_stream *out_stream = b_stream_open_fp(out);
|
||||
|
||||
b_cstream *cstream;
|
||||
b_status status = b_cstream_open(
|
||||
out_stream, compressor_type, B_COMPRESSOR_MODE_COMPRESS, &cstream);
|
||||
|
||||
if (!B_OK(status)) {
|
||||
fprintf(stderr, "cannot initialise compressor\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_cstream_begin_compressed_section(cstream, NULL);
|
||||
|
||||
char buf[4096];
|
||||
while (1) {
|
||||
size_t r = fread(buf, 1, sizeof buf, in);
|
||||
|
||||
size_t w = 0;
|
||||
b_cstream_write(cstream, buf, r, &w);
|
||||
|
||||
if (r != w) {
|
||||
fprintf(stderr, "write error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (r < sizeof buf) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
b_cstream_end_compressed_section(cstream, NULL, NULL);
|
||||
b_cstream_unref(cstream);
|
||||
b_stream_unref(out_stream);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int decompress(b_type compressor_type, FILE *in, FILE *out)
|
||||
{
|
||||
b_stream *in_stream = b_stream_open_fp(in);
|
||||
|
||||
b_cstream *cstream;
|
||||
b_status status = b_cstream_open(
|
||||
in_stream, compressor_type, B_COMPRESSOR_MODE_DECOMPRESS, &cstream);
|
||||
|
||||
if (!B_OK(status)) {
|
||||
fprintf(stderr, "cannot initialise compressor\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_cstream_begin_compressed_section(cstream, NULL);
|
||||
|
||||
char buf[4096];
|
||||
while (1) {
|
||||
size_t r = 0;
|
||||
b_status status = b_cstream_read(cstream, buf, sizeof buf, &r);
|
||||
if (!B_OK(status)) {
|
||||
fprintf(stderr, "read error: %s\n",
|
||||
b_status_description(status));
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t w = fwrite(buf, 1, r, out);
|
||||
if (r != w) {
|
||||
fprintf(stderr, "write error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (r < sizeof buf) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
b_cstream_end_compressed_section(cstream, NULL, NULL);
|
||||
b_cstream_unref(cstream);
|
||||
b_stream_unref(in_stream);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
if (argc < 4) {
|
||||
fprintf(stderr, "usage: %s <C/D> <inpath> <outpath>\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_compressor_mode mode;
|
||||
if (!strcmp(argv[1], "C")) {
|
||||
mode = B_COMPRESSOR_MODE_COMPRESS;
|
||||
} else if (!strcmp(argv[1], "D")) {
|
||||
mode = B_COMPRESSOR_MODE_DECOMPRESS;
|
||||
} else {
|
||||
fprintf(stderr, "invalid mode %s\n", argv[1]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *in_fp = fopen(argv[2], "rb");
|
||||
if (!in_fp) {
|
||||
fprintf(stderr, "cannot open input file %s\n", argv[2]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
FILE *out_fp = fopen(argv[3], "wb");
|
||||
if (!out_fp) {
|
||||
fclose(in_fp);
|
||||
fprintf(stderr, "cannot open output file %s\n", argv[3]);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
switch (mode) {
|
||||
case B_COMPRESSOR_MODE_COMPRESS:
|
||||
ret = compress(B_TYPE_ZSTD_COMPRESSOR, in_fp, out_fp);
|
||||
break;
|
||||
case B_COMPRESSOR_MODE_DECOMPRESS:
|
||||
ret = decompress(B_TYPE_ZSTD_COMPRESSOR, in_fp, out_fp);
|
||||
break;
|
||||
default:
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
printf("Done\n");
|
||||
|
||||
fclose(in_fp);
|
||||
fclose(out_fp);
|
||||
|
||||
return ret;
|
||||
}
|
||||
Reference in New Issue
Block a user