2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include <vppinfra/ptclosure.h>
17 #include <vppinfra/hash.h>
25 test_main_t test_main;
27 static char *items[] = {
34 char *constraints[] = {
38 // "c,a", /* no partial order possible */
48 dump_closure (test_main_t * tm, char *s, u8 ** orig)
52 fformat (stdout, "--------- %s --------------\n", s);
53 for (i = 0; i < vec_len (orig); i++)
55 for (j = 0; j < vec_len (orig); j++)
58 fformat (stdout, "%s <before> %s\n", items[i], items[j]);
64 comma_split (u8 * s, u8 ** a, u8 ** b)
68 while (*s && *s != ',')
81 test_ptclosure_main (unformat_input_t * input)
83 test_main_t *tm = &test_main;
95 tm->index_by_name = hash_create_string (0, sizeof (uword));
97 n = ARRAY_LEN (items);
99 for (i = 0; i < n; i++)
101 item_name = (u8 *) items[i];
102 hash_set_mem (tm->index_by_name, item_name, i);
105 orig = clib_ptclosure_alloc (n);
107 for (i = 0; i < ARRAY_LEN (constraints); i++)
109 this_constraint = format (0, "%s%c", constraints[i], 0);
111 if (comma_split (this_constraint, &a_name, &b_name))
113 clib_warning ("couldn't split '%s'", constraints[i]);
117 p = hash_get_mem (tm->index_by_name, a_name);
120 clib_warning ("couldn't find '%s'", a_name);
125 p = hash_get_mem (tm->index_by_name, b_name);
128 clib_warning ("couldn't find '%s'", b_name);
133 orig[a_index][b_index] = 1;
134 vec_free (this_constraint);
137 dump_closure (tm, "original relation", orig);
139 closure = clib_ptclosure (orig);
141 dump_closure (tm, "closure", closure);
144 * Output partial order
148 for (i = 0; i < n; i++)
150 for (j = 0; j < n; j++)
153 goto item_constrained;
155 /* Item i can be output */
156 vec_add1 (result, i);
159 for (k = 0; k < n; k++)
161 /* "Magic" a before a, to keep from ever outputting it again */
169 if (vec_len (result) != n)
171 clib_warning ("no partial order exists");
175 fformat (stdout, "Partial order:\n");
177 for (i = vec_len (result) - 1; i >= 0; i--)
179 fformat (stdout, "%s\n", items[result[i]]);
183 clib_ptclosure_free (orig);
184 clib_ptclosure_free (closure);
191 main (int argc, char *argv[])
196 clib_mem_init (0, 3ULL << 30);
198 unformat_init_command_line (&i, argv);
199 ret = test_ptclosure_main (&i);
204 #endif /* CLIB_UNIX */
207 * fd.io coding-style-patch-verification: ON
210 * eval: (c-set-style "gnu")