1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/* Unit tests for GPathBuf
*
* SPDX-FileCopyrightText: 2023 Emmanuele Bassi
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "config.h"
#include <string.h>
#include <errno.h>
#include <glib.h>
#ifndef g_assert_path_buf_equal
#define g_assert_path_buf_equal(p1,p2) \
G_STMT_START { \
if (g_path_buf_equal ((p1), (p2))) ; else { \
char *__p1 = g_path_buf_to_path ((p1)); \
char *__p2 = g_path_buf_to_path ((p2)); \
g_assertion_message_cmpstr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
#p1 " == " #p2, __p1, "==", __p2); \
g_free (__p1); \
g_free (__p2); \
} \
} G_STMT_END
#endif
static void
test_pathbuf_init (void)
{
#ifdef G_OS_UNIX
GPathBuf buf, cmp;
char *path;
g_test_message ("Initializing empty path buf");
g_path_buf_init (&buf);
g_assert_null (g_path_buf_to_path (&buf));
g_path_buf_clear (&buf);
g_test_message ("Initializing with empty path");
g_path_buf_init_from_path (&buf, NULL);
g_assert_null (g_path_buf_to_path (&buf));
g_path_buf_clear (&buf);
g_test_message ("Initializing with full path");
g_path_buf_init_from_path (&buf, "/usr/bin/echo");
path = g_path_buf_clear_to_path (&buf);
g_assert_nonnull (path);
g_assert_cmpstr (path, ==, "/usr/bin/echo");
g_free (path);
g_test_message ("Initializing with no path");
g_path_buf_init (&buf);
g_assert_null (g_path_buf_to_path (&buf));
g_path_buf_clear (&buf);
g_test_message ("Allocating GPathBuf on the heap");
GPathBuf *allocated = g_path_buf_new ();
g_assert_null (g_path_buf_to_path (allocated));
g_path_buf_clear (allocated);
g_path_buf_init_from_path (allocated, "/bin/sh");
path = g_path_buf_to_path (allocated);
g_assert_cmpstr (path, ==, "/bin/sh");
g_free (path);
g_path_buf_clear (allocated);
g_assert_null (g_path_buf_to_path (allocated));
g_assert_null (g_path_buf_free_to_path (allocated));
allocated = g_path_buf_new_from_path ("/bin/sh");
g_path_buf_init_from_path (&cmp, "/bin/sh");
g_assert_path_buf_equal (allocated, &cmp);
g_path_buf_clear (&cmp);
g_path_buf_free (allocated);
g_path_buf_init_from_path (&buf, "/usr/bin/bash");
allocated = g_path_buf_copy (&buf);
g_assert_path_buf_equal (allocated, allocated);
g_assert_path_buf_equal (allocated, &buf);
g_path_buf_clear (&buf);
g_path_buf_init_from_path (&cmp, "/usr/bin/bash");
g_assert_path_buf_equal (allocated, &cmp);
g_path_buf_clear (&cmp);
g_path_buf_free (allocated);
#elif defined(G_OS_WIN32)
GPathBuf buf;
char *path;
/* Forward slashes and backslashes are treated as interchangeable
* on input... */
g_path_buf_init_from_path (&buf, "C:\\windows/system32.dll");
path = g_path_buf_clear_to_path (&buf);
g_assert_nonnull (path);
/* ... and normalized to backslashes on output */
g_assert_cmpstr (path, ==, "C:\\windows\\system32.dll");
g_free (path);
g_path_buf_init (&buf);
g_assert_null (g_path_buf_to_path (&buf));
g_path_buf_clear (&buf);
g_test_message ("Allocating GPathBuf on the heap");
GPathBuf *allocated = g_path_buf_new ();
g_assert_null (g_path_buf_to_path (allocated));
g_path_buf_clear (allocated);
g_path_buf_init_from_path (allocated, "C:\\does-not-exist.txt");
path = g_path_buf_to_path (allocated);
g_assert_cmpstr (path, ==, "C:\\does-not-exist.txt");
g_free (path);
g_path_buf_clear (allocated);
g_assert_null (g_path_buf_to_path (allocated));
g_assert_null (g_path_buf_free_to_path (allocated));
#else
g_test_skip ("Unsupported platform"):
#endif
}
static void
test_pathbuf_push_pop (void)
{
#ifdef G_OS_UNIX
GPathBuf buf, cmp;
g_test_message ("Pushing relative path component");
g_path_buf_init_from_path (&buf, "/tmp");
g_path_buf_push (&buf, ".X11-unix/X0");
g_path_buf_init_from_path (&cmp, "/tmp/.X11-unix/X0");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_test_message ("Pushing absolute path component");
g_path_buf_push (&buf, "/etc/locale.conf");
g_path_buf_init_from_path (&cmp, "/etc/locale.conf");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_path_buf_clear (&buf);
g_test_message ("Popping a path component");
g_path_buf_init_from_path (&buf, "/bin/sh");
g_assert_true (g_path_buf_pop (&buf));
g_path_buf_init_from_path (&cmp, "/bin");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_assert_true (g_path_buf_pop (&buf));
g_path_buf_init_from_path (&cmp, "/");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_test_message ("Can't pop the last element of a path buffer");
g_assert_false (g_path_buf_pop (&buf));
g_path_buf_clear (&buf);
g_path_buf_clear (&cmp);
#elif defined(G_OS_WIN32)
GPathBuf buf, cmp;
g_test_message ("Pushing relative path component");
g_path_buf_init_from_path (&buf, "C:\\");
g_path_buf_push (&buf, "windows");
g_path_buf_push (&buf, "system32.dll");
g_test_message ("Popping a path component");
g_path_buf_init_from_path (&cmp, "C:\\windows/system32.dll");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_assert_true (g_path_buf_pop (&buf));
g_path_buf_init_from_path (&cmp, "C:\\windows");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_assert_true (g_path_buf_pop (&buf));
g_path_buf_init_from_path (&cmp, "C:");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_test_message ("Can't pop the last element of a path buffer");
g_assert_false (g_path_buf_pop (&buf));
g_path_buf_clear (&buf);
g_path_buf_clear (&cmp);
#else
g_test_skip ("Unsupported platform"):
#endif
}
static void
test_pathbuf_filename_extension (void)
{
#ifdef G_OS_UNIX
GPathBuf buf, cmp;
g_path_buf_init (&buf);
g_assert_false (g_path_buf_set_filename (&buf, "foo"));
g_assert_false (g_path_buf_set_extension (&buf, "txt"));
g_assert_null (g_path_buf_to_path (&buf));
g_path_buf_clear (&buf);
g_path_buf_init_from_path (&buf, "/");
g_path_buf_set_filename (&buf, "bar");
g_path_buf_init_from_path (&cmp, "/bar");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_path_buf_set_filename (&buf, "baz.txt");
g_path_buf_init_from_path (&cmp, "/baz.txt");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_path_buf_push (&buf, "/usr");
g_path_buf_push (&buf, "lib64");
g_path_buf_push (&buf, "libc");
g_assert_true (g_path_buf_set_extension (&buf, "so.6"));
g_path_buf_init_from_path (&cmp, "/usr/lib64/libc.so.6");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_path_buf_clear (&buf);
#elif defined(G_OS_WIN32)
GPathBuf buf, cmp;
g_path_buf_init_from_path (&buf, "C:\\");
g_path_buf_push (&buf, "windows");
g_path_buf_push (&buf, "system32");
g_assert_true (g_path_buf_set_extension (&buf, "dll"));
g_path_buf_init_from_path (&cmp, "C:\\windows\\system32.dll");
g_assert_path_buf_equal (&buf, &cmp);
g_path_buf_clear (&cmp);
g_path_buf_clear (&buf);
#else
g_test_skip ("Unsupported platform"):
#endif
}
int
main (int argc,
char *argv[])
{
g_setenv ("LC_ALL", "C", TRUE);
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
g_test_add_func ("/pathbuf/init", test_pathbuf_init);
g_test_add_func ("/pathbuf/push-pop", test_pathbuf_push_pop);
g_test_add_func ("/pathbuf/filename-extension", test_pathbuf_filename_extension);
return g_test_run ();
}
|