Fix bug in GListTruncate, extends templates tests
[tedtools.git] / tmpltest.c
1 /*
2  * Copyright (c) 2008 Teodor Sigaev <teodor@sigaev.ru>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer in the
12  *      documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the author nor the names of any co-contributors
14  *      may be used to endorse or promote products derived from this software
15  *      without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34
35
36 #include "tmalloc.h"
37 #include "tlog.h"
38 #include "tools.h"
39 #include "template.h"
40
41 static void
42 usage() {
43         puts(
44         "Usage:\n"
45         "memtest [-t TEMPLATENAME]\n"
46         );
47         exit(1);
48 }
49
50
51 static void
52 outfunc(char *str, int len) {
53         fputs( str, stdout ); 
54 }
55
56 extern char *optarg;
57 extern int opterr;
58
59 int
60 main(int argn, char *argv[]) {
61         MemoryContext   *base;
62         char                    *name = NULL;
63         TemplateData    template;
64         int                     i;
65
66         opentlog(TL_OPEN_STDERR,TL_DEBUG, NULL);
67         opterr=0;
68
69         while((i=getopt(argn,argv,"ht:")) != EOF) {
70                 switch(i) {
71                         case 't':
72                                 name = tstrdup(optarg);
73                                 break;
74                         case 'h':
75                         default:
76                                 usage();
77                                 break;
78                 }
79         }
80                         
81         if (!name)
82                 usage();
83
84         base = allocMemoryContext(NULL, MC_DEBUG);
85
86         printf("initTemplate: %d\n",  initTemplate(&template, base, ".", name) );
87         /* dumpTemplate(&template); */ 
88
89         setTemplateValueInt(&template, "ID", 17);
90         setTemplateValueUndefined(&template, "emptyID");
91         setTemplateValueInt(&template, "zeroid", 0);
92
93         addTemplateRow(&template, "outerLoop");
94         setTemplateValueString(&template, "outerLoop.data1", "ha1");
95         setTemplateValueUndefined(&template, "outerLoop.data2");
96
97         addTemplateRow(&template, "outerLoop");
98         setTemplateValueInt(&template, "outerLoop.data1", 10);
99         setTemplateValueString(&template, "outerLoop.data2", "WOW");
100                 addTemplateRow(&template, "outerLoop.innerLoop");
101                 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Number 1");
102                 addTemplateRow(&template, "outerLoop.innerLoop");
103                 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Number 2");
104
105         addTemplateRow(&template, "outerLoop");
106         setTemplateValueString(&template, "outerLoop.data1", "ha3");
107
108         template.printString = outfunc;
109         printTemplate( &template );
110
111         resetTemplate(&template);
112
113         setTemplateValueInt(&template, "ID", 23);
114         setTemplateValueUndefined(&template, "emptyID");
115         setTemplateValueInt(&template, "zeroid", 0);
116         addTemplateRow(&template, "outerLoop");
117         setTemplateValueString(&template, "outerLoop.data1", "ha1");
118         setTemplateValueInt(&template, "outerLoop.data1", 1234);
119         setTemplateValueString(&template, "outerLoop.data2", "FOO");
120         addTemplateRow(&template, "outerLoop.innerLoop");
121                 setTemplateValueString(&template, "outerLoop.innerLoop.camenty", "Again 1");
122
123         fputs("================================================\n", stdout);
124         printTemplate( &template );
125
126         resetTemplate(&template);
127         freeTemplate(&template);
128
129         return 0;
130 }
131
132         
133