File: test64.c

package info (click to toggle)
libtpl 1.6.1-1.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 2,028 kB
  • sloc: ansic: 5,669; perl: 1,062; makefile: 101; cpp: 32; sh: 18
file content (80 lines) | stat: -rw-r--r-- 2,245 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "tpl.h"

struct str_holder {
    char str[10];
};


/* the real purpose of this test is to be run under dbx "check -all" 
 * mode to ensure the level-0 data are freed when replaced (i.e. the
 * level 0 nodes are packed more than once, this is an edge case but
 * the required behavior is to free the previously-packed data when
 * packing the new replacement data). Do the test for s,S,and B types.
 */
int main() {
    tpl_node *tn;
    char *s;
    struct str_holder sh;
    tpl_bin bin;

    /* test a replacement pack (s type) of the level 0 node */
    tn = tpl_map("s", &s);
    s = "alpha";
    tpl_pack(tn,0);  /* copies alpha */
    s = "beta"; 
    tpl_pack(tn,0);  /* should free alpha, copy beta */
    tpl_dump(tn,TPL_FILE,"/tmp/test64_0.tpl");
    tpl_free(tn);

    /* print out dumped tpl */
    s = "";
    tn = tpl_map("s", &s);
    tpl_load(tn,TPL_FILE,"/tmp/test64_0.tpl");
    tpl_unpack(tn,0);
    printf("s is %s\n", s);
    free(s);
    tpl_free(tn);

    /* test replacement pack (S type) of the level 0 node */
    tn = tpl_map("c#", sh.str, 10);
    strncpy(sh.str, "gamma", 10);
    tpl_pack(tn,0);  /* copies gamma */
    strncpy(sh.str, "delta", 10);
    tpl_pack(tn,0);  /* should free gamma, copy delta */
    tpl_dump(tn,TPL_FILE,"/tmp/test64_1.tpl");
    tpl_free(tn);

    /* print out dumped tpl */
    sh.str[0] = '\0';
    tn = tpl_map("c#", sh.str, 10);
    tpl_load(tn,TPL_FILE,"/tmp/test64_1.tpl");
    tpl_unpack(tn,0);
    printf("sh.str is %s\n", sh.str);
    tpl_free(tn);

    /* test replacement pack (B type) of the level 0 node */
    tn = tpl_map("B", &bin);
    bin.addr = "epsilon";
    bin.sz = strlen("epsilon")+1;
    tpl_pack(tn,0);  /* copies epsilon */
    bin.addr = "zeta";
    bin.sz = strlen("zeta")+1;
    tpl_pack(tn,0);  /* should free epsilon, copy zeta */
    tpl_dump(tn,TPL_FILE,"/tmp/test64_2.tpl");
    tpl_free(tn);

    /* print out dumped tpl */
    bin.addr = "";
    bin.sz = 1;
    tn = tpl_map("B", &bin);
    tpl_load(tn,TPL_FILE,"/tmp/test64_2.tpl");
    tpl_unpack(tn,0);
    printf("bin.addr is %s, size %d\n", (char*)(bin.addr), bin.sz);
    free(bin.addr);
    tpl_free(tn);
    return(0);

}