Skip to content

Commit 49b5dde

Browse files
author
Ken Gaillot
authored
Merge pull request #2014 from clumens/infinity
Fix: libcrmcommon: Add pcmk_str_is_infinity and pcmk_str_is_minus_inf…
2 parents ef12081 + 5c76169 commit 49b5dde

9 files changed

Lines changed: 135 additions & 11 deletions

File tree

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,6 +2009,7 @@ AC_CONFIG_FILES(Makefile \
20092009
lib/common/Makefile \
20102010
lib/common/tests/Makefile \
20112011
lib/common/tests/strings/Makefile \
2012+
lib/common/tests/utils/Makefile \
20122013
lib/cluster/Makefile \
20132014
lib/cib/Makefile \
20142015
lib/gnu/Makefile \

daemons/controld/controld_te_events.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ update_failcount(xmlNode * event, const char *event_node_uuid, int rc,
156156
}
157157

158158
/* Fail count will be either incremented or set to infinity */
159-
if (value == NULL || safe_str_neq(value, CRM_INFINITY_S)) {
159+
if (!pcmk_str_is_infinity(value)) {
160160
value = XML_NVPAIR_ATTR_VALUE "++";
161161
}
162162

include/crm/common/util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ void crm_gnutls_global_init(void);
205205

206206
char *pcmk_hostname(void);
207207

208+
bool pcmk_str_is_infinity(const char *s);
209+
bool pcmk_str_is_minus_infinity(const char *s);
210+
208211
#ifndef PCMK__NO_COMPAT
209212
/* Everything here is deprecated and kept only for public API backward
210213
* compatibility. It will be moved to compatibility.h when 2.1.0 is released.

lib/common/options.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ pcmk__valid_number(const char *value)
382382
if (value == NULL) {
383383
return false;
384384

385-
} else if (safe_str_eq(value, CRM_MINUS_INFINITY_S)
386-
|| safe_str_eq(value, CRM_INFINITY_S)) {
385+
} else if (pcmk_str_is_minus_infinity(value) ||
386+
pcmk_str_is_infinity(value)) {
387387
return true;
388388
}
389389

@@ -395,8 +395,8 @@ pcmk__valid_number(const char *value)
395395
bool
396396
pcmk__valid_positive_number(const char *value)
397397
{
398-
return safe_str_eq(value, CRM_INFINITY_S)
399-
|| (crm_parse_ll(value, NULL) > 0);
398+
return pcmk_str_is_infinity(value) ||
399+
(crm_parse_ll(value, NULL) > 0);
400400
}
401401

402402
bool

lib/common/tests/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
SUBDIRS = strings
1+
SUBDIRS = strings utils

lib/common/tests/utils/Makefile.am

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
2+
LDADD = $(top_builddir)/lib/common/libcrmcommon.la
3+
4+
include $(top_srcdir)/mk/glib-tap.mk
5+
6+
# Add each test program here. Each test should be written as a little standalone
7+
# program using the glib unit testing functions. See the documentation for more
8+
# information.
9+
#
10+
# https://developer.gnome.org/glib/unstable/glib-Testing.html
11+
test_programs = pcmk_str_is_infinity \
12+
pcmk_str_is_minus_infinity
13+
14+
# If any extra data needs to be added to the source distribution, add it to the
15+
# following list.
16+
dist_test_data =
17+
18+
# If any extra data needs to be used by tests but should not be added to the
19+
# source distribution, add it to the following list.
20+
test_data =
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <glib.h>
2+
3+
#include <crm_internal.h>
4+
5+
static void
6+
uppercase_str_passes(void) {
7+
g_assert(pcmk_str_is_infinity("INFINITY") == TRUE);
8+
g_assert(pcmk_str_is_infinity("+INFINITY") == TRUE);
9+
}
10+
11+
static void
12+
mixed_case_str_fails(void) {
13+
g_assert(pcmk_str_is_infinity("infinity") == FALSE);
14+
g_assert(pcmk_str_is_infinity("+infinity") == FALSE);
15+
g_assert(pcmk_str_is_infinity("Infinity") == FALSE);
16+
g_assert(pcmk_str_is_infinity("+Infinity") == FALSE);
17+
}
18+
19+
static void
20+
added_whitespace_fails(void) {
21+
g_assert(pcmk_str_is_infinity(" INFINITY") == FALSE);
22+
g_assert(pcmk_str_is_infinity("INFINITY ") == FALSE);
23+
g_assert(pcmk_str_is_infinity(" INFINITY ") == FALSE);
24+
g_assert(pcmk_str_is_infinity("+ INFINITY") == FALSE);
25+
}
26+
27+
static void
28+
empty_str_fails(void) {
29+
g_assert(pcmk_str_is_infinity(NULL) == FALSE);
30+
g_assert(pcmk_str_is_infinity("") == FALSE);
31+
}
32+
33+
static void
34+
minus_infinity_fails(void) {
35+
g_assert(pcmk_str_is_infinity("-INFINITY") == FALSE);
36+
}
37+
38+
int main(int argc, char **argv) {
39+
g_test_init(&argc, &argv, NULL);
40+
41+
g_test_add_func("/common/utils/infinity/uppercase", uppercase_str_passes);
42+
g_test_add_func("/common/utils/infinity/mixed_case", mixed_case_str_fails);
43+
g_test_add_func("/common/utils/infinity/whitespace", added_whitespace_fails);
44+
g_test_add_func("/common/utils/infinity/empty", empty_str_fails);
45+
g_test_add_func("/common/utils/infinity/minus_infinity", minus_infinity_fails);
46+
47+
return g_test_run();
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <glib.h>
2+
3+
#include <crm_internal.h>
4+
5+
static void
6+
uppercase_str_passes(void) {
7+
g_assert(pcmk_str_is_minus_infinity("-INFINITY") == TRUE);
8+
}
9+
10+
static void
11+
mixed_case_str_fails(void) {
12+
g_assert(pcmk_str_is_minus_infinity("-infinity") == FALSE);
13+
g_assert(pcmk_str_is_minus_infinity("-Infinity") == FALSE);
14+
}
15+
16+
static void
17+
added_whitespace_fails(void) {
18+
g_assert(pcmk_str_is_minus_infinity(" -INFINITY") == FALSE);
19+
g_assert(pcmk_str_is_minus_infinity("-INFINITY ") == FALSE);
20+
g_assert(pcmk_str_is_minus_infinity(" -INFINITY ") == FALSE);
21+
g_assert(pcmk_str_is_minus_infinity("- INFINITY") == FALSE);
22+
}
23+
24+
static void
25+
empty_str_fails(void) {
26+
g_assert(pcmk_str_is_minus_infinity(NULL) == FALSE);
27+
g_assert(pcmk_str_is_minus_infinity("") == FALSE);
28+
}
29+
30+
static void
31+
infinity_fails(void) {
32+
g_assert(pcmk_str_is_minus_infinity("INFINITY") == FALSE);
33+
}
34+
35+
int main(int argc, char **argv) {
36+
g_test_init(&argc, &argv, NULL);
37+
38+
g_test_add_func("/common/utils/minus_infinity/uppercase", uppercase_str_passes);
39+
g_test_add_func("/common/utils/minus_infinity/mixed_case", mixed_case_str_fails);
40+
g_test_add_func("/common/utils/minus_infinity/whitespace", added_whitespace_fails);
41+
g_test_add_func("/common/utils/minus_infinity/empty", empty_str_fails);
42+
g_test_add_func("/common/utils/minus_infinity/infinity", infinity_fails);
43+
44+
return g_test_run();
45+
}

lib/common/utils.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,10 @@ char2score(const char *score)
6767

6868
if (score == NULL) {
6969

70-
} else if (safe_str_eq(score, CRM_MINUS_INFINITY_S)) {
70+
} else if (pcmk_str_is_minus_infinity(score)) {
7171
score_f = -CRM_SCORE_INFINITY;
7272

73-
} else if (safe_str_eq(score, CRM_INFINITY_S)) {
74-
score_f = CRM_SCORE_INFINITY;
75-
76-
} else if (safe_str_eq(score, CRM_PLUS_INFINITY_S)) {
73+
} else if (pcmk_str_is_infinity(score)) {
7774
score_f = CRM_SCORE_INFINITY;
7875

7976
} else if (safe_str_eq(score, "red")) {
@@ -652,3 +649,13 @@ pcmk_hostname()
652649

653650
return (uname(&hostinfo) < 0)? NULL : strdup(hostinfo.nodename);
654651
}
652+
653+
bool
654+
pcmk_str_is_infinity(const char *s) {
655+
return crm_str_eq(s, CRM_INFINITY_S, TRUE) || crm_str_eq(s, CRM_PLUS_INFINITY_S, TRUE);
656+
}
657+
658+
bool
659+
pcmk_str_is_minus_infinity(const char *s) {
660+
return crm_str_eq(s, CRM_MINUS_INFINITY_S, TRUE);
661+
}

0 commit comments

Comments
 (0)