File: bit_unfmt_hexmask-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 (153 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (7)
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
#include <check.h>
#include <stdio.h>
#include <stdlib.h>

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

START_TEST(null_test)
{
	bitstr_t *bit_str = bit_alloc(64);
	char *hex_str = NULL;

	ck_assert(bit_unfmt_hexmask(NULL, NULL) == -1);
	ck_assert(bit_unfmt_hexmask(NULL, hex_str) == -1);
	ck_assert(bit_unfmt_hexmask(bit_str, NULL) == -1);

	ck_assert(bit_unfmt_hexmask(bit_str, "Z") == -1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0xZ") == -1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0xZ0") == -1);

	bit_free(bit_str);
}
END_TEST

START_TEST(bounds_test)
{
	bitstr_t *bit_str;

	bit_str = bit_alloc(1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0x2") == -1);
	bit_free(bit_str);

	bit_str = bit_alloc(1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0x4") == -1);
	bit_free(bit_str);

	bit_str = bit_alloc(1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0x8") == -1);
	bit_free(bit_str);

	bit_str = bit_alloc(1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0x10") == -1);
	bit_free(bit_str);

	bit_str = bit_alloc(1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0x20") == -1);
	bit_free(bit_str);

	bit_str = bit_alloc(1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0x40") == -1);
	bit_free(bit_str);

	bit_str = bit_alloc(1);
	ck_assert(bit_unfmt_hexmask(bit_str, "0x80") == -1);
	bit_free(bit_str);
}
END_TEST

START_TEST(good_test)
{
	int rc;
	bitstr_t *bit_str = bit_alloc(64);
	char *out_str;

	rc = bit_unfmt_hexmask(bit_str, "4321");
	ck_assert(rc == 0);
	out_str = bit_fmt_hexmask(bit_str);
	ck_assert(!xstrcmp(out_str, "0x0000000000004321"));
	xfree(out_str);

	rc = bit_unfmt_hexmask(bit_str, "0x4321");
	ck_assert(rc == 0);
	out_str = bit_fmt_hexmask(bit_str);
	ck_assert(!xstrcmp(out_str, "0x0000000000004321"));
	xfree(out_str);

	bit_clear_all(bit_str);
	rc = bit_unfmt_hexmask(bit_str, "0xAbCd");
	ck_assert(rc == 0);
	out_str = bit_fmt_hexmask(bit_str);
	ck_assert(!xstrcmp(out_str, "0x000000000000ABCD"));
	xfree(out_str);

	bit_clear_all(bit_str);
	rc = bit_unfmt_hexmask(bit_str, "0x1248AbCd");
	ck_assert(rc == 0);
	out_str = bit_fmt_hexmask(bit_str);
	ck_assert(!xstrcmp(out_str, "0x000000001248ABCD"));
	xfree(out_str);

	bit_clear_all(bit_str);
	rc = bit_unfmt_hexmask(bit_str, "0x123AbCd");
	ck_assert(rc == 0);
	out_str = bit_fmt_hexmask(bit_str);
	ck_assert(!xstrcmp(out_str, "0x000000000123ABCD"));
	xfree(out_str);

	bit_clear_all(bit_str);
	rc = bit_unfmt_hexmask(bit_str, "0x5555555555155");
	ck_assert(rc == 0);
	out_str = bit_fmt_hexmask(bit_str);
	ck_assert(!xstrcmp(out_str, "0x0005555555555155"));
	xfree(out_str);

	bit_free(bit_str);
	bit_str = bit_alloc(65);

	bit_clear_all(bit_str);
	rc = bit_unfmt_hexmask(bit_str, "0x10000000000000002");
	ck_assert(rc == 0);
	out_str = bit_fmt_hexmask(bit_str);
	ck_assert(!xstrcmp(out_str, "0x10000000000000002"));
	xfree(out_str);

	bit_free(bit_str);
}
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, bounds_test);
	tcase_add_test(tc_core, good_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;
}