diff --git a/libc3/array.c b/libc3/array.c
index 3b1c0fd..cbb4720 100644
--- a/libc3/array.c
+++ b/libc3/array.c
@@ -15,6 +15,8 @@
#include <string.h>
#include <err.h>
#include "array.h"
+#include "buf.h"
+#include "buf_parse.h"
#include "tag.h"
#include "type.h"
@@ -55,6 +57,19 @@ s_array * array_copy (const s_array *src, s_array *dest)
return dest;
}
+void * array_data (const s_array *a, const uw *address)
+{
+ uw i = 0;
+ uw offset = 0;
+ assert(a);
+ assert(address);
+ while (i < a->dimension) {
+ offset += address[i] * a->dimensions[i].item_size;
+ i++;
+ }
+ return (s8 *) a->data + offset;
+}
+
s_array * array_init (s_array *a, e_tag_type type, uw dimension,
const uw *dimensions)
{
@@ -93,14 +108,18 @@ s_array * array_init (s_array *a, e_tag_type type, uw dimension,
return a;
}
-void * array_data (const s_array *a, const uw *address)
+s_array * array_init_1 (s_array *a, s8 *p)
{
- uw i = 0;
- uw offset = 0;
- assert(a);
- assert(address);
- while (i < a->dimension) {
- offset += address[i] * a->dimensions[i].item_size;
+ s_buf buf;
+ sw r;
+ s_array tmp;
+ buf_init_1(&buf, p);
+ if ((r = buf_parse_array(&buf, &tmp)) != (sw) strlen(p)) {
+ warnx("buf_parse_array(%s) => %ld != %ld", p, r, strlen(p));
+ buf_clean(&buf);
+ return NULL;
}
- return (s8 *) a->data + offset;
+ *a = tmp;
+ buf_clean(&buf);
+ return a;
}
diff --git a/libc3/array.h b/libc3/array.h
index bb6c3e1..a480103 100644
--- a/libc3/array.h
+++ b/libc3/array.h
@@ -19,6 +19,7 @@ void array_clean (s_array *a);
s_array * array_copy (const s_array *src, s_array *dest);
s_array * array_init (s_array *a, e_tag_type type, uw dimension,
const uw *dimensions);
+s_array * array_init_1 (s_array *a, s8 *p);
void * array_data (const s_array *a, const uw *address);
#endif /* ARRAY_H */
diff --git a/test/array_test.c b/test/array_test.c
index 03782d7..ebef86d 100644
--- a/test/array_test.c
+++ b/test/array_test.c
@@ -21,14 +21,35 @@
array_clean(&a); \
} while(0)
+#define ARRAY_TEST_INIT_1_CLEAN(test) \
+ do { \
+ s_array a; \
+ TEST_EQ(array_init_1(&a, (test)), &a); \
+ array_clean(&a); \
+ } while(0)
+
void array_test ();
+TEST_CASE_PROTOTYPE(array_data);
TEST_CASE_PROTOTYPE(array_init_clean);
+TEST_CASE_PROTOTYPE(array_init_1_clean);
void array_test ()
{
TEST_CASE_RUN(array_init_clean);
+ TEST_CASE_RUN(array_init_1_clean);
+ TEST_CASE_RUN(array_data);
}
+TEST_CASE(array_data)
+{
+ s_array a;
+ TEST_EQ(array_init_1(&a, "(u8) {1, 2, 3}"), &a);
+ TEST_EQ(* (u8 *) array_data(&a, (uw []) {0}), 1);
+ TEST_EQ(* (u8 *) array_data(&a, (uw []) {1}), 2);
+ TEST_EQ(* (u8 *) array_data(&a, (uw []) {2}), 3);
+}
+TEST_CASE_END(array_data)
+
TEST_CASE(array_init_clean)
{
ARRAY_TEST_INIT_CLEAN(TAG_U8, 1, (uw []) {1});
@@ -42,3 +63,15 @@ TEST_CASE(array_init_clean)
ARRAY_TEST_INIT_CLEAN(TAG_U8, 3, ((uw []) {100, 100, 100}));
}
TEST_CASE_END(array_init_clean)
+
+TEST_CASE(array_init_1_clean)
+{
+ ARRAY_TEST_INIT_1_CLEAN("(u8) {0}");
+ ARRAY_TEST_INIT_1_CLEAN("(u8) {0, 0}");
+ ARRAY_TEST_INIT_1_CLEAN("(u8) {0, 0, 0}");
+ ARRAY_TEST_INIT_1_CLEAN("(u8) {{0}, {0}}");
+ ARRAY_TEST_INIT_1_CLEAN("(u8) {{0, 0}, {0, 0}}");
+ ARRAY_TEST_INIT_1_CLEAN("(u8) {{0, 0}, {0, 0}, {0, 0}}");
+ ARRAY_TEST_INIT_1_CLEAN("(u8) {{{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}, {{0, 0}, {0, 0}}}");
+}
+TEST_CASE_END(array_init_1_clean)