1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/*****************************************************************************\
* Copyright (C) SchedMD LLC.
*
* This file is part of Slurm, a resource management program.
* For details, see <https://slurm.schedmd.com/>.
* Please also read the included file: DISCLAIMER.
*
* Slurm is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* In addition, as a special exception, the copyright holders give permission
* to link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two. You must obey the GNU
* General Public License in all respects for all of the code used other than
* OpenSSL. If you modify file(s) with this exception, you may extend this
* exception to your version of the file(s), but you are not obligated to do
* so. If you do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source files in
* the program, then also delete it here.
*
* Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with Slurm; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <check.h>
#include "slurm/slurm_errno.h"
#include "src/common/data.h"
#include "src/common/log.h"
#include "src/common/read_config.h"
#include "src/common/slurm_protocol_defs.h"
#include "src/common/xmalloc.h"
#include "src/common/xstring.h"
#define check_with_data_get_bool_converted(str, b) \
do { \
bool bres; \
int rc; \
data_set_string(d, str); \
rc = data_get_bool_converted(d, &bres); \
ck_assert_msg(rc == 0, \
"bool convert string:%s->%s rc:%s [%d]", \
str ? str : "(null)", (b ? "true" : "false"), \
slurm_strerror(rc), rc); \
if (!rc) \
ck_assert_msg(bres == b, \
"bool converted: %s -> %s == %s", \
str ? str : "(null)", (bres ? "true" : "false"), \
(b ? "true" : "false")); \
} while (0)
static data_for_each_cmd_t
_find_dict_bool(const char *key, const data_t *data, void *arg)
{
int *found = arg;
ck_assert_msg(data_get_type(data) == DATA_TYPE_BOOL, "entry bool type");
if (data_get_bool(data))
(*found)++;
ck_assert(key != NULL);
return DATA_FOR_EACH_CONT;
}
static data_for_each_cmd_t
_invert_dict_bool(const char *key, data_t *data, void *arg)
{
ck_assert_msg(data_get_type(data) == DATA_TYPE_BOOL, "entry bool type");
ck_assert(key != NULL);
data_set_bool(data, !data_get_bool(data));
return DATA_FOR_EACH_CONT;
}
static data_for_each_cmd_t
_del_dict_bool_true(const char *key, data_t *data, void *arg)
{
int *max = arg;
ck_assert(key != NULL);
ck_assert_msg(data_get_type(data) == DATA_TYPE_BOOL, "entry bool type");
if (*max <= 0)
return DATA_FOR_EACH_STOP;
if (data_get_bool(data)) {
*max -= 1;
return DATA_FOR_EACH_DELETE;
}
return DATA_FOR_EACH_CONT;
}
static data_for_each_cmd_t _del_list_odd(data_t *data, void *arg)
{
int *max = arg;
ck_assert_msg(data_get_type(data) == DATA_TYPE_INT_64,
"entry int type");
if (*max <= 0)
return DATA_FOR_EACH_STOP;
if (data_get_int(data) % 2 == 1) {
*max -= 1;
return DATA_FOR_EACH_DELETE;
}
return DATA_FOR_EACH_CONT;
}
data_for_each_cmd_t _check_list_order(const data_t *data, void *arg)
{
int *found = arg;
ck_assert_msg(data_get_int(data) == *found,
"check value");
*found += 1;
return DATA_FOR_EACH_CONT;
}
START_TEST(test_list_iteration)
{
int max;
int found = 0;
data_t *d = data_new();
data_set_list(d);
ck_assert_msg(data_get_type(d) == DATA_TYPE_LIST, "check list type");
data_set_int(data_list_append(d), 5);
data_set_int(data_list_prepend(d), 4);
data_set_int(data_list_append(d), 6);
data_set_int(data_list_prepend(d), 3);
data_set_int(data_list_append(d), 7);
data_set_int(data_list_prepend(d), 2);
data_set_int(data_list_append(d), 8);
data_set_int(data_list_prepend(d), 1);
data_set_int(data_list_append(d), 9);
data_set_int(data_list_prepend(d), 0);
ck_assert_msg(data_get_type(d) == DATA_TYPE_LIST, "check list type");
ck_assert_msg(data_get_list_length(d) == 10, "list count");
found = 0;
ck_assert_msg(data_list_for_each_const(d, _check_list_order, &found) ==
10, "order touch count");
ck_assert_msg(found == 10, "check max found");
data_set_int(data_list_append(d), 10);
found = 0;
ck_assert_msg(data_list_for_each_const(d, _check_list_order, &found) ==
11, "order touch count");
ck_assert_msg(found == 11, "check max found");
max = 1;
data_list_for_each(d, _del_list_odd, &max);
ck_assert_msg(data_get_list_length(d) == 10, "list count");
ck_assert_msg(max == 0, "check remove count");
max = 20;
data_list_for_each(d, _del_list_odd, &max);
ck_assert_msg(data_get_list_length(d) == 6, "list count");
ck_assert_msg(max == 16, "check remove count");
FREE_NULL_DATA(d);
}
END_TEST
START_TEST(test_dict_iteration)
{
int max;
int found = 0;
data_t *d = data_new();
data_set_dict(d);
data_set_bool(data_key_set(d, "true1"), true);
data_set_bool(data_key_set(d, "true2"), true);
data_set_bool(data_key_set(d, "true3"), true);
data_set_bool(data_key_set(d, "true4"), true);
data_set_bool(data_key_set(d, "true5"), true);
data_set_bool(data_key_set(d, "false1"), false);
data_set_bool(data_key_set(d, "false2"), false);
data_set_bool(data_key_set(d, "false3"), false);
data_set_bool(data_key_set(d, "false4"), false);
data_set_bool(data_key_set(d, "false5"), false);
ck_assert_msg(data_get_dict_length(d) == 10, "dict cardinality");
ck_assert_msg(data_dict_for_each_const(d, _find_dict_bool, &found) ==
10, "find true");
ck_assert_msg(found == 5, "found true");
ck_assert_msg(data_dict_for_each(d, _invert_dict_bool, NULL) == 10,
"invert true");
ck_assert_msg(data_get_dict_length(d) == 10, "dict cardinality");
found = 0;
ck_assert_msg(data_dict_for_each_const(d, _find_dict_bool, &found) ==
10, "find true");
ck_assert_msg(found == 5, "found true");
max = 1;
data_dict_for_each(d, _del_dict_bool_true, &max);
ck_assert_msg(max == 0, "remove 1 true");
found = 0;
ck_assert_msg(data_dict_for_each_const(d, _find_dict_bool, &found) == 9,
"find true");
ck_assert_msg(found == 4, "found true");
ck_assert_msg(data_get_dict_length(d) == 9, "dict cardinality");
max = 0;
data_dict_for_each(d, _del_dict_bool_true, &max);
ck_assert_msg(max == 0, "no op remove");
ck_assert_msg(data_get_dict_length(d) == 9,
"dict cardinality after no op");
max = 4;
data_dict_for_each(d, _del_dict_bool_true, &max);
ck_assert_msg(max == 0, "remove all true");
ck_assert_msg(data_get_dict_length(d) == 5, "dict cardinality");
FREE_NULL_DATA(d);
}
END_TEST
START_TEST(test_dict_typeset)
{
data_t *d = data_new();
ck_assert_msg(data_get_type(d) == DATA_TYPE_NULL, "default type");
data_set_dict(d);
ck_assert_msg(data_get_type(d) == DATA_TYPE_DICT, "dict type");
ck_assert_msg(data_get_dict_length(d) == 0, "dict cardinality");
data_key_set(d, "test1");
data_key_set(d, "test2");
data_key_set(d, "test3");
data_key_set(d, "test4");
data_key_set(d, "test5");
ck_assert_msg(data_get_dict_length(d) == 5, "dict cardinality");
data_set_list(d);
ck_assert_msg(data_get_type(d) == DATA_TYPE_LIST, "list type");
ck_assert_msg(data_get_list_length(d) == 0, "list cardinality");
data_list_append(d);
data_list_prepend(d);
data_list_prepend(d);
data_list_append(d);
data_list_append(d);
ck_assert_msg(data_get_list_length(d) == 5, "list cardinality");
data_set_int(d, 100);
ck_assert_msg(data_get_type(d) == DATA_TYPE_INT_64, "int type");
ck_assert_msg(data_get_int(d) == 100, "check int value");
char *str = NULL;
ck_assert_msg(data_get_string_converted(d, &str) == 0,
"convert 100 to string");
ck_assert_msg(xstrcmp(str, "100") == 0, "check 100 got converted");
xfree(str);
ck_assert_msg(data_convert_type(d, DATA_TYPE_STRING) ==
DATA_TYPE_STRING, "convert 100 to string");
ck_assert_msg(data_get_type(d) == DATA_TYPE_STRING, "int type");
ck_assert_msg(xstrcmp(data_get_string(d), "100") == 0,
"check 100 got converted");
int64_t b = 0;
ck_assert_msg(data_get_int_converted(d, &b) == 0,
"convert 100 from string");
ck_assert_msg(data_get_type(d) == DATA_TYPE_STRING,
"check still string type");
ck_assert_msg(b == 100, "check string conversion from 100");
ck_assert_msg(data_convert_type(d, DATA_TYPE_INT_64) ==
DATA_TYPE_INT_64, "convert 100 from string");
ck_assert_msg(data_get_type(d) == DATA_TYPE_INT_64, "int type");
ck_assert_msg(data_get_int(d) == 100,
"check string conversion from 100");
data_set_float(d, 3.14);
ck_assert_msg(data_get_type(d) == DATA_TYPE_FLOAT, "float type");
str = NULL;
ck_assert_msg(data_get_string_converted(d, &str) == 0,
"convert 3.14 to string");
ck_assert_msg(xstrcmp(str, "3.140000") == 0,
"check 3.14 got converted");
xfree(str);
ck_assert_msg(data_get_type(d) == DATA_TYPE_FLOAT, "float type");
ck_assert_msg(data_convert_type(d, DATA_TYPE_FLOAT) == DATA_TYPE_FLOAT,
"convert 100 from string");
ck_assert_msg(data_get_type(d) == DATA_TYPE_FLOAT, "int type");
ck_assert_msg(data_get_float(d) == 3.14,
"check string conversion from 3.14");
data_set_float(d, -3.14);
ck_assert_msg(data_get_type(d) == DATA_TYPE_FLOAT, "float type");
str = NULL;
ck_assert_msg(data_get_string_converted(d, &str) == 0,
"convert -3.14 to string");
ck_assert_msg(xstrcmp(str, "-3.140000") == 0,
"check -3.14 got converted");
xfree(str);
ck_assert_msg(data_get_type(d) == DATA_TYPE_FLOAT, "float type");
ck_assert_msg(data_get_float(d) == -3.14,
"check string conversion from -3.14");
data_set_null(d);
ck_assert_msg(data_get_type(d) == DATA_TYPE_NULL, "default type");
FREE_NULL_DATA(d);
ck_assert_msg(d == NULL, "free check");
}
END_TEST
START_TEST(test_detection)
{
data_t *d = data_new();
check_with_data_get_bool_converted("1", true);
check_with_data_get_bool_converted("100", true);
check_with_data_get_bool_converted("-100", true);
check_with_data_get_bool_converted("true", true);
check_with_data_get_bool_converted("taco", true);
check_with_data_get_bool_converted("0", false);
check_with_data_get_bool_converted("false", false);
check_with_data_get_bool_converted("-0", false);
check_with_data_get_bool_converted(NULL, false);
FREE_NULL_DATA(d);
}
END_TEST
Suite *suite_data(void)
{
Suite *s = suite_create("Data");
TCase *tc_core = tcase_create("Data");
tcase_add_test(tc_core, test_detection);
tcase_add_test(tc_core, test_dict_typeset);
tcase_add_test(tc_core, test_dict_iteration);
tcase_add_test(tc_core, test_list_iteration);
suite_add_tcase(s, tc_core);
return s;
}
int main(void)
{
int number_failed;
log_options_t log_opts = LOG_OPTS_INITIALIZER;
const char *debug_env = getenv("SLURM_DEBUG");
const char *debug_flags_env = getenv("SLURM_DEBUG_FLAGS");
if (debug_env)
log_opts.stderr_level = log_string2num(debug_env);
if (debug_flags_env)
debug_str2flags(debug_flags_env, &slurm_conf.debug_flags);
log_init("data-test", log_opts, 0, NULL);
SRunner *sr = srunner_create(suite_data());
srunner_run_all(sr, CK_ENV);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
log_fini();
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
|