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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/*
* Copyright (c) 2020, Masatake YAMATO
*
* This source code is released into the public domain.
*
* Testing tagsOpen() API function
*/
#include "readtags.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int
check_info (tagFileInfo *info, tagFileInfo *expected)
{
fprintf (stderr, "inspecting info->file.format...");
if (info->file.format != expected->file.format)
{
fprintf (stderr, "unexpected result: %d\n", info->file.format);
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "inspecting info->file.sort...");
if (info->file.sort != expected->file.sort)
{
fprintf (stderr, "unexpected result: %d\n", info->file.sort);
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "inspecting info->program.author...");
if (strcmp (info->program.author, expected->program.author))
{
fprintf (stderr, "unexpected result: %s\n", info->program.author);
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "inspecting info->program.name...");
if (strcmp (info->program.name, expected->program.name))
{
fprintf (stderr, "unexpected result: %s\n", info->program.name);
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "inspecting info->program.url...");
if (strcmp (info->program.url, expected->program.url))
{
fprintf (stderr, "unexpected result: %s\n", info->program.url);
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "inspecting info->program.version...");
if (strcmp (info->program.version, expected->program.version))
{
fprintf (stderr, "unexpected result: %s\n", expected->program.version);
return 1;
}
fprintf (stderr, "ok\n");
return 0;
}
int
main (void)
{
char *srcdir = getenv ("srcdir");
if (srcdir)
{
if (chdir (srcdir) == -1)
{
perror ("chdir");
return 99;
}
}
tagFile *t;
tagFileInfo info;
const char *tags0 = "./no-such-file.tags";
fprintf (stderr, "opening no-existing tags file...");
t = tagsOpen (tags0, &info);
if (! (t == NULL
&& info.status.opened == 0
&& info.status.error_number != 0))
{
fprintf (stderr, "unexpected result (t: %p, opened: %d, error_number: %d)\n",
t, info.status.opened, info.status.error_number);
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "opening no-existing tags file with NULL tagFileInfo...");
t = tagsOpen (tags0, NULL);
if (t != NULL)
{
fprintf (stderr, "unexpected result (t: %p)\n", t);
return 1;
}
fprintf (stderr, "ok\n");
tagFileInfo expected1 = {
.file.format = 2,
.file.sort = TAG_SORTED,
.program.author = "Darren Hiebert",
.program.name = "Exuberant Ctags",
.program.url = "http://ctags.sourceforge.net",
.program.version = "5.8",
};
const char *tags1 = "./api-tagsOpen-ectags.tags";
fprintf (stderr, "opening an existing tags file...");
t = tagsOpen (tags1, &info);
if (t == NULL
|| info.status.opened == 0)
{
fprintf (stderr, "unexpected result (t: %p, opened: %d)\n",
t, info.status.opened);
return 1;
}
fprintf (stderr, "ok\n");
if (check_info (&info, &expected1) != 0)
return 1;
fprintf (stderr, "closing the tag file...");
if (tagsClose (t) != TagSuccess)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "opening an existing tags file with NULL tagFileInfo...");
t = tagsOpen (tags1, NULL);
if (t == NULL)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "closing the tag file...");
if (tagsClose (t) != TagSuccess)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "opening a / (an error is expected)...");
info.status.error_number = 0;
t = tagsOpen ("/", &info);
if (t != NULL)
{
fprintf (stderr, "unexpected result (!NULL)\n");
return 1;
}
else if (info.status.opened)
{
fprintf (stderr, "unexpected result (opened != 0)\n");
return 1;
}
else if (info.status.error_number == 0)
{
fprintf (stderr, "no error\n");
return 1;
}
fprintf (stderr, "ok (errno: %d)\n", info.status.error_number);
fprintf (stderr, "closing the unopened tag file...");
if (tagsClose (t) == TagSuccess)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "opening an broken tags file (format: unexpected number)...");
t = tagsOpen ("./api-tagsOpen-wrong-format-num.tags", &info);
if (t)
{
fprintf (stderr, "opened well unexpectedly (NULL)\n");
return 1;
}
else if (info.status.error_number != TagErrnoUnexpectedFormat)
{
fprintf (stderr, "unexpected error (!= TagErrnoUnexpectedFormat)\n");
}
fprintf (stderr, "ok\n");
fprintf (stderr, "closing the unopened tag file...");
if (tagsClose (t) == TagSuccess)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "opening an broken tags file (format: not a number)...");
t = tagsOpen ("./api-tagsOpen-wrong-format-nonum.tags", &info);
if (t)
{
fprintf (stderr, "opened well unexpectedly (NULL)\n");
return 1;
}
else if (info.status.error_number != TagErrnoUnexpectedFormat)
{
fprintf (stderr, "unexpected error (!= TagErrnoUnexpectedFormat)\n");
}
fprintf (stderr, "ok\n");
fprintf (stderr, "closing the unopened tag file...");
if (tagsClose (t) == TagSuccess)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "opening an broken tags file (sort: unexpected number)...");
t = tagsOpen ("./api-tagsOpen-wrong-sort-method-num.tags", &info);
if (t)
{
fprintf (stderr, "opened well unexpectedly (NULL)\n");
return 1;
}
else if (info.status.error_number != TagErrnoUnexpectedSortedMethod)
{
fprintf (stderr, "unexpected error (!= TagErrnoUnexpectedSortedMethod)\n");
}
fprintf (stderr, "ok\n");
fprintf (stderr, "closing the unopened tag file...");
if (tagsClose (t) == TagSuccess)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");
fprintf (stderr, "opening an broken tags file (sort: not a number)...");
t = tagsOpen ("./api-tagsOpen-wrong-sort-method-nonum.tags", &info);
if (t)
{
fprintf (stderr, "opened well unexpectedly (NULL)\n");
return 1;
}
else if (info.status.error_number != TagErrnoUnexpectedSortedMethod)
{
fprintf (stderr, "unexpected error (!= TagErrnoUnexpectedSortedMethod)\n");
}
fprintf (stderr, "ok\n");
fprintf (stderr, "closing the unopened tag file...");
if (tagsClose (t) == TagSuccess)
{
fprintf (stderr, "unexpected result\n");
return 1;
}
fprintf (stderr, "ok\n");
const char* broken_PROGRAM_AUTHOR [6] = {
"Universal Ctags Team",
"Universal Ctags Team",
"",
NULL,
NULL,
NULL,
};
for (int i = 0; i < 6; i++)
{
char tagf_name_tmpl [] = "./api-tagsOpen-incomplete-program-author-%d.tags";
char tagf_name [sizeof (tagf_name_tmpl) - 1];
fprintf (stderr, "opening a tags file with incomplete PROGRAM_AUTHOR field [trimming level: %d]...", i);
snprintf (tagf_name, sizeof (tagf_name), tagf_name_tmpl, i);
t = tagsOpen (tagf_name, &info);
if (t == NULL)
{
fprintf (stderr, "unexpected error: %d %s\n", info.status.error_number,
info.status.error_number > 0
? strerror (info.status.error_number)
: "");
return 1;
}
if (!((broken_PROGRAM_AUTHOR [i] == info.program.author)
|| (broken_PROGRAM_AUTHOR [i]
&& info.program.author
&& strcmp (broken_PROGRAM_AUTHOR [i], info.program.author)) == 0))
{
fprintf (stderr, "unexpected value: %s (!= %s)\n",
info.program.author? info.program.author: "(null)",
broken_PROGRAM_AUTHOR [i]? broken_PROGRAM_AUTHOR [i]: "(null)");
return 1;
}
fprintf (stderr, "closing the unopened tag file...");
if (tagsClose (t) == TagFailure)
{
fprintf (stderr, "successful unexpectedly\n");
return 1;
}
fprintf (stderr, "ok\n");
}
return 0;
}
|