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