aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/exess/test/test_int.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-02-25 10:27:59 -0500
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:05 -0500
commitc4821c8e6bf1f81c6ea31e11ebc0fc1666e9337b (patch)
treea62995534f5f606ac2f8bae22d525532b824cb5e /subprojects/exess/test/test_int.c
parent6bcd18ae60482790b645a345f718e7099250f261 (diff)
downloadserd-c4821c8e6bf1f81c6ea31e11ebc0fc1666e9337b.tar.gz
serd-c4821c8e6bf1f81c6ea31e11ebc0fc1666e9337b.tar.bz2
serd-c4821c8e6bf1f81c6ea31e11ebc0fc1666e9337b.zip
Add exess from git@gitlab.com:drobilla/exess.git 4638b1f
Diffstat (limited to 'subprojects/exess/test/test_int.c')
-rw-r--r--subprojects/exess/test/test_int.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/subprojects/exess/test/test_int.c b/subprojects/exess/test/test_int.c
new file mode 100644
index 00000000..d3e43f7c
--- /dev/null
+++ b/subprojects/exess/test/test_int.c
@@ -0,0 +1,129 @@
+/*
+ Copyright 2011-2021 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#undef NDEBUG
+
+#include "int_test_data.h"
+#include "num_test_utils.h"
+
+#include "exess/exess.h"
+
+#include <assert.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+
+static void
+check_read(const char* const string,
+ const ExessStatus expected_status,
+ const int32_t expected_value,
+ const size_t expected_count)
+{
+ int32_t value = 0;
+ const ExessResult r = exess_read_int(&value, string);
+
+ assert(r.status == expected_status);
+ assert(r.count == expected_count);
+ assert(value == expected_value);
+}
+
+static void
+test_read_int(void)
+{
+ // Limits
+ check_read("-2147483648", EXESS_SUCCESS, INT32_MIN, EXESS_MAX_INT_LENGTH);
+ check_read("2147483647", EXESS_SUCCESS, INT32_MAX, 10);
+
+ // Out of range
+ check_read("-2147483649", EXESS_OUT_OF_RANGE, 0, 11);
+ check_read("2147483648", EXESS_OUT_OF_RANGE, 0, 10);
+ check_read("10000000000", EXESS_OUT_OF_RANGE, 0, 11);
+
+ // Garbage
+ check_read("+", EXESS_EXPECTED_DIGIT, 0, 1);
+}
+
+static void
+check_write(const int32_t value,
+ const ExessStatus expected_status,
+ const size_t buf_size,
+ const char* const expected_string)
+{
+ char buf[EXESS_MAX_INT_LENGTH + 1] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+
+ assert(buf_size <= sizeof(buf));
+
+ const ExessResult r = exess_write_int(value, buf_size, buf);
+ assert(!strcmp(buf, expected_string));
+ assert(r.status == expected_status);
+ assert(r.count == strlen(buf));
+ assert(r.status || exess_write_int(value, 0, NULL).count == r.count);
+}
+
+static void
+test_write_int(void)
+{
+ check_write(INT32_MIN, EXESS_SUCCESS, 12, "-2147483648");
+ check_write(INT32_MAX, EXESS_SUCCESS, 11, "2147483647");
+}
+
+static void
+test_round_trip(const ExessNumTestOptions opts)
+{
+ int32_t parsed_value = 0;
+ char buf[EXESS_MAX_INT_LENGTH + 1] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
+
+ if (opts.exhaustive) {
+ fprintf(stderr, "Testing xsd:int exhaustively\n");
+
+ for (int64_t i = INT32_MIN; i <= INT32_MAX; ++i) {
+ assert(!exess_write_int((int32_t)i, sizeof(buf), buf).status);
+ assert(!exess_read_int(&parsed_value, buf).status);
+ assert(parsed_value == i);
+
+ print_num_test_progress((uint64_t)(i - (int64_t)INT32_MIN), UINT32_MAX);
+ }
+ } else {
+ fprintf(stderr, "Testing xsd:int randomly with seed %u\n", opts.seed);
+ uint32_t rep = opts.seed;
+ for (uint64_t i = 0; i < opts.n_tests; ++i) {
+ rep = lcg32(rep);
+
+ const int32_t value = (int32_t)rep;
+
+ assert(!exess_write_int(value, sizeof(buf), buf).status);
+ assert(!exess_read_int(&parsed_value, buf).status);
+ assert(parsed_value == value);
+
+ print_num_test_progress(i, opts.n_tests);
+ }
+ }
+}
+
+int
+main(int argc, char** argv)
+{
+ const ExessNumTestOptions opts = parse_num_test_options(argc, argv);
+ if (opts.error) {
+ return 1;
+ }
+
+ test_read_int();
+ test_write_int();
+ test_round_trip(opts);
+
+ return 0;
+}