xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.python/py-nested-maps.c (revision 801f73adf8029e41ec107911c58034bb925796a2)
1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2019-2020 Free Software Foundation, Inc.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see  <http://www.gnu.org/licenses/>.  */
17 
18 #include <stdlib.h>
19 #include <string.h>
20 
21 #define FIXED_MAP_SIZE 10
22 
23 struct key_t
24 {
25   int a;
26   int b;
27 };
28 
29 struct value_t
30 {
31   int x;
32   int y;
33   int z;
34 };
35 
36 struct map_t
37 {
38   const char *name;
39   int length;
40   struct key_t *keys;
41   struct value_t *values;
42 
43   /* This field is used only by the pretty printer.  */
44   int show_header;
45 };
46 
47 struct map_map_t
48 {
49   int length;
50   struct map_t **values;
51 
52   /* This field is used only by the pretty printer.  */
53   int show_header;
54 };
55 
56 struct map_t *
57 create_map (const char *name)
58 {
59   struct map_t *m = malloc (sizeof (struct map_t));
60   m->name = strdup (name);
61   m->length = 0;
62   m->keys = NULL;
63   m->values = NULL;
64   m->show_header = 0;
65   return m;
66 }
67 
68 void
69 add_map_element (struct map_t *m, struct key_t k, struct value_t v)
70 {
71   if (m->length == 0)
72     {
73       m->keys = malloc (sizeof (struct key_t) * FIXED_MAP_SIZE);
74       m->values = malloc (sizeof (struct value_t) * FIXED_MAP_SIZE);
75     }
76 
77   m->keys[m->length] = k;
78   m->values[m->length] = v;
79   m->length++;
80 }
81 
82 struct map_map_t *
83 create_map_map (void)
84 {
85   struct map_map_t *mm = malloc (sizeof (struct map_map_t));
86   mm->length = 0;
87   mm->values = NULL;
88   mm->show_header = 0;
89   return mm;
90 }
91 
92 void
93 add_map_map_element (struct map_map_t *mm, struct map_t *map)
94 {
95   if (mm->length == 0)
96     mm->values = malloc (sizeof (struct map_t *) * FIXED_MAP_SIZE);
97 
98   mm->values[mm->length] = map;
99   mm->length++;
100 }
101 
102 int
103 main (void)
104 {
105   struct map_t *m1 = create_map ("m1");
106   struct key_t k1 = {3, 4};
107   struct key_t k2 = {4, 5};
108   struct key_t k3 = {5, 6};
109   struct key_t k4 = {6, 7};
110   struct key_t k5 = {7, 8};
111   struct key_t k6 = {8, 9};
112   struct value_t v1 = {0, 1, 2};
113   struct value_t v2 = {3, 4, 5};
114   struct value_t v3 = {6, 7, 8};
115   struct value_t v4 = {9, 0, 1};
116   struct value_t v5 = {2, 3, 4};
117   struct value_t v6 = {5, 6, 7};
118   add_map_element (m1, k1, v1);
119   add_map_element (m1, k2, v2);
120   add_map_element (m1, k3, v3);
121 
122   struct map_t *m2 = create_map ("m2");
123   add_map_element (m2, k4, v4);
124   add_map_element (m2, k5, v5);
125   add_map_element (m2, k6, v6);
126 
127   struct map_map_t *mm = create_map_map ();
128   add_map_map_element (mm, m1);
129   add_map_map_element (mm, m2);
130 
131   return 0; /* Break here.  */
132 }
133