File: xstring-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 (124 lines) | stat: -rw-r--r-- 4,256 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
/*****************************************************************************\
 *  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 <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <check.h>

#include <src/common/xmalloc.h>
#include <src/common/xstring.h>

typedef struct {
	const char *input;
	const char *expected;
} xstrtrim_data_t;

static xstrtrim_data_t xstrtrim_data[] = {
	{"",""},
	{" a ", "a"},
	{"", ""},
	{" a ", "a"},
	{"   ", ""},
	{"	   ", ""},
	/* test with spaces */
	{" aaaaaaaa ", "aaaaaaaa"},
	{"  aaaaaaaa ", "aaaaaaaa"},
	{"  aaaaaaaa  ", "aaaaaaaa"},
	{" aaaaaaaa  ", "aaaaaaaa"},
	{"           aaaaaaaa ", "aaaaaaaa"},
	{"           aaaaaaaa           ", "aaaaaaaa"},
	{"aaaaaaaa           ", "aaaaaaaa"},
	{"aaaaaaaa", "aaaaaaaa"},
	{"aa   aa  aa   aa", "aa   aa  aa   aa"},
	{"      aa   aa  aa   aa", "aa   aa  aa   aa"},
	{"      aa   aa  aa   aa       ", "aa   aa  aa   aa"},
	{"aa   aa  aa   aa       ", "aa   aa  aa   aa"},
	/* test with spaces and tabs */
	{"	", ""},
	{"  	", ""},
	{"  	  ", ""},
	{"  	  	", ""},
	{"	aaaaaaaa	", "aaaaaaaa"},
	{"           	aaaaaaaa	", "aaaaaaaa"},
	{"           	aaaaaaaa           	", "aaaaaaaa"},
	{"aaaaaaaa	           ", "aaaaaaaa"},
	{"aaaaaaaa", "aaaaaaaa"},
	{"aa   	aa  aa   	aa", "aa   	aa  aa   	aa"},
	{"      	aa   aa  	 aa   	aa", "aa   aa  	 aa   	aa"},
	{"	      aa   aa  aa   aa       	", "aa   aa  aa   aa"},
	{"aa   aa  aa   aa       	", "aa   aa  aa   aa"},
	{"aa   aa  aa   aa       	", "aa   aa  aa   aa"}
};

/* check xstrtrim against given expected xstrtrim result */
START_TEST(test_xstrtrim)
{
	const char *input = xstrtrim_data[_i].input;
	const char *expected = xstrtrim_data[_i].expected;
	char *buf = xstrdup(input);

	xstrtrim(buf);
	ck_assert_msg(strcmp(buf, expected) == 0, "check xstrtrim: \"%s\" -> \"%s\" == \"%s\"",
		      input, buf, expected);

	xfree(buf);
}
END_TEST

Suite *xstring_suite(void)
{
	Suite *s = suite_create("xstring");
	TCase *tc_core = tcase_create("Core");
	tcase_add_loop_test(tc_core, test_xstrtrim, 0 , sizeof(xstrtrim_data) /
			    sizeof(xstrtrim_data_t) );
	suite_add_tcase(s, tc_core);
	return s;
}

int main(int argc, char *argv[])
{
	log_options_t log_opts = LOG_OPTS_INITIALIZER;
	log_opts.stderr_level = LOG_LEVEL_DEBUG5;
	log_init("xstring-test", log_opts, 0, NULL);

	int number_failed;
	SRunner *sr = srunner_create(xstring_suite());

	srunner_run_all(sr, CK_ENV);
	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

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