Skip to content

Commit

Permalink
Add methods for z_owned_strings_array_t creation
Browse files Browse the repository at this point in the history
z_string_array_new
z_string_array_push_by_alias
z_string_array_push_by_copy
  • Loading branch information
sashacmc committed Sep 16, 2024
1 parent 7b8a151 commit 70c3205
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
6 changes: 6 additions & 0 deletions include/zenoh-pico/api/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,19 @@ _Z_OWNED_TYPE_VALUE(_z_reply_t, reply)
*
* Operations over :c:type:`z_loaned_string_array_t` must be done using the provided functions:
*
* - :c:func:`z_string_array_new`
* - :c:func:`z_string_array_push_by_alias`
* - :c:func:`z_string_array_push_by_copy`
* - :c:func:`z_string_array_get`
* - :c:func:`z_string_array_len`
* - :c:func:`z_str_array_array_is_empty`
*/
_Z_OWNED_TYPE_VALUE(_z_string_svec_t, string_array)
_Z_VIEW_TYPE(_z_string_svec_t, string_array)

void z_string_array_new(z_owned_string_array_t *a);
size_t z_string_array_push_by_alias(z_loaned_string_array_t *a, const z_loaned_string_t *value);
size_t z_string_array_push_by_copy(z_loaned_string_array_t *a, const z_loaned_string_t *value);
const z_loaned_string_t *z_string_array_get(const z_loaned_string_array_t *a, size_t k);
size_t z_string_array_len(const z_loaned_string_array_t *a);
_Bool z_string_array_is_empty(const z_loaned_string_array_t *a);
Expand Down
21 changes: 20 additions & 1 deletion src/api/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "zenoh-pico/api/primitives.h"
#include "zenoh-pico/api/types.h"
#include "zenoh-pico/collections/slice.h"
#include "zenoh-pico/collections/string.h"
#include "zenoh-pico/config.h"
#include "zenoh-pico/net/config.h"
#include "zenoh-pico/net/filtering.h"
Expand Down Expand Up @@ -58,6 +59,25 @@ int8_t z_view_string_from_substr(z_view_string_t *str, const char *value, size_t
return _Z_RES_OK;
}

_z_string_svec_t _z_string_array_null(void) { return _z_string_svec_make(0); }

void z_string_array_new(z_owned_string_array_t *a) { a->_val = _z_string_array_null(); }

size_t z_string_array_push_by_alias(z_loaned_string_array_t *a, const z_loaned_string_t *value) {
_z_string_t str = _z_string_alias(value);
_z_string_svec_append(a, &str);

return _z_string_svec_len(a);
}

size_t z_string_array_push_by_copy(z_loaned_string_array_t *a, const z_loaned_string_t *value) {
_z_string_t str;
_z_string_copy(&str, value);
_z_string_svec_append(a, &str);

return _z_string_svec_len(a);
}

const z_loaned_string_t *z_string_array_get(const z_loaned_string_array_t *a, size_t k) {
return _z_string_svec_get(a, k);
}
Expand Down Expand Up @@ -799,7 +819,6 @@ int8_t _z_string_array_copy(_z_string_svec_t *dst, const _z_string_svec_t *src)
_z_string_svec_copy(dst, src);
return dst->_len == src->_len ? _Z_RES_OK : _Z_ERR_SYSTEM_OUT_OF_MEMORY;
}
_z_string_svec_t _z_string_array_null(void) { return _z_string_svec_make(0); }
_Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_string_svec_t, string_array, _z_string_array_check, _z_string_array_null,
_z_string_array_copy, _z_string_svec_clear)
_Z_OWNED_FUNCTIONS_VALUE_IMPL(_z_slice_t, slice, _z_slice_check, _z_slice_empty, _z_slice_copy, _z_slice_clear)
Expand Down
74 changes: 73 additions & 1 deletion tests/z_data_struct_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
#include <stdio.h>
#include <stdlib.h>

#include "zenoh-pico/api/primitives.h"
#include "zenoh-pico/api/types.h"
#include "zenoh-pico/collections/string.h"
#include "zenoh-pico/protocol/core.h"
#include "zenoh-pico/system/platform.h"
#include "zenoh-pico/transport/transport.h"

#undef NDEBUG
Expand Down Expand Up @@ -167,10 +168,81 @@ void z_slice_custom_delete_test(void) {
assert(counter == 2);
}

void z_string_array_test(void) {
// create new array
z_owned_string_array_t a;
z_string_array_new(&a);

char *s1 = strdup("string1");
char *s2 = strdup("string2");
char *s3 = strdup("string3");
char *s4 = strdup("string4");

// add by copy
z_view_string_t vs1;
z_view_string_from_str(&vs1, s1);
assert(z_string_array_push_by_copy(z_string_array_loan_mut(&a), z_view_string_loan(&vs1)) == 1);

z_view_string_t vs2;
z_view_string_from_str(&vs2, s2);
assert(z_string_array_push_by_copy(z_string_array_loan_mut(&a), z_view_string_loan(&vs2)) == 2);

// add by alias
z_view_string_t vs3;
z_view_string_from_str(&vs3, s3);
assert(z_string_array_push_by_alias(z_string_array_loan_mut(&a), z_view_string_loan(&vs3)) == 3);

z_view_string_t vs4;
z_view_string_from_str(&vs4, s4);
assert(z_string_array_push_by_alias(z_string_array_loan_mut(&a), z_view_string_loan(&vs4)) == 4);

// check values
const z_loaned_string_t *ls1 = z_string_array_get(z_string_array_loan(&a), 0);
assert(strncmp(z_string_data(ls1), s1, z_string_len(ls1)) == 0);

const z_loaned_string_t *ls2 = z_string_array_get(z_string_array_loan(&a), 1);
assert(strncmp(z_string_data(ls2), s2, z_string_len(ls2)) == 0);

const z_loaned_string_t *ls3 = z_string_array_get(z_string_array_loan(&a), 2);
assert(strncmp(z_string_data(ls3), s3, z_string_len(ls3)) == 0);

const z_loaned_string_t *ls4 = z_string_array_get(z_string_array_loan(&a), 3);
assert(strncmp(z_string_data(ls4), s4, z_string_len(ls4)) == 0);

// modify original strings values
s1[0] = 'X';
s2[0] = 'X';
s3[0] = 'X';
s4[0] = 'X';

// values passed by copy should NOT be changed
ls1 = z_string_array_get(z_string_array_loan(&a), 0);
assert(strncmp(z_string_data(ls1), "string1", z_string_len(ls1)) == 0);

ls2 = z_string_array_get(z_string_array_loan(&a), 1);
assert(strncmp(z_string_data(ls2), "string2", z_string_len(ls2)) == 0);

// values passed by alias should be changed
ls3 = z_string_array_get(z_string_array_loan(&a), 2);
assert(strncmp(z_string_data(ls3), s3, z_string_len(ls3)) == 0);

ls4 = z_string_array_get(z_string_array_loan(&a), 3);
assert(strncmp(z_string_data(ls4), s4, z_string_len(ls4)) == 0);

// cleanup
free(s1);
free(s2);
free(s3);
free(s4);

z_string_array_drop(z_string_array_move(&a));
}

int main(void) {
entry_list_test();
str_vec_list_intmap_test();
z_slice_custom_delete_test();
z_string_array_test();

return 0;
}

0 comments on commit 70c3205

Please sign in to comment.