The FreeRADIUS server
$Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
src
lib
util
test
misc_tests.c
Go to the documentation of this file.
1
/*
2
* This library is free software; you can redistribute it and/or
3
* modify it under the terms of the GNU Lesser General Public
4
* License as published by the Free Software Foundation; either
5
* version 2.1 of the License, or (at your option) any later version.
6
*
7
* This library is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10
* Lesser General Public License for more details.
11
*
12
* You should have received a copy of the GNU Lesser General Public
13
* License along with this library; if not, write to the Free Software
14
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15
*/
16
17
/** Tests for miscellaneous utility functions
18
*
19
* @file src/lib/util/test/misc_tests.c
20
*
21
* @copyright 2026 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
22
*/
23
#include "
acutest.h
"
24
25
#include <freeradius-devel/util/misc.h>
26
27
static
bool
poisoned
;
28
29
static
fr_cmp_ret_t
_int_ptr_cmp
(
void
const
*one,
void
const
*two)
30
{
31
int
const
*a = one, *b = two;
32
33
if
(
poisoned
) {
34
fr_strerror_const
(
"Poisoned comparator"
);
35
return
CMP_ERR
;
36
}
37
38
return
CMP
(*a, *b);
39
}
40
41
/*
42
* A comparator returning CMP_ERR must leave the array a
43
* permutation of its input (order undefined), and report the
44
* error through the int return.
45
*/
46
static
void
test_quick_sort_cmp_err
(
void
)
47
{
48
int
values[8] = { 5, 1, 7, 3, 0, 6, 2, 4 };
49
int
const
*to_sort[8];
50
bool
seen[8];
51
size_t
i;
52
53
TEST_CASE
(
"comparator error leaves the array a permutation"
);
54
for
(i = 0; i <
NUM_ELEMENTS
(values); i++) {
55
to_sort[i] = &values[i];
56
}
57
58
poisoned
=
true
;
59
TEST_CHECK
(
fr_quick_sort
((
void
const
**)to_sort, 0,
NUM_ELEMENTS
(to_sort) - 1,
_int_ptr_cmp
) == -1);
60
61
/*
62
* Every original element must appear exactly once.
63
*/
64
for
(i = 0; i <
NUM_ELEMENTS
(seen); i++) {
65
seen[i] =
false
;
66
}
67
for
(i = 0; i <
NUM_ELEMENTS
(to_sort); i++) {
68
TEST_CHECK
(*to_sort[i] >= 0 && *to_sort[i] < (
int
)
NUM_ELEMENTS
(seen));
69
TEST_CHECK
(!seen[*to_sort[i]]);
70
seen[*to_sort[i]] =
true
;
71
}
72
73
/*
74
* Once the comparator recovers the sort orders the array.
75
*/
76
poisoned
=
false
;
77
TEST_CHECK
(
fr_quick_sort
((
void
const
**)to_sort, 0,
NUM_ELEMENTS
(to_sort) - 1,
_int_ptr_cmp
) == 0);
78
for
(i = 0; i <
NUM_ELEMENTS
(to_sort); i++) {
79
TEST_CHECK
(*to_sort[i] == (
int
)i);
80
}
81
}
82
83
TEST_LIST
= {
84
{
"fr_quick_sort_cmp_err"
,
test_quick_sort_cmp_err
},
85
86
TEST_TERMINATOR
87
};
acutest.h
TEST_CHECK
#define TEST_CHECK(cond)
Definition
acutest.h:87
TEST_CASE
#define TEST_CASE(name)
Definition
acutest.h:186
TEST_TERMINATOR
#define TEST_TERMINATOR
Definition
acutest.h:64
CMP
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition
build.h:113
NUM_ELEMENTS
#define NUM_ELEMENTS(_t)
Definition
build.h:406
fr_quick_sort
int fr_quick_sort(void const *to_sort[], int start, int end, fr_cmp_t cmp)
Quick sort an array of pointers using a comparator.
Definition
misc.c:495
fr_cmp_ret_t
fr_cmp_ret_t
Result of an ordering comparison.
Definition
misc.h:50
CMP_ERR
@ CMP_ERR
comparison failed
Definition
misc.h:51
TEST_LIST
TEST_LIST
Definition
misc_tests.c:83
test_quick_sort_cmp_err
static void test_quick_sort_cmp_err(void)
Definition
misc_tests.c:46
poisoned
static bool poisoned
Definition
misc_tests.c:27
_int_ptr_cmp
static fr_cmp_ret_t _int_ptr_cmp(void const *one, void const *two)
Definition
misc_tests.c:29
fr_strerror_const
#define fr_strerror_const(_msg)
Definition
strerror.h:223
Generated by
1.9.8