File: hostlist_nth-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 (197 lines) | stat: -rw-r--r-- 5,891 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
/*****************************************************************************\
 *  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 <signal.h>

#include "slurm/slurm.h"
#include "src/common/hostlist.h"

/* these are not in slurm.h */
char *slurm_hostlist_nth(hostlist_t *, int);
int slurm_hostlist_delete_nth(hostlist_t *, int);
char *slurm_hostset_nth(hostset_t *, int);
hostset_t *slurm_hostset_create(const char*);
void slurm_hostset_destroy(hostset_t *);
int slurm_hostset_count(hostset_t *);

#ifndef NDEBUG
/* note: only works in CK_FORK mode */
START_TEST(hostlist_nth_lo_assert_check)
{
	hostlist_t *hl = slurm_hostlist_create("host1,host2");
	ck_assert(hl != NULL);

	/* expect SIGABRT */
	/* index must be >= 0 */
	slurm_hostlist_nth(hl, -1);
}
END_TEST

/* note: only works in CK_FORK mode */
START_TEST(hostlist_delete_nth_hi_assert_check)
{
	hostlist_t *hl = slurm_hostlist_create("host1,host2");
	ck_assert(hl != NULL);

	/* expect SIGABRT */
	/* index must be < 2 */
	slurm_hostlist_delete_nth(hl, 2);
}
END_TEST
#endif

START_TEST(hostlist_nth_check)
{
	hostlist_t *hl = NULL;
	char *p;
	int n;

	ck_assert(slurm_hostlist_nth(hl, 0) == NULL);

	hl = slurm_hostlist_create("host[1-3],host5");
	ck_assert(hl != NULL);

	n = slurm_hostlist_count(hl);
	ck_assert_int_eq(n, 4);

	p = slurm_hostlist_nth(hl, 0);
	ck_assert_str_eq(p, "host1");
	free(p);

	p = slurm_hostlist_nth(hl, 1);
	ck_assert_str_eq(p, "host2");
	free(p);

	p = slurm_hostlist_nth(hl, 2);
	ck_assert_str_eq(p, "host3");
	free(p);

	p = slurm_hostlist_nth(hl, 3);
	ck_assert_str_eq(p, "host5");
	free(p);

	ck_assert_int_eq(slurm_hostlist_delete_nth(hl, 0), 1);
	ck_assert_int_eq(slurm_hostlist_count(hl), n-1);

	ck_assert_int_eq(slurm_hostlist_delete_nth(hl, 0), 1);
	ck_assert_int_eq(slurm_hostlist_count(hl), n-2);

	ck_assert_int_eq(slurm_hostlist_delete_nth(hl, 0), 1);
	ck_assert_int_eq(slurm_hostlist_count(hl), n-3);

	ck_assert_int_eq(slurm_hostlist_delete_nth(hl, 0), 1);
	ck_assert_int_eq(slurm_hostlist_count(hl), n-4);

	slurm_hostlist_destroy(hl);
}
END_TEST

START_TEST(hostset_nth_check)
{
	hostset_t *hs;
	char *p;
	int n;

	hs = slurm_hostset_create("two[1-2]");
	ck_assert(hs != NULL);

	n = slurm_hostset_count(hs);
	ck_assert_int_eq(n, 2);

	p = slurm_hostset_nth(hs, 0);
	ck_assert_str_eq(p, "two1");
	free(p);

	p = slurm_hostset_nth(hs, 1);
	ck_assert_str_eq(p, "two2");
	free(p);

	slurm_hostset_destroy(hs);
}
END_TEST

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

Suite *make_nonassert_suite(void)
{
	Suite *s = suite_create("host_nth_check_nonassert");
	TCase *tc_core = tcase_create("host_nth_check_nonassert");
	tcase_add_test(tc_core, hostlist_nth_check);
	tcase_add_test(tc_core, hostset_nth_check);
	suite_add_tcase(s, tc_core);
	return s;
}

#ifndef NDEBUG
Suite *make_assert_suite(void)
{
	Suite *s = suite_create("host_nth_check_assert");
	TCase *tc_core = tcase_create("host_nth_check_assert");
	tcase_add_test_raise_signal(tc_core, hostlist_nth_lo_assert_check, SIGABRT);
	tcase_add_test_raise_signal(tc_core, hostlist_delete_nth_hi_assert_check, SIGABRT);
	suite_add_tcase(s, tc_core);
	return s;
}
#endif

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

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

#ifdef NDEBUG
	printf("Can't perform assert tests with NDEBUG set.\n");
#else
	/* assert tests can only be run in forking mode */
	if (srunner_fork_status(sr) == CK_FORK)
		srunner_add_suite(sr, make_assert_suite());
	else
		printf("Skipping assert tests since not in forking mode.\n");
#endif

	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;
}