summaryrefslogtreecommitdiffstats
path: root/lib/common/tests/scores
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 06:53:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 06:53:20 +0000
commite5a812082ae033afb1eed82c0f2df3d0f6bdc93f (patch)
treea6716c9275b4b413f6c9194798b34b91affb3cc7 /lib/common/tests/scores
parentInitial commit. (diff)
downloadpacemaker-e5a812082ae033afb1eed82c0f2df3d0f6bdc93f.tar.xz
pacemaker-e5a812082ae033afb1eed82c0f2df3d0f6bdc93f.zip
Adding upstream version 2.1.6.upstream/2.1.6
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/common/tests/scores')
-rw-r--r--lib/common/tests/scores/Makefile.am19
-rw-r--r--lib/common/tests/scores/char2score_test.c75
-rw-r--r--lib/common/tests/scores/pcmk__add_scores_test.c74
-rw-r--r--lib/common/tests/scores/pcmk_readable_score_test.c33
4 files changed, 201 insertions, 0 deletions
diff --git a/lib/common/tests/scores/Makefile.am b/lib/common/tests/scores/Makefile.am
new file mode 100644
index 0000000..66ca073
--- /dev/null
+++ b/lib/common/tests/scores/Makefile.am
@@ -0,0 +1,19 @@
+#
+# Copyright 2020-2022 the Pacemaker project contributors
+#
+# The version control history for this file may have further details.
+#
+# This source code is licensed under the GNU General Public License version 2
+# or later (GPLv2+) WITHOUT ANY WARRANTY.
+#
+
+include $(top_srcdir)/mk/tap.mk
+include $(top_srcdir)/mk/unittest.mk
+
+# Add "_test" to the end of all test program names to simplify .gitignore.
+check_PROGRAMS = \
+ char2score_test \
+ pcmk__add_scores_test \
+ pcmk_readable_score_test
+
+TESTS = $(check_PROGRAMS)
diff --git a/lib/common/tests/scores/char2score_test.c b/lib/common/tests/scores/char2score_test.c
new file mode 100644
index 0000000..fbba12a
--- /dev/null
+++ b/lib/common/tests/scores/char2score_test.c
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2022 the Pacemaker project contributors
+ *
+ * The version control history for this file may have further details.
+ *
+ * This source code is licensed under the GNU General Public License version 2
+ * or later (GPLv2+) WITHOUT ANY WARRANTY.
+ */
+
+#include <crm_internal.h>
+
+#include <crm/common/unittest_internal.h>
+
+extern int pcmk__score_red;
+extern int pcmk__score_green;
+extern int pcmk__score_yellow;
+
+static void
+empty_input(void **state)
+{
+ assert_int_equal(char2score(NULL), 0);
+}
+
+static void
+bad_input(void **state)
+{
+ assert_int_equal(char2score("PQRST"), 0);
+ assert_int_equal(char2score("3.141592"), 3);
+ assert_int_equal(char2score("0xf00d"), 0);
+}
+
+static void
+special_values(void **state)
+{
+ assert_int_equal(char2score("-INFINITY"), -CRM_SCORE_INFINITY);
+ assert_int_equal(char2score("INFINITY"), CRM_SCORE_INFINITY);
+ assert_int_equal(char2score("+INFINITY"), CRM_SCORE_INFINITY);
+
+ pcmk__score_red = 10;
+ pcmk__score_green = 20;
+ pcmk__score_yellow = 30;
+
+ assert_int_equal(char2score("red"), pcmk__score_red);
+ assert_int_equal(char2score("green"), pcmk__score_green);
+ assert_int_equal(char2score("yellow"), pcmk__score_yellow);
+
+ assert_int_equal(char2score("ReD"), pcmk__score_red);
+ assert_int_equal(char2score("GrEeN"), pcmk__score_green);
+ assert_int_equal(char2score("yElLoW"), pcmk__score_yellow);
+}
+
+/* These ridiculous macros turn an integer constant into a string constant. */
+#define A(x) #x
+#define B(x) A(x)
+
+static void
+outside_limits(void **state)
+{
+ assert_int_equal(char2score(B(CRM_SCORE_INFINITY) "00"), CRM_SCORE_INFINITY);
+ assert_int_equal(char2score("-" B(CRM_SCORE_INFINITY) "00"), -CRM_SCORE_INFINITY);
+}
+
+static void
+inside_limits(void **state)
+{
+ assert_int_equal(char2score("1234"), 1234);
+ assert_int_equal(char2score("-1234"), -1234);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(empty_input),
+ cmocka_unit_test(bad_input),
+ cmocka_unit_test(special_values),
+ cmocka_unit_test(outside_limits),
+ cmocka_unit_test(inside_limits))
diff --git a/lib/common/tests/scores/pcmk__add_scores_test.c b/lib/common/tests/scores/pcmk__add_scores_test.c
new file mode 100644
index 0000000..85ac232
--- /dev/null
+++ b/lib/common/tests/scores/pcmk__add_scores_test.c
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2022 the Pacemaker project contributors
+ *
+ * The version control history for this file may have further details.
+ *
+ * This source code is licensed under the GNU General Public License version 2
+ * or later (GPLv2+) WITHOUT ANY WARRANTY.
+ */
+
+#include <crm_internal.h>
+
+#include <crm/common/unittest_internal.h>
+
+static void
+score1_minus_inf(void **state)
+{
+ assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, -1), -CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, 0), -CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, 1), -CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(-CRM_SCORE_INFINITY, CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
+}
+
+static void
+score2_minus_inf(void **state)
+{
+ assert_int_equal(pcmk__add_scores(-1, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(0, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(1, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, -CRM_SCORE_INFINITY), -CRM_SCORE_INFINITY);
+}
+
+static void
+score1_pos_inf(void **state)
+{
+ assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, CRM_SCORE_INFINITY), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, -1), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, 0), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(CRM_SCORE_INFINITY, 1), CRM_SCORE_INFINITY);
+}
+
+static void
+score2_pos_inf(void **state)
+{
+ assert_int_equal(pcmk__add_scores(-1, CRM_SCORE_INFINITY), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(0, CRM_SCORE_INFINITY), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(1, CRM_SCORE_INFINITY), CRM_SCORE_INFINITY);
+}
+
+static void
+result_infinite(void **state)
+{
+ assert_int_equal(pcmk__add_scores(INT_MAX, INT_MAX), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(INT_MIN, INT_MIN), -CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(2000000, 50), CRM_SCORE_INFINITY);
+ assert_int_equal(pcmk__add_scores(-4000000, 50), -CRM_SCORE_INFINITY);
+}
+
+static void
+result_finite(void **state)
+{
+ assert_int_equal(pcmk__add_scores(0, 0), 0);
+ assert_int_equal(pcmk__add_scores(0, 100), 100);
+ assert_int_equal(pcmk__add_scores(200, 0), 200);
+ assert_int_equal(pcmk__add_scores(200, -50), 150);
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(score1_minus_inf),
+ cmocka_unit_test(score2_minus_inf),
+ cmocka_unit_test(score1_pos_inf),
+ cmocka_unit_test(score2_pos_inf),
+ cmocka_unit_test(result_infinite),
+ cmocka_unit_test(result_finite))
diff --git a/lib/common/tests/scores/pcmk_readable_score_test.c b/lib/common/tests/scores/pcmk_readable_score_test.c
new file mode 100644
index 0000000..ae24159
--- /dev/null
+++ b/lib/common/tests/scores/pcmk_readable_score_test.c
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2022 the Pacemaker project contributors
+ *
+ * The version control history for this file may have further details.
+ *
+ * This source code is licensed under the GNU General Public License version 2
+ * or later (GPLv2+) WITHOUT ANY WARRANTY.
+ */
+
+#include <crm_internal.h>
+
+#include <crm/common/unittest_internal.h>
+
+static void
+outside_limits(void **state)
+{
+ assert_string_equal(pcmk_readable_score(CRM_SCORE_INFINITY * 2),
+ CRM_INFINITY_S);
+ assert_string_equal(pcmk_readable_score(-CRM_SCORE_INFINITY * 2),
+ CRM_MINUS_INFINITY_S);
+}
+
+static void
+inside_limits(void **state)
+{
+ assert_string_equal(pcmk_readable_score(0), "0");
+ assert_string_equal(pcmk_readable_score(1024), "1024");
+ assert_string_equal(pcmk_readable_score(-1024), "-1024");
+}
+
+PCMK__UNIT_TEST(NULL, NULL,
+ cmocka_unit_test(outside_limits),
+ cmocka_unit_test(inside_limits))