test: move all module tests to the test/ directory
This commit is contained in:
175
test/cmd/example.c
Normal file
175
test/cmd/example.c
Normal file
@@ -0,0 +1,175 @@
|
||||
#include <blue/cmd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
enum {
|
||||
CMD_TEST,
|
||||
CMD_SUB,
|
||||
OPT_NAME,
|
||||
OPT_NAME_VALUE,
|
||||
OPT_OTHER,
|
||||
OPT_OTHER_VALUE,
|
||||
OPT_VERBOSE,
|
||||
OPT_REALLY_VERBOSE,
|
||||
OPT_MODE,
|
||||
OPT_MODE_VALUE,
|
||||
OPT_MODE_VALUE2,
|
||||
ARG_FILE,
|
||||
ARG_MORE,
|
||||
};
|
||||
|
||||
const char *text
|
||||
= "No one rejects, dislikes, or avoids pleasure itself, because it is "
|
||||
"pleasure, but because those who do not know how to pursue pleasure "
|
||||
"rationally encounter consequences that are extremely painful. Nor "
|
||||
"again is there anyone who loves or pursues or desires to obtain "
|
||||
"pain of itself, because it is pain, but because occasionally "
|
||||
"circumstances occur in which toil and pain can procure him some "
|
||||
"great pleasure.";
|
||||
|
||||
static int test_command(
|
||||
const b_command *self, const b_arglist *opt, const b_array *args)
|
||||
{
|
||||
printf("Hello, world!\n");
|
||||
|
||||
b_arglist_iterator it;
|
||||
b_arglist_iterator_begin(
|
||||
opt, B_COMMAND_INVALID_ID, B_COMMAND_INVALID_ID, &it);
|
||||
while (b_arglist_iterator_is_valid(&it)) {
|
||||
printf("opt:%u,arg:%u,i:%zu,value: %s\n", it.opt_id,
|
||||
it.value->val_id, it.i, it.value->val_str);
|
||||
|
||||
b_arglist_iterator_next(&it);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
B_COMMAND(CMD_TEST, B_COMMAND_INVALID_ID)
|
||||
{
|
||||
B_COMMAND_NAME("test");
|
||||
B_COMMAND_DESC("A test command.");
|
||||
B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT);
|
||||
B_COMMAND_FUNCTION(test_command);
|
||||
|
||||
B_COMMAND_OPTION(OPT_NAME)
|
||||
{
|
||||
B_OPTION_LONG_NAME("name");
|
||||
B_OPTION_SHORT_NAME('n');
|
||||
B_OPTION_DESC("The name of the data.");
|
||||
B_OPTION_ARG(OPT_NAME_VALUE)
|
||||
{
|
||||
B_ARG_NAME("name");
|
||||
B_ARG_NR_VALUES(2);
|
||||
}
|
||||
}
|
||||
|
||||
B_COMMAND_OPTION(OPT_OTHER)
|
||||
{
|
||||
B_OPTION_LONG_NAME("other");
|
||||
B_OPTION_DESC(text);
|
||||
|
||||
B_OPTION_ARG(OPT_OTHER_VALUE)
|
||||
{
|
||||
B_ARG_NAME("value");
|
||||
B_ARG_NR_VALUES(B_ARG_1_OR_MORE_VALUES);
|
||||
}
|
||||
}
|
||||
|
||||
B_COMMAND_OPTION(OPT_VERBOSE)
|
||||
{
|
||||
B_OPTION_LONG_NAME("verbose");
|
||||
B_OPTION_SHORT_NAME('v');
|
||||
B_OPTION_DESC("Show detailed log output.");
|
||||
}
|
||||
|
||||
B_COMMAND_OPTION(OPT_REALLY_VERBOSE)
|
||||
{
|
||||
B_OPTION_LONG_NAME("really-verbose");
|
||||
B_OPTION_SHORT_NAME('V');
|
||||
B_OPTION_DESC("Show REALLY detailed log output.");
|
||||
}
|
||||
|
||||
B_COMMAND_OPTION(OPT_MODE)
|
||||
{
|
||||
B_OPTION_SHORT_NAME('m');
|
||||
B_OPTION_LONG_NAME("mode");
|
||||
B_OPTION_DESC("modes to operate in.");
|
||||
|
||||
B_OPTION_ARG(OPT_MODE_VALUE)
|
||||
{
|
||||
B_ARG_NAME("mode");
|
||||
B_ARG_NR_VALUES(1);
|
||||
B_ARG_ALLOWED_VALUES("fast", "slow");
|
||||
}
|
||||
|
||||
B_OPTION_ARG(OPT_MODE_VALUE2)
|
||||
{
|
||||
B_ARG_NAME("mode2");
|
||||
B_ARG_NR_VALUES(1);
|
||||
B_ARG_ALLOWED_VALUES("very-fast", "very-slow");
|
||||
}
|
||||
}
|
||||
|
||||
B_COMMAND_ARG(ARG_FILE)
|
||||
{
|
||||
B_ARG_NAME("file");
|
||||
B_ARG_DESC("The file(s) to use");
|
||||
B_ARG_NR_VALUES(2);
|
||||
}
|
||||
|
||||
B_COMMAND_ARG(ARG_MORE)
|
||||
{
|
||||
B_ARG_NAME("more");
|
||||
B_ARG_DESC("More args to use");
|
||||
B_ARG_ALLOWED_VALUES("how", "wow");
|
||||
B_ARG_NR_VALUES(2);
|
||||
}
|
||||
|
||||
B_COMMAND_HELP_OPTION();
|
||||
|
||||
B_COMMAND_USAGE()
|
||||
{
|
||||
B_COMMAND_USAGE_OPT(OPT_NAME);
|
||||
B_COMMAND_USAGE_ARG(ARG_FILE);
|
||||
B_COMMAND_USAGE_ARG(ARG_MORE);
|
||||
}
|
||||
}
|
||||
|
||||
B_COMMAND(CMD_SUB, CMD_TEST)
|
||||
{
|
||||
B_COMMAND_NAME("sub");
|
||||
B_COMMAND_LONG_NAME("sub");
|
||||
B_COMMAND_SHORT_NAME('S');
|
||||
B_COMMAND_DESC("A test subcommand");
|
||||
B_COMMAND_FLAGS(B_COMMAND_SHOW_HELP_BY_DEFAULT);
|
||||
B_COMMAND_FUNCTION(test_command);
|
||||
|
||||
B_COMMAND_OPTION(OPT_NAME)
|
||||
{
|
||||
B_OPTION_LONG_NAME("name");
|
||||
B_OPTION_SHORT_NAME('n');
|
||||
B_OPTION_DESC("The name of the data");
|
||||
B_OPTION_ARG(OPT_NAME_VALUE)
|
||||
{
|
||||
B_ARG_NAME("name");
|
||||
}
|
||||
}
|
||||
|
||||
B_COMMAND_OPTION(OPT_OTHER)
|
||||
{
|
||||
B_OPTION_LONG_NAME("other");
|
||||
B_OPTION_SHORT_NAME('o');
|
||||
B_OPTION_DESC("The other argument");
|
||||
B_OPTION_ARG(OPT_OTHER_VALUE)
|
||||
{
|
||||
B_ARG_NAME("value");
|
||||
}
|
||||
}
|
||||
|
||||
B_COMMAND_HELP_OPTION();
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
return b_command_dispatch(CMD_TEST, argc, argv);
|
||||
}
|
||||
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;
|
||||
}
|
||||
203
test/core/core-units.c
Normal file
203
test/core/core-units.c
Normal file
@@ -0,0 +1,203 @@
|
||||
#include "blue/core/misc.h"
|
||||
|
||||
#include <CuTest.h>
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/core/queue.h>
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
struct test_tree_node {
|
||||
int value;
|
||||
b_btree_node node;
|
||||
};
|
||||
|
||||
struct test_queue_entry {
|
||||
int value;
|
||||
b_queue_entry entry;
|
||||
};
|
||||
|
||||
B_BTREE_DEFINE_SIMPLE_INSERT(struct test_tree_node, node, value, test_tree_insert);
|
||||
|
||||
void test_btree_insert(CuTest *tc)
|
||||
{
|
||||
b_btree tree = {0};
|
||||
struct test_tree_node nodes[3] = {0};
|
||||
|
||||
for (int i = 0; i < sizeof nodes / sizeof *nodes; i++) {
|
||||
nodes[i].value = i;
|
||||
}
|
||||
|
||||
test_tree_insert(&tree, &nodes[0]);
|
||||
|
||||
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left);
|
||||
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_right);
|
||||
CuAssertIntEquals(tc, 1, nodes[0].node.b_height);
|
||||
|
||||
test_tree_insert(&tree, &nodes[1]);
|
||||
|
||||
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left);
|
||||
CuAssertPtrEquals(tc, &nodes[1].node, nodes[0].node.b_right);
|
||||
CuAssertIntEquals(tc, 2, nodes[0].node.b_height);
|
||||
|
||||
CuAssertPtrEquals(tc, NULL, nodes[1].node.b_left);
|
||||
CuAssertPtrEquals(tc, NULL, nodes[1].node.b_right);
|
||||
CuAssertIntEquals(tc, 1, nodes[1].node.b_height);
|
||||
|
||||
test_tree_insert(&tree, &nodes[2]);
|
||||
|
||||
CuAssertPtrEquals(tc, &nodes[0].node, nodes[1].node.b_left);
|
||||
CuAssertPtrEquals(tc, &nodes[2].node, nodes[1].node.b_right);
|
||||
CuAssertIntEquals(tc, 2, nodes[1].node.b_height);
|
||||
|
||||
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_left);
|
||||
CuAssertPtrEquals(tc, NULL, nodes[0].node.b_right);
|
||||
CuAssertIntEquals(tc, 1, nodes[0].node.b_height);
|
||||
|
||||
CuAssertPtrEquals(tc, NULL, nodes[2].node.b_left);
|
||||
CuAssertPtrEquals(tc, NULL, nodes[2].node.b_right);
|
||||
CuAssertIntEquals(tc, 1, nodes[2].node.b_height);
|
||||
}
|
||||
|
||||
void test_btree_iterate(CuTest *tc)
|
||||
{
|
||||
static const size_t nr_nodes = 256;
|
||||
srand(time(NULL));
|
||||
|
||||
b_btree tree = {0};
|
||||
struct test_tree_node *nodes = calloc(nr_nodes, sizeof *nodes);
|
||||
CuAssertPtrNotNull(tc, nodes);
|
||||
|
||||
for (int i = 0; i < nr_nodes; i++) {
|
||||
nodes[i].value = rand();
|
||||
test_tree_insert(&tree, &nodes[i]);
|
||||
}
|
||||
|
||||
int prev = -1;
|
||||
b_btree_node *bnode = b_btree_first(&tree);
|
||||
while (bnode) {
|
||||
struct test_tree_node *node
|
||||
= b_unbox(struct test_tree_node, bnode, node);
|
||||
CuAssertPtrNotNull(tc, node);
|
||||
|
||||
if (prev == -1) {
|
||||
prev = node->value;
|
||||
bnode = b_btree_next(bnode);
|
||||
continue;
|
||||
}
|
||||
|
||||
CuAssertTrue(tc, prev <= node->value);
|
||||
prev = node->value;
|
||||
bnode = b_btree_next(bnode);
|
||||
}
|
||||
|
||||
free(nodes);
|
||||
}
|
||||
|
||||
void test_queue_insert(CuTest *tc)
|
||||
{
|
||||
struct test_queue_entry entries[5] = {0};
|
||||
for (int i = 0; i < sizeof entries / sizeof *entries; i++) {
|
||||
entries[i].value = i;
|
||||
}
|
||||
|
||||
b_queue q = B_QUEUE_INIT;
|
||||
|
||||
b_queue_push_back(&q, &entries[0].entry);
|
||||
b_queue_push_back(&q, &entries[2].entry);
|
||||
b_queue_push_back(&q, &entries[4].entry);
|
||||
b_queue_insert_after(&q, &entries[3].entry, &entries[2].entry);
|
||||
b_queue_insert_before(&q, &entries[1].entry, &entries[2].entry);
|
||||
|
||||
CuAssertPtrEquals(tc, NULL, entries[0].entry.qe_prev);
|
||||
CuAssertPtrEquals(tc, &entries[1].entry, entries[0].entry.qe_next);
|
||||
|
||||
CuAssertPtrEquals(tc, &entries[0].entry, entries[1].entry.qe_prev);
|
||||
CuAssertPtrEquals(tc, &entries[2].entry, entries[1].entry.qe_next);
|
||||
|
||||
CuAssertPtrEquals(tc, &entries[1].entry, entries[2].entry.qe_prev);
|
||||
CuAssertPtrEquals(tc, &entries[3].entry, entries[2].entry.qe_next);
|
||||
|
||||
CuAssertPtrEquals(tc, &entries[2].entry, entries[3].entry.qe_prev);
|
||||
CuAssertPtrEquals(tc, &entries[4].entry, entries[3].entry.qe_next);
|
||||
|
||||
CuAssertPtrEquals(tc, &entries[3].entry, entries[4].entry.qe_prev);
|
||||
CuAssertPtrEquals(tc, NULL, entries[4].entry.qe_next);
|
||||
}
|
||||
|
||||
void test_queue_iterate(CuTest *tc)
|
||||
{
|
||||
b_queue q = B_QUEUE_INIT;
|
||||
struct test_queue_entry entries[32] = {0};
|
||||
|
||||
for (int i = 0; i < sizeof entries / sizeof *entries; i++) {
|
||||
entries[i].value = i;
|
||||
b_queue_push_back(&q, &entries[i].entry);
|
||||
}
|
||||
|
||||
int prev = -1;
|
||||
struct b_queue_entry *entry = b_queue_first(&q);
|
||||
while (entry) {
|
||||
struct test_queue_entry *e
|
||||
= b_unbox(struct test_queue_entry, entry, entry);
|
||||
CuAssertPtrNotNull(tc, e);
|
||||
|
||||
if (prev == -1) {
|
||||
prev = e->value;
|
||||
goto skip;
|
||||
}
|
||||
|
||||
CuAssertTrue(tc, prev < e->value);
|
||||
prev = e->value;
|
||||
skip:
|
||||
entry = b_queue_next(entry);
|
||||
}
|
||||
}
|
||||
|
||||
void test_stringstream_1(CuTest *tc)
|
||||
{
|
||||
char buf[1024];
|
||||
b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf);
|
||||
|
||||
b_stream_write_string(s, "hello", NULL);
|
||||
b_stream_write_fmt(s, NULL, "(%d + %.1f)", 32, 2.3);
|
||||
|
||||
char *end = b_stringstream_steal(s);
|
||||
b_stringstream_unref(s);
|
||||
|
||||
CuAssertStrEquals(tc, "hello(32 + 2.3)", end);
|
||||
}
|
||||
|
||||
void test_stringstream_2(CuTest *tc)
|
||||
{
|
||||
char buf[1024];
|
||||
b_stringstream *s = b_stringstream_create_with_buffer(buf, sizeof buf);
|
||||
|
||||
b_stream_write_string(s, "{\n", NULL);
|
||||
b_stream_push_indent(s, 1);
|
||||
|
||||
b_stream_write_string(s, "a = 32,\n", NULL);
|
||||
b_stream_write_string(s, "b = 64\n", NULL);
|
||||
|
||||
b_stream_pop_indent(s);
|
||||
b_stream_write_string(s, "}", NULL);
|
||||
|
||||
char *str = b_stringstream_steal(s);
|
||||
b_stringstream_unref(s);
|
||||
|
||||
CuAssertStrEquals(tc, "{\n a = 32,\n b = 64\n}", str);
|
||||
}
|
||||
|
||||
CuSuite *get_all_tests(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
|
||||
SUITE_ADD_TEST(suite, test_btree_insert);
|
||||
SUITE_ADD_TEST(suite, test_btree_iterate);
|
||||
SUITE_ADD_TEST(suite, test_queue_insert);
|
||||
SUITE_ADD_TEST(suite, test_queue_iterate);
|
||||
SUITE_ADD_TEST(suite, test_stringstream_1);
|
||||
SUITE_ADD_TEST(suite, test_stringstream_2);
|
||||
|
||||
return suite;
|
||||
}
|
||||
6
test/core/errors.c
Normal file
6
test/core/errors.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include <blue/core/error.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
74
test/core/hash.c
Normal file
74
test/core/hash.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include <blue/core/hash.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static void print_digest(
|
||||
const char *func_name, b_hash_function func, const char *msg,
|
||||
size_t msg_len, size_t digest_len)
|
||||
{
|
||||
unsigned char digest[128];
|
||||
|
||||
b_hash_ctx ctx;
|
||||
if (!B_OK(b_hash_ctx_init(&ctx, func))) {
|
||||
printf("b_hash_ctx_init failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!B_OK(b_hash_ctx_update(&ctx, msg, msg_len))) {
|
||||
printf("b_hash_ctx_update failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!B_OK(b_hash_ctx_finish(&ctx, digest, sizeof digest))) {
|
||||
printf("b_hash_ctx_finish failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
char digest_str[256];
|
||||
for (size_t i = 0; i < digest_len; i++) {
|
||||
snprintf(
|
||||
digest_str + (i * 2), sizeof digest_str - (i * 2),
|
||||
"%02x", digest[i]);
|
||||
}
|
||||
|
||||
printf("%s(%s) = %s\n", func_name, msg, digest_str);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
|
||||
const char *msg = "Hello, world!";
|
||||
size_t msg_len = strlen(msg);
|
||||
|
||||
print_digest("MD4", B_HASH_MD4, msg, msg_len, B_DIGEST_LENGTH_MD4);
|
||||
print_digest("MD5", B_HASH_MD5, msg, msg_len, B_DIGEST_LENGTH_MD5);
|
||||
print_digest("SHA1", B_HASH_SHA1, msg, msg_len, B_DIGEST_LENGTH_SHA1);
|
||||
print_digest(
|
||||
"SHA224", B_HASH_SHA2_224, msg, msg_len, B_DIGEST_LENGTH_SHA2_224);
|
||||
print_digest(
|
||||
"SHA256", B_HASH_SHA2_256, msg, msg_len, B_DIGEST_LENGTH_SHA2_256);
|
||||
print_digest(
|
||||
"SHA384", B_HASH_SHA2_384, msg, msg_len, B_DIGEST_LENGTH_SHA2_384);
|
||||
print_digest(
|
||||
"SHA512", B_HASH_SHA2_512, msg, msg_len, B_DIGEST_LENGTH_SHA2_512);
|
||||
print_digest(
|
||||
"SHA3-224", B_HASH_SHA3_224, msg, msg_len,
|
||||
B_DIGEST_LENGTH_SHA3_224);
|
||||
print_digest(
|
||||
"SHA3-256", B_HASH_SHA3_256, msg, msg_len,
|
||||
B_DIGEST_LENGTH_SHA3_256);
|
||||
print_digest(
|
||||
"SHA3-384", B_HASH_SHA3_384, msg, msg_len,
|
||||
B_DIGEST_LENGTH_SHA3_384);
|
||||
print_digest(
|
||||
"SHA3-512", B_HASH_SHA3_512, msg, msg_len,
|
||||
B_DIGEST_LENGTH_SHA3_512);
|
||||
print_digest(
|
||||
"SHAKE128", B_HASH_SHAKE128, msg, msg_len,
|
||||
B_DIGEST_LENGTH_SHAKE128);
|
||||
print_digest(
|
||||
"SHAKE256", B_HASH_SHAKE256, msg, msg_len,
|
||||
B_DIGEST_LENGTH_SHAKE256);
|
||||
|
||||
return 0;
|
||||
}
|
||||
39
test/core/randomise.c
Normal file
39
test/core/randomise.c
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <blue/core/random.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define NRAND_NUMBERS 12
|
||||
#define NRAND_BYTES 128
|
||||
#define NRAND_DOUBLES 8
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_random_ctx random;
|
||||
b_random_init(&random, B_RANDOM_SECURE | B_RANDOM_MT19937);
|
||||
|
||||
printf("generating %d random numbers:\n", NRAND_NUMBERS);
|
||||
for (int i = 0; i < NRAND_NUMBERS; i++) {
|
||||
unsigned long long v = b_random_next_int64(&random);
|
||||
printf(" %llu\n", v);
|
||||
}
|
||||
|
||||
printf("\ngenerating %d random bytes:", NRAND_BYTES);
|
||||
unsigned char bytes[16];
|
||||
for (int i = 0; i < NRAND_BYTES; i++) {
|
||||
if (i == 0 || (i % 16) == 0) {
|
||||
printf("\n ");
|
||||
b_random_next_bytes(&random, bytes, sizeof bytes);
|
||||
} else if ((i % 4) == 0) {
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
printf("%02x", bytes[i % 16]);
|
||||
}
|
||||
|
||||
printf("\n\ngenerating %d random doubles:\n", NRAND_DOUBLES);
|
||||
for (int i = 0; i < NRAND_DOUBLES; i++) {
|
||||
double v = b_random_next_double(&random);
|
||||
printf(" %lf\n", v);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
67
test/core/ringbuffers.c
Normal file
67
test/core/ringbuffers.c
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <assert.h>
|
||||
#include <blue/core/ringbuffer.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define BUF_SIZE 32
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_ringbuffer *buf = b_ringbuffer_create(BUF_SIZE);
|
||||
|
||||
size_t read_available = b_ringbuffer_available_data_remaining(buf);
|
||||
size_t write_available = b_ringbuffer_write_capacity_remaining(buf);
|
||||
|
||||
printf("read available: %zu\n", read_available);
|
||||
printf("write available: %zu\n", write_available);
|
||||
|
||||
assert(read_available == 0);
|
||||
assert(write_available == BUF_SIZE - 1);
|
||||
|
||||
const char ch = 'X';
|
||||
printf("putc(%c)\n", ch);
|
||||
|
||||
b_ringbuffer_putc(buf, ch);
|
||||
read_available = b_ringbuffer_available_data_remaining(buf);
|
||||
write_available = b_ringbuffer_write_capacity_remaining(buf);
|
||||
printf("read available: %zu\n", read_available);
|
||||
printf("write available: %zu\n", write_available);
|
||||
assert(read_available == 1);
|
||||
assert(write_available == BUF_SIZE - 2);
|
||||
|
||||
int c = b_ringbuffer_getc(buf);
|
||||
printf("getc() = %c\n", c);
|
||||
assert(c == ch);
|
||||
|
||||
const char s[]
|
||||
= "A very long string that is designed to overflow the "
|
||||
"ringbuffer";
|
||||
size_t s_len = strlen(s);
|
||||
|
||||
size_t nr_written = 0;
|
||||
printf("write(%s)\n", s);
|
||||
b_ringbuffer_write(buf, s, s_len, &nr_written);
|
||||
read_available = b_ringbuffer_available_data_remaining(buf);
|
||||
write_available = b_ringbuffer_write_capacity_remaining(buf);
|
||||
printf("nr written: %zu\n", nr_written);
|
||||
printf("read available: %zu\n", read_available);
|
||||
printf("write available: %zu\n", write_available);
|
||||
assert(read_available == BUF_SIZE - 1);
|
||||
assert(write_available == 0);
|
||||
|
||||
char data[BUF_SIZE + 32] = {0};
|
||||
size_t nr_read = 0;
|
||||
b_ringbuffer_read(buf, data, sizeof data, &nr_read);
|
||||
printf("read(%u) = %zu bytes\n", BUF_SIZE + 32, nr_read);
|
||||
printf(" = %s\n", data);
|
||||
read_available = b_ringbuffer_available_data_remaining(buf);
|
||||
write_available = b_ringbuffer_write_capacity_remaining(buf);
|
||||
printf("read available: %zu\n", read_available);
|
||||
printf("write available: %zu\n", write_available);
|
||||
assert(read_available == 0);
|
||||
assert(write_available == BUF_SIZE - 1);
|
||||
|
||||
b_ringbuffer_unref(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
28
test/core/ropes.c
Normal file
28
test/core/ropes.c
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <blue/core/rope.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_rope a = B_ROPE_CHAR('a');
|
||||
b_rope b = B_ROPE_CSTR_STATIC("Hello, world!");
|
||||
b_rope c = B_ROPE_INT(-4096);
|
||||
b_rope d = B_ROPE_UINT(4096);
|
||||
|
||||
b_rope str;
|
||||
|
||||
const b_rope *ropes[] = {
|
||||
&a,
|
||||
&b,
|
||||
&c,
|
||||
&d,
|
||||
};
|
||||
|
||||
b_rope_join(&str, ropes, sizeof ropes / sizeof ropes[0]);
|
||||
|
||||
char cstr[1024];
|
||||
b_rope_to_cstr(&str, cstr, sizeof cstr);
|
||||
b_rope_destroy(&str);
|
||||
|
||||
printf("%s\n", cstr);
|
||||
return 0;
|
||||
}
|
||||
10
test/core/streams.c
Normal file
10
test/core/streams.c
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <blue/core/stream.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_stream_read_line_s(b_stdin, b_stdout);
|
||||
b_stream_write_char(b_stdout, '\n');
|
||||
|
||||
return 0;
|
||||
}
|
||||
21
test/ds/arrays.c
Normal file
21
test/ds/arrays.c
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <blue/ds/array.h>
|
||||
#include <blue/ds/number.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_array *array = b_array_create();
|
||||
b_array_append(array, B_RV_INT(32));
|
||||
b_array_append(array, B_RV_INT(64));
|
||||
b_array_append(array, B_RV_INT(128));
|
||||
|
||||
b_iterator *it = b_iterator_begin(array);
|
||||
b_foreach_ptr(b_object, obj, it)
|
||||
{
|
||||
printf("object %p\n", obj);
|
||||
}
|
||||
b_iterator_unref(it);
|
||||
|
||||
b_array_unref(array);
|
||||
return 0;
|
||||
}
|
||||
50
test/ds/ds-units.c
Normal file
50
test/ds/ds-units.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <CuTest.h>
|
||||
#include <blue/ds/string.h>
|
||||
|
||||
static void test_string_create(CuTest *tc)
|
||||
{
|
||||
b_string *str = b_string_create();
|
||||
|
||||
CuAssertPtrNotNull(tc, str);
|
||||
CuAssertIntEquals(tc, 0, b_string_get_size(str, B_STRLEN_NORMAL));
|
||||
CuAssertStrEquals(tc, "", b_string_ptr(str));
|
||||
|
||||
b_string_unref(str);
|
||||
|
||||
str = b_string_create_from_c('A', 8);
|
||||
|
||||
CuAssertPtrNotNull(tc, str);
|
||||
CuAssertIntEquals(tc, 8, b_string_get_size(str, B_STRLEN_NORMAL));
|
||||
CuAssertStrEquals(tc, "AAAAAAAA", b_string_ptr(str));
|
||||
|
||||
b_string_unref(str);
|
||||
|
||||
str = b_string_create_from_cstr("Hello, world!");
|
||||
|
||||
CuAssertPtrNotNull(tc, str);
|
||||
CuAssertIntEquals(tc, 13, b_string_get_size(str, B_STRLEN_NORMAL));
|
||||
CuAssertStrEquals(tc, "Hello, world!", b_string_ptr(str));
|
||||
|
||||
b_string_unref(str);
|
||||
}
|
||||
|
||||
static void test_string_length(CuTest *tc)
|
||||
{
|
||||
const char *cstr = "Hello, \033[91;1mworld!";
|
||||
b_string *s = b_string_create_from_cstr(cstr);
|
||||
|
||||
CuAssertIntEquals(tc, 13, b_string_get_size(s, B_STRLEN_IGNORE_ESC));
|
||||
CuAssertIntEquals(tc, 20, b_string_get_size(s, B_STRLEN_NORMAL));
|
||||
|
||||
b_string_unref(s);
|
||||
}
|
||||
|
||||
CuSuite *get_all_tests(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
|
||||
SUITE_ADD_TEST(suite, test_string_create);
|
||||
SUITE_ADD_TEST(suite, test_string_length);
|
||||
|
||||
return suite;
|
||||
}
|
||||
11
test/ds/numbers.c
Normal file
11
test/ds/numbers.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <blue/ds/number.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_number *number = b_number_create_float(6.8);
|
||||
|
||||
printf("number=%zd\n", B_NUMBER_IVAL(number));
|
||||
b_number_unref(number);
|
||||
return 0;
|
||||
}
|
||||
11
test/ds/simple.c
Normal file
11
test/ds/simple.c
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <blue/ds/string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_string *string = B_CSTR("Hello, world!");
|
||||
printf("string object = ");
|
||||
b_object_to_string(string, b_stdout);
|
||||
printf("\n");
|
||||
b_string_unref(string);
|
||||
return 0;
|
||||
}
|
||||
19
test/ds/streams.c
Normal file
19
test/ds/streams.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <blue/core/stream.h>
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
size_t nr_read = 0;
|
||||
b_stringstream *dest_stream = b_stringstream_create();
|
||||
b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024);
|
||||
b_stream_read_all_bytes_s(b_stdin, dest_stream, buf, &nr_read);
|
||||
|
||||
printf("done. read %zu bytes total.\n", nr_read);
|
||||
printf("%s\n", b_stringstream_ptr(dest_stream));
|
||||
|
||||
b_stringstream_unref(dest_stream);
|
||||
b_stream_buffer_unref(buf);
|
||||
return 0;
|
||||
}
|
||||
43
test/ds/strings.c
Normal file
43
test/ds/strings.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("-------------\n");
|
||||
b_string *str = b_string_create_from_cstr("Hello, world!\n");
|
||||
printf("%s\n", b_string_ptr(str));
|
||||
printf("len:%zu, max:%zu\n", b_string_get_size(str, B_STRLEN_NORMAL),
|
||||
b_string_get_capacity(str));
|
||||
|
||||
b_string_insert_cstr(str, "WOW!", 4);
|
||||
|
||||
printf("-------------\n");
|
||||
printf("%s\n", b_string_ptr(str));
|
||||
printf("len:%zu, max:%zu\n", b_string_get_size(str, B_STRLEN_NORMAL),
|
||||
b_string_get_capacity(str));
|
||||
|
||||
b_string_replace(str, 4, 4, "+");
|
||||
|
||||
printf("-------------\n");
|
||||
printf("%s\n", b_string_ptr(str));
|
||||
printf("len:%zu, max:%zu\n", b_string_get_size(str, B_STRLEN_NORMAL),
|
||||
b_string_get_capacity(str));
|
||||
printf("-------------\n");
|
||||
|
||||
b_string_unref(str);
|
||||
|
||||
b_stringstream *strv = b_stringstream_create();
|
||||
b_stream_write_string(strv, "Hello", NULL);
|
||||
b_stream_write_string(strv, ", world", NULL);
|
||||
b_stream_write_string(strv, "!", NULL);
|
||||
|
||||
char *s = b_stringstream_steal(strv);
|
||||
b_stringstream_unref(strv);
|
||||
|
||||
printf("%s\n", s);
|
||||
free(s);
|
||||
|
||||
return 0;
|
||||
}
|
||||
125
test/ds/trees.c
Normal file
125
test/ds/trees.c
Normal file
@@ -0,0 +1,125 @@
|
||||
#include <blue/core/btree.h>
|
||||
#include <blue/core/iterator.h>
|
||||
#include <blue/ds/dict.h>
|
||||
#include <blue/ds/number.h>
|
||||
#include <blue/ds/tree.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define NITEMS 16
|
||||
|
||||
struct tree_item {
|
||||
int value;
|
||||
b_tree_node node;
|
||||
};
|
||||
|
||||
struct btree_item {
|
||||
int value;
|
||||
b_btree_node node;
|
||||
};
|
||||
|
||||
B_BTREE_DEFINE_SIMPLE_GET(struct btree_item, int, node, value, get_node)
|
||||
B_BTREE_DEFINE_SIMPLE_INSERT(struct btree_item, node, value, put_node)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_dict *dict = b_dict_create();
|
||||
b_dict_put(dict, "hello", B_RV_INT(32));
|
||||
b_dict_put(dict, "world", B_RV_INT(64));
|
||||
b_dict_put(dict, "more", B_RV_INT(128));
|
||||
b_dict_put(dict, "other", B_RV_INT(256));
|
||||
|
||||
b_iterator *it = b_iterator_begin(dict);
|
||||
|
||||
size_t i = 0;
|
||||
b_foreach(b_dict_item *, item, it)
|
||||
{
|
||||
printf("item %zu: %s=%d\n", i++, b_string_ptr(item->key),
|
||||
b_number_get_int(item->value));
|
||||
}
|
||||
|
||||
b_iterator_unref(it);
|
||||
|
||||
b_tree *tree = b_tree_create();
|
||||
struct tree_item items2[NITEMS];
|
||||
|
||||
for (int i = 0; i < NITEMS; i++) {
|
||||
items2[i].value = i;
|
||||
items2[i].node = B_TREE_NODE_INIT;
|
||||
}
|
||||
|
||||
b_tree_set_root(tree, &items2[0].node);
|
||||
|
||||
b_tree_node_add_child(&items2[0].node, &items2[1].node);
|
||||
b_tree_node_add_child(&items2[0].node, &items2[2].node);
|
||||
b_tree_node_add_child(&items2[0].node, &items2[3].node);
|
||||
b_tree_node_add_child(&items2[0].node, &items2[7].node);
|
||||
b_tree_node_add_child(&items2[1].node, &items2[4].node);
|
||||
b_tree_node_add_child(&items2[1].node, &items2[5].node);
|
||||
b_tree_node_add_child(&items2[4].node, &items2[6].node);
|
||||
|
||||
#if 0
|
||||
it = b_iterator_begin(tree);
|
||||
b_tree_iterator it2;
|
||||
b_tree_foreach(&it2, tree)
|
||||
{
|
||||
struct tree_item *item = b_unbox(struct tree_item, it2.node, node);
|
||||
|
||||
for (size_t i = 0; i < it2.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%u\n", item->value);
|
||||
}
|
||||
|
||||
b_btree btree = {0};
|
||||
struct btree_item items3[NITEMS] = {0};
|
||||
for (int i = 0; i < NITEMS; i++) {
|
||||
items3[i].value = i;
|
||||
put_node(&btree, &items3[i]);
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
b_btree_iterator it3;
|
||||
b_btree_foreach (&it3, &btree) {
|
||||
struct btree_item *item
|
||||
= b_unbox(struct btree_item, it3.node, node);
|
||||
|
||||
for (size_t i = 0; i < it3.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%d\n", item->value);
|
||||
}
|
||||
|
||||
b_btree_iterator_begin(&btree, &it3);
|
||||
while (b_btree_iterator_is_valid(&it3)) {
|
||||
struct btree_item *item
|
||||
= b_unbox(struct btree_item, it3.node, node);
|
||||
|
||||
if (item->value == 9) {
|
||||
b_btree_iterator_erase(&it3);
|
||||
} else {
|
||||
b_btree_iterator_next(&it3);
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n\n");
|
||||
|
||||
b_btree_foreach (&it3, &btree) {
|
||||
struct btree_item *item
|
||||
= b_unbox(struct btree_item, it3.node, node);
|
||||
|
||||
for (size_t i = 0; i < it3.depth; i++) {
|
||||
fputs(" ", stdout);
|
||||
}
|
||||
|
||||
printf("%d\n", item->value);
|
||||
}
|
||||
|
||||
b_tree_unref(tree);
|
||||
#endif
|
||||
b_dict_unref(dict);
|
||||
|
||||
return 0;
|
||||
}
|
||||
26
test/ds/unicode-strings.c
Normal file
26
test/ds/unicode-strings.c
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("здравс\u26A0твуите\n");
|
||||
b_string *str = b_string_create_from_cstr("здравствуите");
|
||||
const char *s = b_string_ptr(str);
|
||||
printf("%s\n", s);
|
||||
printf("len: %zu\n", b_string_get_size(str, B_STRLEN_NORMAL));
|
||||
printf("codepoints: %zu\n", b_string_get_size(str, B_STRLEN_CODEPOINTS));
|
||||
|
||||
const char *delims[] = {"в"};
|
||||
size_t nr_delims = sizeof delims / sizeof delims[0];
|
||||
|
||||
b_iterator *it = b_string_tokenise(str, delims, nr_delims, 0);
|
||||
b_foreach(const char *, tok, it)
|
||||
{
|
||||
printf("%s\n", tok);
|
||||
}
|
||||
b_iterator_unref(it);
|
||||
|
||||
return 0;
|
||||
}
|
||||
14
test/ds/uuids.c
Normal file
14
test/ds/uuids.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <blue/ds/uuid.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_uuid *uuid = b_uuid_create_from_cstr(
|
||||
"5b80ad1f-367f-4a1f-88f3-b3a6f8d1f63d");
|
||||
char str[B_UUID_STRING_MAX];
|
||||
b_uuid_to_cstr(uuid, str);
|
||||
printf("%s\n", str);
|
||||
|
||||
b_uuid_unref(uuid);
|
||||
return 0;
|
||||
}
|
||||
35
test/io/io-units.c
Normal file
35
test/io/io-units.c
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <CuTest.h>
|
||||
#include <blue/core/stringstream.h>
|
||||
#include <blue/io/path.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void test_path_1(CuTest *tc)
|
||||
{
|
||||
b_path *path = b_path_create_from_cstr("C:\\hello\\world\\");
|
||||
char buf[512];
|
||||
b_stringstream *str = b_stringstream_create_with_buffer(buf, sizeof buf);
|
||||
|
||||
b_object_to_string(path, str);
|
||||
|
||||
printf("%s\n", buf);
|
||||
|
||||
b_path *path2 = b_path_create_from_cstr("path1\\path2\\");
|
||||
b_path *path3 = b_path_create_from_cstr("path3\\path4\\");
|
||||
|
||||
const b_path *paths[] = {path, path2, path3};
|
||||
|
||||
b_path *path4 = b_path_join(paths, sizeof paths / sizeof paths[0]);
|
||||
|
||||
b_stringstream_reset_with_buffer(str, buf, sizeof buf);
|
||||
b_object_to_string(path4, str);
|
||||
printf("%s\n", buf);
|
||||
}
|
||||
|
||||
CuSuite *get_all_tests(void)
|
||||
{
|
||||
CuSuite *suite = CuSuiteNew();
|
||||
|
||||
SUITE_ADD_TEST(suite, test_path_1);
|
||||
|
||||
return suite;
|
||||
}
|
||||
22
test/io/mkdir.c
Normal file
22
test/io/mkdir.c
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <blue/io/directory.h>
|
||||
#include <blue/io/path.h>
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *path = argv[1];
|
||||
|
||||
b_directory *dir;
|
||||
b_result result = b_directory_open(
|
||||
NULL, B_RV_PATH(path), B_DIRECTORY_OPEN_CREATE_INTERMEDIATE, &dir);
|
||||
if (b_result_is_error(result)) {
|
||||
b_throw(result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_directory_unref(dir);
|
||||
return 0;
|
||||
}
|
||||
25
test/io/rmdir.c
Normal file
25
test/io/rmdir.c
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <blue/io/directory.h>
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *path = argv[1];
|
||||
|
||||
b_directory *dir;
|
||||
b_result result = b_directory_open(NULL, B_RV_PATH(path), 0, &dir);
|
||||
if (b_result_is_error(result)) {
|
||||
b_throw(result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = b_directory_delete(dir);
|
||||
if (b_result_is_error(result)) {
|
||||
b_throw(result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
test/io/streams.c
Normal file
23
test/io/streams.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <blue/core/stream.h>
|
||||
#include <blue/io/file.h>
|
||||
#include <blue/io/path.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
b_file *dest;
|
||||
b_path *path = b_path_create_from_cstr("data.txt");
|
||||
b_result result = b_file_open(
|
||||
NULL, path, B_FILE_WRITE_ONLY | B_FILE_CREATE, &dest);
|
||||
if (b_result_is_error(result)) {
|
||||
b_throw(result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t nr_read = 0;
|
||||
b_stream_buffer *buf = b_stream_buffer_create_dynamic(1024);
|
||||
b_stream_read_all_bytes_s(b_stdin, dest, buf, &nr_read);
|
||||
|
||||
printf("done. read %zu bytes total.\n", nr_read);
|
||||
return 0;
|
||||
}
|
||||
32
test/io/tree.c
Normal file
32
test/io/tree.c
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <blue/io/directory.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define NRAND_NUMBERS 12
|
||||
#define NRAND_BYTES 128
|
||||
#define NRAND_DOUBLES 8
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
{
|
||||
if (argc < 2) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_directory *dir = NULL;
|
||||
b_path *path = b_path_create_from_cstr(argv[1]);
|
||||
b_result result = b_directory_open(B_DIRECTORY_ROOT, path, 0, &dir);
|
||||
|
||||
if (b_result_is_error(result)) {
|
||||
b_throw(result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_iterator *it = b_directory_begin(dir, B_DIRECTORY_ITERATE_PARENT_FIRST);
|
||||
b_foreach(b_directory_entry *, entry, it)
|
||||
{
|
||||
printf("%s\n", b_path_ptr(entry->filepath));
|
||||
}
|
||||
|
||||
b_iterator_unref(it);
|
||||
|
||||
return 0;
|
||||
}
|
||||
37
test/serial/streams.c
Normal file
37
test/serial/streams.c
Normal file
@@ -0,0 +1,37 @@
|
||||
#include <blue/core/stream.h>
|
||||
#include <blue/ds/array.h>
|
||||
#include <blue/ds/dict.h>
|
||||
#include <blue/ds/number.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <blue/serial.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_serial_ctx *ctx = b_toml_serial_ctx_create();
|
||||
|
||||
b_dict *dict = b_dict_create();
|
||||
|
||||
b_array *array = b_array_create();
|
||||
b_array_append(array, B_RV_INT(32));
|
||||
b_array_append(array, B_RV_INT(64));
|
||||
b_array_append(array, B_RV_INT(128));
|
||||
|
||||
b_dict_put(dict, "numbers", B_RV(array));
|
||||
|
||||
array = b_array_create();
|
||||
b_array_append(array, B_RV_CSTR("hello"));
|
||||
b_array_append(array, B_RV_CSTR("world"));
|
||||
|
||||
b_dict_put(dict, "strings", B_RV(array));
|
||||
|
||||
b_object_to_string(dict, b_stdout);
|
||||
b_stream_write_char(b_stdout, '\n');
|
||||
|
||||
b_serial_ctx_serialise(ctx, dict, b_stdout, 0);
|
||||
|
||||
b_dict_unref(dict);
|
||||
b_serial_ctx_unref(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
223
test/serial/toml-decode.c
Normal file
223
test/serial/toml-decode.c
Normal file
@@ -0,0 +1,223 @@
|
||||
#include <blue/ds/array.h>
|
||||
#include <blue/ds/datetime.h>
|
||||
#include <blue/ds/dict.h>
|
||||
#include <blue/ds/number.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <blue/serial.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
void write_tagged_value(b_object *data);
|
||||
|
||||
void write_raw_string(const b_string *data)
|
||||
{
|
||||
b_stream_write_string(b_stdout, "\"", NULL);
|
||||
|
||||
const b_iterator *it = b_iterator_cbegin(data);
|
||||
b_foreach_c(b_wchar, c, it)
|
||||
{
|
||||
if (c >= 0x10000) {
|
||||
c -= 0x10000;
|
||||
long hi = 0xD800 | ((c >> 10) & 0x3FF);
|
||||
long lo = 0xDC00 | (c & 0x3FF);
|
||||
b_stream_write_fmt(b_stdout, NULL, "\\u%04x\\u%04x", hi, lo);
|
||||
} else if (c <= 0x1F || c >= 0x7F) {
|
||||
b_stream_write_fmt(b_stdout, NULL, "\\u%04x", c);
|
||||
} else if (c == '\\' || c == '"') {
|
||||
b_stream_write_fmt(b_stdout, NULL, "\\%c", c);
|
||||
} else {
|
||||
b_stream_write_char(b_stdout, c);
|
||||
}
|
||||
}
|
||||
b_iterator_unref(it);
|
||||
|
||||
b_stream_write_string(b_stdout, "\"", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_string(b_string *data)
|
||||
{
|
||||
b_stream_write_string(b_stdout, "{ \"type\": \"string\", \"value\": ", NULL);
|
||||
|
||||
write_raw_string(data);
|
||||
|
||||
b_stream_write_string(b_stdout, " }", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_integer(b_number *data)
|
||||
{
|
||||
b_stream_write_string(
|
||||
b_stdout, "{ \"type\": \"integer\", \"value\": \"", NULL);
|
||||
|
||||
if (b_number_is_inf_positive(data)) {
|
||||
b_stream_write_string(b_stdout, "inf", NULL);
|
||||
} else if (b_number_is_inf_negative(data)) {
|
||||
b_stream_write_string(b_stdout, "-inf", NULL);
|
||||
} else if (b_number_is_nan_positive(data)) {
|
||||
b_stream_write_string(b_stdout, "nan", NULL);
|
||||
} else if (b_number_is_nan_negative(data)) {
|
||||
b_stream_write_string(b_stdout, "-nan", NULL);
|
||||
} else {
|
||||
b_stream_write_fmt(
|
||||
b_stdout, NULL, "%lld", b_number_get_longlong(data), NULL);
|
||||
}
|
||||
|
||||
b_stream_write_string(b_stdout, "\" }", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_float(b_number *data)
|
||||
{
|
||||
b_stream_write_string(
|
||||
b_stdout, "{ \"type\": \"float\", \"value\": \"", NULL);
|
||||
|
||||
if (b_number_is_inf_positive(data)) {
|
||||
b_stream_write_string(b_stdout, "inf", NULL);
|
||||
} else if (b_number_is_inf_negative(data)) {
|
||||
b_stream_write_string(b_stdout, "-inf", NULL);
|
||||
} else if (b_number_is_nan_positive(data)) {
|
||||
b_stream_write_string(b_stdout, "nan", NULL);
|
||||
} else if (b_number_is_nan_negative(data)) {
|
||||
b_stream_write_string(b_stdout, "-nan", NULL);
|
||||
} else {
|
||||
double v = b_number_get_double(data);
|
||||
if ((v <= 0.00000001 && v > 0) || (v >= -0.00000001 && v < 0)
|
||||
|| (v >= 1000000000) || (v <= -1000000000)) {
|
||||
b_stream_write_fmt(b_stdout, NULL, "%.15e", v, NULL);
|
||||
} else {
|
||||
b_stream_write_fmt(b_stdout, NULL, "%.15f", v, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
b_stream_write_string(b_stdout, "\" }", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_bool(b_number *data)
|
||||
{
|
||||
int v = b_number_get_int8(data);
|
||||
b_stream_write_fmt(
|
||||
b_stdout, NULL, "{ \"type\": \"bool\", \"value\": \"%s\" }",
|
||||
(v > 0) ? "true" : "false", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_datetime(b_datetime *data)
|
||||
{
|
||||
bool has_date = b_datetime_has_date(data);
|
||||
bool has_time = b_datetime_has_time(data);
|
||||
bool localtime = b_datetime_is_localtime(data);
|
||||
|
||||
b_stream_write_string(b_stdout, "{ \"type\": \"", NULL);
|
||||
|
||||
if (has_date && has_time) {
|
||||
b_stream_write_string(
|
||||
b_stdout, localtime ? "datetime-local" : "datetime", NULL);
|
||||
} else if (has_date) {
|
||||
b_stream_write_string(
|
||||
b_stdout, localtime ? "date-local" : "date", NULL);
|
||||
} else if (has_time) {
|
||||
b_stream_write_string(
|
||||
b_stdout, localtime ? "time-local" : "time", NULL);
|
||||
}
|
||||
|
||||
b_stream_write_string(b_stdout, "\", \"value\": \"", NULL);
|
||||
|
||||
b_string *new_data = b_string_create();
|
||||
b_datetime_to_string(data, B_DATETIME_FORMAT_RFC3339, new_data);
|
||||
b_stream_write_string(b_stdout, b_string_ptr(new_data), NULL);
|
||||
|
||||
b_stream_write_string(b_stdout, "\" }", NULL);
|
||||
|
||||
b_string_unref(new_data);
|
||||
}
|
||||
|
||||
void write_tagged_dict(b_dict *data)
|
||||
{
|
||||
b_stream_write_string(b_stdout, "{ ", NULL);
|
||||
|
||||
int i = 0;
|
||||
|
||||
b_iterator *it = b_iterator_begin(data);
|
||||
b_foreach(b_dict_item *, item, it)
|
||||
{
|
||||
if (i++ > 0) {
|
||||
b_stream_write_string(b_stdout, ", ", NULL);
|
||||
}
|
||||
|
||||
write_raw_string(item->key);
|
||||
b_stream_write_string(b_stdout, ": ", NULL);
|
||||
write_tagged_value(item->value);
|
||||
}
|
||||
b_iterator_unref(it);
|
||||
|
||||
b_stream_write_string(b_stdout, " }", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_array(b_array *data)
|
||||
{
|
||||
b_stream_write_string(b_stdout, "[ ", NULL);
|
||||
|
||||
int i = 0;
|
||||
b_iterator *it = b_iterator_begin(data);
|
||||
b_foreach(b_object *, obj, it)
|
||||
{
|
||||
if (i++ > 0) {
|
||||
b_stream_write_string(b_stdout, ", ", NULL);
|
||||
}
|
||||
|
||||
write_tagged_value(obj);
|
||||
}
|
||||
b_iterator_unref(it);
|
||||
|
||||
b_stream_write_string(b_stdout, " ]", NULL);
|
||||
}
|
||||
|
||||
void write_tagged_value(b_object *data)
|
||||
{
|
||||
if (b_object_is_type(data, B_TYPE_DICT)) {
|
||||
write_tagged_dict(data);
|
||||
|
||||
} else if (b_object_is_type(data, B_TYPE_ARRAY)) {
|
||||
write_tagged_array(data);
|
||||
|
||||
} else if (b_object_is_type(data, B_TYPE_STRING)) {
|
||||
write_tagged_string(data);
|
||||
|
||||
} else if (b_object_is_type(data, B_TYPE_DATETIME)) {
|
||||
write_tagged_datetime(data);
|
||||
|
||||
} else if (b_object_is_type(data, B_TYPE_NUMBER)) {
|
||||
switch (b_number_get_number_type(data)) {
|
||||
case B_NUMBER_LONGLONG:
|
||||
write_tagged_integer(data);
|
||||
break;
|
||||
case B_NUMBER_INT8:
|
||||
write_tagged_bool(data);
|
||||
break;
|
||||
case B_NUMBER_DOUBLE:
|
||||
write_tagged_float(data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_stream *src = b_stdin;
|
||||
b_stream *dest = b_stdout;
|
||||
|
||||
b_serial_ctx *ctx = b_toml_serial_ctx_create();
|
||||
|
||||
b_object *data;
|
||||
b_status status = b_serial_ctx_deserialise(ctx, src, &data, 0);
|
||||
if (!B_OK(status)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
write_tagged_value(data);
|
||||
|
||||
b_stream_write_char(b_stdout, '\n');
|
||||
|
||||
b_serial_ctx_unref(ctx);
|
||||
b_object_unref(data);
|
||||
|
||||
return 0;
|
||||
}
|
||||
27
test/serial/toml-encode.c
Normal file
27
test/serial/toml-encode.c
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <blue/serial.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_stream *src = b_stdin;
|
||||
b_stream *dest = b_stdout;
|
||||
|
||||
#if 0
|
||||
b_serial_ctx *ctx;
|
||||
b_serial_ctx_create(&ctx);
|
||||
|
||||
b_object *data;
|
||||
b_status status = b_serial_ctx_deserialise(
|
||||
ctx, B_SERIAL_FORMAT_JSON, src, &data, 0);
|
||||
if (!B_OK(status)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
b_to_string(B_OBJECT(data), dest);
|
||||
b_stream_write_char(b_stdout, '\n');
|
||||
|
||||
b_release(data);
|
||||
b_serial_ctx_destroy(ctx);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
138
test/term/errors.c
Normal file
138
test/term/errors.c
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <blue/core/error.h>
|
||||
#include <blue/term/print.h>
|
||||
#include <stdio.h>
|
||||
|
||||
enum sample_code {
|
||||
SAMPLE_OK = 0,
|
||||
SAMPLE_ERR_IO_FAILURE,
|
||||
SAMPLE_ERR_FILE_READ_FAILED,
|
||||
};
|
||||
|
||||
enum sample_msg {
|
||||
SAMPLE_MSG_SUCCESS = 0,
|
||||
SAMPLE_MSG_A_TEMPLATED_MSG,
|
||||
};
|
||||
|
||||
static const b_error_definition sample_errors[] = {
|
||||
B_ERROR_DEFINITION(SAMPLE_OK, "OK", "Success"),
|
||||
B_ERROR_DEFINITION(SAMPLE_ERR_IO_FAILURE, "IO_FAILURE", "I/O failure"),
|
||||
B_ERROR_DEFINITION_TEMPLATE(
|
||||
SAMPLE_ERR_FILE_READ_FAILED, "FILE_READ_FAILED",
|
||||
"Failed to read file @i[filepath]",
|
||||
B_ERROR_TEMPLATE_PARAM(
|
||||
"filepath", B_ERROR_TEMPLATE_PARAM_STRING, "%s")),
|
||||
};
|
||||
|
||||
static const b_error_msg sample_error_msg[] = {
|
||||
B_ERROR_MSG_TEMPLATE(
|
||||
SAMPLE_MSG_A_TEMPLATED_MSG, "A templated message: @e[param1]",
|
||||
B_ERROR_TEMPLATE_PARAM(
|
||||
"param1", B_ERROR_TEMPLATE_PARAM_STRING, "%s")),
|
||||
};
|
||||
|
||||
static const char *sample_code_to_string(
|
||||
const struct b_error_vendor *vendor, b_error_status_code code)
|
||||
{
|
||||
switch (code) {
|
||||
case SAMPLE_OK:
|
||||
return "OK";
|
||||
case SAMPLE_ERR_IO_FAILURE:
|
||||
return "IO_FAILURE";
|
||||
case SAMPLE_ERR_FILE_READ_FAILED:
|
||||
return "FILE_READ_FAILED";
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static b_error_vendor sample_vendor = {
|
||||
.v_name = "Sample",
|
||||
.v_error_definitions = sample_errors,
|
||||
.v_error_definitions_length = sizeof sample_errors,
|
||||
.v_msg = sample_error_msg,
|
||||
.v_msg_length = sizeof sample_error_msg,
|
||||
};
|
||||
|
||||
static b_result error_return_3(void)
|
||||
{
|
||||
b_result err = b_error_with_string(
|
||||
&sample_vendor, SAMPLE_ERR_IO_FAILURE,
|
||||
"I/O failure while reading file");
|
||||
|
||||
b_error_add_submsg_string(
|
||||
err, B_ERROR_SUBMSG_ERROR, "An @e{error} message");
|
||||
b_error_add_submsg_string(
|
||||
err, B_ERROR_SUBMSG_WARNING, "A @w{warning} message");
|
||||
b_error_add_submsg_template(
|
||||
err, B_ERROR_SUBMSG_WARNING, SAMPLE_MSG_A_TEMPLATED_MSG,
|
||||
B_ERROR_PARAM("param1", "Hello!"));
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static b_result error_return_2(void)
|
||||
{
|
||||
return b_result_propagate(error_return_3());
|
||||
}
|
||||
|
||||
static b_result error_return_1(void)
|
||||
{
|
||||
return b_result_propagate(error_return_2());
|
||||
}
|
||||
|
||||
struct param {
|
||||
const char *name;
|
||||
int value;
|
||||
};
|
||||
|
||||
#define PARAM(n, v) \
|
||||
(struct param) \
|
||||
{ \
|
||||
.name = (n), .value = (v) \
|
||||
}
|
||||
|
||||
static void __test(struct param params[])
|
||||
{
|
||||
for (size_t i = 0; params[i].name; i++) {
|
||||
printf("%s = %d\n", params[i].name, params[i].value);
|
||||
}
|
||||
}
|
||||
|
||||
#define test(...) __test((struct param[]) {__VA_ARGS__, {}})
|
||||
|
||||
static b_result some_operation(void)
|
||||
{
|
||||
b_result result = error_return_2();
|
||||
if (b_result_is_error(result)) {
|
||||
b_result err = b_error_with_template(
|
||||
&sample_vendor, SAMPLE_ERR_FILE_READ_FAILED,
|
||||
B_ERROR_PARAM("filepath", "src/Manifest.json"));
|
||||
b_error_add_submsg_string(
|
||||
err, B_ERROR_SUBMSG_INFO,
|
||||
"An @i{informational} message");
|
||||
|
||||
b_error_caused_by_b_status(result, B_ERR_IO_FAILURE);
|
||||
b_result err2 = b_error_caused_by(err, result);
|
||||
|
||||
return err2;
|
||||
}
|
||||
|
||||
return B_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
b_set_error_report_function(b_enhanced_error_reporter, B_ERROR_REPORT_ALL);
|
||||
|
||||
test(PARAM("Hello", 1), PARAM("Goodbye", 2));
|
||||
|
||||
b_result err;
|
||||
if (B_CATCH(err, some_operation())) {
|
||||
b_throw(err);
|
||||
}
|
||||
|
||||
b_throw_status(B_ERR_INVALID_ARGUMENT);
|
||||
b_throw_status_string(B_ERR_INVALID_ARGUMENT, "Hello!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
79
test/term/printing.c
Normal file
79
test/term/printing.c
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <blue/term/tty.h>
|
||||
#include <blue/term/print.h>
|
||||
#include <blue/ds/string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define F_GREEN "[green]"
|
||||
#define F_YELLOW "[yellow]"
|
||||
#define F_RESET "[reset]"
|
||||
|
||||
static const char *text = F_YELLOW
|
||||
"But I must " F_GREEN "explain " F_YELLOW "to you " F_GREEN
|
||||
"how all this " F_YELLOW "mistaken idea of" F_RESET
|
||||
" denouncing pleasure " F_GREEN "and praising pain was born " F_YELLOW
|
||||
"and I will give you " F_RESET "a complete account of " F_YELLOW
|
||||
"the system, and " F_RESET
|
||||
"expound the actual teachings of the great explorer of the truth, the "
|
||||
"master-builder of human happiness.\n"
|
||||
"No one rejects, dislikes, or avoids pleasure itself, because it is "
|
||||
"pleasure, but because those who do not know how to pursue pleasure "
|
||||
"rationally encounter consequences that are extremely painful. Nor "
|
||||
"again is there anyone who loves or pursues or desires to obtain pain "
|
||||
"of itself, because it is pain, but because occasionally circumstances "
|
||||
"occur in which toil and pain can procure him some great pleasure.\n"
|
||||
"To take a trivial example, which of us ever undertakes laborious "
|
||||
"physical exercise, except to obtain some advantage from it? But who "
|
||||
"has any right to find fault with a man who chooses to enjoy a "
|
||||
"pleasure that has no annoying consequences, or one who avoids a pain "
|
||||
"that produces no resultant pleasure? On the other hand, we denounce "
|
||||
"with righteous indignation and dislike men who are so beguiled and "
|
||||
"demoralized by the charms of pleasure of the moment, so blinded by "
|
||||
"desire, that they cannot foresee.";
|
||||
|
||||
static const char *text2
|
||||
= "But I must explain to you how all this mistaken idea of denouncing "
|
||||
"pleasure and praising pain was born and I will give you a complete "
|
||||
"account of the system, and expound the actual teachings of the "
|
||||
"great explorer of the truth, the master-builder of human "
|
||||
"happiness.\n"
|
||||
"No one rejects, dislikes, or avoids pleasure itself, because it is "
|
||||
"pleasure, but because those who do not know how to pursue pleasure "
|
||||
"rationally encounter consequences that are extremely painful. Nor "
|
||||
"again is there anyone who loves or pursues or desires to obtain "
|
||||
"pain of itself, because it is pain, but because occasionally "
|
||||
"circumstances occur in which toil and pain can procure him some "
|
||||
"great pleasure.\n"
|
||||
"To take a trivial example, which of us ever undertakes laborious "
|
||||
"physical exercise, except to obtain some advantage from it? But who "
|
||||
"has any right to find fault with a man who chooses to enjoy a "
|
||||
"pleasure that has no annoying consequences, or one who avoids a "
|
||||
"pain that produces no resultant pleasure? On the other hand, we "
|
||||
"denounce with righteous indignation and dislike men who are so "
|
||||
"beguiled and demoralized by the charms of pleasure of the moment, "
|
||||
"so blinded by desire, that they cannot foresee.";
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char *s = "[magenta,uline]Hello, [bright_magenta]world![reset]";
|
||||
b_puts(s);
|
||||
b_putc('\n');
|
||||
|
||||
b_string *str = b_string_create_from_cstr(s);
|
||||
size_t len = b_string_get_size(str, B_STRLEN_IGNORE_MOD);
|
||||
printf("length = %zu\n", len);
|
||||
|
||||
b_paragraph_format format = { 0 };
|
||||
format.p_left_margin = 5;
|
||||
format.p_right_margin = 5;
|
||||
format.p_flags = B_PARAGRAPH_DOUBLE_LINE_BREAK;
|
||||
|
||||
b_print_paragraph(text, b_stdtty, &format);
|
||||
|
||||
b_i("An informational message\n\nWith multiple lines");
|
||||
b_warn("A warning message\nWith multiple lines");
|
||||
b_err("An error message\nWith multiple lines");
|
||||
|
||||
b_printf("[red]formatting ignored: '%s'[reset]\n[dark_grey]dark text[reset]\n", "[blue]wow![reset]");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user