annotate gcc/vec.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 /* Vector API for GNU compiler.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2004-2018 Free Software Foundation, Inc.
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
111
kono
parents: 67
diff changeset
4 Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
5
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 This file is part of GCC.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 GCC is free software; you can redistribute it and/or modify it under
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 the terms of the GNU General Public License as published by the Free
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 Software Foundation; either version 3, or (at your option) any later
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 version.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 for more details.
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 along with GCC; see the file COPYING3. If not see
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 <http://www.gnu.org/licenses/>. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 /* This file is compiled twice: once for the generator programs
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 once for the compiler. */
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #ifdef GENERATOR_FILE
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include "bconfig.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 #else
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 #include "config.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #endif
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 #include "system.h"
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 #include "coretypes.h"
111
kono
parents: 67
diff changeset
32 #include "hash-table.h"
kono
parents: 67
diff changeset
33 #include "selftest.h"
kono
parents: 67
diff changeset
34 #ifdef GENERATOR_FILE
kono
parents: 67
diff changeset
35 #include "errors.h"
kono
parents: 67
diff changeset
36 #else
kono
parents: 67
diff changeset
37 #include "input.h"
67
f6334be47118 update gcc from gcc-4.6-20100522 to gcc-4.6-20110318
nobuyasu <dimolto@cr.ie.u-ryukyu.ac.jp>
parents: 55
diff changeset
38 #include "diagnostic-core.h"
111
kono
parents: 67
diff changeset
39 #endif
kono
parents: 67
diff changeset
40
kono
parents: 67
diff changeset
41 /* vNULL is an empty type with a template cast operation that returns
kono
parents: 67
diff changeset
42 a zero-initialized vec<T, A, L> instance. Use this when you want
kono
parents: 67
diff changeset
43 to assign nil values to new vec instances or pass a nil vector as
kono
parents: 67
diff changeset
44 a function call argument.
kono
parents: 67
diff changeset
45
kono
parents: 67
diff changeset
46 We use this technique because vec<T, A, L> must be PODs (they are
kono
parents: 67
diff changeset
47 stored in unions and passed in vararg functions), this means that
kono
parents: 67
diff changeset
48 they cannot have ctors/dtors. */
kono
parents: 67
diff changeset
49 vnull vNULL;
kono
parents: 67
diff changeset
50
kono
parents: 67
diff changeset
51 /* Vector memory usage. */
kono
parents: 67
diff changeset
52 struct vec_usage: public mem_usage
kono
parents: 67
diff changeset
53 {
kono
parents: 67
diff changeset
54 /* Default constructor. */
kono
parents: 67
diff changeset
55 vec_usage (): m_items (0), m_items_peak (0) {}
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
56
111
kono
parents: 67
diff changeset
57 /* Constructor. */
kono
parents: 67
diff changeset
58 vec_usage (size_t allocated, size_t times, size_t peak,
kono
parents: 67
diff changeset
59 size_t items, size_t items_peak)
kono
parents: 67
diff changeset
60 : mem_usage (allocated, times, peak),
kono
parents: 67
diff changeset
61 m_items (items), m_items_peak (items_peak) {}
kono
parents: 67
diff changeset
62
kono
parents: 67
diff changeset
63 /* Sum the usage with SECOND usage. */
kono
parents: 67
diff changeset
64 vec_usage
kono
parents: 67
diff changeset
65 operator+ (const vec_usage &second)
kono
parents: 67
diff changeset
66 {
kono
parents: 67
diff changeset
67 return vec_usage (m_allocated + second.m_allocated,
kono
parents: 67
diff changeset
68 m_times + second.m_times,
kono
parents: 67
diff changeset
69 m_peak + second.m_peak,
kono
parents: 67
diff changeset
70 m_items + second.m_items,
kono
parents: 67
diff changeset
71 m_items_peak + second.m_items_peak);
kono
parents: 67
diff changeset
72 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
111
kono
parents: 67
diff changeset
74 /* Dump usage coupled to LOC location, where TOTAL is sum of all rows. */
kono
parents: 67
diff changeset
75 inline void
kono
parents: 67
diff changeset
76 dump (mem_location *loc, mem_usage &total) const
kono
parents: 67
diff changeset
77 {
kono
parents: 67
diff changeset
78 char s[4096];
kono
parents: 67
diff changeset
79 sprintf (s, "%s:%i (%s)", loc->get_trimmed_filename (),
kono
parents: 67
diff changeset
80 loc->m_line, loc->m_function);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
81
111
kono
parents: 67
diff changeset
82 s[48] = '\0';
kono
parents: 67
diff changeset
83
kono
parents: 67
diff changeset
84 fprintf (stderr, "%-48s %10li:%4.1f%%%10li%10li:%4.1f%%%11li%11li\n", s,
kono
parents: 67
diff changeset
85 (long)m_allocated, m_allocated * 100.0 / total.m_allocated,
kono
parents: 67
diff changeset
86 (long)m_peak, (long)m_times, m_times * 100.0 / total.m_times,
kono
parents: 67
diff changeset
87 (long)m_items, (long)m_items_peak);
kono
parents: 67
diff changeset
88 }
kono
parents: 67
diff changeset
89
kono
parents: 67
diff changeset
90 /* Dump footer. */
kono
parents: 67
diff changeset
91 inline void
kono
parents: 67
diff changeset
92 dump_footer ()
kono
parents: 67
diff changeset
93 {
kono
parents: 67
diff changeset
94 print_dash_line ();
kono
parents: 67
diff changeset
95 fprintf (stderr, "%s%55li%25li%17li\n", "Total", (long)m_allocated,
kono
parents: 67
diff changeset
96 (long)m_times, (long)m_items);
kono
parents: 67
diff changeset
97 print_dash_line ();
kono
parents: 67
diff changeset
98 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
99
111
kono
parents: 67
diff changeset
100 /* Dump header with NAME. */
kono
parents: 67
diff changeset
101 static inline void
kono
parents: 67
diff changeset
102 dump_header (const char *name)
kono
parents: 67
diff changeset
103 {
kono
parents: 67
diff changeset
104 fprintf (stderr, "%-48s %11s%15s%10s%17s%11s\n", name, "Leak", "Peak",
kono
parents: 67
diff changeset
105 "Times", "Leak items", "Peak items");
kono
parents: 67
diff changeset
106 print_dash_line ();
kono
parents: 67
diff changeset
107 }
kono
parents: 67
diff changeset
108
kono
parents: 67
diff changeset
109 /* Current number of items allocated. */
kono
parents: 67
diff changeset
110 size_t m_items;
kono
parents: 67
diff changeset
111 /* Peak value of number of allocated items. */
kono
parents: 67
diff changeset
112 size_t m_items_peak;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 };
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
111
kono
parents: 67
diff changeset
115 /* Vector memory description. */
kono
parents: 67
diff changeset
116 static mem_alloc_description <vec_usage> vec_mem_desc;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
117
111
kono
parents: 67
diff changeset
118 /* Account the overhead. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
119
111
kono
parents: 67
diff changeset
120 void
kono
parents: 67
diff changeset
121 vec_prefix::register_overhead (void *ptr, size_t size, size_t elements
kono
parents: 67
diff changeset
122 MEM_STAT_DECL)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 {
111
kono
parents: 67
diff changeset
124 vec_mem_desc.register_descriptor (ptr, VEC_ORIGIN, false
kono
parents: 67
diff changeset
125 FINAL_PASS_MEM_STAT);
kono
parents: 67
diff changeset
126 vec_usage *usage = vec_mem_desc.register_instance_overhead (size, ptr);
kono
parents: 67
diff changeset
127 usage->m_items += elements;
kono
parents: 67
diff changeset
128 if (usage->m_items_peak < usage->m_items)
kono
parents: 67
diff changeset
129 usage->m_items_peak = usage->m_items;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
131
111
kono
parents: 67
diff changeset
132 /* Notice that the memory allocated for the vector has been freed. */
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
133
111
kono
parents: 67
diff changeset
134 void
kono
parents: 67
diff changeset
135 vec_prefix::release_overhead (void *ptr, size_t size, bool in_dtor
kono
parents: 67
diff changeset
136 MEM_STAT_DECL)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 {
111
kono
parents: 67
diff changeset
138 if (!vec_mem_desc.contains_descriptor_for_instance (ptr))
kono
parents: 67
diff changeset
139 vec_mem_desc.register_descriptor (ptr, VEC_ORIGIN,
kono
parents: 67
diff changeset
140 false FINAL_PASS_MEM_STAT);
kono
parents: 67
diff changeset
141 vec_mem_desc.release_instance_overhead (ptr, size, in_dtor);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
143
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
144
111
kono
parents: 67
diff changeset
145 /* Calculate the number of slots to reserve a vector, making sure that
kono
parents: 67
diff changeset
146 it is of at least DESIRED size by growing ALLOC exponentially. */
kono
parents: 67
diff changeset
147
kono
parents: 67
diff changeset
148 unsigned
kono
parents: 67
diff changeset
149 vec_prefix::calculate_allocation_1 (unsigned alloc, unsigned desired)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 {
111
kono
parents: 67
diff changeset
151 /* We must have run out of room. */
kono
parents: 67
diff changeset
152 gcc_assert (alloc < desired);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
153
111
kono
parents: 67
diff changeset
154 /* Exponential growth. */
kono
parents: 67
diff changeset
155 if (!alloc)
kono
parents: 67
diff changeset
156 alloc = 4;
kono
parents: 67
diff changeset
157 else if (alloc < 16)
kono
parents: 67
diff changeset
158 /* Double when small. */
kono
parents: 67
diff changeset
159 alloc = alloc * 2;
kono
parents: 67
diff changeset
160 else
kono
parents: 67
diff changeset
161 /* Grow slower when large. */
kono
parents: 67
diff changeset
162 alloc = (alloc * 3 / 2);
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
163
111
kono
parents: 67
diff changeset
164 /* If this is still too small, set it to the right size. */
kono
parents: 67
diff changeset
165 if (alloc < desired)
kono
parents: 67
diff changeset
166 alloc = desired;
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 return alloc;
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 }
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
169
111
kono
parents: 67
diff changeset
170 /* Dump per-site memory statistics. */
55
77e2b8dfacca update it from 4.4.3 to 4.5.0
ryoma <e075725@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
171
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 void
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 dump_vec_loc_statistics (void)
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 {
111
kono
parents: 67
diff changeset
175 vec_mem_desc.dump (VEC_ORIGIN);
kono
parents: 67
diff changeset
176 }
kono
parents: 67
diff changeset
177
kono
parents: 67
diff changeset
178 #if CHECKING_P
kono
parents: 67
diff changeset
179 /* Report qsort comparator CMP consistency check failure with P1, P2, P3 as
kono
parents: 67
diff changeset
180 witness elements. */
kono
parents: 67
diff changeset
181 ATTRIBUTE_NORETURN ATTRIBUTE_COLD
kono
parents: 67
diff changeset
182 static void
kono
parents: 67
diff changeset
183 qsort_chk_error (const void *p1, const void *p2, const void *p3,
kono
parents: 67
diff changeset
184 int (*cmp) (const void *, const void *))
kono
parents: 67
diff changeset
185 {
kono
parents: 67
diff changeset
186 if (!p3)
kono
parents: 67
diff changeset
187 {
kono
parents: 67
diff changeset
188 int r1 = cmp (p1, p2), r2 = cmp (p2, p1);
kono
parents: 67
diff changeset
189 error ("qsort comparator not anti-commutative: %d, %d", r1, r2);
kono
parents: 67
diff changeset
190 }
kono
parents: 67
diff changeset
191 else if (p1 == p2)
kono
parents: 67
diff changeset
192 {
kono
parents: 67
diff changeset
193 int r = cmp (p1, p3);
kono
parents: 67
diff changeset
194 error ("qsort comparator non-negative on sorted output: %d", r);
kono
parents: 67
diff changeset
195 }
kono
parents: 67
diff changeset
196 else
kono
parents: 67
diff changeset
197 {
kono
parents: 67
diff changeset
198 int r1 = cmp (p1, p2), r2 = cmp (p2, p3), r3 = cmp (p1, p3);
kono
parents: 67
diff changeset
199 error ("qsort comparator not transitive: %d, %d, %d", r1, r2, r3);
kono
parents: 67
diff changeset
200 }
kono
parents: 67
diff changeset
201 internal_error ("qsort checking failed");
kono
parents: 67
diff changeset
202 }
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
203
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
204 /* Verify anti-symmetry and transitivity for comparator CMP on sorted array
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
205 of N SIZE-sized elements pointed to by BASE. */
111
kono
parents: 67
diff changeset
206 void
kono
parents: 67
diff changeset
207 qsort_chk (void *base, size_t n, size_t size,
kono
parents: 67
diff changeset
208 int (*cmp)(const void *, const void *))
kono
parents: 67
diff changeset
209 {
kono
parents: 67
diff changeset
210 #if 0
kono
parents: 67
diff changeset
211 #define LIM(n) (n)
kono
parents: 67
diff changeset
212 #else
kono
parents: 67
diff changeset
213 /* Limit overall time complexity to O(n log n). */
kono
parents: 67
diff changeset
214 #define LIM(n) ((n) <= 16 ? (n) : 12 + floor_log2 (n))
kono
parents: 67
diff changeset
215 #endif
kono
parents: 67
diff changeset
216 #define ELT(i) ((const char *) base + (i) * size)
kono
parents: 67
diff changeset
217 #define CMP(i, j) cmp (ELT (i), ELT (j))
kono
parents: 67
diff changeset
218 #define ERR2(i, j) qsort_chk_error (ELT (i), ELT (j), NULL, cmp)
kono
parents: 67
diff changeset
219 #define ERR3(i, j, k) qsort_chk_error (ELT (i), ELT (j), ELT (k), cmp)
kono
parents: 67
diff changeset
220 size_t i1, i2, i, j;
kono
parents: 67
diff changeset
221 /* This outer loop iterates over maximum spans [i1, i2) such that
kono
parents: 67
diff changeset
222 elements within each span compare equal to each other. */
kono
parents: 67
diff changeset
223 for (i1 = 0; i1 < n; i1 = i2)
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 {
111
kono
parents: 67
diff changeset
225 /* Position i2 one past last element that compares equal to i1'th. */
kono
parents: 67
diff changeset
226 for (i2 = i1 + 1; i2 < n; i2++)
kono
parents: 67
diff changeset
227 if (CMP (i1, i2))
kono
parents: 67
diff changeset
228 break;
kono
parents: 67
diff changeset
229 else if (CMP (i2, i1))
kono
parents: 67
diff changeset
230 return ERR2 (i1, i2);
kono
parents: 67
diff changeset
231 size_t lim1 = LIM (i2 - i1), lim2 = LIM (n - i2);
kono
parents: 67
diff changeset
232 /* Verify that other pairs within current span compare equal. */
kono
parents: 67
diff changeset
233 for (i = i1 + 1; i + 1 < i2; i++)
kono
parents: 67
diff changeset
234 for (j = i + 1; j < i1 + lim1; j++)
kono
parents: 67
diff changeset
235 if (CMP (i, j))
kono
parents: 67
diff changeset
236 return ERR3 (i, i1, j);
kono
parents: 67
diff changeset
237 else if (CMP (j, i))
kono
parents: 67
diff changeset
238 return ERR2 (i, j);
kono
parents: 67
diff changeset
239 /* Verify that elements within this span compare less than
kono
parents: 67
diff changeset
240 elements beyond the span. */
kono
parents: 67
diff changeset
241 for (i = i1; i < i2; i++)
kono
parents: 67
diff changeset
242 for (j = i2; j < i2 + lim2; j++)
kono
parents: 67
diff changeset
243 if (CMP (i, j) >= 0)
kono
parents: 67
diff changeset
244 return ERR3 (i, i1, j);
kono
parents: 67
diff changeset
245 else if (CMP (j, i) <= 0)
kono
parents: 67
diff changeset
246 return ERR2 (i, j);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 }
111
kono
parents: 67
diff changeset
248 #undef ERR3
kono
parents: 67
diff changeset
249 #undef ERR2
kono
parents: 67
diff changeset
250 #undef CMP
kono
parents: 67
diff changeset
251 #undef ELT
kono
parents: 67
diff changeset
252 #undef LIM
kono
parents: 67
diff changeset
253 }
kono
parents: 67
diff changeset
254 #endif /* #if CHECKING_P */
kono
parents: 67
diff changeset
255
kono
parents: 67
diff changeset
256 #ifndef GENERATOR_FILE
kono
parents: 67
diff changeset
257 #if CHECKING_P
kono
parents: 67
diff changeset
258
kono
parents: 67
diff changeset
259 namespace selftest {
kono
parents: 67
diff changeset
260
kono
parents: 67
diff changeset
261 /* Selftests. */
kono
parents: 67
diff changeset
262
kono
parents: 67
diff changeset
263 /* Call V.safe_push for all ints from START up to, but not including LIMIT.
kono
parents: 67
diff changeset
264 Helper function for selftests. */
kono
parents: 67
diff changeset
265
kono
parents: 67
diff changeset
266 static void
kono
parents: 67
diff changeset
267 safe_push_range (vec <int>&v, int start, int limit)
kono
parents: 67
diff changeset
268 {
kono
parents: 67
diff changeset
269 for (int i = start; i < limit; i++)
kono
parents: 67
diff changeset
270 v.safe_push (i);
kono
parents: 67
diff changeset
271 }
kono
parents: 67
diff changeset
272
kono
parents: 67
diff changeset
273 /* Verify that vec::quick_push works correctly. */
kono
parents: 67
diff changeset
274
kono
parents: 67
diff changeset
275 static void
kono
parents: 67
diff changeset
276 test_quick_push ()
kono
parents: 67
diff changeset
277 {
kono
parents: 67
diff changeset
278 auto_vec <int> v;
kono
parents: 67
diff changeset
279 ASSERT_EQ (0, v.length ());
kono
parents: 67
diff changeset
280 v.reserve (3);
kono
parents: 67
diff changeset
281 ASSERT_EQ (0, v.length ());
kono
parents: 67
diff changeset
282 ASSERT_TRUE (v.space (3));
kono
parents: 67
diff changeset
283 v.quick_push (5);
kono
parents: 67
diff changeset
284 v.quick_push (6);
kono
parents: 67
diff changeset
285 v.quick_push (7);
kono
parents: 67
diff changeset
286 ASSERT_EQ (3, v.length ());
kono
parents: 67
diff changeset
287 ASSERT_EQ (5, v[0]);
kono
parents: 67
diff changeset
288 ASSERT_EQ (6, v[1]);
kono
parents: 67
diff changeset
289 ASSERT_EQ (7, v[2]);
kono
parents: 67
diff changeset
290 }
kono
parents: 67
diff changeset
291
kono
parents: 67
diff changeset
292 /* Verify that vec::safe_push works correctly. */
kono
parents: 67
diff changeset
293
kono
parents: 67
diff changeset
294 static void
kono
parents: 67
diff changeset
295 test_safe_push ()
kono
parents: 67
diff changeset
296 {
kono
parents: 67
diff changeset
297 auto_vec <int> v;
kono
parents: 67
diff changeset
298 ASSERT_EQ (0, v.length ());
kono
parents: 67
diff changeset
299 v.safe_push (5);
kono
parents: 67
diff changeset
300 v.safe_push (6);
kono
parents: 67
diff changeset
301 v.safe_push (7);
kono
parents: 67
diff changeset
302 ASSERT_EQ (3, v.length ());
kono
parents: 67
diff changeset
303 ASSERT_EQ (5, v[0]);
kono
parents: 67
diff changeset
304 ASSERT_EQ (6, v[1]);
kono
parents: 67
diff changeset
305 ASSERT_EQ (7, v[2]);
kono
parents: 67
diff changeset
306 }
kono
parents: 67
diff changeset
307
kono
parents: 67
diff changeset
308 /* Verify that vec::truncate works correctly. */
kono
parents: 67
diff changeset
309
kono
parents: 67
diff changeset
310 static void
kono
parents: 67
diff changeset
311 test_truncate ()
kono
parents: 67
diff changeset
312 {
kono
parents: 67
diff changeset
313 auto_vec <int> v;
kono
parents: 67
diff changeset
314 ASSERT_EQ (0, v.length ());
kono
parents: 67
diff changeset
315 safe_push_range (v, 0, 10);
kono
parents: 67
diff changeset
316 ASSERT_EQ (10, v.length ());
kono
parents: 67
diff changeset
317
kono
parents: 67
diff changeset
318 v.truncate (5);
kono
parents: 67
diff changeset
319 ASSERT_EQ (5, v.length ());
kono
parents: 67
diff changeset
320 }
kono
parents: 67
diff changeset
321
kono
parents: 67
diff changeset
322 /* Verify that vec::safe_grow_cleared works correctly. */
kono
parents: 67
diff changeset
323
kono
parents: 67
diff changeset
324 static void
kono
parents: 67
diff changeset
325 test_safe_grow_cleared ()
kono
parents: 67
diff changeset
326 {
kono
parents: 67
diff changeset
327 auto_vec <int> v;
kono
parents: 67
diff changeset
328 ASSERT_EQ (0, v.length ());
kono
parents: 67
diff changeset
329 v.safe_grow_cleared (50);
kono
parents: 67
diff changeset
330 ASSERT_EQ (50, v.length ());
kono
parents: 67
diff changeset
331 ASSERT_EQ (0, v[0]);
kono
parents: 67
diff changeset
332 ASSERT_EQ (0, v[49]);
0
a06113de4d67 first commit
kent <kent@cr.ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 }
111
kono
parents: 67
diff changeset
334
kono
parents: 67
diff changeset
335 /* Verify that vec::pop works correctly. */
kono
parents: 67
diff changeset
336
kono
parents: 67
diff changeset
337 static void
kono
parents: 67
diff changeset
338 test_pop ()
kono
parents: 67
diff changeset
339 {
kono
parents: 67
diff changeset
340 auto_vec <int> v;
kono
parents: 67
diff changeset
341 safe_push_range (v, 5, 20);
kono
parents: 67
diff changeset
342 ASSERT_EQ (15, v.length ());
kono
parents: 67
diff changeset
343
kono
parents: 67
diff changeset
344 int last = v.pop ();
kono
parents: 67
diff changeset
345 ASSERT_EQ (19, last);
kono
parents: 67
diff changeset
346 ASSERT_EQ (14, v.length ());
kono
parents: 67
diff changeset
347 }
kono
parents: 67
diff changeset
348
kono
parents: 67
diff changeset
349 /* Verify that vec::safe_insert works correctly. */
kono
parents: 67
diff changeset
350
kono
parents: 67
diff changeset
351 static void
kono
parents: 67
diff changeset
352 test_safe_insert ()
kono
parents: 67
diff changeset
353 {
kono
parents: 67
diff changeset
354 auto_vec <int> v;
kono
parents: 67
diff changeset
355 safe_push_range (v, 0, 10);
kono
parents: 67
diff changeset
356 v.safe_insert (5, 42);
kono
parents: 67
diff changeset
357 ASSERT_EQ (4, v[4]);
kono
parents: 67
diff changeset
358 ASSERT_EQ (42, v[5]);
kono
parents: 67
diff changeset
359 ASSERT_EQ (5, v[6]);
kono
parents: 67
diff changeset
360 ASSERT_EQ (11, v.length ());
kono
parents: 67
diff changeset
361 }
kono
parents: 67
diff changeset
362
kono
parents: 67
diff changeset
363 /* Verify that vec::ordered_remove works correctly. */
kono
parents: 67
diff changeset
364
kono
parents: 67
diff changeset
365 static void
kono
parents: 67
diff changeset
366 test_ordered_remove ()
kono
parents: 67
diff changeset
367 {
kono
parents: 67
diff changeset
368 auto_vec <int> v;
kono
parents: 67
diff changeset
369 safe_push_range (v, 0, 10);
kono
parents: 67
diff changeset
370 v.ordered_remove (5);
kono
parents: 67
diff changeset
371 ASSERT_EQ (4, v[4]);
kono
parents: 67
diff changeset
372 ASSERT_EQ (6, v[5]);
kono
parents: 67
diff changeset
373 ASSERT_EQ (9, v.length ());
kono
parents: 67
diff changeset
374 }
kono
parents: 67
diff changeset
375
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
376 /* Verify that vec::ordered_remove_if works correctly. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
377
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
378 static void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
379 test_ordered_remove_if (void)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
380 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
381 auto_vec <int> v;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
382 safe_push_range (v, 0, 10);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
383 unsigned ix, ix2;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
384 int *elem_ptr;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
385 VEC_ORDERED_REMOVE_IF (v, ix, ix2, elem_ptr,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
386 *elem_ptr == 5 || *elem_ptr == 7);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
387 ASSERT_EQ (4, v[4]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
388 ASSERT_EQ (6, v[5]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
389 ASSERT_EQ (8, v[6]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
390 ASSERT_EQ (8, v.length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
391
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
392 v.truncate (0);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
393 safe_push_range (v, 0, 10);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
394 VEC_ORDERED_REMOVE_IF_FROM_TO (v, ix, ix2, elem_ptr, 0, 6,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
395 *elem_ptr == 5 || *elem_ptr == 7);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
396 ASSERT_EQ (4, v[4]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
397 ASSERT_EQ (6, v[5]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
398 ASSERT_EQ (7, v[6]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
399 ASSERT_EQ (9, v.length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
400
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
401 v.truncate (0);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
402 safe_push_range (v, 0, 10);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
403 VEC_ORDERED_REMOVE_IF_FROM_TO (v, ix, ix2, elem_ptr, 0, 5,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
404 *elem_ptr == 5 || *elem_ptr == 7);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
405 VEC_ORDERED_REMOVE_IF_FROM_TO (v, ix, ix2, elem_ptr, 8, 10,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
406 *elem_ptr == 5 || *elem_ptr == 7);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
407 ASSERT_EQ (4, v[4]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
408 ASSERT_EQ (5, v[5]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
409 ASSERT_EQ (6, v[6]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
410 ASSERT_EQ (10, v.length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
411
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
412 v.truncate (0);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
413 safe_push_range (v, 0, 10);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
414 VEC_ORDERED_REMOVE_IF (v, ix, ix2, elem_ptr, *elem_ptr == 5);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
415 ASSERT_EQ (4, v[4]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
416 ASSERT_EQ (6, v[5]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
417 ASSERT_EQ (7, v[6]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
418 ASSERT_EQ (9, v.length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
419 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
420
111
kono
parents: 67
diff changeset
421 /* Verify that vec::unordered_remove works correctly. */
kono
parents: 67
diff changeset
422
kono
parents: 67
diff changeset
423 static void
kono
parents: 67
diff changeset
424 test_unordered_remove ()
kono
parents: 67
diff changeset
425 {
kono
parents: 67
diff changeset
426 auto_vec <int> v;
kono
parents: 67
diff changeset
427 safe_push_range (v, 0, 10);
kono
parents: 67
diff changeset
428 v.unordered_remove (5);
kono
parents: 67
diff changeset
429 ASSERT_EQ (9, v.length ());
kono
parents: 67
diff changeset
430 }
kono
parents: 67
diff changeset
431
kono
parents: 67
diff changeset
432 /* Verify that vec::block_remove works correctly. */
kono
parents: 67
diff changeset
433
kono
parents: 67
diff changeset
434 static void
kono
parents: 67
diff changeset
435 test_block_remove ()
kono
parents: 67
diff changeset
436 {
kono
parents: 67
diff changeset
437 auto_vec <int> v;
kono
parents: 67
diff changeset
438 safe_push_range (v, 0, 10);
kono
parents: 67
diff changeset
439 v.block_remove (5, 3);
kono
parents: 67
diff changeset
440 ASSERT_EQ (3, v[3]);
kono
parents: 67
diff changeset
441 ASSERT_EQ (4, v[4]);
kono
parents: 67
diff changeset
442 ASSERT_EQ (8, v[5]);
kono
parents: 67
diff changeset
443 ASSERT_EQ (9, v[6]);
kono
parents: 67
diff changeset
444 ASSERT_EQ (7, v.length ());
kono
parents: 67
diff changeset
445 }
kono
parents: 67
diff changeset
446
kono
parents: 67
diff changeset
447 /* Comparator for use by test_qsort. */
kono
parents: 67
diff changeset
448
kono
parents: 67
diff changeset
449 static int
kono
parents: 67
diff changeset
450 reverse_cmp (const void *p_i, const void *p_j)
kono
parents: 67
diff changeset
451 {
kono
parents: 67
diff changeset
452 return *(const int *)p_j - *(const int *)p_i;
kono
parents: 67
diff changeset
453 }
kono
parents: 67
diff changeset
454
kono
parents: 67
diff changeset
455 /* Verify that vec::qsort works correctly. */
kono
parents: 67
diff changeset
456
kono
parents: 67
diff changeset
457 static void
kono
parents: 67
diff changeset
458 test_qsort ()
kono
parents: 67
diff changeset
459 {
kono
parents: 67
diff changeset
460 auto_vec <int> v;
kono
parents: 67
diff changeset
461 safe_push_range (v, 0, 10);
kono
parents: 67
diff changeset
462 v.qsort (reverse_cmp);
kono
parents: 67
diff changeset
463 ASSERT_EQ (9, v[0]);
kono
parents: 67
diff changeset
464 ASSERT_EQ (8, v[1]);
kono
parents: 67
diff changeset
465 ASSERT_EQ (1, v[8]);
kono
parents: 67
diff changeset
466 ASSERT_EQ (0, v[9]);
kono
parents: 67
diff changeset
467 ASSERT_EQ (10, v.length ());
kono
parents: 67
diff changeset
468 }
kono
parents: 67
diff changeset
469
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
470 /* Verify that vec::reverse works correctly. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
471
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
472 static void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
473 test_reverse ()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
474 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
475 /* Reversing an empty vec ought to be a no-op. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
476 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
477 auto_vec <int> v;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
478 ASSERT_EQ (0, v.length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
479 v.reverse ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
480 ASSERT_EQ (0, v.length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
481 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
482
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
483 /* Verify reversing a vec with even length. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
484 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
485 auto_vec <int> v;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
486 safe_push_range (v, 0, 4);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
487 v.reverse ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
488 ASSERT_EQ (3, v[0]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
489 ASSERT_EQ (2, v[1]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
490 ASSERT_EQ (1, v[2]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
491 ASSERT_EQ (0, v[3]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
492 ASSERT_EQ (4, v.length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
493 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
494
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
495 /* Verify reversing a vec with odd length. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
496 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
497 auto_vec <int> v;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
498 safe_push_range (v, 0, 3);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
499 v.reverse ();
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
500 ASSERT_EQ (2, v[0]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
501 ASSERT_EQ (1, v[1]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
502 ASSERT_EQ (0, v[2]);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
503 ASSERT_EQ (3, v.length ());
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
504 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
505 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
506
111
kono
parents: 67
diff changeset
507 /* Run all of the selftests within this file. */
kono
parents: 67
diff changeset
508
kono
parents: 67
diff changeset
509 void
kono
parents: 67
diff changeset
510 vec_c_tests ()
kono
parents: 67
diff changeset
511 {
kono
parents: 67
diff changeset
512 test_quick_push ();
kono
parents: 67
diff changeset
513 test_safe_push ();
kono
parents: 67
diff changeset
514 test_truncate ();
kono
parents: 67
diff changeset
515 test_safe_grow_cleared ();
kono
parents: 67
diff changeset
516 test_pop ();
kono
parents: 67
diff changeset
517 test_safe_insert ();
kono
parents: 67
diff changeset
518 test_ordered_remove ();
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
519 test_ordered_remove_if ();
111
kono
parents: 67
diff changeset
520 test_unordered_remove ();
kono
parents: 67
diff changeset
521 test_block_remove ();
kono
parents: 67
diff changeset
522 test_qsort ();
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
523 test_reverse ();
111
kono
parents: 67
diff changeset
524 }
kono
parents: 67
diff changeset
525
kono
parents: 67
diff changeset
526 } // namespace selftest
kono
parents: 67
diff changeset
527
kono
parents: 67
diff changeset
528 #endif /* #if CHECKING_P */
kono
parents: 67
diff changeset
529 #endif /* #ifndef GENERATOR_FILE */