Home | History | Annotate | Line # | Download | only in test
      1  1.1  mrg /*
      2  1.1  mrg  * Copyright (c) 2023, Oracle and/or its affiliates.
      3  1.1  mrg  *
      4  1.1  mrg  * Permission is hereby granted, free of charge, to any person obtaining a
      5  1.1  mrg  * copy of this software and associated documentation files (the "Software"),
      6  1.1  mrg  * to deal in the Software without restriction, including without limitation
      7  1.1  mrg  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  1.1  mrg  * and/or sell copies of the Software, and to permit persons to whom the
      9  1.1  mrg  * Software is furnished to do so, subject to the following conditions:
     10  1.1  mrg  *
     11  1.1  mrg  * The above copyright notice and this permission notice (including the next
     12  1.1  mrg  * paragraph) shall be included in all copies or substantial portions of the
     13  1.1  mrg  * Software.
     14  1.1  mrg  *
     15  1.1  mrg  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  1.1  mrg  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  1.1  mrg  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  1.1  mrg  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  1.1  mrg  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  1.1  mrg  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     21  1.1  mrg  * DEALINGS IN THE SOFTWARE.
     22  1.1  mrg  */
     23  1.1  mrg 
     24  1.1  mrg /* Test code for functions in src/StrToOrnt.c */
     25  1.1  mrg #include "config.h"
     26  1.1  mrg #include <X11/Xmu/Converters.h>
     27  1.1  mrg #include <X11/Xmu/CharSet.h>
     28  1.1  mrg #include <X11/StringDefs.h>
     29  1.1  mrg #include <glib.h>
     30  1.1  mrg 
     31  1.1  mrg struct TestData {
     32  1.1  mrg     const char *name;
     33  1.1  mrg     XtOrientation value;
     34  1.1  mrg };
     35  1.1  mrg 
     36  1.1  mrg static const struct TestData data[] = {
     37  1.1  mrg         { XtEvertical,          XtorientVertical },
     38  1.1  mrg         { XtEhorizontal,        XtorientHorizontal }
     39  1.1  mrg };
     40  1.1  mrg #define DATA_ENTRIES (sizeof(data) / sizeof(data[0]))
     41  1.1  mrg 
     42  1.1  mrg static int warning_count;
     43  1.1  mrg 
     44  1.1  mrg static void
     45  1.1  mrg xt_warning_handler(String message)
     46  1.1  mrg {
     47  1.1  mrg     g_test_message("Caught warning: %s", message ? message : "<NULL>");
     48  1.1  mrg     warning_count++;
     49  1.1  mrg }
     50  1.1  mrg 
     51  1.1  mrg 
     52  1.1  mrg 
     53  1.1  mrg static void
     54  1.1  mrg test_XmuCvtStringToOrientation(void)
     55  1.1  mrg {
     56  1.1  mrg     XrmValue from, to;
     57  1.1  mrg     Cardinal nargs = 0;
     58  1.1  mrg 
     59  1.1  mrg     char namebuf[16];
     60  1.1  mrg 
     61  1.1  mrg     for (unsigned int i = 0; i < DATA_ENTRIES; i++) {
     62  1.1  mrg         g_test_message("StringToOrientation(%s)", data[i].name);
     63  1.1  mrg 
     64  1.1  mrg         strncpy(namebuf, data[i].name, sizeof(namebuf) - 1);
     65  1.1  mrg         namebuf[sizeof(namebuf) - 1] = 0;
     66  1.1  mrg         from.addr = namebuf;
     67  1.1  mrg         from.size = sizeof(char *);
     68  1.1  mrg         XmuCvtStringToOrientation(NULL, &nargs, &from, &to);
     69  1.1  mrg         g_assert_cmpint(*(XtOrientation *)to.addr, ==, data[i].value);
     70  1.1  mrg         g_assert_cmpint(to.size, ==, sizeof(int));
     71  1.1  mrg 
     72  1.1  mrg 
     73  1.1  mrg         XmuNCopyISOLatin1Uppered(namebuf, data[i].name, sizeof(namebuf));
     74  1.1  mrg         from.addr = namebuf;
     75  1.1  mrg         from.size = sizeof(char *);
     76  1.1  mrg         XmuCvtStringToOrientation(NULL, &nargs, &from, &to);
     77  1.1  mrg         g_assert_cmpint(*(XtOrientation *)to.addr, ==, data[i].value);
     78  1.1  mrg         g_assert_cmpint(to.size, ==, sizeof(int));
     79  1.1  mrg     }
     80  1.1  mrg 
     81  1.1  mrg     /* No warning is currently issued for unused args */
     82  1.1  mrg #if 0
     83  1.1  mrg     warning_count = 0;
     84  1.1  mrg     nargs = 1;
     85  1.1  mrg     g_test_message("StringToOrientation with extra args");
     86  1.1  mrg     XmuCvtStringToOrientation(&args, &nargs, &from, &to);
     87  1.1  mrg     g_assert_cmpint(warning_count, >, 0);
     88  1.1  mrg #endif
     89  1.1  mrg 
     90  1.1  mrg     /* Verify warning issued for unknown string */
     91  1.1  mrg     warning_count = 0;
     92  1.1  mrg     from.addr = (char *) "DoesNotExist";
     93  1.1  mrg     nargs = 0;
     94  1.1  mrg     g_test_message("StringToOrientation(%s)", from.addr);
     95  1.1  mrg     XmuCvtStringToOrientation(NULL, &nargs, &from, &to);
     96  1.1  mrg     g_assert_cmpint(warning_count, >, 0);
     97  1.1  mrg }
     98  1.1  mrg 
     99  1.1  mrg static void
    100  1.1  mrg test_XmuCvtOrientationToString(void)
    101  1.1  mrg {
    102  1.1  mrg     XrmValue from, to;
    103  1.1  mrg     XtOrientation value;
    104  1.1  mrg     Cardinal nargs = 0;
    105  1.1  mrg     Boolean ret;
    106  1.1  mrg     char namebuf[16];
    107  1.1  mrg 
    108  1.1  mrg 
    109  1.1  mrg     for (unsigned int i = 0; i < DATA_ENTRIES; i++) {
    110  1.1  mrg         g_test_message("OrientationToString(%d)", data[i].value);
    111  1.1  mrg 
    112  1.1  mrg         value = data[i].value;
    113  1.1  mrg         from.addr = (XPointer) &value;
    114  1.1  mrg         from.size = sizeof(XtOrientation);
    115  1.1  mrg 
    116  1.1  mrg         /* First test without providing a buffer to copy the string into */
    117  1.1  mrg         to.addr = NULL;
    118  1.1  mrg         to.size = 0;
    119  1.1  mrg         ret = XmuCvtOrientationToString(NULL, NULL, &nargs, &from, &to, NULL);
    120  1.1  mrg         g_assert_cmpint(ret, ==, True);
    121  1.1  mrg         g_assert_cmpstr(to.addr, ==, data[i].name);
    122  1.1  mrg         g_assert_cmpint(to.size, ==, sizeof(char *));
    123  1.1  mrg 
    124  1.1  mrg         /* Then test with a buffer that's too small to copy the string into */
    125  1.1  mrg         to.addr = namebuf;
    126  1.1  mrg         to.size = 4;
    127  1.1  mrg         ret = XmuCvtOrientationToString(NULL, NULL, &nargs, &from, &to, NULL);
    128  1.1  mrg         g_assert_cmpint(ret, ==, False);
    129  1.1  mrg         g_assert_cmpint(to.size, ==, strlen(data[i].name) + 1);
    130  1.1  mrg 
    131  1.1  mrg         /* Then test with a buffer that's big enough to copy the string into */
    132  1.1  mrg         to.addr = namebuf;
    133  1.1  mrg         to.size = sizeof(namebuf);
    134  1.1  mrg         ret = XmuCvtOrientationToString(NULL, NULL, &nargs, &from, &to, NULL);
    135  1.1  mrg         g_assert_cmpint(ret, ==, True);
    136  1.1  mrg         g_assert_cmpstr(to.addr, ==, data[i].name);
    137  1.1  mrg         g_assert_cmpint(to.size, ==, sizeof(char *));
    138  1.1  mrg     }
    139  1.1  mrg 
    140  1.1  mrg     /* Verify warning and return of False for invalid value */
    141  1.1  mrg     warning_count = 0;
    142  1.1  mrg     value = 1984;
    143  1.1  mrg     from.addr = (XPointer) &value;
    144  1.1  mrg     g_test_message("OrientationToString(%d)", value);
    145  1.1  mrg     ret = XmuCvtOrientationToString(NULL, NULL, &nargs, &from, &to, NULL);
    146  1.1  mrg     g_assert_cmpint(ret, ==, False);
    147  1.1  mrg     g_assert_cmpint(warning_count, >, 0);
    148  1.1  mrg }
    149  1.1  mrg 
    150  1.1  mrg int
    151  1.1  mrg main(int argc, char** argv)
    152  1.1  mrg {
    153  1.1  mrg     g_test_init(&argc, &argv, NULL);
    154  1.1  mrg     g_test_bug_base(PACKAGE_BUGREPORT);
    155  1.1  mrg 
    156  1.1  mrg     XtSetWarningHandler(xt_warning_handler);
    157  1.1  mrg 
    158  1.1  mrg     g_test_add_func("/StrToOrnt/XmuCvtStringToOrientation",
    159  1.1  mrg                     test_XmuCvtStringToOrientation);
    160  1.1  mrg     g_test_add_func("/StrToOrnt/XmuCvtOrientationToString",
    161  1.1  mrg                     test_XmuCvtOrientationToString);
    162  1.1  mrg 
    163  1.1  mrg 
    164  1.1  mrg     return g_test_run();
    165  1.1  mrg }
    166