|
| 1 | +/* |
| 2 | + * Copyright © 2011-2023 Inria. All rights reserved. |
| 3 | + * See COPYING in top-level directory. |
| 4 | + */ |
| 5 | + |
| 6 | +#include "hwloc.h" |
| 7 | + |
| 8 | +#include <assert.h> |
| 9 | + |
| 10 | +/* check obj infos */ |
| 11 | + |
| 12 | +#define NAME1 "foobar" |
| 13 | +#define VALUE1 "myvalue" |
| 14 | +#define NAME2 "foobaz" |
| 15 | +#define VALUE2 "myothervalue" |
| 16 | + |
| 17 | +int main(void) |
| 18 | +{ |
| 19 | + hwloc_topology_t topology; |
| 20 | + hwloc_obj_t obj; |
| 21 | + int err; |
| 22 | + |
| 23 | + hwloc_topology_init(&topology); |
| 24 | + hwloc_topology_load(topology); |
| 25 | + |
| 26 | + obj = hwloc_get_root_obj(topology); |
| 27 | + |
| 28 | + /* basics */ |
| 29 | + if (hwloc_obj_get_info_by_name(obj, NAME1) |
| 30 | + || hwloc_obj_get_info_by_name(obj, NAME2)) |
| 31 | + return 0; |
| 32 | + |
| 33 | + hwloc_obj_add_info(obj, NAME1, VALUE1); |
| 34 | + assert(!hwloc_obj_get_info_by_name(obj, NAME2)); |
| 35 | + hwloc_obj_add_info(obj, NAME2, VALUE2); |
| 36 | + |
| 37 | + if (strcmp(hwloc_obj_get_info_by_name(obj, NAME1), VALUE1)) |
| 38 | + assert(0); |
| 39 | + if (strcmp(hwloc_obj_get_info_by_name(obj, NAME2), VALUE2)) |
| 40 | + assert(0); |
| 41 | + |
| 42 | + /* removes */ |
| 43 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, VALUE1, NULL); /* no match */ |
| 44 | + assert(!err); |
| 45 | + /* cannot check obj->infos.count since it contained normal info attrs after load() */ |
| 46 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, NAME1, VALUE2); /* no match */ |
| 47 | + assert(!err); |
| 48 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, NULL, NAME2); /* no match */ |
| 49 | + assert(!err); |
| 50 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, NULL, NULL); /* remove all */ |
| 51 | + assert(!err); |
| 52 | + assert(obj->infos.count == 0); |
| 53 | + |
| 54 | + /* invalid add */ |
| 55 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, NULL, ""); |
| 56 | + assert(err == -1); |
| 57 | + /* 9 interleaved duplicates */ |
| 58 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo1"); |
| 59 | + assert(!err); |
| 60 | + assert(obj->infos.count == 1); |
| 61 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo1"); |
| 62 | + assert(!err); |
| 63 | + assert(obj->infos.count == 2); |
| 64 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo1"); |
| 65 | + assert(!err); |
| 66 | + assert(obj->infos.count == 3); |
| 67 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo2"); |
| 68 | + assert(!err); |
| 69 | + assert(obj->infos.count == 4); |
| 70 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo2"); |
| 71 | + assert(!err); |
| 72 | + assert(obj->infos.count == 5); |
| 73 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo2"); |
| 74 | + assert(!err); |
| 75 | + assert(obj->infos.count == 6); |
| 76 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin1", "foo3"); |
| 77 | + assert(!err); |
| 78 | + assert(obj->infos.count == 7); |
| 79 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin2", "foo3"); |
| 80 | + assert(!err); |
| 81 | + assert(obj->infos.count == 8); |
| 82 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD, "coin3", "foo3"); |
| 83 | + assert(!err); |
| 84 | + assert(obj->infos.count == 9); |
| 85 | + |
| 86 | + /* invalid replace */ |
| 87 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "", NULL); |
| 88 | + assert(err == -1); |
| 89 | + /* replace the third set of duplicates */ |
| 90 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "coin3", "foo4"); |
| 91 | + assert(!err); |
| 92 | + assert(obj->infos.count == 7); |
| 93 | + assert(!strcmp(obj->infos.array[2].name, "coin3")); |
| 94 | + assert(!strcmp(obj->infos.array[2].value, "foo4")); |
| 95 | + /* remove second set of duplicates */ |
| 96 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, "coin2", NULL); |
| 97 | + assert(!err); |
| 98 | + assert(obj->infos.count == 4); |
| 99 | + /* remove second instance of first duplicates */ |
| 100 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REMOVE, "coin1", "foo2"); |
| 101 | + assert(!err); |
| 102 | + assert(obj->infos.count == 3); |
| 103 | + /* replace reminder of the first set of duplicates */ |
| 104 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_REPLACE, "coin1", "foo5"); |
| 105 | + assert(!err); |
| 106 | + assert(obj->infos.count == 2); |
| 107 | + assert(!strcmp(obj->infos.array[0].name, "coin1")); |
| 108 | + assert(!strcmp(obj->infos.array[0].value, "foo5")); |
| 109 | + /* check the other remaining entry */ |
| 110 | + assert(!strcmp(obj->infos.array[1].name, "coin3")); |
| 111 | + assert(!strcmp(obj->infos.array[1].value, "foo4")); |
| 112 | + |
| 113 | + /* check add_unique */ |
| 114 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD_UNIQUE, "coin1", "foo5"); |
| 115 | + assert(!err); |
| 116 | + assert(obj->infos.count == 2); |
| 117 | + err = hwloc_modify_infos(&obj->infos, HWLOC_MODIFY_INFOS_OP_ADD_UNIQUE, "coin1", "foo4"); |
| 118 | + assert(!err); |
| 119 | + assert(obj->infos.count == 3); |
| 120 | + assert(!strcmp(obj->infos.array[2].name, "coin1")); |
| 121 | + assert(!strcmp(obj->infos.array[2].value, "foo4")); |
| 122 | + |
| 123 | + hwloc_topology_destroy(topology); |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
0 commit comments