File: xlate_array_task_str-test.c

package info (click to toggle)
slurm-wlm-contrib 24.11.5-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 50,596 kB
  • sloc: ansic: 529,598; exp: 64,795; python: 17,051; sh: 9,411; javascript: 6,528; makefile: 4,030; perl: 3,762; pascal: 131
file content (237 lines) | stat: -rw-r--r-- 8,579 bytes parent folder | download | duplicates (5)
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
/*****************************************************************************\
 *  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 <check.h>
#include <stdio.h>
#include <stdlib.h>

#include "src/common/slurm_protocol_defs.h"
#include "src/common/xmalloc.h"
#include "src/common/xstring.h"

START_TEST(null_test)
{
	bitstr_t *array_bitmap = NULL;
	char *array_task_str = NULL;

	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(array_task_str == NULL);
	ck_assert(array_bitmap == NULL);

	array_task_str = xstrdup("");
	array_bitmap = bit_alloc(1);
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(array_task_str[0] == '\0');
	ck_assert(array_bitmap == NULL);

	/* Test must have 0x at front */
	array_task_str = xstrdup("h");
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "h"));
	ck_assert(array_bitmap == NULL);

	array_task_str = xstrdup("hello");
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "hello"));
	ck_assert(array_bitmap == NULL);
}
END_TEST

START_TEST(good_test)
{
	bitstr_t *array_bitmap = NULL;
	char *array_task_str = NULL;

	array_task_str = xstrdup("0x7");
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "0-2"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "0-2"));

	array_task_str = xstrdup("0x9C6");
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "1-2,6-8,11"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "1-2,6-8,11"));

	array_task_str = xstrdup("0x9C6");
	xlate_array_task_str(&array_task_str, 9, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "1-2,6-8,11%9"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "1-2,6-8,11"));

	/* Max task count */
	array_task_str = xstrdup("0x9C6");
	xlate_array_task_str(&array_task_str, 9, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "1-2,6-8,11%9"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "1-2,6-8,11"));

	/* Stepped task */
	array_task_str = xstrdup("0x55554");
	xlate_array_task_str(&array_task_str, 9, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "2-18:2%9"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "2,4,6,8,10,12,14,16,18"));

	/* Broken up stepped task */
	array_task_str = xstrdup("0x45174");
	xlate_array_task_str(&array_task_str, 9, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "2,4-6,8,12,14,18%9"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "2,4-6,8,12,14,18"));

	/* test with array_bitmap -- frees task_bitmap */
	array_task_str = xstrdup("0x55154");
	xlate_array_task_str(&array_task_str, 9, NULL);
	ck_assert(!xstrcmp(array_task_str, "2,4,6,8,12,14,16,18%9"));

}
END_TEST

START_TEST(BITSTR_LEN_no_max_test)
{
	bitstr_t *array_bitmap = NULL;
	char *array_task_str = NULL;

	/* SLURM_BITSTR_LEN */
	setenv("SLURM_BITSTR_LEN", "10", 1);
	array_task_str = xstrdup("0x55154");
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "2,4,6,..."));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "2,4,6,8,12,14,16,18"));
}
END_TEST

START_TEST(BITSTR_LEN_with_max_test)
{
	bitstr_t *array_bitmap = NULL;
	char *array_task_str = NULL;

	/* SLURM_BITSTR_LEN */
	setenv("SLURM_BITSTR_LEN", "10", 1);
	array_task_str = xstrdup("0x55154");
	xlate_array_task_str(&array_task_str, 9, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "2,4,6,...%9"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "2,4,6,8,12,14,16,18"));
}
END_TEST

START_TEST(BITSTR_LEN_negative_test)
{
	bitstr_t *array_bitmap = NULL;
	char *array_task_str = NULL;

	setenv("SLURM_BITSTR_LEN", "-1", 1);
	array_task_str = xstrdup("0x5555555555155");
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "0,2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44..."));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "0,2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50"));
}
END_TEST

START_TEST(BITSTR_LEN_negative_max_test)
{
	bitstr_t *array_bitmap = NULL;
	char *array_task_str = NULL;

	setenv("SLURM_BITSTR_LEN", "-1", 1);
	array_task_str = xstrdup("0x5555555555155");
	xlate_array_task_str(&array_task_str, 9, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "0,2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44...%9"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "0,2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50"));
}
END_TEST

START_TEST(BITSTR_LEN_65_test)
{
	bitstr_t *array_bitmap = NULL;
	char *array_task_str = NULL;

	setenv("SLURM_BITSTR_LEN", "65", 1);
	array_task_str = xstrdup("0x5555555555155");
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "0,2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,..."));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "0,2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50"));
}
END_TEST

START_TEST(BITSTR_LEN_0_test)
{
	bitstr_t *array_bitmap = NULL;
	char *array_task_str = NULL;

	setenv("SLURM_BITSTR_LEN", "0", 1);
	array_task_str = xstrdup("0x5555555555155");
	xlate_array_task_str(&array_task_str, 0, &array_bitmap);
	ck_assert(!xstrcmp(array_task_str, "0,2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50"));
	ck_assert(!xstrcmp(bit_fmt_full(array_bitmap), "0,2,4,6,8,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50"));
}
END_TEST

/*****************************************************************************
 * TEST SUITE                                                                *
 ****************************************************************************/

Suite *suite(void)
{
	Suite *s = suite_create("bit_unfmt_hexmask test");
	TCase *tc_core = tcase_create("Testing bit_unfmt_hexmask");

	tcase_add_test(tc_core, null_test);
	tcase_add_test(tc_core, good_test);

	/* has to be run in forked mode because bitstr_len is a static */
	tcase_add_test(tc_core, BITSTR_LEN_no_max_test);
	tcase_add_test(tc_core, BITSTR_LEN_with_max_test);
	tcase_add_test(tc_core, BITSTR_LEN_negative_test);
	tcase_add_test(tc_core, BITSTR_LEN_negative_max_test);
	tcase_add_test(tc_core, BITSTR_LEN_65_test);
	tcase_add_test(tc_core, BITSTR_LEN_0_test);

	suite_add_tcase(s, tc_core);
	return s;
}

/*****************************************************************************
 * TEST RUNNER                                                               *
 ****************************************************************************/

int main(void)
{
	int number_failed;
	SRunner *sr = srunner_create(suite());

	//srunner_set_fork_status(sr, CK_NOFORK);

	srunner_run_all(sr, CK_VERBOSE);
	//srunner_run_all(sr, CK_NORMAL);
	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}