From: James Prestwood <prestwoj at gmail.com>
To: iwd at lists.01.org
Subject: [PATCH v2 9/9] unit: add test for nested arrays
Date: Thu, 06 Jan 2022 11:50:07 -0800 [thread overview]
Message-ID: <20220106195007.528618-9-prestwoj@gmail.com> (raw)
In-Reply-To: 20220106195007.528618-1-prestwoj@gmail.com
[-- Attachment #1: Type: text/plain, Size: 1746 bytes --]
---
unit/test-json.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/unit/test-json.c b/unit/test-json.c
index 52e3f675..3d7e690b 100644
--- a/unit/test-json.c
+++ b/unit/test-json.c
@@ -424,6 +424,51 @@ static void test_json_arrays(const void *data)
json_contents_free(c);
}
+static void test_json_nested_arrays(const void *data)
+{
+ char json[] = "{\"array\":[[], {}, [1, 2], {\"key\":\"value\"}]}";
+ int count = 0;
+ struct json_iter iter;
+ struct json_iter array;
+ struct json_iter inner;
+ struct json_contents *c = json_contents_new(json, strlen(json));
+
+ json_iter_init(&iter, c);
+ assert(json_iter_parse(&iter,
+ JSON_MANDATORY("array", JSON_ARRAY, &array),
+ JSON_UNDEFINED));
+
+ while (json_iter_next(&array)) {
+ int count2 = 0;
+
+ assert(json_iter_get_container(&array, &inner));
+
+ while (json_iter_next(&inner))
+ count2++;
+
+ /*
+ * TODO: add checks for object iteration. Currently these will
+ * just count all tokens in the object.
+ */
+ switch (count) {
+ case 0:
+ assert(count2 == 0);
+ break;
+ case 2:
+ assert(count2 == 2);
+ break;
+ default:
+ break;
+ }
+
+ count++;
+ }
+
+ assert(count == 4);
+
+ json_contents_free(c);
+}
+
int main(int argc, char *argv[])
{
l_test_init(&argc, &argv);
@@ -436,6 +481,7 @@ int main(int argc, char *argv[])
l_test_add("json larger object", test_json_larger_object, NULL);
l_test_add("json test primitives", test_json_primitives, NULL);
l_test_add("json test arrays", test_json_arrays, NULL);
+ l_test_add("json test nested arrays", test_json_nested_arrays, NULL);
return l_test_run();
}
--
2.31.1
reply other threads:[~2022-01-06 19:50 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220106195007.528618-9-prestwoj@gmail.com \
--to=iwd@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox