aboutsummaryrefslogtreecommitdiff
path: root/test.c
diff options
context:
space:
mode:
authorkartofen <mladenovnasko0@gmail.com>2025-04-19 18:23:06 +0300
committerkartofen <mladenovnasko0@gmail.com>2025-04-19 18:23:06 +0300
commitcb71851d51bf4af8e13c3917b361b2aac81d6733 (patch)
tree441a8c60490e8a48a87b2f4eb86eb4047315f52e /test.c
parent81b8e14fd785141193c62bcd0023815b2a6cb810 (diff)
arrays and maps added
Diffstat (limited to 'test.c')
-rw-r--r--test.c69
1 files changed, 60 insertions, 9 deletions
diff --git a/test.c b/test.c
index 6b405dd..8ed77a5 100644
--- a/test.c
+++ b/test.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
-#include <unistd.h>
#include <sys/sendfile.h>
#include "msgpack.h"
@@ -46,9 +45,9 @@ static FILE *logfp;
#define logbuf(...) OVERLOAD_MACRO_4(__VA_ARGS__, _logbuf4, _logbuf3, _, _) (__VA_ARGS__)
#define logbytes(...) logbuf(__VA_ARGS__, "0x%hhx")
-// copy source fp from its file position indicator
-// to dest fp using sendfile (logfprintf is for that)
-static int fsendfile(FILE *dest, FILE *source)
+// sendfile for file pointers
+// when count is -1, copy to the end
+static ssize_t fsendfile(FILE *dest, FILE *source, long *offset, ssize_t count)
{
fflush(dest);
fflush(source);
@@ -56,12 +55,15 @@ static int fsendfile(FILE *dest, FILE *source)
long cur = ftell(source);
fseek(source, 0, SEEK_END);
long end = ftell(source);
+
+ if(!offset) offset = &cur;
+ if(count < 0)
+ count = end - *offset;
- int fd = fileno(source);
- return sendfile(fileno(dest), fd, &cur, end - cur);
+ fseek(source, count + *offset, SEEK_SET);
+ return sendfile(fileno(dest), fileno(source), offset, count);
}
-
static int passed_test_counter;
static int failed_test_counter;
static int failed_asserts;
@@ -72,7 +74,7 @@ static int failed_asserts;
call; \
assert("Failed Test Condition", \
prev_failed_asserts == failed_asserts, \
- body); \
+ body; logprintf("\n")); \
} while(0)
#define TEST(test) \
@@ -86,7 +88,7 @@ static int failed_asserts;
printf("[PASSED] "#test"\n"); \
passed_test_counter++; \
} \
- fsendfile(stdout, logfp) \
+ fsendfile(stdout, logfp, NULL, -1) \
? printf("\n") : 0; \
} while(0)
@@ -95,6 +97,8 @@ int test_bool();
int test_int();
int test_float();
int test_raw();
+int test_array();
+int test_map();
int test_compound();
int main(void)
@@ -106,6 +110,8 @@ int main(void)
TEST(test_int());
TEST(test_float());
TEST(test_raw());
+ TEST(test_array());
+ TEST(test_map());
TEST(test_compound());
printf("------------------------------\n");
@@ -334,6 +340,51 @@ int test_raw()
return failed_asserts;
}
+#define make_arrayormap_1(stype, btype) \
+ void stype##_1(uint8_t *bin, size_t size, size_t value) \
+ { \
+ uint8_t b[64] = {0}; \
+ size_t v = {0}; \
+ \
+ MSGPACK_CHECK2(msgpack_read_##stype(&msgpack_init(bin, size, NULL), &v), \
+ (t, e, a), \
+ BODY_SUCCESS_1(t, btype), \
+ BODY_FAILED_1(t, e, a)); \
+ \
+ assert(value == v, logvar(v, "%zu")); \
+ \
+ MSGPACK_CHECK2(msgpack_write_##stype(&msgpack_init(b, sizeof(b), NULL), &value), \
+ (t, e, a), \
+ BODY_SUCCESS_1(t, btype), \
+ BODY_FAILED_1(t, e, a)); \
+ \
+ assert(memcmp(b, bin, size) == 0, logbytes(b, size)); \
+ }
+
+make_arrayormap_1(array, MSGPACK_ARRAY)
+
+int test_array()
+{
+ failed_asserts = 0;
+
+ check_condition(array_1("\x95", 1, 5),);
+ check_condition(array_1("\xdc\x00\x11", 3, 17),);
+
+ return failed_asserts;
+}
+
+make_arrayormap_1(map, MSGPACK_MAP)
+
+int test_map()
+{
+ failed_asserts = 0;
+
+ check_condition(map_1("\x85", 1, 5),);
+ check_condition(map_1("\xde\x00\x11", 3, 17),);
+
+ return failed_asserts;
+}
+
void compound_1()
{
uint8_t bin[] = {0x0A, 0xCF, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x01};