Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1 | # Copyright 2013, Big Switch Networks, Inc. |
| 2 | # |
| 3 | # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | # the following special exception: |
| 5 | # |
| 6 | # LOXI Exception |
| 7 | # |
| 8 | # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | # |
| 15 | # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | # |
| 17 | # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | # a copy of the EPL at: |
| 19 | # |
| 20 | # http://www.eclipse.org/legal/epl-v10.html |
| 21 | # |
| 22 | # Unless required by applicable law or agreed to in writing, software |
| 23 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | # EPL for the specific language governing permissions and limitations |
| 26 | # under the EPL. |
| 27 | |
| 28 | """ |
| 29 | @brief Test case generation functions |
| 30 | |
| 31 | @fixme Update the following |
| 32 | The following components are generated. |
| 33 | |
| 34 | test_common.[ch]: A collection of common code for tests. Currently |
| 35 | this includes the ability to set the scalar members of an object with |
| 36 | incrementing values and then similarly verify those values |
| 37 | |
| 38 | test_scalar_acc.c: Instantiate each type of object, then set and get |
| 39 | scalar values in the objects. |
| 40 | |
| 41 | test_list.c: Instantiate each type of list, add an element of each |
| 42 | type the list supports, setting scalar values of the elements. |
| 43 | |
| 44 | test_match.c: Various tests for match objects |
| 45 | |
| 46 | test_msg.c: Instantiate top level messages |
| 47 | |
| 48 | These will move towards unified tests that do the following: |
| 49 | |
| 50 | Create or init an object. |
| 51 | Populate the object with incrementing values. |
| 52 | Possibly transform the object in some way (e.g., run the underlying |
| 53 | wire buffer through a parse routine). |
| 54 | Verify that the members all have the appropriate value |
| 55 | |
| 56 | Through out, checking the consistency of memory and memory operations |
YAMAMOTO Takashi | 071b591 | 2013-07-02 08:37:39 +0900 | [diff] [blame] | 57 | is done with mcheck (if available). |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 58 | |
| 59 | """ |
| 60 | |
| 61 | import sys |
Andreas Wundsam | 76db006 | 2013-11-15 13:34:41 -0800 | [diff] [blame] | 62 | import c_gen.of_g_legacy as of_g |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 63 | import c_gen.match as match |
| 64 | import c_gen.flags as flags |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 65 | from generic_utils import * |
Andreas Wundsam | 542a13c | 2013-11-15 13:28:55 -0800 | [diff] [blame] | 66 | import c_gen.type_maps as type_maps |
| 67 | import c_gen.loxi_utils_legacy as loxi_utils |
| 68 | import c_gen.identifiers as identifiers |
Rich Lane | ccae031 | 2013-07-21 23:34:13 -0700 | [diff] [blame] | 69 | import util |
| 70 | import test_data |
Rich Lane | 79c8719 | 2014-03-04 10:56:59 -0800 | [diff] [blame] | 71 | import loxi_globals |
| 72 | from loxi_ir import * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 73 | |
| 74 | def var_name_map(m_type): |
| 75 | """ |
| 76 | Map a type to a generic variable name for the type. |
| 77 | @param m_type The data type |
| 78 | |
| 79 | Used mostly in test code generation, but also for the dup functions. |
| 80 | """ |
| 81 | _var_name_map= dict( |
| 82 | uint8_t="val8", |
| 83 | uint16_t="val16", |
| 84 | uint32_t="val32", |
| 85 | uint64_t="val64", |
Andreas Wundsam | b566a16 | 2013-07-18 19:30:23 -0700 | [diff] [blame] | 86 | of_ipv4_t="ipv4", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 87 | of_port_no_t="port_no", |
| 88 | of_fm_cmd_t="fm_cmd", |
| 89 | of_wc_bmap_t="wc_bmap", |
| 90 | of_match_bmap_t = "match_bmap", |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 91 | of_port_name_t="port_name", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 92 | of_table_name_t="table_name", |
| 93 | of_desc_str_t="desc_str", |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 94 | of_serial_num_t="ser_num", |
Rich Lane | f8a3d00 | 2014-03-19 13:33:52 -0700 | [diff] [blame] | 95 | of_str64_t="str64", |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 96 | of_mac_addr_t="mac_addr", |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 97 | of_ipv6_t="ipv6", |
| 98 | # Non-scalars; more TBD |
| 99 | of_octets_t="octets", |
| 100 | of_meter_features_t="features", |
Dan Talayco | c0e802e | 2013-05-18 23:52:39 -0700 | [diff] [blame] | 101 | of_match_t="match", |
Rich Lane | c247ade | 2014-11-07 09:33:24 -0800 | [diff] [blame] | 102 | of_oxm_t="oxm", |
| 103 | of_bsn_vport_t="bsn_vport", |
Rich Lane | 41b278f | 2014-10-17 18:29:56 -0700 | [diff] [blame] | 104 | of_table_desc_t="table_desc", |
Dan Talayco | c0e802e | 2013-05-18 23:52:39 -0700 | [diff] [blame] | 105 | # BSN extensions |
| 106 | of_bsn_vport_q_in_q_t="vport", |
Rich Lane | 3b2fd83 | 2013-09-24 13:44:08 -0700 | [diff] [blame] | 107 | of_bitmap_128_t="bitmap_128", |
Rich Lane | fab0c82 | 2013-12-30 11:46:48 -0800 | [diff] [blame] | 108 | of_checksum_128_t="checksum_128", |
Dan Talayco | c0e802e | 2013-05-18 23:52:39 -0700 | [diff] [blame] | 109 | ) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 110 | |
| 111 | if m_type.find("of_list_") == 0: |
| 112 | return "list" |
| 113 | if m_type in of_g.of_mixed_types: |
| 114 | return of_g.of_mixed_types[m_type]["short_name"] |
| 115 | return _var_name_map[m_type] |
| 116 | |
| 117 | integer_types = ["uint8_t", "uint16_t", "uint32_t", "uint64_t", |
| 118 | "of_port_no_t", "of_fm_cmd_t", "of_wc_bmap_t", |
Andreas Wundsam | b566a16 | 2013-07-18 19:30:23 -0700 | [diff] [blame] | 119 | "of_match_bmap_t", "of_ipv4_t"] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 120 | string_types = [ "of_port_name_t", "of_table_name_t", |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 121 | "of_desc_str_t", "of_serial_num_t", "of_mac_addr_t", |
Rich Lane | f8a3d00 | 2014-03-19 13:33:52 -0700 | [diff] [blame] | 122 | "of_ipv6_t", "of_bitmap_128_t", "of_checksum_128_t", |
| 123 | "of_str64_t"] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 124 | |
| 125 | scalar_types = integer_types[:] |
| 126 | scalar_types.extend(string_types) |
| 127 | |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 128 | # When embedding an object inside of another object we have to pick a single |
| 129 | # subclass to use, unlike lists where we use all subclasses. |
| 130 | embedded_subclasses = { |
Rich Lane | c247ade | 2014-11-07 09:33:24 -0800 | [diff] [blame] | 131 | 'of_oxm_t': 'of_oxm_eth_type', |
| 132 | 'of_bsn_vport_t': 'of_bsn_vport_q_in_q', |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 135 | def ignore_member(cls, version, m_name, m_type): |
| 136 | """ |
| 137 | Filter out names or types that either don't have accessors |
| 138 | or those that should not be messed with |
| 139 | or whose types we're not ready to deal with yet. |
| 140 | """ |
Rich Lane | 79c8719 | 2014-03-04 10:56:59 -0800 | [diff] [blame] | 141 | |
| 142 | uclass = loxi_globals.unified.class_by_name(cls) |
| 143 | if not uclass: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 144 | return True |
Rich Lane | 353a79f | 2013-11-13 10:39:56 -0800 | [diff] [blame] | 145 | |
Rich Lane | 79c8719 | 2014-03-04 10:56:59 -0800 | [diff] [blame] | 146 | if not isinstance(uclass.member_by_name(m_name), OFDataMember): |
Rich Lane | 353a79f | 2013-11-13 10:39:56 -0800 | [diff] [blame] | 147 | return True |
Rich Lane | 79c8719 | 2014-03-04 10:56:59 -0800 | [diff] [blame] | 148 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 149 | return loxi_utils.skip_member_name(m_name) or m_type not in scalar_types |
| 150 | |
| 151 | def gen_fill_string(out): |
| 152 | out.write(""" |
| 153 | |
| 154 | /** |
| 155 | * The increment to use on values inside a string |
| 156 | */ |
| 157 | #define OF_TEST_STR_INCR 3 |
| 158 | |
| 159 | /** |
| 160 | * Fill in a buffer with incrementing values starting |
| 161 | * at the given offset with the given value |
| 162 | * @param buf The buffer to fill |
| 163 | * @param value The value to use for data |
| 164 | * @param len The number of bytes to fill |
| 165 | */ |
| 166 | |
| 167 | void |
| 168 | of_test_str_fill(uint8_t *buf, int value, int len) |
| 169 | { |
| 170 | int i; |
| 171 | |
| 172 | for (i = 0; i < len; i++) { |
| 173 | *buf = value; |
| 174 | value += OF_TEST_STR_INCR; |
| 175 | buf++; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Given a buffer, verify that it's filled as above |
| 181 | * @param buf The buffer to check |
| 182 | * @param value The value to use for data |
| 183 | * @param len The number of bytes to fill |
| 184 | * @return Boolean True on equality (success) |
| 185 | */ |
| 186 | |
| 187 | int |
| 188 | of_test_str_check(uint8_t *buf, int value, int len) |
| 189 | { |
| 190 | int i; |
| 191 | uint8_t val8; |
| 192 | |
| 193 | val8 = value; |
| 194 | |
| 195 | for (i = 0; i < len; i++) { |
| 196 | if (*buf != val8) { |
| 197 | return 0; |
| 198 | } |
| 199 | val8 += OF_TEST_STR_INCR; |
| 200 | buf++; |
| 201 | } |
| 202 | |
| 203 | return 1; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Global that determines how octets should be populated |
| 208 | * -1 means use value % MAX (below) to determine length |
| 209 | * 0, 1, ... means used that fixed length |
| 210 | * |
| 211 | * Note: Was 16K, but that made objects too big. May add flexibility |
| 212 | * to call populate with a max parameter for length |
| 213 | */ |
| 214 | int octets_pop_style = -1; |
| 215 | #define OCTETS_MAX_VALUE (128) /* 16K was too big */ |
| 216 | #define OCTETS_MULTIPLIER 6367 /* A prime */ |
| 217 | |
| 218 | int |
| 219 | of_octets_populate(of_octets_t *octets, int value) |
| 220 | { |
| 221 | if (octets_pop_style < 0) { |
| 222 | octets->bytes = (value * OCTETS_MULTIPLIER) % OCTETS_MAX_VALUE; |
| 223 | } else { |
| 224 | octets->bytes = octets_pop_style; |
| 225 | } |
| 226 | |
| 227 | if (octets->bytes != 0) { |
| 228 | if ((octets->data = (uint8_t *)MALLOC(octets->bytes)) == NULL) { |
| 229 | return 0; |
| 230 | } |
| 231 | of_test_str_fill(octets->data, value, octets->bytes); |
| 232 | value += 1; |
| 233 | } |
| 234 | |
| 235 | return value; |
| 236 | } |
| 237 | |
| 238 | int |
| 239 | of_octets_check(of_octets_t *octets, int value) |
| 240 | { |
| 241 | int len; |
| 242 | |
| 243 | if (octets_pop_style < 0) { |
| 244 | len = (value * OCTETS_MULTIPLIER) % OCTETS_MAX_VALUE; |
| 245 | TEST_ASSERT(octets->bytes == len); |
| 246 | } else { |
| 247 | TEST_ASSERT(octets->bytes == octets_pop_style); |
| 248 | } |
| 249 | |
| 250 | if (octets->bytes != 0) { |
| 251 | TEST_ASSERT(of_test_str_check(octets->data, value, octets->bytes) |
| 252 | == 1); |
| 253 | value += 1; |
| 254 | } |
| 255 | |
| 256 | return value; |
| 257 | } |
| 258 | |
| 259 | int |
| 260 | of_match_populate(of_match_t *match, of_version_t version, int value) |
| 261 | { |
| 262 | MEMSET(match, 0, sizeof(*match)); |
| 263 | match->version = version; |
| 264 | """) |
| 265 | |
Rich Lane | d56f8d2 | 2014-05-06 14:52:55 -0700 | [diff] [blame] | 266 | def populate_match_version(wire_version, keys): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 267 | out.write(""" |
Rich Lane | d56f8d2 | 2014-05-06 14:52:55 -0700 | [diff] [blame] | 268 | if (version == %d) {\ |
| 269 | """ % wire_version) |
| 270 | for key in keys: |
| 271 | entry = match.of_match_members[key] |
| 272 | out.write(""" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 273 | OF_MATCH_MASK_%(ku)s_EXACT_SET(match); |
| 274 | VAR_%(u_type)s_INIT(match->fields.%(key)s, value); |
| 275 | value += 1; |
Rich Lane | d56f8d2 | 2014-05-06 14:52:55 -0700 | [diff] [blame] | 276 | """ % dict(key=key, u_type=entry["m_type"].upper(), ku=key.upper())) |
| 277 | out.write(""" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Rich Lane | d56f8d2 | 2014-05-06 14:52:55 -0700 | [diff] [blame] | 280 | """) |
| 281 | |
Rich Lane | b4a63a5 | 2014-05-22 14:41:57 -0700 | [diff] [blame] | 282 | for wire_version, match_keys in match.match_keys.items(): |
| 283 | populate_match_version(wire_version, match_keys) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 284 | |
| 285 | out.write(""" |
| 286 | if (value % 2) { |
| 287 | /* Sometimes set ipv4 addr masks to non-exact */ |
| 288 | match->masks.ipv4_src = 0xffff0000; |
| 289 | match->masks.ipv4_dst = 0xfffff800; |
| 290 | } |
Dan Talayco | fb50d38 | 2013-08-05 16:00:17 -0700 | [diff] [blame] | 291 | |
| 292 | /* Restrict values according to masks */ |
Rich Lane | 68798a5 | 2014-04-16 14:57:52 -0700 | [diff] [blame] | 293 | of_memmask(&match->fields, &match->masks, sizeof(match->fields)); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 294 | return value; |
| 295 | } |
| 296 | |
| 297 | int |
| 298 | of_match_check(of_match_t *match, of_version_t version, int value) |
| 299 | { |
| 300 | of_match_t check; |
| 301 | |
| 302 | value = of_match_populate(&check, match->version, value); |
| 303 | TEST_ASSERT(value != 0); |
| 304 | TEST_ASSERT(MEMCMP(match, &check, sizeof(check)) == 0); |
| 305 | |
| 306 | return value; |
| 307 | } |
| 308 | """) |
| 309 | |
| 310 | def gen_common_test_header(out, name): |
| 311 | loxi_utils.gen_c_copy_license(out) |
| 312 | out.write(""" |
| 313 | /* |
| 314 | * Test header file |
| 315 | * |
| 316 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 317 | */ |
| 318 | |
| 319 | #if !defined(_TEST_COMMON_H_) |
| 320 | #define _TEST_COMMON_H_ |
| 321 | |
| 322 | #define DISABLE_WARN_UNUSED_RESULT |
| 323 | #include <loci/loci.h> |
| 324 | #include <locitest/of_dup.h> |
| 325 | #include <locitest/unittest.h> |
| 326 | |
| 327 | extern int global_error; |
| 328 | extern int exit_on_error; |
| 329 | |
| 330 | /* @todo Make option for -k to continue tests if errors */ |
| 331 | #define RUN_TEST(test) do { \\ |
| 332 | int rv; \\ |
| 333 | TESTCASE(test, rv); \\ |
| 334 | if (rv != TEST_PASS) { \\ |
| 335 | global_error=1; \\ |
| 336 | if (exit_on_error) return(1); \\ |
| 337 | } \\ |
| 338 | } while(0) |
| 339 | |
| 340 | #define TEST_OK(op) TEST_ASSERT((op) == OF_ERROR_NONE) |
| 341 | #define TEST_INDIGO_OK(op) TEST_ASSERT((op) == INDIGO_ERROR_NONE) |
| 342 | |
| 343 | /* |
| 344 | * Declarations of functions to populate scalar values in a a class |
| 345 | */ |
| 346 | |
| 347 | extern void of_test_str_fill(uint8_t *buf, int value, int len); |
| 348 | extern int of_test_str_check(uint8_t *buf, int value, int len); |
| 349 | |
| 350 | |
| 351 | extern int of_octets_populate(of_octets_t *octets, int value); |
| 352 | extern int of_octets_check(of_octets_t *octets, int value); |
| 353 | extern int of_match_populate(of_match_t *match, of_version_t version, |
| 354 | int value); |
| 355 | extern int of_match_check(of_match_t *match, of_version_t version, int value); |
| 356 | extern int test_ident_macros(void); |
| 357 | extern int test_dump_objs(void); |
| 358 | |
| 359 | /* In test_match_utils.c */ |
| 360 | extern int test_match_utils(void); |
| 361 | |
| 362 | extern int run_unified_accessor_tests(void); |
| 363 | extern int run_match_tests(void); |
| 364 | extern int run_utility_tests(void); |
| 365 | |
| 366 | extern int run_scalar_acc_tests(void); |
| 367 | extern int run_list_tests(void); |
| 368 | extern int run_message_tests(void); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 369 | |
| 370 | extern int run_validator_tests(void); |
| 371 | |
| 372 | extern int run_list_limits_tests(void); |
| 373 | |
| 374 | extern int test_ext_objs(void); |
Rich Lane | ccae031 | 2013-07-21 23:34:13 -0700 | [diff] [blame] | 375 | extern int test_datafiles(void); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 376 | |
| 377 | """) |
| 378 | |
| 379 | for version in of_g.of_version_range: |
| 380 | for cls in of_g.standard_class_order: |
| 381 | if not loxi_utils.class_in_version(cls, version): |
| 382 | continue |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 383 | if type_maps.class_is_virtual(cls) and not loxi_utils.class_is_list(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 384 | continue |
| 385 | out.write(""" |
| 386 | extern int %(cls)s_%(v_name)s_populate( |
| 387 | %(cls)s_t *obj, int value); |
| 388 | extern int %(cls)s_%(v_name)s_check( |
| 389 | %(cls)s_t *obj, int value); |
| 390 | extern int %(cls)s_%(v_name)s_populate_scalars( |
| 391 | %(cls)s_t *obj, int value); |
| 392 | extern int %(cls)s_%(v_name)s_check_scalars( |
| 393 | %(cls)s_t *obj, int value); |
| 394 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 395 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 396 | out.write("\n#endif /* _TEST_COMMON_H_ */\n") |
| 397 | |
| 398 | def gen_common_test(out, name): |
| 399 | """ |
| 400 | Generate common test content including main |
| 401 | """ |
| 402 | loxi_utils.gen_c_copy_license(out) |
| 403 | out.write(""" |
| 404 | /* |
| 405 | * Common test code for LOCI |
| 406 | * |
| 407 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 408 | */ |
| 409 | |
| 410 | #define DISABLE_WARN_UNUSED_RESULT |
| 411 | #include "loci_log.h" |
| 412 | #include <loci/loci_obj_dump.h> |
| 413 | #include <locitest/unittest.h> |
| 414 | #include <locitest/test_common.h> |
| 415 | |
YAMAMOTO Takashi | 071b591 | 2013-07-02 08:37:39 +0900 | [diff] [blame] | 416 | /* mcheck is a glibc extension */ |
| 417 | #if defined(__linux__) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 418 | #include <mcheck.h> |
| 419 | #define MCHECK_INIT mcheck(NULL) |
YAMAMOTO Takashi | 071b591 | 2013-07-02 08:37:39 +0900 | [diff] [blame] | 420 | #else |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 421 | #define MCHECK_INIT do { } while (0) |
| 422 | #endif |
| 423 | |
| 424 | /** |
| 425 | * Exit on error if set to 1 |
| 426 | */ |
| 427 | int exit_on_error = 1; |
| 428 | |
| 429 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 430 | * Global error state: 0 is okay, 1 is error |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 431 | */ |
| 432 | int global_error = 0; |
| 433 | |
| 434 | extern int run_unified_accessor_tests(void); |
| 435 | extern int run_match_tests(void); |
| 436 | extern int run_utility_tests(void); |
| 437 | |
| 438 | extern int run_scalar_acc_tests(void); |
| 439 | extern int run_list_tests(void); |
| 440 | extern int run_message_tests(void); |
| 441 | |
| 442 | /** |
| 443 | * Macros for initializing and checking scalar types |
| 444 | * |
| 445 | * @param var The variable being initialized or checked |
| 446 | * @param val The integer value to set/check against, see below |
| 447 | * |
| 448 | * Note that equality means something special for strings. Each byte |
| 449 | * is initialized to an incrementing value. So check is done against that. |
| 450 | * |
| 451 | */ |
| 452 | |
| 453 | """) |
| 454 | for t in scalar_types: |
| 455 | if t in integer_types: |
| 456 | out.write(""" |
| 457 | #define VAR_%s_INIT(var, val) var = (%s)(val) |
| 458 | #define VAR_%s_CHECK(var, val) ((var) == (%s)(val)) |
| 459 | """ % (t.upper(), t, t.upper(), t)) |
| 460 | else: |
| 461 | out.write(""" |
| 462 | #define VAR_%s_INIT(var, val) \\ |
| 463 | of_test_str_fill((uint8_t *)&(var), val, sizeof(var)) |
| 464 | #define VAR_%s_CHECK(var, val) \\ |
| 465 | of_test_str_check((uint8_t *)&(var), val, sizeof(var)) |
| 466 | """ % (t.upper(), t.upper())) |
| 467 | |
| 468 | gen_fill_string(out) |
| 469 | gen_scalar_set_check_funs(out) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 470 | gen_unified_accessor_funs(out) |
| 471 | |
| 472 | gen_ident_tests(out) |
| 473 | gen_log_test(out) |
| 474 | |
| 475 | def gen_message_scalar_test(out, name): |
| 476 | """ |
| 477 | Generate test cases for message objects, scalar accessors |
| 478 | """ |
| 479 | |
| 480 | loxi_utils.gen_c_copy_license(out) |
| 481 | out.write(""" |
| 482 | /** |
| 483 | * |
| 484 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 485 | * |
| 486 | * Message-scalar tests for all versions |
| 487 | */ |
| 488 | |
| 489 | #include <locitest/test_common.h> |
| 490 | """) |
| 491 | for version in of_g.of_version_range: |
| 492 | v_name = loxi_utils.version_to_name(version) |
| 493 | out.write(""" |
| 494 | /** |
| 495 | * Message-scalar tests for version %s |
| 496 | */ |
| 497 | """ % v_name) |
| 498 | for cls in of_g.standard_class_order: |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 499 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 500 | continue |
| 501 | if version in of_g.unified[cls]: |
| 502 | message_scalar_test(out, version, cls) |
| 503 | |
| 504 | out.write(""" |
| 505 | int |
| 506 | run_scalar_acc_tests(void) |
| 507 | { |
| 508 | """) |
| 509 | for version in of_g.of_version_range: |
| 510 | v_name = loxi_utils.version_to_name(version) |
| 511 | for cls in of_g.standard_class_order: |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 512 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 513 | continue |
| 514 | if version in of_g.unified[cls]: |
| 515 | test_name = "%s_%s" % (cls, v_name) |
| 516 | out.write(" RUN_TEST(%s_scalar);\n" % test_name) |
| 517 | |
| 518 | out.write(" return TEST_PASS;\n}\n"); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 519 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 520 | def message_scalar_test(out, version, cls): |
| 521 | """ |
| 522 | Generate one test case for the given version and class |
| 523 | """ |
| 524 | |
| 525 | members, member_types = scalar_member_types_get(cls, version) |
Rich Lane | e3d3fb5 | 2014-10-16 12:45:17 -0700 | [diff] [blame] | 526 | length = of_g.base_length[(cls, version)] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 527 | v_name = loxi_utils.version_to_name(version) |
| 528 | |
| 529 | out.write(""" |
| 530 | static int |
| 531 | test_%(cls)s_%(v_name)s_scalar(void) |
| 532 | { |
| 533 | %(cls)s_t *obj; |
| 534 | |
| 535 | obj = %(cls)s_new(%(v_name)s); |
| 536 | TEST_ASSERT(obj != NULL); |
| 537 | TEST_ASSERT(obj->version == %(v_name)s); |
| 538 | TEST_ASSERT(obj->length == %(length)d); |
| 539 | TEST_ASSERT(obj->parent == NULL); |
| 540 | TEST_ASSERT(obj->object_id == %(u_cls)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 541 | """ % dict(cls=cls, u_cls=cls.upper(), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 542 | v_name=v_name, length=length, version=version)) |
Rich Lane | a4b6830 | 2014-03-12 15:17:58 -0700 | [diff] [blame] | 543 | |
| 544 | # If this class is a concrete member of an inheritance hierarchy, |
| 545 | # run the hierarchy's root wire type parser and assert it returns |
| 546 | # the expected object id. |
| 547 | ofclass = loxi_globals.unified.class_by_name(cls) |
| 548 | if ofclass and not ofclass.virtual: |
| 549 | root = ofclass |
| 550 | while root.superclass: |
| 551 | root = root.superclass |
| 552 | if root.virtual: |
| 553 | out.write(""" |
| 554 | { |
| 555 | of_object_id_t object_id; |
| 556 | %(root_cls)s_wire_object_id_get(obj, &object_id); |
| 557 | TEST_ASSERT(object_id == %(u_cls)s); |
| 558 | } |
| 559 | """ % dict(root_cls=root.name, u_cls=cls.upper())) |
| 560 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 561 | if not type_maps.class_is_virtual(cls): |
| 562 | out.write(""" |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 563 | if (loci_class_metadata[obj->object_id].wire_length_get != NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 564 | int length; |
| 565 | |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 566 | loci_class_metadata[obj->object_id].wire_length_get((of_object_t *)obj, &length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 567 | TEST_ASSERT(length == %(length)d); |
| 568 | } |
| 569 | |
| 570 | /* Set up incrementing values for scalar members */ |
| 571 | %(cls)s_%(v_name)s_populate_scalars(obj, 1); |
| 572 | |
| 573 | /* Check values just set */ |
| 574 | TEST_ASSERT(%(cls)s_%(v_name)s_check_scalars(obj, 1) != 0); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 575 | """ % dict(cls=cls, u_cls=cls.upper(), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 576 | v_name=v_name, length=length, version=version)) |
| 577 | |
| 578 | out.write(""" |
| 579 | %(cls)s_delete(obj); |
| 580 | |
| 581 | /* To do: Check memory */ |
| 582 | return TEST_PASS; |
| 583 | } |
| 584 | """ % dict(cls=cls)) |
| 585 | |
| 586 | # Get the members and list of scalar types for members of a given class |
| 587 | def scalar_member_types_get(cls, version): |
| 588 | member_types = [] |
| 589 | |
| 590 | if not version in of_g.unified[cls]: |
| 591 | return ([], []) |
| 592 | |
| 593 | if "use_version" in of_g.unified[cls][version]: |
| 594 | v = of_g.unified[cls][version]["use_version"] |
| 595 | members = of_g.unified[cls][v]["members"] |
| 596 | else: |
| 597 | members = of_g.unified[cls][version]["members"] |
| 598 | # Accumulate variables that are supported |
| 599 | for member in members: |
| 600 | m_type = member["m_type"] |
| 601 | m_name = member["name"] |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 602 | if (not loxi_utils.type_is_scalar(m_type) or |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 603 | ignore_member(cls, version, m_name, m_type)): |
| 604 | continue |
| 605 | if not m_type in member_types: |
| 606 | member_types.append(m_type) |
| 607 | |
| 608 | return (members, member_types) |
| 609 | |
| 610 | def scalar_funs_instance(out, cls, version, members, member_types): |
| 611 | """ |
| 612 | Generate one instance of scalar set/check functions |
| 613 | """ |
| 614 | out.write(""" |
| 615 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 616 | * Populate the scalar values in obj of type %(cls)s, |
| 617 | * version %(v_name)s |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 618 | * @param obj Pointer to an object to populate |
| 619 | * @param value The seed value to use in populating the object |
| 620 | * @returns The value after increments for this object's values |
| 621 | */ |
| 622 | int %(cls)s_%(v_name)s_populate_scalars( |
| 623 | %(cls)s_t *obj, int value) { |
| 624 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 625 | # Declare string types |
| 626 | for t in member_types: |
| 627 | out.write(" %s %s;\n" % (t, var_name_map(t))) |
| 628 | for member in members: |
| 629 | m_type = member["m_type"] |
| 630 | m_name = member["name"] |
| 631 | if (not loxi_utils.type_is_scalar(m_type) or |
| 632 | ignore_member(cls, version, m_name, m_type)): |
| 633 | continue |
| 634 | v_name = var_name_map(m_type); |
| 635 | out.write(""" |
| 636 | VAR_%(u_type)s_INIT(%(v_name)s, value); |
| 637 | %(cls)s_%(m_name)s_set(obj, %(v_name)s); |
| 638 | value += 1; |
| 639 | """ % dict(cls=cls, m_name=m_name, u_type=m_type.upper(), v_name=v_name)) |
| 640 | out.write(""" |
| 641 | return value; |
| 642 | } |
| 643 | """) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 644 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 645 | out.write(""" |
| 646 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 647 | * Check scalar values in obj of type %(cls)s, |
| 648 | * version %(v_name)s |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 649 | * @param obj Pointer to an object to check |
| 650 | * @param value Starting value for checking |
| 651 | * @returns The value after increments for this object's values |
| 652 | */ |
| 653 | int %(cls)s_%(v_name)s_check_scalars( |
| 654 | %(cls)s_t *obj, int value) { |
| 655 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 656 | |
| 657 | for t in member_types: |
| 658 | out.write(" %s %s;\n" % (t, var_name_map(t))) |
| 659 | for member in members: |
| 660 | m_type = member["m_type"] |
| 661 | m_name = member["name"] |
| 662 | if (not loxi_utils.type_is_scalar(m_type) or |
| 663 | ignore_member(cls, version, m_name, m_type)): |
| 664 | continue |
| 665 | v_name = var_name_map(m_type); |
| 666 | out.write(""" |
| 667 | %(cls)s_%(m_name)s_get(obj, &%(v_name)s); |
| 668 | TEST_ASSERT(VAR_%(u_type)s_CHECK(%(v_name)s, value)); |
| 669 | value += 1; |
| 670 | """ % dict(cls=cls, m_name=m_name, u_type=m_type.upper(), v_name=v_name)) |
| 671 | |
| 672 | out.write(""" |
| 673 | return value; |
| 674 | } |
| 675 | |
| 676 | """) |
| 677 | |
| 678 | def gen_scalar_set_check_funs(out): |
| 679 | """ |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 680 | For each object class with scalar members, generate functions that |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 681 | set and check their values |
| 682 | """ |
| 683 | for version in of_g.of_version_range: |
| 684 | for cls in of_g.standard_class_order: |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 685 | if type_maps.class_is_virtual(cls): |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 686 | continue |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 687 | (members, member_types) = scalar_member_types_get(cls, version) |
| 688 | scalar_funs_instance(out, cls, version, members, member_types) |
| 689 | |
| 690 | |
| 691 | # Helper function to set up a subclass instance for a test |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 692 | def setup_instance(out, cls, subcls, instance, v_name, version): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 693 | base_type = loxi_utils.list_to_entry_type(cls) |
| 694 | setup_template = """ |
| 695 | %(subcls)s_init(%(inst)s, %(v_name)s, -1, 1); |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 696 | %(cls)s_append_bind(list, %(inst)s); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 697 | value = %(subcls)s_%(v_name)s_populate( |
| 698 | %(inst)s, value); |
| 699 | cur_len += %(inst)s->length; |
| 700 | TEST_ASSERT(list->length == cur_len); |
| 701 | """ |
| 702 | out.write(""" |
| 703 | /* Append two instances of type %s */ |
| 704 | """ % subcls) |
| 705 | for i in range(2): |
| 706 | out.write(setup_template % |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 707 | dict(inst=instance, subcls=subcls, v_name=v_name, |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 708 | base_type=base_type, cls=cls, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 709 | version=version)) |
| 710 | |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 711 | def check_instance(out, cls, subcls, instance, v_name, version, last): |
| 712 | check_template = """ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 713 | TEST_ASSERT(%(inst)s->object_id == %(elt_name)s); |
| 714 | value = %(subcls)s_%(v_name)s_check( |
| 715 | %(inst)s, value); |
| 716 | TEST_ASSERT(value != 0); |
| 717 | """ |
| 718 | out.write("\n /* Check two instances of type %s */" % instance) |
| 719 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 720 | out.write(check_template % |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 721 | dict(elt_name=loxi_utils.enum_name(subcls), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 722 | inst=instance, subcls=subcls, |
| 723 | v_name=loxi_utils.version_to_name(version))) |
| 724 | out.write("""\ |
| 725 | TEST_OK(%(cls)s_next(list, &elt)); |
| 726 | """ % dict(cls=cls)) |
| 727 | |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 728 | out.write(check_template % |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 729 | dict(elt_name=loxi_utils.enum_name(subcls), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 730 | inst=instance, subcls=subcls, |
| 731 | v_name=loxi_utils.version_to_name(version))) |
| 732 | if last: |
| 733 | out.write("""\ |
| 734 | TEST_ASSERT(%(cls)s_next(list, &elt) == OF_ERROR_RANGE); |
| 735 | """ % dict(cls=cls)) |
| 736 | else: |
| 737 | out.write("""\ |
| 738 | TEST_OK(%(cls)s_next(list, &elt)); |
| 739 | """ % dict(cls=cls)) |
| 740 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 741 | # Maybe: Get a map from list class to parent, mem_name of container |
| 742 | |
| 743 | def list_test(out, version, cls): |
| 744 | out.write(""" |
| 745 | static int |
| 746 | test_%(cls)s_%(v_name)s(void) |
| 747 | { |
| 748 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 749 | base_type = loxi_utils.list_to_entry_type(cls) |
| 750 | |
| 751 | out.write(""" %(cls)s_t *list; |
| 752 | int value = 1; |
| 753 | """ % dict(cls=cls, base_type=base_type)) |
| 754 | |
| 755 | out.write(""" |
| 756 | list = %(cls)s_new(%(v_name)s); |
| 757 | TEST_ASSERT(list != NULL); |
| 758 | TEST_ASSERT(list->version == %(v_name)s); |
| 759 | TEST_ASSERT(list->length == 0); |
| 760 | TEST_ASSERT(list->parent == NULL); |
| 761 | TEST_ASSERT(list->object_id == %(enum_cls)s); |
| 762 | |
Rich Lane | c6e97f7 | 2014-12-03 12:08:25 -0800 | [diff] [blame] | 763 | value = %(cls)s_%(v_name)s_populate(list, value); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 764 | TEST_ASSERT(value != 0); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 765 | """ % dict(cls=cls, base_type=base_type, v_name=loxi_utils.version_to_name(version), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 766 | enum_cls=loxi_utils.enum_name(cls))) |
| 767 | |
| 768 | out.write(""" |
| 769 | /* Now check values */ |
| 770 | value = 1; |
Rich Lane | c6e97f7 | 2014-12-03 12:08:25 -0800 | [diff] [blame] | 771 | value = %(cls)s_%(v_name)s_check(list, value); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 772 | TEST_ASSERT(value != 0); |
| 773 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 774 | |
| 775 | out.write(""" |
| 776 | %(cls)s_delete(list); |
| 777 | |
| 778 | return TEST_PASS; |
| 779 | } |
| 780 | """ % dict(cls=cls)) |
| 781 | |
| 782 | def gen_list_test(out, name): |
| 783 | """ |
| 784 | Generate base line test cases for lists |
| 785 | @param out The file handle to write to |
| 786 | """ |
| 787 | |
| 788 | loxi_utils.gen_c_copy_license(out) |
| 789 | out.write(""" |
| 790 | /** |
| 791 | * |
| 792 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 793 | * |
| 794 | * Message-scalar tests for all versions |
| 795 | */ |
| 796 | |
| 797 | #include <locitest/test_common.h> |
| 798 | """) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 799 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 800 | for version in of_g.of_version_range: |
| 801 | v_name = loxi_utils.version_to_name(version) |
| 802 | out.write(""" |
| 803 | /** |
| 804 | * Baseline list tests for version %s |
| 805 | */ |
| 806 | """ % v_name) |
| 807 | for cls in of_g.ordered_list_objects: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 808 | if version in of_g.unified[cls]: |
| 809 | list_test(out, version, cls) |
| 810 | |
| 811 | out.write(""" |
| 812 | int |
| 813 | run_list_tests(void) |
| 814 | { |
| 815 | """) |
| 816 | for version in of_g.of_version_range: |
| 817 | v_name = loxi_utils.version_to_name(version) |
| 818 | for cls in of_g.ordered_list_objects: |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 819 | if version in of_g.unified[cls]: |
| 820 | test_name = "%s_%s" % (cls, v_name) |
| 821 | out.write(" RUN_TEST(%s);\n" % test_name) |
| 822 | |
| 823 | out.write("\n return TEST_PASS;\n}\n"); |
| 824 | |
| 825 | def gen_match_test(out, name): |
| 826 | """ |
| 827 | Generate baseline tests for match functions |
| 828 | """ |
| 829 | |
| 830 | loxi_utils.gen_c_copy_license(out) |
| 831 | out.write("""\ |
| 832 | /** |
| 833 | * |
| 834 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 835 | * |
| 836 | * Message-scalar tests for all versions |
| 837 | * @fixme These are mostly hard coded now. |
| 838 | */ |
| 839 | |
| 840 | #include <locitest/test_common.h> |
| 841 | |
| 842 | static int |
| 843 | test_match_1(void) |
| 844 | { |
Rich Lane | f87f4c3 | 2014-10-14 11:56:02 -0700 | [diff] [blame] | 845 | """) |
| 846 | |
| 847 | for version in of_g.of_version_range: |
| 848 | out.write(" of_match_v%(v)d_t *m_v%(v)d;\n" % dict(v=version)) |
| 849 | |
| 850 | out.write("""\ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 851 | of_match_t match; |
| 852 | int value = 1; |
| 853 | int idx; |
| 854 | uint32_t exp_value; |
| 855 | |
| 856 | /* Verify default values for ip mask map */ |
Rich Lane | 70c7094 | 2014-05-06 15:31:47 -0700 | [diff] [blame] | 857 | for (idx = 0; idx < 64; idx++) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 858 | exp_value = (idx < 32) ? ~((1 << idx) - 1) : 0; |
| 859 | TEST_ASSERT(of_ip_index_to_mask(idx) == exp_value); |
| 860 | if (idx < 32) { |
| 861 | TEST_ASSERT(of_ip_mask_to_index(exp_value) == idx); |
| 862 | } |
| 863 | } |
| 864 | """) |
| 865 | |
| 866 | for version in of_g.of_version_range: |
| 867 | out.write(""" |
| 868 | /* Create/populate/convert and delete for version %(v_name)s */ |
| 869 | m_v%(version)d = of_match_v%(version)d_new(%(v_name)s); |
| 870 | TEST_ASSERT(m_v%(version)d != NULL); |
| 871 | TEST_ASSERT((value = of_match_populate(&match, %(v_name)s, value)) > 0); |
| 872 | TEST_OK(of_match_to_wire_match_v%(version)d(&match, m_v%(version)d)); |
| 873 | of_match_v%(version)d_delete(m_v%(version)d); |
| 874 | """ % dict(v_name=loxi_utils.version_to_name(version), version=version)) |
| 875 | |
| 876 | out.write(""" |
| 877 | return TEST_PASS; |
| 878 | } |
| 879 | """) |
| 880 | |
| 881 | out.write(""" |
| 882 | static int |
| 883 | test_match_2(void) |
| 884 | { |
Rich Lane | f87f4c3 | 2014-10-14 11:56:02 -0700 | [diff] [blame] | 885 | """) |
| 886 | |
| 887 | for version in of_g.of_version_range: |
| 888 | out.write(" of_match_v%(v)d_t *m_v%(v)d;\n" % dict(v=version)) |
| 889 | |
| 890 | out.write("""\ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 891 | of_match_t match1; |
| 892 | of_match_t match2; |
| 893 | int value = 1; |
| 894 | """) |
| 895 | |
| 896 | for version in of_g.of_version_range: |
| 897 | out.write(""" |
| 898 | TEST_ASSERT((value = of_match_populate(&match1, %(v_name)s, value)) > 0); |
| 899 | m_v%(version)d = of_match_v%(version)d_new(%(v_name)s); |
| 900 | TEST_ASSERT(m_v%(version)d != NULL); |
| 901 | TEST_OK(of_match_to_wire_match_v%(version)d(&match1, m_v%(version)d)); |
| 902 | TEST_OK(of_match_v%(version)d_to_match(m_v%(version)d, &match2)); |
| 903 | TEST_ASSERT(memcmp(&match1, &match2, sizeof(match1)) == 0); |
| 904 | of_match_v%(version)d_delete(m_v%(version)d); |
| 905 | """ % dict(v_name=loxi_utils.version_to_name(version), version=version)) |
| 906 | |
| 907 | out.write(""" |
| 908 | return TEST_PASS; |
| 909 | } |
| 910 | """) |
| 911 | |
| 912 | out.write(""" |
| 913 | static int |
| 914 | test_match_3(void) |
| 915 | { |
| 916 | of_match_t match1; |
| 917 | of_match_t match2; |
| 918 | int value = 1; |
| 919 | of_octets_t octets; |
Rich Lane | e0b70cc | 2014-06-12 15:02:22 -0700 | [diff] [blame] | 920 | of_object_storage_t storage; |
| 921 | memset(&storage, 0, sizeof(storage)); |
| 922 | storage.obj.wbuf = &storage.wbuf; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 923 | """) |
| 924 | for version in of_g.of_version_range: |
| 925 | out.write(""" |
| 926 | /* Serialize to version %(v_name)s */ |
| 927 | TEST_ASSERT((value = of_match_populate(&match1, %(v_name)s, value)) > 0); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 928 | TEST_ASSERT(of_match_serialize(%(v_name)s, &match1, &octets) == |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 929 | OF_ERROR_NONE); |
Rich Lane | e0b70cc | 2014-06-12 15:02:22 -0700 | [diff] [blame] | 930 | storage.obj.wbuf->buf = octets.data; |
| 931 | storage.obj.wbuf->alloc_bytes = octets.bytes; |
| 932 | storage.obj.wbuf->current_bytes = octets.bytes; |
| 933 | TEST_ASSERT(of_match_deserialize(%(v_name)s, &match2, &storage.obj, 0, octets.bytes) == |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 934 | OF_ERROR_NONE); |
| 935 | TEST_ASSERT(memcmp(&match1, &match2, sizeof(match1)) == 0); |
| 936 | FREE(octets.data); |
| 937 | """ % dict(v_name=loxi_utils.version_to_name(version), version=version)) |
| 938 | |
| 939 | out.write(""" |
| 940 | return TEST_PASS; |
| 941 | } |
| 942 | """) |
| 943 | |
| 944 | out.write(""" |
| 945 | int run_match_tests(void) |
| 946 | { |
| 947 | RUN_TEST(match_1); |
| 948 | RUN_TEST(match_2); |
| 949 | RUN_TEST(match_3); |
| 950 | RUN_TEST(match_utils); |
| 951 | |
| 952 | return TEST_PASS; |
| 953 | } |
| 954 | """) |
| 955 | |
| 956 | def gen_msg_test(out, name): |
| 957 | loxi_utils.gen_c_copy_license(out) |
| 958 | out.write(""" |
| 959 | /** |
| 960 | * |
| 961 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 962 | * |
| 963 | * Message-scalar tests for all versions |
| 964 | */ |
| 965 | |
| 966 | #include <locitest/test_common.h> |
| 967 | """) |
| 968 | for version in of_g.of_version_range: |
| 969 | for cls in of_g.ordered_messages: |
| 970 | if not (cls, version) in of_g.base_length: |
| 971 | continue |
Rich Lane | 488b3c5 | 2013-06-21 18:11:02 -0700 | [diff] [blame] | 972 | if type_maps.class_is_virtual(cls): |
| 973 | continue |
Rich Lane | e3d3fb5 | 2014-10-16 12:45:17 -0700 | [diff] [blame] | 974 | bytes = of_g.base_length[(cls, version)] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 975 | out.write(""" |
| 976 | static int |
| 977 | test_%(cls)s_create_%(v_name)s(void) |
| 978 | { |
| 979 | %(cls)s_t *obj; |
| 980 | uint8_t *msg_buf; |
| 981 | int value; |
Rich Lane | a4b6830 | 2014-03-12 15:17:58 -0700 | [diff] [blame] | 982 | of_object_id_t object_id; |
Rich Lane | bb8f17c | 2014-06-12 13:14:09 -0700 | [diff] [blame] | 983 | int len; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 984 | |
| 985 | obj = %(cls)s_new(%(v_name)s); |
| 986 | TEST_ASSERT(obj != NULL); |
| 987 | TEST_ASSERT(obj->version == %(v_name)s); |
| 988 | TEST_ASSERT(obj->length == %(bytes)d); |
| 989 | TEST_ASSERT(obj->parent == NULL); |
| 990 | TEST_ASSERT(obj->object_id == %(enum)s); |
| 991 | |
Rich Lane | a4b6830 | 2014-03-12 15:17:58 -0700 | [diff] [blame] | 992 | of_header_wire_object_id_get(obj, &object_id); |
| 993 | TEST_ASSERT(object_id == %(enum)s); |
| 994 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 995 | /* Set up incrementing values for scalar members */ |
| 996 | value = %(cls)s_%(v_name)s_populate_scalars(obj, 1); |
| 997 | TEST_ASSERT(value != 0); |
| 998 | |
Rich Lane | bb8f17c | 2014-06-12 13:14:09 -0700 | [diff] [blame] | 999 | len = obj->length; |
| 1000 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1001 | /* Grab the underlying buffer from the message */ |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1002 | of_object_wire_buffer_steal((of_object_t *)obj, &msg_buf); |
| 1003 | TEST_ASSERT(msg_buf != NULL); |
| 1004 | %(cls)s_delete(obj); |
Rich Lane | bb8f17c | 2014-06-12 13:14:09 -0700 | [diff] [blame] | 1005 | obj = of_object_new_from_message(OF_BUFFER_TO_MESSAGE(msg_buf), len); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1006 | |
| 1007 | TEST_ASSERT(obj != NULL); |
| 1008 | |
| 1009 | /* @fixme Set up all message objects (recursively?) */ |
| 1010 | |
| 1011 | value = %(cls)s_%(v_name)s_check_scalars(obj, 1); |
| 1012 | TEST_ASSERT(value != 0); |
| 1013 | |
| 1014 | %(cls)s_delete(obj); |
| 1015 | |
| 1016 | return TEST_PASS; |
| 1017 | } |
| 1018 | """ % dict(cls=cls, version=version, enum=loxi_utils.enum_name(cls), |
| 1019 | v_name=loxi_utils.version_to_name(version), bytes=bytes)) |
| 1020 | |
| 1021 | out.write(""" |
| 1022 | int |
| 1023 | run_message_tests(void) |
| 1024 | { |
| 1025 | """) |
| 1026 | for version in of_g.of_version_range: |
| 1027 | for cls in of_g.ordered_messages: |
| 1028 | if not (cls, version) in of_g.base_length: |
| 1029 | continue |
Rich Lane | 488b3c5 | 2013-06-21 18:11:02 -0700 | [diff] [blame] | 1030 | if type_maps.class_is_virtual(cls): |
| 1031 | continue |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1032 | test_name = "%s_create_%s" % (cls, loxi_utils.version_to_name(version)) |
| 1033 | out.write(" RUN_TEST(%s);\n" % test_name) |
| 1034 | |
| 1035 | out.write("\n return TEST_PASS;\n}\n"); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1036 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1037 | |
| 1038 | def gen_list_setup_check(out, cls, version): |
| 1039 | """ |
| 1040 | Generate functions that populate and check a list with two |
| 1041 | of each type of subclass it supports |
| 1042 | """ |
| 1043 | out.write(""" |
| 1044 | /** |
| 1045 | * Populate a list of type %(cls)s with two of each type of subclass |
| 1046 | * @param list Pointer to the list to be populated |
| 1047 | * @param value The seed value to use in populating the list |
| 1048 | * @returns The value after increments for this object's values |
| 1049 | */ |
| 1050 | int |
| 1051 | %(cls)s_%(v_name)s_populate( |
| 1052 | %(cls)s_t *list, int value) |
| 1053 | { |
| 1054 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 1055 | base_type = loxi_utils.list_to_entry_type(cls) |
| 1056 | out.write(""" |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1057 | of_object_t elt; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1058 | int cur_len = 0; |
Rich Lane | ad70803 | 2014-12-03 12:40:58 -0800 | [diff] [blame] | 1059 | static int recursion; |
Rich Lane | e98ee5d | 2014-10-14 10:43:44 -0700 | [diff] [blame] | 1060 | (void) elt; |
| 1061 | (void) cur_len; |
Rich Lane | ad70803 | 2014-12-03 12:40:58 -0800 | [diff] [blame] | 1062 | |
| 1063 | if (recursion > 0) { |
| 1064 | return value; |
| 1065 | } |
| 1066 | |
| 1067 | recursion++; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1068 | """ % dict(cls=cls, base_type=base_type)) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1069 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1070 | sub_classes = type_maps.sub_class_map(base_type, version) |
Rich Lane | 1bd2bae | 2013-07-09 10:06:07 -0700 | [diff] [blame] | 1071 | sub_classes = [(instance, subcls) for (instance, subcls) in sub_classes if not type_maps.class_is_virtual(subcls)] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1072 | v_name = loxi_utils.version_to_name(version) |
| 1073 | |
Rich Lane | e98ee5d | 2014-10-14 10:43:44 -0700 | [diff] [blame] | 1074 | if not type_maps.class_is_virtual(base_type): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1075 | out.write(" /* No subclasses for %s */\n"% base_type) |
| 1076 | out.write(" %s_t *elt_p;\n" % base_type) |
| 1077 | out.write("\n elt_p = &elt;\n") |
| 1078 | else: |
| 1079 | out.write(" /* Declare pointers for each subclass */\n") |
| 1080 | for instance, subcls in sub_classes: |
| 1081 | out.write(" %s_t *%s;\n" % (subcls, instance)) |
| 1082 | out.write("\n /* Instantiate pointers for each subclass */\n") |
| 1083 | for instance, subcls in sub_classes: |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1084 | out.write(" %s = &elt;\n" % (instance)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1085 | |
Rich Lane | e98ee5d | 2014-10-14 10:43:44 -0700 | [diff] [blame] | 1086 | if not type_maps.class_is_virtual(base_type): # No inheritance case |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 1087 | setup_instance(out, cls, base_type, "elt_p", v_name, version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1088 | else: |
| 1089 | for instance, subcls in sub_classes: |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 1090 | setup_instance(out, cls, subcls, instance, v_name, version) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1091 | out.write(""" |
Rich Lane | ad70803 | 2014-12-03 12:40:58 -0800 | [diff] [blame] | 1092 | recursion--; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1093 | return value; |
| 1094 | } |
| 1095 | """) |
| 1096 | out.write(""" |
| 1097 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1098 | * Check a list of type %(cls)s generated by |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1099 | * %(cls)s_%(v_name)s_populate |
| 1100 | * @param list Pointer to the list that was populated |
| 1101 | * @param value Starting value for checking |
| 1102 | * @returns The value after increments for this object's values |
| 1103 | */ |
| 1104 | int |
| 1105 | %(cls)s_%(v_name)s_check( |
| 1106 | %(cls)s_t *list, int value) |
| 1107 | { |
| 1108 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 1109 | base_type = loxi_utils.list_to_entry_type(cls) |
| 1110 | out.write(""" |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1111 | of_object_t elt; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1112 | int count = 0; |
| 1113 | int rv; |
Rich Lane | ad70803 | 2014-12-03 12:40:58 -0800 | [diff] [blame] | 1114 | static int recursion; |
| 1115 | |
| 1116 | if (recursion > 0) { |
| 1117 | return value; |
| 1118 | } |
| 1119 | |
| 1120 | recursion++; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1121 | """ % dict(cls=cls, base_type=base_type)) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1122 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1123 | |
| 1124 | sub_classes = type_maps.sub_class_map(base_type, version) |
Rich Lane | 1bd2bae | 2013-07-09 10:06:07 -0700 | [diff] [blame] | 1125 | sub_classes = [(instance, subcls) for (instance, subcls) in sub_classes if not type_maps.class_is_virtual(subcls)] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1126 | v_name = loxi_utils.version_to_name(version) |
| 1127 | |
Rich Lane | e98ee5d | 2014-10-14 10:43:44 -0700 | [diff] [blame] | 1128 | if not type_maps.class_is_virtual(base_type): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1129 | entry_count = 2 |
| 1130 | out.write(" /* No subclasses for %s */\n"% base_type) |
| 1131 | out.write(" %s_t *elt_p;\n" % base_type) |
| 1132 | out.write("\n elt_p = &elt;\n") |
| 1133 | else: |
| 1134 | entry_count = 2 * len(sub_classes) # Two of each type appended |
| 1135 | out.write(" /* Declare pointers for each subclass */\n") |
| 1136 | for instance, subcls in sub_classes: |
| 1137 | out.write(" %s_t *%s;\n" % (subcls, instance)) |
| 1138 | out.write("\n /* Instantiate pointers for each subclass */\n") |
| 1139 | for instance, subcls in sub_classes: |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1140 | out.write(" %s = &elt;\n" % (instance)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1141 | |
Rich Lane | e98ee5d | 2014-10-14 10:43:44 -0700 | [diff] [blame] | 1142 | if not type_maps.class_is_virtual(base_type) or sub_classes: |
| 1143 | out.write(" TEST_OK(%(cls)s_first(list, &elt));\n" % dict(cls=cls)) |
| 1144 | |
| 1145 | if not type_maps.class_is_virtual(base_type): # No inheritance case |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 1146 | check_instance(out, cls, base_type, "elt_p", v_name, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1147 | version, True) |
| 1148 | else: |
| 1149 | count = 0 |
| 1150 | for instance, subcls in sub_classes: |
| 1151 | count += 1 |
Rich Lane | 9afc3b9 | 2014-04-09 22:55:53 -0700 | [diff] [blame] | 1152 | check_instance(out, cls, subcls, instance, v_name, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1153 | version, count==len(sub_classes)) |
| 1154 | out.write(""" |
| 1155 | """ % dict(base_type=base_type)) |
| 1156 | |
| 1157 | out.write(""" |
| 1158 | /* Do an iterate to test the iterator */ |
| 1159 | %(u_cls)s_ITER(list, &elt, rv) { |
| 1160 | count += 1; |
| 1161 | } |
| 1162 | |
| 1163 | TEST_ASSERT(rv == OF_ERROR_RANGE); |
| 1164 | TEST_ASSERT(count == %(entry_count)d); |
| 1165 | |
| 1166 | /* We shoehorn a test of the dup functions here */ |
| 1167 | { |
| 1168 | %(cls)s_t *dup; |
| 1169 | |
| 1170 | TEST_ASSERT((dup = %(cls)s_dup(list)) != NULL); |
| 1171 | TEST_ASSERT(dup->length == list->length); |
| 1172 | TEST_ASSERT(dup->object_id == list->object_id); |
| 1173 | TEST_ASSERT(dup->version == list->version); |
| 1174 | TEST_ASSERT(MEMCMP(OF_OBJECT_BUFFER_INDEX(dup, 0), |
| 1175 | OF_OBJECT_BUFFER_INDEX(list, 0), list->length) == 0); |
| 1176 | of_object_delete((of_object_t *)dup); |
| 1177 | |
| 1178 | /* And now for the generic dup function */ |
| 1179 | TEST_ASSERT((dup = (%(cls)s_t *) |
| 1180 | of_object_dup(list)) != NULL); |
| 1181 | TEST_ASSERT(dup->length == list->length); |
| 1182 | TEST_ASSERT(dup->object_id == list->object_id); |
| 1183 | TEST_ASSERT(dup->version == list->version); |
| 1184 | TEST_ASSERT(MEMCMP(OF_OBJECT_BUFFER_INDEX(dup, 0), |
| 1185 | OF_OBJECT_BUFFER_INDEX(list, 0), list->length) == 0); |
| 1186 | of_object_delete((of_object_t *)dup); |
| 1187 | } |
| 1188 | |
Rich Lane | ad70803 | 2014-12-03 12:40:58 -0800 | [diff] [blame] | 1189 | recursion--; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1190 | return value; |
| 1191 | } |
| 1192 | """ % dict(cls=cls, u_cls=cls.upper(), entry_count=entry_count)) |
| 1193 | |
| 1194 | |
| 1195 | def gen_class_setup_check(out, cls, version): |
| 1196 | out.write(""" |
| 1197 | /** |
| 1198 | * Populate all members of an object of type %(cls)s |
| 1199 | * with incrementing values |
| 1200 | * @param obj Pointer to an object to populate |
| 1201 | * @param value The seed value to use in populating the object |
| 1202 | * @returns The value after increments for this object's values |
| 1203 | */ |
| 1204 | |
| 1205 | int |
| 1206 | %(cls)s_%(v_name)s_populate( |
| 1207 | %(cls)s_t *obj, int value) |
| 1208 | { |
| 1209 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 1210 | members, member_types = loxi_utils.all_member_types_get(cls, version) |
| 1211 | for m_type in member_types: |
| 1212 | if loxi_utils.type_is_scalar(m_type) or m_type in ["of_match_t", "of_octets_t"]: |
| 1213 | out.write(" %s %s;\n" % (m_type, var_name_map(m_type))) |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 1214 | elif m_type in embedded_subclasses: |
| 1215 | subcls = embedded_subclasses[m_type] |
| 1216 | out.write(" %s_t *%s;\n" % (subcls, var_name_map(m_type))) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1217 | else: |
| 1218 | out.write(" %s *%s;\n" % (m_type, var_name_map(m_type))) |
| 1219 | out.write(""" |
| 1220 | /* Run thru accessors after new to ensure okay */ |
| 1221 | """) |
| 1222 | for member in members: |
| 1223 | m_type = member["m_type"] |
| 1224 | m_name = member["name"] |
| 1225 | if loxi_utils.skip_member_name(m_name): |
| 1226 | continue |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 1227 | if m_type in embedded_subclasses: |
Wilson Ng | 734a1d6 | 2014-04-17 18:34:17 -0700 | [diff] [blame] | 1228 | continue |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1229 | if loxi_utils.type_is_scalar(m_type) or m_type in ["of_match_t", "of_octets_t"]: |
| 1230 | out.write("""\ |
| 1231 | %(cls)s_%(m_name)s_get(obj, &%(var_name)s); |
| 1232 | """ % dict(var_name=var_name_map(m_type), cls=cls, m_name=m_name)) |
| 1233 | else: |
| 1234 | sub_cls = m_type[:-2] # Trim _t |
| 1235 | out.write("""\ |
| 1236 | { |
| 1237 | %(sub_cls)s_t sub_cls; |
| 1238 | |
| 1239 | /* Test bind */ |
| 1240 | %(cls)s_%(m_name)s_bind(obj, &sub_cls); |
| 1241 | } |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1242 | """ % dict(var_name=var_name_map(m_type), cls=cls, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1243 | m_name=m_name, sub_cls=sub_cls, |
| 1244 | v_name=loxi_utils.version_to_name(version))) |
| 1245 | |
| 1246 | out.write(""" |
| 1247 | value = %(cls)s_%(v_name)s_populate_scalars( |
| 1248 | obj, value); |
| 1249 | TEST_ASSERT(value != 0); |
| 1250 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 1251 | |
| 1252 | for member in members: |
| 1253 | m_type = member["m_type"] |
| 1254 | m_name = member["name"] |
| 1255 | if loxi_utils.type_is_scalar(m_type): # Handled by call to scalar setup |
| 1256 | continue |
| 1257 | if loxi_utils.skip_member_name(m_name): |
| 1258 | continue |
| 1259 | if m_type == "of_match_t": |
| 1260 | out.write("""\ |
| 1261 | value = of_match_populate(&%(var_name)s, %(v_name)s, value); |
| 1262 | TEST_ASSERT(value != 0); |
| 1263 | %(cls)s_%(m_name)s_set( |
| 1264 | obj, &%(var_name)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1265 | """ % dict(cls=cls, var_name=var_name_map(m_type), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1266 | m_name=m_name, v_name=loxi_utils.version_to_name(version))) |
| 1267 | elif m_type == "of_octets_t": |
| 1268 | out.write("""\ |
| 1269 | value = of_octets_populate(&%(var_name)s, value); |
| 1270 | TEST_ASSERT(value != 0); |
| 1271 | %(cls)s_%(m_name)s_set( |
| 1272 | obj, &%(var_name)s); |
| 1273 | if (octets.bytes) { |
| 1274 | FREE(octets.data); |
| 1275 | } |
| 1276 | """ % dict(var_name=var_name_map(m_type), cls=cls, m_name=m_name)) |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 1277 | elif m_type in embedded_subclasses: |
| 1278 | sub_cls = embedded_subclasses[m_type] |
Wilson Ng | d618188 | 2014-04-14 16:28:35 -0700 | [diff] [blame] | 1279 | out.write("""\ |
| 1280 | %(var_name)s = %(sub_cls)s_new(%(v_name)s); |
| 1281 | TEST_ASSERT(%(var_name)s != NULL); |
Wilson Ng | 734a1d6 | 2014-04-17 18:34:17 -0700 | [diff] [blame] | 1282 | value = %(sub_cls)s_%(v_name)s_populate( |
| 1283 | %(var_name)s, value); |
Wilson Ng | d618188 | 2014-04-14 16:28:35 -0700 | [diff] [blame] | 1284 | TEST_ASSERT(value != 0); |
| 1285 | %(cls)s_%(m_name)s_set( |
| 1286 | obj, %(var_name)s); |
| 1287 | %(sub_cls)s_delete(%(var_name)s); |
| 1288 | """ % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, m_type=m_type, |
| 1289 | var_name=var_name_map(m_type), |
| 1290 | v_name=loxi_utils.version_to_name(version))) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1291 | else: |
| 1292 | sub_cls = m_type[:-2] # Trim _t |
| 1293 | out.write(""" |
| 1294 | %(var_name)s = %(sub_cls)s_new(%(v_name)s); |
| 1295 | TEST_ASSERT(%(var_name)s != NULL); |
| 1296 | value = %(sub_cls)s_%(v_name)s_populate( |
| 1297 | %(var_name)s, value); |
| 1298 | TEST_ASSERT(value != 0); |
| 1299 | %(cls)s_%(m_name)s_set( |
| 1300 | obj, %(var_name)s); |
| 1301 | %(sub_cls)s_delete(%(var_name)s); |
| 1302 | """ % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, m_type=m_type, |
| 1303 | var_name=var_name_map(m_type), |
| 1304 | v_name=loxi_utils.version_to_name(version))) |
| 1305 | |
| 1306 | out.write(""" |
| 1307 | return value; |
| 1308 | } |
| 1309 | """) |
| 1310 | |
| 1311 | out.write(""" |
| 1312 | /** |
| 1313 | * Check all members of an object of type %(cls)s |
| 1314 | * populated by the above function |
| 1315 | * @param obj Pointer to an object to check |
| 1316 | * @param value Starting value for checking |
| 1317 | * @returns The value after increments for this object's values |
| 1318 | */ |
| 1319 | |
| 1320 | int |
| 1321 | %(cls)s_%(v_name)s_check( |
| 1322 | %(cls)s_t *obj, int value) |
| 1323 | { |
| 1324 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 1325 | members, member_types = loxi_utils.all_member_types_get(cls, version) |
| 1326 | for m_type in member_types: |
| 1327 | if loxi_utils.type_is_scalar(m_type): # Handled by call to scalar setup |
| 1328 | continue |
| 1329 | if loxi_utils.type_is_of_object(m_type): |
| 1330 | continue |
| 1331 | out.write(" %s %s;\n" % (m_type, var_name_map(m_type))) |
| 1332 | out.write(""" |
| 1333 | value = %(cls)s_%(v_name)s_check_scalars( |
| 1334 | obj, value); |
| 1335 | TEST_ASSERT(value != 0); |
| 1336 | """ % dict(cls=cls, v_name=loxi_utils.version_to_name(version))) |
| 1337 | |
| 1338 | for member in members: |
| 1339 | m_type = member["m_type"] |
| 1340 | m_name = member["name"] |
| 1341 | if loxi_utils.type_is_scalar(m_type): # Handled by call to scalar setup |
| 1342 | continue |
| 1343 | if loxi_utils.skip_member_name(m_name): |
| 1344 | continue |
| 1345 | if m_type == "of_match_t": |
| 1346 | out.write("""\ |
| 1347 | %(cls)s_%(m_name)s_get(obj, &%(var_name)s); |
| 1348 | value = of_match_check(&%(var_name)s, %(v_name)s, value); |
| 1349 | """ % dict(cls=cls, var_name=var_name_map(m_type), m_name=m_name, |
| 1350 | v_name=loxi_utils.version_to_name(version))) |
| 1351 | elif m_type == "of_octets_t": |
| 1352 | out.write("""\ |
| 1353 | %(cls)s_%(m_name)s_get(obj, &%(var_name)s); |
| 1354 | value = of_octets_check(&%(var_name)s, value); |
| 1355 | """ % dict(cls=cls, var_name=var_name_map(m_type), m_name=m_name, |
| 1356 | v_name=loxi_utils.version_to_name(version))) |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 1357 | elif m_type in embedded_subclasses: |
| 1358 | sub_cls = embedded_subclasses[m_type] |
Wilson Ng | d618188 | 2014-04-14 16:28:35 -0700 | [diff] [blame] | 1359 | out.write(""" |
| 1360 | { /* Use get/delete to access on check */ |
Wilson Ng | 734a1d6 | 2014-04-17 18:34:17 -0700 | [diff] [blame] | 1361 | %(sub_cls)s_t *%(m_name)s_ptr; |
Wilson Ng | d618188 | 2014-04-14 16:28:35 -0700 | [diff] [blame] | 1362 | |
| 1363 | %(m_name)s_ptr = %(cls)s_%(m_name)s_get(obj); |
| 1364 | TEST_ASSERT(%(m_name)s_ptr != NULL); |
Wilson Ng | 734a1d6 | 2014-04-17 18:34:17 -0700 | [diff] [blame] | 1365 | value = %(sub_cls)s_%(v_name)s_check( |
| 1366 | %(m_name)s_ptr, value); |
Wilson Ng | d618188 | 2014-04-14 16:28:35 -0700 | [diff] [blame] | 1367 | TEST_ASSERT(value != 0); |
| 1368 | %(sub_cls)s_delete(%(m_name)s_ptr); |
| 1369 | } |
| 1370 | """ % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, m_type=m_type, |
| 1371 | var_name=var_name_map(m_type), |
| 1372 | v_name=loxi_utils.version_to_name(version))) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1373 | else: |
| 1374 | sub_cls = m_type[:-2] # Trim _t |
| 1375 | out.write(""" |
| 1376 | { /* Use get/delete to access on check */ |
| 1377 | %(m_type)s *%(m_name)s_ptr; |
| 1378 | |
| 1379 | %(m_name)s_ptr = %(cls)s_%(m_name)s_get(obj); |
| 1380 | TEST_ASSERT(%(m_name)s_ptr != NULL); |
| 1381 | value = %(sub_cls)s_%(v_name)s_check( |
| 1382 | %(m_name)s_ptr, value); |
| 1383 | TEST_ASSERT(value != 0); |
| 1384 | %(sub_cls)s_delete(%(m_name)s_ptr); |
| 1385 | } |
| 1386 | """ % dict(cls=cls, sub_cls=sub_cls, m_name=m_name, m_type=m_type, |
| 1387 | var_name=var_name_map(m_type), |
| 1388 | v_name=loxi_utils.version_to_name(version))) |
| 1389 | |
| 1390 | out.write(""" |
| 1391 | /* We shoehorn a test of the dup functions here */ |
| 1392 | { |
| 1393 | %(cls)s_t *dup; |
| 1394 | |
| 1395 | TEST_ASSERT((dup = %(cls)s_dup(obj)) != NULL); |
| 1396 | TEST_ASSERT(dup->length == obj->length); |
| 1397 | TEST_ASSERT(dup->object_id == obj->object_id); |
| 1398 | TEST_ASSERT(dup->version == obj->version); |
| 1399 | TEST_ASSERT(MEMCMP(OF_OBJECT_BUFFER_INDEX(dup, 0), |
| 1400 | OF_OBJECT_BUFFER_INDEX(obj, 0), obj->length) == 0); |
| 1401 | of_object_delete((of_object_t *)dup); |
| 1402 | |
| 1403 | /* And now for the generic dup function */ |
| 1404 | TEST_ASSERT((dup = (%(cls)s_t *) |
| 1405 | of_object_dup(obj)) != NULL); |
| 1406 | TEST_ASSERT(dup->length == obj->length); |
| 1407 | TEST_ASSERT(dup->object_id == obj->object_id); |
| 1408 | TEST_ASSERT(dup->version == obj->version); |
| 1409 | TEST_ASSERT(MEMCMP(OF_OBJECT_BUFFER_INDEX(dup, 0), |
| 1410 | OF_OBJECT_BUFFER_INDEX(obj, 0), obj->length) == 0); |
| 1411 | of_object_delete((of_object_t *)dup); |
| 1412 | } |
| 1413 | |
| 1414 | return value; |
| 1415 | } |
| 1416 | """ % dict(cls=cls)) |
| 1417 | |
| 1418 | def unified_accessor_test_case(out, cls, version): |
| 1419 | """ |
| 1420 | Generate one test case for the given version and class |
| 1421 | """ |
| 1422 | |
| 1423 | members, member_types = scalar_member_types_get(cls, version) |
Rich Lane | e3d3fb5 | 2014-10-16 12:45:17 -0700 | [diff] [blame] | 1424 | length = of_g.base_length[(cls, version)] |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1425 | v_name = loxi_utils.version_to_name(version) |
| 1426 | |
| 1427 | out.write(""" |
| 1428 | static int |
| 1429 | test_%(cls)s_%(v_name)s(void) |
| 1430 | { |
| 1431 | %(cls)s_t *obj; |
| 1432 | obj = %(cls)s_new(%(v_name)s); |
| 1433 | TEST_ASSERT(obj != NULL); |
| 1434 | TEST_ASSERT(obj->version == %(v_name)s); |
| 1435 | TEST_ASSERT(obj->length == %(length)d); |
| 1436 | TEST_ASSERT(obj->parent == NULL); |
| 1437 | TEST_ASSERT(obj->object_id == %(u_cls)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1438 | """ % dict(cls=cls, u_cls=cls.upper(), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1439 | v_name=v_name, length=length, version=version)) |
| 1440 | if (not type_maps.class_is_virtual(cls)) or loxi_utils.class_is_list(cls): |
| 1441 | out.write(""" |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 1442 | if (loci_class_metadata[obj->object_id].wire_length_get != NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1443 | int length; |
| 1444 | |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 1445 | loci_class_metadata[obj->object_id].wire_length_get((of_object_t *)obj, &length); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1446 | TEST_ASSERT(length == %(length)d); |
| 1447 | } |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 1448 | if (loci_class_metadata[obj->object_id].wire_type_get != NULL) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1449 | of_object_id_t obj_id; |
| 1450 | |
Rich Lane | dc46fe2 | 2014-04-03 15:10:38 -0700 | [diff] [blame] | 1451 | loci_class_metadata[obj->object_id].wire_type_get((of_object_t *)obj, &obj_id); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1452 | TEST_ASSERT(obj_id == %(u_cls)s); |
| 1453 | } |
| 1454 | |
| 1455 | /* Set up incrementing values for members */ |
| 1456 | TEST_ASSERT(%(cls)s_%(v_name)s_populate( |
| 1457 | obj, 1) != 0); |
| 1458 | |
| 1459 | /* Check values just set */ |
| 1460 | TEST_ASSERT(%(cls)s_%(v_name)s_check( |
| 1461 | obj, 1) != 0); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1462 | """ % dict(cls=cls, u_cls=cls.upper(), |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1463 | v_name=v_name, length=length, version=version)) |
| 1464 | |
| 1465 | out.write(""" |
| 1466 | %(cls)s_delete(obj); |
| 1467 | |
| 1468 | /* To do: Check memory */ |
| 1469 | return TEST_PASS; |
| 1470 | } |
| 1471 | """ % dict(cls=cls)) |
| 1472 | |
| 1473 | |
| 1474 | def gen_unified_accessor_funs(out): |
| 1475 | for version in of_g.of_version_range: |
| 1476 | for cls in of_g.standard_class_order: |
| 1477 | if not loxi_utils.class_in_version(cls, version): |
| 1478 | continue |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1479 | elif loxi_utils.class_is_list(cls): |
| 1480 | gen_list_setup_check(out, cls, version) |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 1481 | elif not type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1482 | gen_class_setup_check(out, cls, version) |
| 1483 | |
| 1484 | def gen_unified_accessor_tests(out, name): |
| 1485 | loxi_utils.gen_c_copy_license(out) |
| 1486 | out.write(""" |
| 1487 | /** |
| 1488 | * |
| 1489 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 1490 | * |
| 1491 | * Unified simple class instantiation tests for all versions |
| 1492 | */ |
| 1493 | |
| 1494 | #include <locitest/test_common.h> |
| 1495 | """) |
| 1496 | for version in of_g.of_version_range: |
| 1497 | for cls in of_g.standard_class_order: |
| 1498 | if not loxi_utils.class_in_version(cls, version): |
| 1499 | continue |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 1500 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1501 | continue |
| 1502 | unified_accessor_test_case(out, cls, version) |
| 1503 | |
| 1504 | out.write(""" |
| 1505 | int |
| 1506 | run_unified_accessor_tests(void) |
| 1507 | { |
| 1508 | """) |
| 1509 | for version in of_g.of_version_range: |
| 1510 | v_name = loxi_utils.version_to_name(version) |
| 1511 | for cls in of_g.standard_class_order: |
| 1512 | if not loxi_utils.class_in_version(cls, version): |
| 1513 | continue |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 1514 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1515 | continue |
| 1516 | test_name = "%s_%s" % (cls, v_name) |
| 1517 | out.write(" RUN_TEST(%s);\n" % test_name) |
| 1518 | |
| 1519 | out.write(" return TEST_PASS;\n}\n"); |
| 1520 | |
| 1521 | |
| 1522 | |
| 1523 | ################################################################ |
| 1524 | # |
| 1525 | # Object duplication functions |
| 1526 | # |
| 1527 | # These exercise the accessors to create duplicate objects. |
| 1528 | # They are used in the LOCI test shim which sits in an OF |
| 1529 | # protocol stream. |
| 1530 | # |
| 1531 | # TODO |
| 1532 | # Resolve version stuff |
| 1533 | # Complete list dup |
| 1534 | |
| 1535 | def gen_dup_list(out, cls, version): |
| 1536 | ver_name = loxi_utils.version_to_name(version) |
| 1537 | elt_type = loxi_utils.list_to_entry_type(cls) |
| 1538 | out.write(""" |
| 1539 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1540 | * Duplicate a list of type %(cls)s |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1541 | * using accessor functions |
| 1542 | * @param src Pointer to object to be duplicated |
| 1543 | * @returns A new object of type %(cls)s. |
| 1544 | * |
| 1545 | * The caller is responsible for deleting the returned value |
| 1546 | */ |
| 1547 | %(cls)s_t * |
| 1548 | %(cls)s_%(ver_name)s_dup( |
| 1549 | %(cls)s_t *src) |
| 1550 | { |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1551 | of_object_t src_elt; |
| 1552 | of_object_t *dst_elt; |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1553 | int rv; |
| 1554 | %(cls)s_t *dst; |
| 1555 | |
| 1556 | if ((dst = %(cls)s_new(src->version)) == NULL) { |
| 1557 | return NULL; |
| 1558 | } |
| 1559 | """ % dict(elt_type=elt_type, cls=cls, ver_name=ver_name)) |
| 1560 | |
| 1561 | out.write(""" |
| 1562 | %(u_cls)s_ITER(src, &src_elt, rv) { |
| 1563 | if ((dst_elt = %(elt_type)s_%(ver_name)s_dup(&src_elt)) == NULL) { |
| 1564 | of_object_delete((of_object_t *)dst); |
| 1565 | return NULL; |
| 1566 | } |
| 1567 | _TRY_FREE(%(cls)s_append(dst, dst_elt), |
| 1568 | dst, NULL); |
| 1569 | of_object_delete((of_object_t *)dst_elt); |
| 1570 | } |
| 1571 | |
| 1572 | return dst; |
| 1573 | } |
| 1574 | """ % dict(u_cls=cls.upper(), elt_type=elt_type, cls=cls, ver_name=ver_name)) |
| 1575 | |
| 1576 | |
| 1577 | def gen_dup_inheritance(out, cls, version): |
| 1578 | ver_name = loxi_utils.version_to_name(version) |
| 1579 | out.write(""" |
| 1580 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1581 | * Duplicate a super class object of type %(cls)s |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1582 | * @param src Pointer to object to be duplicated |
| 1583 | * @returns A new object of type %(cls)s. |
| 1584 | * |
| 1585 | * The caller is responsible for deleting the returned value |
| 1586 | */ |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1587 | of_object_t * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1588 | %(cls)s_%(ver_name)s_dup( |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1589 | of_object_t *src) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1590 | { |
| 1591 | """ % dict(cls=cls, ver_name=ver_name)) |
| 1592 | |
| 1593 | # For each subclass, check if this is an instance of that subclass |
Rich Lane | 10daa5b | 2014-10-12 11:36:50 -0700 | [diff] [blame] | 1594 | sub_classes = type_maps.sub_class_map(cls, version) |
| 1595 | for (_, sub_cls) in sub_classes: |
| 1596 | sub_enum = sub_cls.upper() |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 1597 | if type_maps.class_is_virtual(sub_cls): |
| 1598 | continue |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1599 | out.write(""" |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1600 | if (src->object_id == %(sub_enum)s) { |
| 1601 | return %(sub_cls)s_%(ver_name)s_dup(src); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1602 | } |
| 1603 | """ % dict(sub_cls=sub_cls, ver_name=ver_name, sub_enum=sub_enum, cls=cls)) |
| 1604 | |
| 1605 | out.write(""" |
| 1606 | return NULL; |
| 1607 | } |
| 1608 | """) |
| 1609 | |
| 1610 | |
| 1611 | def gen_dup_cls(out, cls, version): |
| 1612 | """ |
| 1613 | Generate duplication routine for class cls |
| 1614 | """ |
| 1615 | ver_name = loxi_utils.version_to_name(version) |
| 1616 | |
| 1617 | out.write(""" |
| 1618 | /** |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1619 | * Duplicate an object of type %(cls)s |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1620 | * using accessor functions |
| 1621 | * @param src Pointer to object to be duplicated |
| 1622 | * @returns A new object of type %(cls)s. |
| 1623 | * |
| 1624 | * The caller is responsible for deleting the returned value |
| 1625 | */ |
| 1626 | %(cls)s_t * |
| 1627 | %(cls)s_%(ver_name)s_dup( |
| 1628 | %(cls)s_t *src) |
| 1629 | { |
| 1630 | %(cls)s_t *dst; |
| 1631 | """ % dict(cls=cls, ver_name=ver_name)) |
| 1632 | |
| 1633 | # Get members and types for the class |
| 1634 | members, member_types = loxi_utils.all_member_types_get(cls, version) |
| 1635 | |
| 1636 | # Add declarations for each member type |
| 1637 | for m_type in member_types: |
| 1638 | if loxi_utils.type_is_scalar(m_type) or m_type in ["of_match_t", "of_octets_t"]: |
| 1639 | # Declare instance of these |
| 1640 | out.write(" %s %s;\n" % (m_type, var_name_map(m_type))) |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 1641 | elif m_type in embedded_subclasses: |
| 1642 | sub_cls = embedded_subclasses[m_type] |
Wilson Ng | 734a1d6 | 2014-04-17 18:34:17 -0700 | [diff] [blame] | 1643 | out.write(""" |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 1644 | %(sub_cls)s_t src_%(v_name)s; |
| 1645 | %(sub_cls)s_t *dst_%(v_name)s; |
| 1646 | """ % dict(v_name=var_name_map(m_type), sub_cls=sub_cls)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1647 | else: |
| 1648 | out.write(""" |
| 1649 | %(m_type)s src_%(v_name)s; |
| 1650 | %(m_type)s *dst_%(v_name)s; |
| 1651 | """ % dict(m_type=m_type, v_name=var_name_map(m_type))) |
| 1652 | |
| 1653 | out.write(""" |
| 1654 | if ((dst = %(cls)s_new(src->version)) == NULL) { |
| 1655 | return NULL; |
| 1656 | } |
| 1657 | """ % dict(cls=cls)) |
| 1658 | |
| 1659 | for member in members: |
| 1660 | m_type = member["m_type"] |
| 1661 | m_name = member["name"] |
| 1662 | if loxi_utils.skip_member_name(m_name): |
| 1663 | continue |
| 1664 | if loxi_utils.type_is_scalar(m_type): # Handled by call to scalar setup |
| 1665 | out.write(""" |
| 1666 | %(cls)s_%(m_name)s_get(src, &%(v_name)s); |
| 1667 | %(cls)s_%(m_name)s_set(dst, %(v_name)s); |
| 1668 | """ % dict(cls=cls, m_name=m_name, v_name=var_name_map(m_type))) |
| 1669 | elif m_type in ["of_match_t", "of_octets_t"]: |
| 1670 | out.write(""" |
| 1671 | %(cls)s_%(m_name)s_get(src, &%(v_name)s); |
| 1672 | %(cls)s_%(m_name)s_set(dst, &%(v_name)s); |
| 1673 | """ % dict(cls=cls, m_name=m_name, v_name=var_name_map(m_type))) |
Rich Lane | 1f315aa | 2014-10-13 17:11:35 -0700 | [diff] [blame] | 1674 | elif m_type in embedded_subclasses: |
| 1675 | sub_cls = embedded_subclasses[m_type] |
Wilson Ng | 734a1d6 | 2014-04-17 18:34:17 -0700 | [diff] [blame] | 1676 | out.write(""" |
| 1677 | %(cls)s_%(m_name)s_bind( |
| 1678 | src, &src_%(v_name)s); |
| 1679 | dst_%(v_name)s = %(sub_cls)s_%(ver_name)s_dup(&src_%(v_name)s); |
| 1680 | if (dst_%(v_name)s == NULL) { |
| 1681 | %(cls)s_delete(dst); |
| 1682 | return NULL; |
| 1683 | } |
| 1684 | %(cls)s_%(m_name)s_set(dst, dst_%(v_name)s); |
| 1685 | %(sub_cls)s_delete(dst_%(v_name)s); |
| 1686 | """ % dict(sub_cls=sub_cls, cls=cls, m_name=m_name, |
| 1687 | v_name=var_name_map(m_type), ver_name=ver_name)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1688 | else: |
| 1689 | sub_cls = m_type[:-2] # Trim _t |
| 1690 | out.write(""" |
| 1691 | %(cls)s_%(m_name)s_bind( |
| 1692 | src, &src_%(v_name)s); |
| 1693 | dst_%(v_name)s = %(sub_cls)s_%(ver_name)s_dup(&src_%(v_name)s); |
| 1694 | if (dst_%(v_name)s == NULL) { |
| 1695 | %(cls)s_delete(dst); |
| 1696 | return NULL; |
| 1697 | } |
| 1698 | %(cls)s_%(m_name)s_set(dst, dst_%(v_name)s); |
| 1699 | %(sub_cls)s_delete(dst_%(v_name)s); |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1700 | """ % dict(sub_cls=sub_cls, cls=cls, m_name=m_name, |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1701 | v_name=var_name_map(m_type), ver_name=ver_name)) |
| 1702 | |
| 1703 | out.write(""" |
| 1704 | return dst; |
| 1705 | } |
| 1706 | """) |
| 1707 | |
| 1708 | def gen_version_dup(out=sys.stdout): |
| 1709 | """ |
| 1710 | Generate duplication routines for each object type |
| 1711 | """ |
| 1712 | out.write(""" |
| 1713 | /* Special try macro for duplicating */ |
| 1714 | #define _TRY_FREE(op, obj, rv) do { \\ |
| 1715 | int _rv; \\ |
| 1716 | if ((_rv = (op)) < 0) { \\ |
| 1717 | LOCI_LOG_ERROR("ERROR %d at %s:%d\\n", _rv, __FILE__, __LINE__); \\ |
| 1718 | of_object_delete((of_object_t *)(obj)); \\ |
| 1719 | return (rv); \\ |
| 1720 | } \\ |
| 1721 | } while (0) |
| 1722 | """) |
| 1723 | |
| 1724 | for version in of_g.of_version_range: |
| 1725 | for cls in of_g.standard_class_order: |
| 1726 | if not loxi_utils.class_in_version(cls, version): |
| 1727 | continue |
Rich Lane | 8841f35 | 2014-10-12 19:18:36 -0700 | [diff] [blame] | 1728 | if type_maps.class_is_inheritance_root(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1729 | gen_dup_inheritance(out, cls, version) |
| 1730 | elif loxi_utils.class_is_list(cls): |
| 1731 | gen_dup_list(out, cls, version) |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 1732 | elif not type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1733 | gen_dup_cls(out, cls, version) |
| 1734 | |
| 1735 | def gen_dup(out=sys.stdout): |
| 1736 | """ |
| 1737 | Generate non-version specific duplication routines for each object type |
| 1738 | """ |
| 1739 | |
| 1740 | for cls in of_g.standard_class_order: |
| 1741 | out.write(""" |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1742 | of_object_t * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1743 | %(cls)s_dup( |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1744 | of_object_t *src) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1745 | { |
| 1746 | """ % dict(cls=cls)) |
| 1747 | for version in of_g.of_version_range: |
| 1748 | if not loxi_utils.class_in_version(cls, version): |
| 1749 | continue |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 1750 | elif type_maps.class_is_inheritance_root(cls): |
| 1751 | pass |
| 1752 | elif loxi_utils.class_is_list(cls): |
| 1753 | pass |
| 1754 | elif type_maps.class_is_virtual(cls): |
| 1755 | continue |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1756 | ver_name = loxi_utils.version_to_name(version) |
| 1757 | out.write(""" |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1758 | if (src->version == %(ver_name)s) { |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1759 | return %(cls)s_%(ver_name)s_dup(src); |
| 1760 | } |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1761 | """ % dict(cls=cls, ver_name=ver_name)) |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1762 | |
| 1763 | out.write(""" |
| 1764 | /* Class not supported in given version */ |
| 1765 | return NULL; |
| 1766 | } |
| 1767 | """) |
| 1768 | |
| 1769 | def dup_c_gen(out, name): |
| 1770 | """ |
| 1771 | Generate the C file for duplication functions |
| 1772 | """ |
| 1773 | loxi_utils.gen_c_copy_license(out) |
| 1774 | out.write("""\ |
| 1775 | /* |
| 1776 | * Duplication functions for all OF objects |
| 1777 | * |
| 1778 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 1779 | * |
| 1780 | * These are test functions for exercising accessors. You can call |
| 1781 | * of_object_dup for an efficient duplication. |
| 1782 | */ |
| 1783 | |
| 1784 | #define DISABLE_WARN_UNUSED_RESULT |
| 1785 | #include "loci_log.h" |
| 1786 | #include <locitest/of_dup.h> |
| 1787 | |
| 1788 | """) |
| 1789 | |
| 1790 | gen_version_dup(out) |
| 1791 | gen_dup(out) |
| 1792 | |
| 1793 | |
| 1794 | def dup_h_gen(out, name): |
| 1795 | """ |
| 1796 | Generate the header file for duplication functions |
| 1797 | """ |
| 1798 | |
| 1799 | loxi_utils.gen_c_copy_license(out) |
| 1800 | out.write(""" |
| 1801 | /* |
| 1802 | * Duplication function header file |
| 1803 | * |
| 1804 | * AUTOMATICALLY GENERATED FILE. Edits will be lost on regen. |
| 1805 | */ |
| 1806 | |
| 1807 | #if !defined(_OF_DUP_H_) |
| 1808 | #define _OF_DUP_H_ |
| 1809 | |
| 1810 | #include <loci/loci.h> |
| 1811 | """) |
| 1812 | |
| 1813 | for cls in of_g.standard_class_order: |
| 1814 | out.write(""" |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1815 | extern of_object_t * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1816 | %(cls)s_dup( |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1817 | of_object_t *src); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1818 | """ % dict(cls=cls)) |
| 1819 | |
| 1820 | for version in of_g.of_version_range: |
| 1821 | for cls in of_g.standard_class_order: |
| 1822 | if not loxi_utils.class_in_version(cls, version): |
| 1823 | continue |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 1824 | elif type_maps.class_is_inheritance_root(cls): |
| 1825 | pass |
| 1826 | elif loxi_utils.class_is_list(cls): |
| 1827 | pass |
| 1828 | elif type_maps.class_is_virtual(cls): |
| 1829 | continue |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1830 | ver_name = loxi_utils.version_to_name(version) |
| 1831 | out.write(""" |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1832 | extern of_object_t * |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1833 | %(cls)s_%(ver_name)s_dup( |
Rich Lane | e499bd1 | 2014-10-28 15:25:09 -0700 | [diff] [blame] | 1834 | of_object_t *src); |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1835 | """ % dict(cls=cls, ver_name=ver_name)) |
| 1836 | |
| 1837 | out.write("\n#endif /* _OF_DUP_H_ */\n") |
| 1838 | |
| 1839 | def gen_log_test(out): |
| 1840 | """ |
| 1841 | Generate test for obj log calls |
| 1842 | |
| 1843 | Define a trivial handler for object logging; call all obj log fns |
| 1844 | """ |
| 1845 | out.write(""" |
| 1846 | |
| 1847 | /** |
| 1848 | * Test object dump functions |
| 1849 | */ |
| 1850 | |
| 1851 | int |
| 1852 | test_dump_objs(void) |
| 1853 | { |
| 1854 | of_object_t *obj; |
| 1855 | |
| 1856 | FILE *out = fopen("/dev/null", "w"); |
| 1857 | |
| 1858 | /* Call each obj dump function */ |
| 1859 | """) |
| 1860 | for version in of_g.of_version_range: |
| 1861 | for j, cls in enumerate(of_g.all_class_order): |
| 1862 | if not loxi_utils.class_in_version(cls, version): |
| 1863 | continue |
Rich Lane | 6171e63 | 2014-11-07 10:23:38 -0800 | [diff] [blame] | 1864 | if type_maps.class_is_virtual(cls): |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1865 | continue |
Wilson Ng | 734a1d6 | 2014-04-17 18:34:17 -0700 | [diff] [blame] | 1866 | if cls == "of_bsn_virtual_port_create_request": # test q_in_q |
| 1867 | out.write(""" |
| 1868 | obj = (of_object_t *)%(cls)s_new(%(version)s); |
| 1869 | { |
| 1870 | of_object_t *vport = of_bsn_vport_q_in_q_new(%(version)s); |
| 1871 | %(cls)s_vport_set(obj, vport); |
| 1872 | of_object_delete(vport); |
| 1873 | } |
| 1874 | of_object_dump((loci_writer_f)fprintf, out, obj); |
| 1875 | of_object_delete(obj); |
| 1876 | """ % dict(cls=cls, version=of_g.of_version_wire2name[version])) |
| 1877 | else: |
| 1878 | out.write(""" |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1879 | obj = (of_object_t *)%(cls)s_new(%(version)s); |
| 1880 | of_object_dump((loci_writer_f)fprintf, out, obj); |
| 1881 | of_object_delete(obj); |
| 1882 | """ % dict(cls=cls, version=of_g.of_version_wire2name[version])) |
Andreas Wundsam | 5325616 | 2013-05-02 14:05:53 -0700 | [diff] [blame] | 1883 | |
Rich Lane | a06d0c3 | 2013-03-25 08:52:03 -0700 | [diff] [blame] | 1884 | out.write(""" |
| 1885 | fclose(out); |
| 1886 | return TEST_PASS; |
| 1887 | } |
| 1888 | """) |
| 1889 | |
| 1890 | def gen_ident_tests(out): |
| 1891 | """ |
| 1892 | Generate tests for identifiers |
| 1893 | |
| 1894 | For all idents, instantiate, test version supported macros |
| 1895 | For flags, set it, test it, clear it, test it. |
| 1896 | """ |
| 1897 | out.write(""" |
| 1898 | /** |
| 1899 | * Test cases for all flag accessor macros |
| 1900 | * These only test self consistency (and that they compile) |
| 1901 | */ |
| 1902 | int |
| 1903 | test_ident_macros(void) |
| 1904 | { |
| 1905 | int value __attribute__((unused)); |
| 1906 | uint32_t flags; |
| 1907 | |
| 1908 | """) |
| 1909 | |
| 1910 | for ident, info in of_g.identifiers.items(): |
| 1911 | if not identifiers.defined_versions_agree(of_g.identifiers, |
| 1912 | of_g.target_version_list, |
| 1913 | ident): |
| 1914 | # @fixme |
| 1915 | continue |
| 1916 | out.write(" value = %s;\n" % ident) |
| 1917 | for version in of_g.target_version_list: |
| 1918 | if version in info["values_by_version"].keys(): |
| 1919 | out.write(" TEST_ASSERT(%s_SUPPORTED(%s));\n" % |
| 1920 | (ident, of_g.of_version_wire2name[version])) |
| 1921 | else: |
| 1922 | out.write(" TEST_ASSERT(!%s_SUPPORTED(%s));\n" % |
| 1923 | (ident, of_g.of_version_wire2name[version])) |
| 1924 | if flags.ident_is_flag(ident): |
| 1925 | # Grab first supported version |
| 1926 | for version in info["values_by_version"]: |
| 1927 | break |
| 1928 | out.write(""" |
| 1929 | flags = 0; |
| 1930 | %(ident)s_SET(flags, %(ver_name)s); |
| 1931 | TEST_ASSERT(flags == %(ident)s_BY_VERSION(%(ver_name)s)); |
| 1932 | TEST_ASSERT(%(ident)s_TEST(flags, %(ver_name)s)); |
| 1933 | %(ident)s_CLEAR(flags, %(ver_name)s); |
| 1934 | TEST_ASSERT(flags == 0); |
| 1935 | TEST_ASSERT(!%(ident)s_TEST(flags, %(ver_name)s)); |
| 1936 | """ % dict(ident=ident, ver_name=of_g.of_version_wire2name[version])) |
| 1937 | |
| 1938 | out.write(""" |
| 1939 | return TEST_PASS; |
| 1940 | } |
| 1941 | """) |
| 1942 | |
Rich Lane | ccae031 | 2013-07-21 23:34:13 -0700 | [diff] [blame] | 1943 | def gen_datafiles_tests(out, name): |
| 1944 | tests = [] |
| 1945 | for filename in test_data.list_files(): |
| 1946 | data = test_data.read(filename) |
| 1947 | if not 'c' in data: |
| 1948 | continue |
| 1949 | name = filename[:-5].replace("/", "_") |
| 1950 | tests.append(dict(name=name, |
| 1951 | filename=filename, |
| 1952 | c=data['c'], |
| 1953 | binary=data['binary'])) |
| 1954 | |
| 1955 | util.render_template(out, "test_data.c", tests=tests) |