1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include "tpl.h"
5 
6 struct str_holder {
7     char str[10];
8 };
9 
10 
11 /* the real purpose of this test is to be run under dbx "check -all"
12  * mode to ensure the level-0 data are freed when replaced (i.e. the
13  * level 0 nodes are packed more than once, this is an edge case but
14  * the required behavior is to free the previously-packed data when
15  * packing the new replacement data). Do the test for s,S,and B types.
16  */
main()17 int main() {
18     tpl_node *tn;
19     char *s;
20     struct str_holder sh;
21     tpl_bin bin;
22 
23     /* test a replacement pack (s type) of the level 0 node */
24     tn = tpl_map("s", &s);
25     s = "alpha";
26     tpl_pack(tn,0);  /* copies alpha */
27     s = "beta";
28     tpl_pack(tn,0);  /* should free alpha, copy beta */
29     tpl_dump(tn,TPL_FILE,"/tmp/test64_0.tpl");
30     tpl_free(tn);
31 
32     /* print out dumped tpl */
33     s = "";
34     tn = tpl_map("s", &s);
35     tpl_load(tn,TPL_FILE,"/tmp/test64_0.tpl");
36     tpl_unpack(tn,0);
37     printf("s is %s\n", s);
38     free(s);
39     tpl_free(tn);
40 
41     /* test replacement pack (S type) of the level 0 node */
42     tn = tpl_map("c#", sh.str, 10);
43     strncpy(sh.str, "gamma", 10);
44     tpl_pack(tn,0);  /* copies gamma */
45     strncpy(sh.str, "delta", 10);
46     tpl_pack(tn,0);  /* should free gamma, copy delta */
47     tpl_dump(tn,TPL_FILE,"/tmp/test64_1.tpl");
48     tpl_free(tn);
49 
50     /* print out dumped tpl */
51     sh.str[0] = '\0';
52     tn = tpl_map("c#", sh.str, 10);
53     tpl_load(tn,TPL_FILE,"/tmp/test64_1.tpl");
54     tpl_unpack(tn,0);
55     printf("sh.str is %s\n", sh.str);
56     tpl_free(tn);
57 
58     /* test replacement pack (B type) of the level 0 node */
59     tn = tpl_map("B", &bin);
60     bin.addr = "epsilon";
61     bin.sz = strlen("epsilon")+1;
62     tpl_pack(tn,0);  /* copies epsilon */
63     bin.addr = "zeta";
64     bin.sz = strlen("zeta")+1;
65     tpl_pack(tn,0);  /* should free epsilon, copy zeta */
66     tpl_dump(tn,TPL_FILE,"/tmp/test64_2.tpl");
67     tpl_free(tn);
68 
69     /* print out dumped tpl */
70     bin.addr = "";
71     bin.sz = 1;
72     tn = tpl_map("B", &bin);
73     tpl_load(tn,TPL_FILE,"/tmp/test64_2.tpl");
74     tpl_unpack(tn,0);
75     printf("bin.addr is %s, size %d\n", (char*)(bin.addr), bin.sz);
76     free(bin.addr);
77     tpl_free(tn);
78     return(0);
79 
80 }
81