BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file ncdvalcons_test.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <string.h>
31 #include <stdio.h>
32  
33 #include <misc/debug.h>
34 #include <ncd/NCDValCons.h>
35 #include <ncd/NCDValGenerator.h>
36  
37 static NCDStringIndex string_index;
38 static NCDValMem mem;
39 static NCDValCons cons;
40  
41 static NCDValConsVal make_string (const char *data)
42 {
43 NCDValConsVal val;
44 int error;
45 int res = NCDValCons_NewString(&cons, (const uint8_t *)data, strlen(data), &val, &error);
46 ASSERT_FORCE(res)
47 return val;
48 }
49  
50 static NCDValConsVal make_list (void)
51 {
52 NCDValConsVal val;
53 NCDValCons_NewList(&cons, &val);
54 return val;
55 }
56  
57 static NCDValConsVal make_map (void)
58 {
59 NCDValConsVal val;
60 NCDValCons_NewMap(&cons, &val);
61 return val;
62 }
63  
64 static NCDValConsVal list_prepend (NCDValConsVal list, NCDValConsVal elem)
65 {
66 int error;
67 int res = NCDValCons_ListPrepend(&cons, &list, elem, &error);
68 ASSERT_FORCE(res)
69 return list;
70 }
71  
72 static NCDValConsVal map_insert (NCDValConsVal map, NCDValConsVal key, NCDValConsVal value)
73 {
74 int error;
75 int res = NCDValCons_MapInsert(&cons, &map, key, value, &error);
76 ASSERT_FORCE(res)
77 return map;
78 }
79  
80 static NCDValRef complete (NCDValConsVal cval)
81 {
82 int error;
83 NCDValRef val;
84 int res = NCDValCons_Complete(&cons, cval, &val, &error);
85 ASSERT_FORCE(res)
86 return val;
87 }
88  
89 int main ()
90 {
91 int res;
92  
93 res = NCDStringIndex_Init(&string_index);
94 ASSERT_FORCE(res)
95  
96 NCDValMem_Init(&mem, &string_index);
97  
98 res = NCDValCons_Init(&cons, &mem);
99 ASSERT_FORCE(res)
100  
101 NCDValRef val1 = complete(list_prepend(list_prepend(list_prepend(make_list(), make_string("hello")), make_string("world")), make_list()));
102 char *str1 = NCDValGenerator_Generate(val1);
103 ASSERT_FORCE(str1)
104 ASSERT_FORCE(!strcmp(str1, "{{}, \"world\", \"hello\"}"))
105 free(str1);
106  
107 NCDValRef val2 = complete(map_insert(map_insert(map_insert(make_map(), make_list(), make_list()), make_string("A"), make_list()), make_string("B"), make_list()));
108 char *str2 = NCDValGenerator_Generate(val2);
109 ASSERT_FORCE(str2)
110 printf("%s\n", str2);
111 ASSERT_FORCE(!strcmp(str2, "[\"A\":{}, \"B\":{}, {}:{}]"))
112 free(str2);
113  
114 NCDValCons_Free(&cons);
115 NCDValMem_Free(&mem);
116 NCDStringIndex_Free(&string_index);
117 return 0;
118 }