From a0557ba999ee43fb4d849040d31c087f59875ac2 Mon Sep 17 00:00:00 2001 From: DenisBiryukov91 <155981813+DenisBiryukov91@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:01:20 +0200 Subject: [PATCH] add encoding tests (#502) * add encoding tests * format --- tests/z_api_encoding_test.c | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/z_api_encoding_test.c diff --git a/tests/z_api_encoding_test.c b/tests/z_api_encoding_test.c new file mode 100644 index 000000000..5aa385646 --- /dev/null +++ b/tests/z_api_encoding_test.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include + +#include "zenoh.h" + +#undef NDEBUG +#include + +void test_null_encoding(void) { + z_owned_encoding_t e; + z_encoding_null(&e); + assert(!z_encoding_check(&e)); + z_encoding_drop(z_move(e)); +} + +void test_encoding_without_id(void) { + z_owned_encoding_t e1; + z_encoding_from_str(&e1, "my_encoding"); + assert(z_encoding_check(&e1)); + z_owned_string_t s; + z_encoding_to_string(z_encoding_loan(&e1), &s); + assert(strncmp("zenoh/bytes;my_encoding", z_string_data(z_string_loan(&s)), z_string_len(z_string_loan(&s))) == 0); + z_encoding_drop(z_move(e1)); + z_string_drop(z_move(s)); + + z_owned_encoding_t e2; + z_encoding_from_substr(&e2, "my_encoding", 4); + assert(z_encoding_check(&e2)); + + z_encoding_to_string(z_encoding_loan(&e2), &s); + assert(strncmp("zenoh/bytes;my_e", z_string_data(z_string_loan(&s)), z_string_len(z_string_loan(&s))) == 0); + z_encoding_drop(z_move(e2)); + z_string_drop(z_move(s)); +} + +void test_encoding_with_id(void) { + z_owned_encoding_t e1; + z_encoding_from_str(&e1, "zenoh/string;utf8"); + assert(z_encoding_check(&e1)); + z_owned_string_t s; + z_encoding_to_string(z_encoding_loan(&e1), &s); + assert(strncmp("zenoh/string;utf8", z_string_data(z_string_loan(&s)), z_string_len(z_string_loan(&s))) == 0); + z_encoding_drop(z_move(e1)); + z_string_drop(z_move(s)); + + z_owned_encoding_t e2; + z_encoding_from_substr(&e2, "zenoh/string;utf8", 15); + assert(z_encoding_check(&e2)); + + z_encoding_to_string(z_encoding_loan(&e2), &s); + assert(strncmp("zenoh/string;utf8", z_string_data(z_string_loan(&s)), z_string_len(z_string_loan(&s))) == 0); + z_encoding_drop(z_move(e2)); + z_string_drop(z_move(s)); + + z_owned_encoding_t e3; + z_encoding_from_str(&e3, "custom_id;custom_schema"); + assert(z_encoding_check(&e3)); + + z_encoding_to_string(z_encoding_loan(&e3), &s); + assert(strncmp("zenoh/bytes;custom_id;custom_schema", z_string_data(z_string_loan(&s)), + z_string_len(z_string_loan(&s))) == 0); + z_encoding_drop(z_move(e3)); + z_string_drop(z_move(s)); + + z_owned_encoding_t e4; + z_encoding_from_substr(&e4, "custom_id;custom_schema", 16); + assert(z_encoding_check(&e4)); + + z_encoding_to_string(z_encoding_loan(&e4), &s); + assert(strncmp("zenoh/bytes;custom_id;custom", z_string_data(z_string_loan(&s)), z_string_len(z_string_loan(&s))) == + 0); + z_encoding_drop(z_move(e4)); + z_string_drop(z_move(s)); +} + +int main(int argc, char **argv) { + test_null_encoding(); + test_encoding_without_id(); + test_encoding_with_id(); +}