summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2023-03-26 12:25:48 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-03-27 13:58:06 -0700
commitb6f70b5167557c8aa845feb53209d5b6ce58ce09 (patch)
tree3d908b68e074ad0dd8b7f9ae116053780c1b6ff1
parentb08c297a37c2b061de783b1954bb908796fe91b0 (diff)
downloadxorg-lib-libXmu-b6f70b5167557c8aa845feb53209d5b6ce58ce09.tar.gz
test: Add simple test cases for functions in src/CursorName.c
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--test/CursorName.c139
-rw-r--r--test/Makefile.am5
2 files changed, 143 insertions, 1 deletions
diff --git a/test/CursorName.c b/test/CursorName.c
new file mode 100644
index 0000000..a3f8aae
--- /dev/null
+++ b/test/CursorName.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2023, Oracle and/or its affiliates.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/* Test code for XmuCursorNameToIndex() in src/CursorName.c */
+#include "config.h"
+#include <X11/Xmu/CurUtil.h>
+#include <X11/Xmu/CharSet.h>
+#include <glib.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define SKIP_WHITESPACE(p) while (isspace(*p)) p++
+
+/* Looks up each entry from <X11/cursorfont.h> to verify value returned */
+static void
+test_CursorNameToIndex_goodnames(void)
+{
+ FILE *cursorfont;
+ char line[256];
+ int cursorschecked = 0;
+ int cursorsexpected = 0;
+
+ cursorfont = fopen("/usr/include/X11/cursorfont.h", "r");
+ if (cursorfont == NULL) {
+ g_test_skip("Could not open /usr/include/X11/cursorfont.h");
+ return;
+ }
+
+ while (fgets(line, sizeof(line), cursorfont) != NULL) {
+ char *p = line;
+
+ /* skip lines that don't start with "#define" */
+ if (strncmp(p, "#define", 7) != 0)
+ continue;
+ else
+ p += 7;
+
+ /* skip over whitespace after #define */
+ SKIP_WHITESPACE(p);
+
+ /* skip #define _X11_CURSORFONT_H_ */
+ if (strncmp(p, "XC_", 3) != 0)
+ continue;
+ else
+ p += 3;
+
+ if (strncmp(p, "num_glyphs", 10) == 0) {
+ /* Use #define XC_num_glyphs to record the number we expect */
+ g_assert_cmpint(cursorsexpected, ==, 0);
+ p += strlen("num_glyphs");
+ SKIP_WHITESPACE(p);
+ cursorsexpected = (int) strtol(p, NULL, 0) / 2;
+ g_test_message("cursors expected = %d", cursorsexpected);
+ continue;
+ }
+ else {
+ /* Should be a cursor name then */
+ char *name = p;
+ int expected_id, returned_id;
+ char upper_name[32];
+
+ while (!isspace(*p))
+ p++;
+ *p++ = '\0';
+ SKIP_WHITESPACE(p);
+ expected_id = (int) strtol(p, NULL, 0);
+
+ g_test_message("%s = %d", name, expected_id);
+
+ returned_id = XmuCursorNameToIndex(name);
+ g_assert_cmpint(returned_id, ==, expected_id);
+
+ XmuNCopyISOLatin1Uppered(upper_name, name, sizeof(upper_name));
+ returned_id = XmuCursorNameToIndex(upper_name);
+ g_assert_cmpint(returned_id, ==, expected_id);
+
+ cursorschecked++;
+ }
+ }
+
+ fclose(cursorfont);
+
+ g_assert_cmpint(cursorschecked, ==, cursorsexpected);
+}
+
+static void
+test_CursorNameToIndex_badnames(void)
+{
+ const char *badnames[] = {
+ "does-not-exist",
+ "starts-with-a-good-name", /* starts with "star" */
+ "num_glyphs",
+ ""
+ };
+#define NUM_BAD_NAMES (sizeof(badnames) / sizeof(badnames[0]))
+
+ for (unsigned int i = 0; i < NUM_BAD_NAMES; i++) {
+ int returned_id = XmuCursorNameToIndex(badnames[i]);
+ g_test_message("%s", badnames[i]);
+ g_assert_cmpint(returned_id, ==, -1);
+ }
+}
+
+int
+main(int argc, char** argv)
+{
+ g_test_init(&argc, &argv, NULL);
+ g_test_bug_base(PACKAGE_BUGREPORT);
+
+ g_test_add_func("/CursorName/XmuCursorNameToIndex/good-names",
+ test_CursorNameToIndex_goodnames);
+
+ g_test_add_func("/CursorName/XmuCursorNameToIndex/bad-names",
+ test_CursorNameToIndex_badnames);
+
+ return g_test_run();
+}
diff --git a/test/Makefile.am b/test/Makefile.am
index 5af50d1..75b95cb 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -30,7 +30,7 @@ AM_CFLAGS = $(CWARNFLAGS) $(XMU_CFLAGS) $(GLIB_CFLAGS)
XMU_TEST_LIBS = ${top_builddir}/src/libXmu.la $(XMU_LIBS) $(GLIB_LIBS)
XMUU_TEST_LIBS = ${top_builddir}/src/libXmuu.la $(XMUU_LIBS) $(GLIB_LIBS)
-check_PROGRAMS = EditResStream ReadBitmapData
+check_PROGRAMS = CursorName EditResStream ReadBitmapData
TESTS = $(check_PROGRAMS)
TESTS_ENVIRONMENT = $(MALLOC_DEBUG_ENV)
@@ -41,6 +41,9 @@ LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \
$(top_srcdir)/tap-driver.sh
LOG_COMPILER = $(srcdir)/tap-test
+CursorName_SOURCES = CursorName.c
+CursorName_LDADD = $(XMUU_TEST_LIBS)
+
EditResStream_SOURCES = EditResStream.c
EditResStream_LDADD = $(XMU_TEST_LIBS)