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>
20 uword * index_by_name;
24 test_main_t test_main;
26 static char * items [] = {
33 char * constraints [] = {
37 // "c,a", /* no partial order possible */
45 static void dump_closure (test_main_t * tm, char * s, u8 ** orig)
49 fformat (stdout, "--------- %s --------------\n", s);
50 for (i = 0; i < vec_len (orig); i++)
52 for (j = 0; j < vec_len (orig); j++)
55 fformat (stdout, "%s <before> %s\n", items[i], items[j]);
60 int comma_split (u8 *s, u8 **a, u8 **b)
64 while (*s && *s != ',')
76 int test_ptclosure_main (unformat_input_t * input)
78 test_main_t * tm = &test_main;
83 u8 * a_name, * b_name;
90 tm->index_by_name = hash_create_string (0, sizeof (uword));
94 for (i = 0; i < n; i++)
96 item_name = (u8 *) items[i];
97 hash_set_mem (tm->index_by_name, item_name, i);
100 orig = clib_ptclosure_alloc (n);
102 for (i = 0; i < ARRAY_LEN(constraints); i++)
104 this_constraint = format (0, "%s%c", constraints[i], 0);
106 if (comma_split (this_constraint, &a_name, &b_name))
108 clib_warning ("couldn't split '%s'", constraints[i]);
112 p = hash_get_mem (tm->index_by_name, a_name);
115 clib_warning ("couldn't find '%s'", a_name);
120 p = hash_get_mem (tm->index_by_name, b_name);
123 clib_warning ("couldn't find '%s'", b_name);
128 orig[a_index][b_index] = 1;
129 vec_free (this_constraint);
132 dump_closure (tm, "original relation", orig);
134 closure = clib_ptclosure (orig);
136 dump_closure (tm, "closure", closure);
139 * Output partial order
143 for (i = 0; i < n; i++)
145 for (j = 0; j < n; j++)
148 goto item_constrained;
150 /* Item i can be output */
151 vec_add1 (result, i);
154 for (k = 0; k < n; k++)
156 /* "Magic" a before a, to keep from ever outputting it again */
164 if (vec_len (result) != n)
166 clib_warning ("no partial order exists");
170 fformat (stdout, "Partial order:\n");
172 for (i = vec_len(result)-1; i >= 0; i--)
174 fformat (stdout, "%s\n", items[result[i]]);
178 clib_ptclosure_free (orig);
179 clib_ptclosure_free (closure);
185 int main (int argc, char * argv[])
190 clib_mem_init (0, 3ULL<<30);
192 unformat_init_command_line (&i, argv);
193 ret = test_ptclosure_main (&i);
198 #endif /* CLIB_UNIX */