1- #include <stdio.h>
2- #include <stdlib.h>
3- #include <string.h>
1+ #include <munit.h>
42
53#include <tinypds.h>
64#include <tinypds_dom.h>
75
8- #include "test.h"
9-
106static const char * pds_string =
117"PDS_VERSION_ID = PDS3\r\n"
128"OBJECT = object_0\r\n"
@@ -45,104 +41,122 @@ static const char *pds_string =
4541"ATTRIBUTE_8 = 14\r\n"
4642"END\r\n" ;
4743
48- PDS_item * item = NULL ;
44+ static void * pds_dom_setup (const MunitParameter params [], void * user_data ) {
45+ (void )params ;
46+ (void )user_data ;
47+ return malloc (sizeof (PDS_item * * ));
48+ }
4949
50- void cleanup ()
51- {
52- if (item )
53- {
54- PDS_DOM_delete (item );
55- }
50+ static void pds_dom_tear_down (void * fixture ) {
51+ if (NULL == fixture ) {
52+ return ;
53+ }
54+ PDS_item * * item = (PDS_item * * )fixture ;
55+ if (* item ) {
56+ PDS_DOM_delete (* item );
57+ }
58+ free (item );
5659}
5760
58- int main ()
59- {
60- PDS_item * found ;
61- PDS_scalar scalar ;
62- PDS_error_description err ;
63- int ret ;
64-
65- atexit (cleanup );
66-
67- ret = PDS_DOM_parse (pds_string , strlen (pds_string ), & item , & err );
68- if (!ret )
69- {
70- fprintf (stderr , "parse error line %d:%d: %s\n" , err .number , err .position , err .msg );
71- return EXIT_FAILURE ;
72- }
73-
74- found = PDS_DOM_find ("ATTRIBUTE_0" , item , PDS_ONLY_SIBLINGS , 0 );
75- check (NULL != found );
76- check (PDS_DOM_is_attribute (found ));
77- check (PDS_SINGLE == PDS_DOM_scalar_typeof (found ));
78-
79- ret = PDS_DOM_scalar_get (found , & scalar );
80- check (ret );
81- check (PDS_INTEGER_VALUE == scalar .type );
82- check (13 == scalar .integer .value );
83-
84- found = PDS_DOM_find ("ATTRIBUTE_8" , found , PDS_ONLY_SIBLINGS , 0 );
85- check (NULL != found );
86- check (PDS_DOM_is_attribute (found ));
87- check (PDS_SINGLE == PDS_DOM_scalar_typeof (found ));
88-
89- ret = PDS_DOM_scalar_get (found , & scalar );
90- check (ret );
91- check (PDS_INTEGER_VALUE == scalar .type );
92- check (14 == scalar .integer .value );
93-
94- found = PDS_DOM_find ("ATTRIBUTE_1" , item , PDS_ONLY_SIBLINGS , 1 );
95- check (NULL == found );
96-
97- found = PDS_DOM_find ("PDS_VERSION_ID" , item , PDS_ONLY_SIBLINGS , 1 );
98- check (NULL != found );
99-
100- found = PDS_DOM_find ("ATTRIBUTE_8" , item , PDS_ONLY_CHILDREN , 0 );
101- check (NULL == found );
102-
103- found = PDS_DOM_find ("object_0" , item , PDS_ONLY_SIBLINGS , 0 );
104- check (NULL != found );
105- check (PDS_DOM_is_object (found ));
106-
107- check (NULL == PDS_DOM_find ("ATTRIBUTE_8" , found , PDS_ONLY_CHILDREN , 0 ));
108-
109- found = PDS_DOM_find ("object_1" , found , PDS_ONLY_CHILDREN , 0 );
110- check (NULL != found );
111- check (PDS_DOM_is_object (found ));
112-
113- found = PDS_DOM_find ("ATTRIBUTE_8" , found , PDS_CHILDREN_RECURSIVE , 0 );
114- check (NULL != found );
115- check (PDS_DOM_is_attribute (found ));
116- check (PDS_SINGLE == PDS_DOM_scalar_typeof (found ));
117-
118- ret = PDS_DOM_scalar_get (found , & scalar );
119- check (ret );
120- check (PDS_INTEGER_VALUE == scalar .type );
121- check (8 == scalar .integer .value );
122-
123- found = PDS_DOM_find ("ATTRIBUTE_E" , item , PDS_SIBLINGS_RECURSIVE , 1 );
124- check (NULL != found );
125- check (PDS_DOM_is_attribute (found ));
126- check (PDS_SINGLE == PDS_DOM_scalar_typeof (found ));
127-
128- ret = PDS_DOM_scalar_get (found , & scalar );
129- check (ret );
130- check (PDS_INTEGER_VALUE == scalar .type );
131- check (16 == scalar .integer .value );
132-
133- found = PDS_DOM_find ("ATTRIBUTE_2" , item , PDS_SIBLINGS_RECURSIVE , 0 );
134- check (NULL != found );
135- check (PDS_DOM_is_attribute (found ));
136- check (PDS_SINGLE == PDS_DOM_scalar_typeof (found ));
137-
138- ret = PDS_DOM_scalar_get (found , & scalar );
139- check (ret );
140- check (PDS_INTEGER_VALUE == scalar .type );
141- check (2 == scalar .integer .value );
142-
143- found = PDS_DOM_find ("object_3" , found , PDS_SIBLINGS_RECURSIVE , 0 );
144- check (NULL != found );
145- check (PDS_DOM_is_object (found ));
146-
147- return EXIT_SUCCESS ;
61+ MunitResult pds_dom_find_test (const MunitParameter params [], void * fixture ) {
62+ (void )params ;
63+ PDS_item * * item = (PDS_item * * )fixture ;
64+ PDS_item * found ;
65+ PDS_scalar scalar ;
66+ PDS_error_description err ;
67+ int ret ;
68+
69+ ret = PDS_DOM_parse (pds_string , strlen (pds_string ), item , & err );
70+ if (!ret ) {
71+ munit_errorf ("parse error line %d:%d: %s\n" , err .number , err .position , err .msg );
72+ return MUNIT_FAIL ;
73+ }
74+
75+ found = PDS_DOM_find ("ATTRIBUTE_0" , * item , PDS_ONLY_SIBLINGS , 0 );
76+ munit_assert_ptr_not_equal (NULL , found );
77+ munit_assert_true (PDS_DOM_is_attribute (found ));
78+ munit_assert_int (PDS_SINGLE , = = , PDS_DOM_scalar_typeof (found ));
79+
80+ ret = PDS_DOM_scalar_get (found , & scalar );
81+ munit_assert_true (ret );
82+ munit_assert_int (PDS_INTEGER_VALUE , = = , scalar .type );
83+ munit_assert_int (13 , = = , scalar .integer .value );
84+
85+ found = PDS_DOM_find ("ATTRIBUTE_8" , found , PDS_ONLY_SIBLINGS , 0 );
86+ munit_assert_ptr_not_equal (NULL , found );
87+ munit_assert_true (PDS_DOM_is_attribute (found ));
88+ munit_assert_int (PDS_SINGLE , = = , PDS_DOM_scalar_typeof (found ));
89+
90+ ret = PDS_DOM_scalar_get (found , & scalar );
91+ munit_assert_true (ret );
92+ munit_assert_int (PDS_INTEGER_VALUE , = = , scalar .type );
93+ munit_assert_int (14 , = = , scalar .integer .value );
94+
95+ found = PDS_DOM_find ("ATTRIBUTE_1" , * item , PDS_ONLY_SIBLINGS , 1 );
96+ munit_assert_ptr_equal (NULL , found );
97+
98+ found = PDS_DOM_find ("PDS_VERSION_ID" , * item , PDS_ONLY_SIBLINGS , 1 );
99+ munit_assert_ptr_not_equal (NULL , found );
100+
101+ found = PDS_DOM_find ("ATTRIBUTE_8" , * item , PDS_ONLY_CHILDREN , 0 );
102+ munit_assert_ptr_equal (NULL , found );
103+
104+ found = PDS_DOM_find ("object_0" , * item , PDS_ONLY_SIBLINGS , 0 );
105+ munit_assert_ptr_not_equal (NULL , found );
106+ munit_assert_true (PDS_DOM_is_object (found ));
107+
108+ munit_assert_ptr_equal (NULL , PDS_DOM_find ("ATTRIBUTE_8" , found , PDS_ONLY_CHILDREN , 0 ));
109+
110+ found = PDS_DOM_find ("object_1" , found , PDS_ONLY_CHILDREN , 0 );
111+ munit_assert_ptr_not_equal (NULL , found );
112+ munit_assert_true (PDS_DOM_is_object (found ));
113+
114+ found = PDS_DOM_find ("ATTRIBUTE_8" , found , PDS_CHILDREN_RECURSIVE , 0 );
115+ munit_assert_ptr_not_equal (NULL , found );
116+ munit_assert_true (PDS_DOM_is_attribute (found ));
117+ munit_assert_int (PDS_SINGLE , = = , PDS_DOM_scalar_typeof (found ));
118+
119+ ret = PDS_DOM_scalar_get (found , & scalar );
120+ munit_assert_true (ret );
121+ munit_assert_int (PDS_INTEGER_VALUE , = = , scalar .type );
122+ munit_assert_int (8 , = = , scalar .integer .value );
123+
124+ found = PDS_DOM_find ("ATTRIBUTE_E" , * item , PDS_SIBLINGS_RECURSIVE , 1 );
125+ munit_assert_ptr_not_equal (NULL , found );
126+ munit_assert_true (PDS_DOM_is_attribute (found ));
127+ munit_assert_int (PDS_SINGLE , = = , PDS_DOM_scalar_typeof (found ));
128+
129+ ret = PDS_DOM_scalar_get (found , & scalar );
130+ munit_assert_true (ret );
131+ munit_assert_int (PDS_INTEGER_VALUE , = = , scalar .type );
132+ munit_assert_int (16 , = = , scalar .integer .value );
133+
134+ found = PDS_DOM_find ("ATTRIBUTE_2" , * item , PDS_SIBLINGS_RECURSIVE , 0 );
135+ munit_assert_ptr_not_equal (NULL , found );
136+ munit_assert_true (PDS_DOM_is_attribute (found ));
137+ munit_assert_int (PDS_SINGLE , = = , PDS_DOM_scalar_typeof (found ));
138+
139+ ret = PDS_DOM_scalar_get (found , & scalar );
140+ munit_assert_true (ret );
141+ munit_assert_int (PDS_INTEGER_VALUE , = = , scalar .type );
142+ munit_assert_int (2 , = = , scalar .integer .value );
143+
144+ found = PDS_DOM_find ("object_3" , found , PDS_SIBLINGS_RECURSIVE , 0 );
145+ munit_assert_ptr_not_equal (NULL , found );
146+ munit_assert_true (PDS_DOM_is_object (found ));
147+
148+ return MUNIT_OK ;
148149}
150+
151+ static MunitTest pds_dom_tests [] = {
152+ { "find" , pds_dom_find_test , pds_dom_setup , pds_dom_tear_down , MUNIT_TEST_OPTION_NONE , NULL },
153+ { NULL , NULL , NULL , NULL , MUNIT_TEST_OPTION_NONE , NULL }
154+ };
155+
156+ static const MunitSuite pds_dom_suite = {
157+ "PDS DOM test suite" , pds_dom_tests , NULL , 1 , MUNIT_SUITE_OPTION_NONE
158+ };
159+
160+ int main (int argc , char * const * argv ) {
161+ return munit_suite_main (& pds_dom_suite , NULL , argc , argv );
162+ }
0 commit comments