diff mbox series

[v1,2/3] test/telemetry: add unit tests for pointer value

Message ID 604cef7dae418edfa928e63731772e51ad26d6d3.1627572033.git.gmuthukrishn@marvell.com (mailing list archive)
State Superseded
Delegated to: Thomas Monjalon
Headers show
Series common/cnxk: enable npa telemetry | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Gowrishankar Muthukrishnan July 29, 2021, 3:25 p.m. UTC
Adding tests to evaluate pointer value in array and dict.

Signed-off-by: Gowrishankar Muthukrishnan <gmuthukrishn@marvell.com>
---
 app/test/test_telemetry_data.c | 124 +++++++++++++++++++++++++++++++++
 app/test/test_telemetry_json.c |  28 ++++++--
 2 files changed, 145 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/app/test/test_telemetry_data.c b/app/test/test_telemetry_data.c
index f34d691265..59f68f135a 100644
--- a/app/test/test_telemetry_data.c
+++ b/app/test/test_telemetry_data.c
@@ -301,6 +301,126 @@  test_array_with_array_u64_values(void)
 	return TEST_OUTPUT("{\"/test\":[[0,1,2,3,4],[0,1,2,3,4]]}");
 }
 
+static int
+test_case_array_ptr(void)
+{
+	int *p, i, j, a[] = {1, 2, 3, 4, 5};
+	char exp[120];
+
+	memset(&response_data, 0, sizeof(response_data));
+	memset(exp, 0, sizeof(exp));
+	rte_tel_data_start_array(&response_data, RTE_TEL_PTR_VAL);
+
+	i = sprintf(exp, "{\"/test\":[");
+	for (j = 0; j < 5; j++) {
+		p = &a[j];
+		i += sprintf(exp + i, "\"%p\",", p);
+		rte_tel_data_add_array_ptr(&response_data, p);
+	}
+
+	sprintf(exp + i - 1, "]}");
+	return TEST_OUTPUT(exp);
+}
+
+static int
+test_case_add_dict_ptr(void)
+{
+	int *p, i, j, a[] = {1, 2, 3, 4, 5};
+	char name[8], exp[160];
+
+	memset(&response_data, 0, sizeof(response_data));
+	memset(exp, 0, sizeof(exp));
+	rte_tel_data_start_dict(&response_data);
+
+	i = sprintf(exp, "{\"/test\":{");
+	for (j = 0; j < 5; j++) {
+		p = &a[j];
+		sprintf(name, "dict_%d", j);
+		i += sprintf(exp + i, "\"%s\":\"%p\",", name, p);
+		rte_tel_data_add_dict_ptr(&response_data, name, p);
+	}
+
+	sprintf(exp + i - 1, "}}");
+	return TEST_OUTPUT(exp);
+}
+
+static int
+test_dict_with_array_ptr_values(void)
+{
+	int *p, i, j, a[] = {1, 2, 3, 4, 5};
+	char exp[256];
+
+	struct rte_tel_data *child_data = rte_tel_data_alloc();
+	rte_tel_data_start_array(child_data, RTE_TEL_PTR_VAL);
+
+	struct rte_tel_data *child_data2 = rte_tel_data_alloc();
+	rte_tel_data_start_array(child_data2, RTE_TEL_PTR_VAL);
+
+	memset(&response_data, 0, sizeof(response_data));
+	memset(exp, 0, sizeof(exp));
+	rte_tel_data_start_dict(&response_data);
+
+	i = sprintf(exp, "{\"/test\":{\"dict_0\":[");
+	for (j = 0; j < 5; j++) {
+		p = &a[j];
+		i += sprintf(exp + i, "\"%p\",", p);
+		rte_tel_data_add_array_ptr(child_data, p);
+	}
+
+	i += sprintf(exp + i - 1, "],\"dict_1\":[");
+	for (j = 5; j > 0; j--) {
+		p = &a[j - 1];
+		i += sprintf(exp + i - 1, "\"%p\",", p);
+		rte_tel_data_add_array_ptr(child_data2, p);
+	}
+
+	sprintf(exp + i - 2, "]}}");
+	rte_tel_data_add_dict_container(&response_data, "dict_0",
+								child_data, 0);
+	rte_tel_data_add_dict_container(&response_data, "dict_1",
+								child_data2, 0);
+
+	return TEST_OUTPUT(exp);
+}
+
+static int
+test_array_with_array_ptr_values(void)
+{
+	int *p, i, j, a[] = {1, 2, 3, 4, 5};
+	char exp[256];
+
+	struct rte_tel_data *child_data = rte_tel_data_alloc();
+	rte_tel_data_start_array(child_data, RTE_TEL_PTR_VAL);
+
+	struct rte_tel_data *child_data2 = rte_tel_data_alloc();
+	rte_tel_data_start_array(child_data2, RTE_TEL_PTR_VAL);
+
+	memset(&response_data, 0, sizeof(response_data));
+	memset(exp, 0, sizeof(exp));
+	rte_tel_data_start_array(&response_data, RTE_TEL_CONTAINER);
+
+	i = sprintf(exp, "{\"/test\":[[");
+	for (j = 0; j < 5; j++) {
+		p = &a[j];
+		i += sprintf(exp + i, "\"%p\",", p);
+		rte_tel_data_add_array_ptr(child_data, p);
+	}
+
+	i += sprintf(exp + i - 1, "],[");
+	for (j = 5; j > 0; j--) {
+		p = &a[j - 1];
+		i += sprintf(exp + i - 1, "\"%p\",", p);
+		rte_tel_data_add_array_ptr(child_data2, p);
+	}
+
+	sprintf(exp + i - 2, "]]}");
+
+	rte_tel_data_add_array_container(&response_data, child_data, 0);
+	rte_tel_data_add_array_container(&response_data, child_data2, 0);
+
+	return TEST_OUTPUT(exp);
+}
+
 static int
 connect_to_socket(void)
 {
@@ -350,13 +470,17 @@  test_telemetry_data(void)
 
 	test_case test_cases[] = {test_case_array_string,
 			test_case_array_int, test_case_array_u64,
+			test_case_array_ptr,
 			test_case_add_dict_int, test_case_add_dict_u64,
+			test_case_add_dict_ptr,
 			test_case_add_dict_string,
 			test_dict_with_array_int_values,
 			test_dict_with_array_u64_values,
+			test_dict_with_array_ptr_values,
 			test_dict_with_array_string_values,
 			test_array_with_array_int_values,
 			test_array_with_array_u64_values,
+			test_array_with_array_ptr_values,
 			test_array_with_array_string_values };
 
 	rte_telemetry_register_cmd(REQUEST_CMD, test_cb, "Test");
diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c
index 790181d316..3e8e60638c 100644
--- a/app/test/test_telemetry_json.c
+++ b/app/test/test_telemetry_json.c
@@ -11,18 +11,22 @@ 
 static int
 test_basic_array(void)
 {
-	const char *expected = "[\"meaning of life\",42]";
-	char buf[1024];
-	int used = 0;
+	char buf[1024], expected[80];
+	int used = 0, n = 42, *p;
 
 	printf("%s: ", __func__);
 	used = rte_tel_json_empty_array(buf, sizeof(buf), used);
 	if (used != 2 || strcmp(buf, "[]"))
 		return -1;
 
+	p = &n;
+	memset(expected, 0, sizeof(expected));
+	sprintf(expected, "[\"meaning of life\",42,\"%p\"]", p);
+
 	used = rte_tel_json_add_array_string(buf, sizeof(buf), used,
 		"meaning of life");
-	used = rte_tel_json_add_array_int(buf, sizeof(buf), used, 42);
+	used = rte_tel_json_add_array_int(buf, sizeof(buf), used, n);
+	used = rte_tel_json_add_array_ptr(buf, sizeof(buf), used, p);
 
 	printf("buf = '%s', expected = '%s'\n", buf, expected);
 	if (used != (int)strlen(expected))
@@ -33,14 +37,24 @@  test_basic_array(void)
 static int
 test_basic_obj(void)
 {
-	const char *expected = "{\"weddings\":4,\"funerals\":1}";
-	char buf[1024];
-	int used = 0;
+	char buf[1024], expected[80];
+	int used = 0, n = 42, *p;
+
+	p = &n;
+	memset(expected, 0, sizeof(expected));
+	sprintf(expected,
+			"{\"weddings\":4,\"funerals\":1,"
+			"\"address\":\"%p\",\"reset\":\"(nil)\"}", p);
 
 	used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
 		"weddings", 4);
 	used = rte_tel_json_add_obj_u64(buf, sizeof(buf), used,
 		"funerals", 1);
+	used = rte_tel_json_add_obj_ptr(buf, sizeof(buf), used,
+		"address", p);
+	p = NULL;
+	used = rte_tel_json_add_obj_ptr(buf, sizeof(buf), used,
+		"reset", p);
 
 	printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected);
 	if (used != (int)strlen(expected))