torture_su_tag.c 14.3 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

Pekka Pessi's avatar
Pekka Pessi committed
25
/**@internal
26
 * @file torture_su_tag.c
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29 30
 *
 * Testing functions for su_tag module.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
31
 *
Pekka Pessi's avatar
Pekka Pessi committed
32 33 34 35 36 37 38 39 40
 * @date Created: Tue Mar  6 18:33:42 2001 ppessi
 */

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

41 42 43 44
#include <sofia-sip/su_tag.h>
#include <sofia-sip/su_tag_io.h>
#include <sofia-sip/su_tag_class.h>
#include <sofia-sip/su_tagarg.h>
Pekka Pessi's avatar
Pekka Pessi committed
45 46 47 48 49

#ifndef WIN32
#define DEVNULL "/dev/null"
#else
#define DEVNULL "nul"
50
#endif
Pekka Pessi's avatar
Pekka Pessi committed
51 52 53 54 55

int tstflags = 0;

#define TSTFLAGS tstflags

56
#include <sofia-sip/tstdef.h>
Pekka Pessi's avatar
Pekka Pessi committed
57 58 59 60 61 62 63 64 65 66 67 68 69

#define TAG_A(s)      tag_a, tag_str_v((s))
#define TAG_A_REF(s)  tag_a_ref, tag_str_vr(&(s))
#define TAG_B(s)      tag_b, tag_str_v((s))
#define TAG_B_REF(s)  tag_b_ref, tag_str_vr(&(s))

#define TAG_I(i)      tag_i, tag_int_v((i))
#define TAG_I_REF(i)  tag_i_ref, tag_int_vr(&(i))
#define TAG_J(i)      tag_j, tag_int_v((i))
#define TAG_J_REF(i)  tag_j_ref, tag_int_vr(&(i))
#define TAG_K(i)      tag_k, tag_int_v((i))
#define TAG_K_REF(i)  tag_k_ref, tag_int_vr(&(i))

70 71
#define TAG_ANY_PQ()  tag_any_pq, (tag_value_t)0

Pekka Pessi's avatar
Pekka Pessi committed
72 73 74 75 76 77 78 79 80 81 82 83 84
#define TAG_P(i)      tag_p, tag_bool_v((i))
#define TAG_P_REF(i)  tag_p_ref, tag_bool_vr(&(i))
#define TAG_Q(i)      tag_q, tag_bool_v((i))
#define TAG_Q_REF(i)  tag_q_ref, tag_bool_vr(&(i))

char const *name = "su_tag_test";

#if HAVE_WIN32
typedef struct tag_type_s tag_typedef_win_t[1];

tag_typedef_win_t tag_a, tag_a_ref, tag_b, tag_b_ref;
tag_typedef_win_t tag_i, tag_i_ref, tag_j, tag_j_ref;
tag_typedef_win_t tag_k, tag_k_ref, tag_n, tag_n_ref;
85
tag_typedef_win_t tag_any_pq;
Pekka Pessi's avatar
Pekka Pessi committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
tag_typedef_win_t tag_p, tag_p_ref, tag_q, tag_q_ref;

#else
tag_typedef_t tag_a = STRTAG_TYPEDEF(a);
tag_typedef_t tag_a_ref = REFTAG_TYPEDEF(tag_a);

tag_typedef_t tag_b = STRTAG_TYPEDEF(b);
tag_typedef_t tag_b_ref = REFTAG_TYPEDEF(tag_b);

tag_typedef_t tag_i = INTTAG_TYPEDEF(i);
tag_typedef_t tag_i_ref = REFTAG_TYPEDEF(tag_i);

tag_typedef_t tag_j = INTTAG_TYPEDEF(j);
tag_typedef_t tag_j_ref = REFTAG_TYPEDEF(tag_j);

tag_typedef_t tag_k = INTTAG_TYPEDEF(k);
tag_typedef_t tag_k_ref = REFTAG_TYPEDEF(tag_k);

tag_typedef_t tag_n = INTTAG_TYPEDEF(n);
tag_typedef_t tag_n_ref = REFTAG_TYPEDEF(tag_n);

107 108 109 110 111
#undef TAG_NAMESPACE
#define TAG_NAMESPACE "pq"

tag_typedef_t tag_any_pq = NSTAG_TYPEDEF(*);

Pekka Pessi's avatar
Pekka Pessi committed
112 113 114 115 116 117 118 119
tag_typedef_t tag_p = BOOLTAG_TYPEDEF(p);
tag_typedef_t tag_p_ref = REFTAG_TYPEDEF(tag_p);

tag_typedef_t tag_q = BOOLTAG_TYPEDEF(q);
tag_typedef_t tag_q_ref = REFTAG_TYPEDEF(tag_q);

#endif

120
static void init_tags(void)
Pekka Pessi's avatar
Pekka Pessi committed
121 122
{
#if HAVE_WIN32
123 124
  /* Automatic initialization with pointers from DLL does not work in WIN32 */

Pekka Pessi's avatar
Pekka Pessi committed
125 126 127 128 129
  tag_typedef_t _tag_a = STRTAG_TYPEDEF(a);
  tag_typedef_t _tag_a_ref = REFTAG_TYPEDEF(tag_a);

  tag_typedef_t _tag_b = STRTAG_TYPEDEF(b);
  tag_typedef_t _tag_b_ref = REFTAG_TYPEDEF(tag_b);
130

Pekka Pessi's avatar
Pekka Pessi committed
131 132
  tag_typedef_t _tag_i = INTTAG_TYPEDEF(i);
  tag_typedef_t _tag_i_ref = REFTAG_TYPEDEF(tag_i);
133

Pekka Pessi's avatar
Pekka Pessi committed
134 135
  tag_typedef_t _tag_j = INTTAG_TYPEDEF(j);
  tag_typedef_t _tag_j_ref = REFTAG_TYPEDEF(tag_j);
136

Pekka Pessi's avatar
Pekka Pessi committed
137 138
  tag_typedef_t _tag_k = INTTAG_TYPEDEF(k);
  tag_typedef_t _tag_k_ref = REFTAG_TYPEDEF(tag_k);
139

Pekka Pessi's avatar
Pekka Pessi committed
140 141
  tag_typedef_t _tag_n = INTTAG_TYPEDEF(n);
  tag_typedef_t _tag_n_ref = REFTAG_TYPEDEF(tag_n);
142 143 144

#undef TAG_NAMESPACE
#define TAG_NAMESPACE "pq"
145

146 147
  tag_typedef_t _tag_any_pq = NSTAG_TYPEDEF(*);

Pekka Pessi's avatar
Pekka Pessi committed
148 149
  tag_typedef_t _tag_p = BOOLTAG_TYPEDEF(p);
  tag_typedef_t _tag_p_ref = REFTAG_TYPEDEF(tag_p);
150

Pekka Pessi's avatar
Pekka Pessi committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  tag_typedef_t _tag_q = BOOLTAG_TYPEDEF(q);
  tag_typedef_t _tag_q_ref = REFTAG_TYPEDEF(tag_q);

  *(struct tag_type_s *)tag_a = *_tag_a;
  *(struct tag_type_s *)tag_a_ref = *_tag_a_ref;
  *(struct tag_type_s *)tag_b = *_tag_b;
  *(struct tag_type_s *)tag_b_ref = *_tag_b_ref;
  *(struct tag_type_s *)tag_i = *_tag_i;
  *(struct tag_type_s *)tag_i_ref = *_tag_i_ref;
  *(struct tag_type_s *)tag_j = *_tag_j;
  *(struct tag_type_s *)tag_j_ref = *_tag_j_ref;
  *(struct tag_type_s *)tag_k = *_tag_k;
  *(struct tag_type_s *)tag_k_ref = *_tag_k_ref;
  *(struct tag_type_s *)tag_n = *_tag_n;
  *(struct tag_type_s *)tag_n_ref = *_tag_n_ref;
166
  *(struct tag_type_s *)tag_any_pq = *_tag_any_pq;
Pekka Pessi's avatar
Pekka Pessi committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
  *(struct tag_type_s *)tag_p = *_tag_p;
  *(struct tag_type_s *)tag_p_ref = *_tag_p_ref;
  *(struct tag_type_s *)tag_q = *_tag_q;
  *(struct tag_type_s *)tag_q_ref = *_tag_q_ref;
#endif
}

static int test_assumptions(void)
{
  tagi_t tags[2], *tagi;

  BEGIN();

  TEST_1(sizeof (tag_value_t) >= sizeof (void *));

  /* First some pointer arithmetics - this is always true, right? */
  tagi = tags;
184
  TEST_P(tagi + 1, tags + 1);
Pekka Pessi's avatar
Pekka Pessi committed
185
  tagi = (tagi_t *)(sizeof(tagi_t) + (char *)tagi);
186
  TEST_P(tagi, tags + 1);
Pekka Pessi's avatar
Pekka Pessi committed
187 188 189 190 191 192 193 194 195 196 197 198

  END();
}

#if SU_HAVE_TAGSTACK

/* Automake thing:

###
### Test if we have stack suitable for handling tags directly
###
AC_TRY_RUN( [
199 200 201 202 203 204
#if HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#if HAVE_STDINT_H
#include <stdint.h>
#endif
Pekka Pessi's avatar
Pekka Pessi committed
205 206
#include <stdarg.h>

207 208 209 210
typedef void *tp;
typedef intptr_t tv;

int test1(tv l, tv h, ...)
Pekka Pessi's avatar
Pekka Pessi committed
211 212
{
  va_list ap;
213
  tv i, *p = &l;
Pekka Pessi's avatar
Pekka Pessi committed
214 215 216 217 218 219 220 221 222 223 224

  va_start(ap, h);

  if (*p++ != l || *p++ != h) return 1;

  for (i = l; i <= h; i++) {
    if (*p++ != i)
      return 1;
  }

  for (i = l; i <= h; i++) {
225
    if (va_arg(ap, tv) != i)
Pekka Pessi's avatar
Pekka Pessi committed
226 227 228 229 230 231 232 233 234 235
      return 1;
  }

  va_end(ap);

  return 0;
}

int main(int avc, char *av[])
{
236 237 238
  return test1((tv)1, (tv)10,
	       (tv)1, (tv)2, (tv)3, (tv)4, (tv)5,
	       (tv)6, (tv)7, (tv)8, (tv)9, (tv)10);
Pekka Pessi's avatar
Pekka Pessi committed
239
}
240
],
241
SAC_SU_DEFINE(SU_HAVE_TAGSTACK, 1, [
242
Define this as 1 if your compiler puts the variable argument list nicely in memory]),
Pekka Pessi's avatar
Pekka Pessi committed
243 244 245 246 247 248
)

*/

static int test_stackargs(int l, ...)
{
249
  va_list ap;
Pekka Pessi's avatar
Pekka Pessi committed
250 251 252 253 254
  int i, *p;

  BEGIN();

  va_start(ap, l);
255

Pekka Pessi's avatar
Pekka Pessi committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
  p = &l;

  for (i = l; i <= 10; i++) {
    TEST(*p++, i);
  }

  for (i = l + 1; i <= 10; i++) {
    TEST(va_arg(ap, int), i);
  }

  va_end(ap);

  END();
}
#else
static int test_stackargs(int l, ...) { return 0; }
#endif

274
/** Test tl_list, tl_llist and tl_dup */
Pekka Pessi's avatar
Pekka Pessi committed
275 276 277
static int test_dup(void)
{
  tagi_t *lst, *dup;
278 279 280 281 282 283 284 285 286
#if HAVE_OPEN_C
  tagi_t rest[2];
  rest[0].t_tag = tag_a;
  rest[0].t_value = tag_str_v("Foo");
  rest[1].t_tag = (tag_type_t)0;
  rest[0].t_value = (tag_value_t)0;
#else
  tagi_t const rest[] = {{ TAG_A("Foo") }, { TAG_NULL() }};
#endif
Pekka Pessi's avatar
Pekka Pessi committed
287 288 289

  BEGIN();

290 291 292 293
  lst = tl_list(TAG_A("Moro"),
		TAG_A("Vaan"),
		TAG_I(1),
		TAG_SKIP(2),
Pekka Pessi's avatar
Pekka Pessi committed
294 295
		TAG_NEXT(rest));

296 297 298 299 300
  TEST_P(lst[0].t_tag, tag_a);
  TEST_P(lst[1].t_tag, tag_a);
  TEST_P(lst[2].t_tag, tag_i);
  TEST_P(lst[3].t_tag, tag_skip);
  TEST_P(lst[4].t_tag, tag_next);
Pekka Pessi's avatar
Pekka Pessi committed
301

302 303
  TEST_SIZE(tl_len(lst), 5 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(lst, 0), strlen("Moro" "Vaan" "Foo") + 3);
Pekka Pessi's avatar
Pekka Pessi committed
304 305 306

  dup = tl_adup(NULL, lst);

307 308 309
  TEST_1(dup != NULL);
  TEST_SIZE(tl_len(dup), 5 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(dup, 0), strlen("Moro" "Vaan" "Foo") + 3);
Pekka Pessi's avatar
Pekka Pessi committed
310 311

  su_free(NULL, dup);
312 313 314

  dup = tl_llist(TAG_B("Moi"), TAG_NEXT(lst));

315 316 317 318 319 320
  TEST_P(dup[0].t_tag, tag_b);
  TEST_P(dup[1].t_tag, tag_a);
  TEST_P(dup[2].t_tag, tag_a);
  TEST_P(dup[3].t_tag, tag_i);
  TEST_P(dup[4].t_tag, tag_a);
  TEST_P(dup[5].t_tag, NULL);
321 322 323 324

  su_free(NULL, dup);

  dup = tl_llist(TAG_NEXT(NULL));
325
  TEST_P(dup[0].t_tag, 0);
326 327 328
  su_free(NULL, dup);

  dup = tl_llist(TAG_END());
329
  TEST_P(dup[0].t_tag, 0);
330 331 332
  su_free(NULL, dup);

  dup = tl_llist(TAG_SKIP(1), TAG_NEXT(lst + 4));
333 334
  TEST_P(dup[0].t_tag, tag_a);
  TEST_P(dup[1].t_tag, 0);
335 336
  su_free(NULL, dup);

Pekka Pessi's avatar
Pekka Pessi committed
337 338 339 340 341
  tl_vfree(lst);

  END();
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
/* filtering function */
int filter(tagi_t const *filter, tagi_t const *t)
{
  if (!t)
    return 0;

  /* Accept even TAG_I() */
  if (t->t_tag == tag_i)
    return (t->t_value & 1) == 0;

  /* TAG_Q(true) */
  if (t->t_tag == tag_q)
    return t->t_value != 0;

  /* TAG_P(false) */
  if (t->t_tag == tag_p)
    return t->t_value == 0;

  if (t->t_tag == tag_a) {
    char const *s = (char *)t->t_value;
    if (s && 'A' <= *s && *s <= 'Z')
      return 1;
  }

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
369 370 371
/* Test tl_afilter() */
static int test_filters(void)
{
372 373
  tagi_t *lst, *filter1, *filter2, *filter3, *filter4, *filter5, *filter6;
  tagi_t *b, *b1, *b2, *b3, *b4;
Pekka Pessi's avatar
Pekka Pessi committed
374

375
  tagi_t *nsfilter, *b5;
376
  su_home_t *home;
377

Pekka Pessi's avatar
Pekka Pessi committed
378 379
  BEGIN();

380 381
  home = su_home_new(sizeof *home); TEST_1(home);

382
  lst = tl_list(TAG_A("Moro"),
383 384
		TAG_I(2),
		TAG_Q(3),
385 386 387
		TAG_SKIP(2),
		TAG_A("vaan"),
		TAG_I(1),
388
		TAG_P(2),
Pekka Pessi's avatar
Pekka Pessi committed
389 390 391 392 393 394
		TAG_NULL());

  filter1 = tl_list(TAG_A(""), TAG_NULL());
  filter2 = tl_list(TAG_I(0), TAG_NULL());
  filter3 = tl_list(TAG_A(""), TAG_I(0), TAG_NULL());
  filter4 = tl_list(TAG_ANY(), TAG_NULL());
395
  filter5 = tl_list(TAG_FILTER(filter), TAG_NULL());
396
  filter6 = tl_list(TAG_NULL());
Pekka Pessi's avatar
Pekka Pessi committed
397

398
  TEST0(lst && filter1 && filter2 && filter3 && filter4 && filter5);
Pekka Pessi's avatar
Pekka Pessi committed
399 400 401

  b1 = tl_afilter(NULL, filter1, lst);

402 403
  TEST_SIZE(tl_len(b1), 3 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(b1, 0), strlen("Moro" "vaan") + 2);
Pekka Pessi's avatar
Pekka Pessi committed
404 405 406

  b2 = tl_afilter(NULL, filter2, lst);

407 408
  TEST_SIZE(tl_len(b2), 3 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(b2, 0), 0);
Pekka Pessi's avatar
Pekka Pessi committed
409 410 411

  b3 = tl_afilter(NULL, filter3, lst);

412 413
  TEST_SIZE(tl_len(b3), 5 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(b3, 0), strlen("Moro" "vaan") + 2);
Pekka Pessi's avatar
Pekka Pessi committed
414 415 416

  b4 = tl_afilter(NULL, filter4, lst);

417 418
  TEST_SIZE(tl_len(b4), 7 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(b4, 0), strlen("Moro" "vaan") + 2);
Pekka Pessi's avatar
Pekka Pessi committed
419 420

  su_free(NULL, b1); su_free(NULL, b2); su_free(NULL, b3); su_free(NULL, b4);
421 422

  nsfilter = tl_list(TAG_ANY_PQ(), TAG_END());
423

424
  b5 = tl_afilter(NULL, nsfilter, lst);
425 426
  TEST_SIZE(tl_len(b5), 3 * sizeof(tagi_t));
  TEST_SIZE(tl_xtra(b5, 0), 0);
427

428 429
  TEST_P(b5[0].t_tag, tag_q);
  TEST_P(b5[1].t_tag, tag_p);
430

431
  b = tl_afilter(home, filter5, lst); TEST_1(b);
432 433 434 435
  TEST_P(b[0].t_tag, tag_a);
  TEST_P(b[1].t_tag, tag_i);
  TEST_P(b[2].t_tag, tag_q);
  TEST_P(b[3].t_tag, 0);
436 437

  b = tl_afilter(home, filter6, lst); TEST_1(b);
438
  TEST_P(b[0].t_tag, 0);
439

440
  tl_vfree(filter1); tl_vfree(filter2); tl_vfree(filter3);
441
  tl_vfree(filter4); tl_vfree(filter5); tl_vfree(filter6);
442

Pekka Pessi's avatar
Pekka Pessi committed
443 444
  tl_vfree(lst);

445 446
  su_home_unref(home);

Pekka Pessi's avatar
Pekka Pessi committed
447 448 449 450 451 452
  END();
}

/* Test tl_print */
static int test_print(void)
{
453 454
  BEGIN();

Pekka Pessi's avatar
Pekka Pessi committed
455 456 457
  tagi_t *lst;
  FILE *out;

458 459
  lst = tl_list(TAG_A("Moro"),
		TAG_I(2),
Pekka Pessi's avatar
Pekka Pessi committed
460 461
		TAG_J(3),
		TAG_K(5),
462 463
		TAG_SKIP(2),
		TAG_A("Vaan"),
Pekka Pessi's avatar
Pekka Pessi committed
464
		TAG_B("b"),
465
		TAG_I(1),
Pekka Pessi's avatar
Pekka Pessi committed
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
		TAG_NULL());

  if (tstflags & tst_verbatim)
    out = stdout;
  else
    out = fopen(DEVNULL, "w");

  if (out) {
    tl_print(out, "test_print: ", lst);

    if ((tstflags & tst_verbatim) == 0)
      fclose(out);
  }

  END();
}

static int test_tagargs2(tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  tagi_t const *t;

  BEGIN();

  ta_start(ta, tag, value);

  t = ta_args(ta);
493
  TEST_P(t->t_tag, tag_a);
Pekka Pessi's avatar
Pekka Pessi committed
494 495 496
  TEST_S((char *)t->t_value, "a");

  t = tl_next(t);
497
  TEST_P(t->t_tag, tag_b);
Pekka Pessi's avatar
Pekka Pessi committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
  TEST_S((char *)t->t_value, "b");

  ta_end(ta);

  END();
}

/* Test tagargs) */
static int test_tagargs(void)
{
  return test_tagargs2(TAG_A("a"),
		       TAG_B("b"),
		       TAG_I(1),
		       TAG_J(2),
		       TAG_K(3),
		       TAG_P(0),
		       TAG_Q(-1),
		       TAG_END());
}

/* Test tl_tgets() and tl_gets() */
static int test_gets(void)
{
521
  tagi_t *lst, *t;
Pekka Pessi's avatar
Pekka Pessi committed
522 523 524 525 526
  char const *a = "B", *b = "A";
  int i = 1, j = 0, k = 0, p = -1;

  BEGIN();

527 528
  lst = tl_list(TAG_A("Moro"),
		TAG_I(2),
Pekka Pessi's avatar
Pekka Pessi committed
529 530 531
		TAG_J(3),
		TAG_K(5),
		TAG_P(3),
532 533
		TAG_SKIP(2),
		TAG_A("Vaan"),
Pekka Pessi's avatar
Pekka Pessi committed
534
		TAG_B("b"),
535
		TAG_I(1),
Pekka Pessi's avatar
Pekka Pessi committed
536 537 538 539
		TAG_NULL());

  TEST_1(lst);

540 541 542
  TEST_1(t = tl_find(lst, tag_i)); TEST(t->t_value, 2);
  TEST_1(t = tl_find_last(lst, tag_i)); TEST(t->t_value, 1);

543
  TEST(tl_gets(lst,
Pekka Pessi's avatar
Pekka Pessi committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	       TAG_A_REF(a),
	       TAG_B_REF(b),
	       TAG_I_REF(i),
	       TAG_J_REF(j),
	       TAG_K_REF(k),
	       TAG_P_REF(p),
	       TAG_END()),
       6);

  /* tl_gets() semantics have changed */
#if 0
  TEST_S(a, "Moro");
  TEST(i, 2);
#else
  TEST_S(a, "Vaan");
  TEST(i, 1);
#endif
  TEST(j, 3);
  TEST(k, 5);
  TEST(p, 1);
  TEST_S(b, "b");

  lst = tl_list(TAG_A_REF(a),
		TAG_I_REF(i),
		TAG_NULL());
569

Pekka Pessi's avatar
Pekka Pessi committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
  TEST(tl_tgets(lst, TAG_A("Foo"), TAG_I(-1), TAG_END()), 2);

  TEST_S(a, "Foo");
  TEST(i, -1);

  END();
}

static int test_scan(void)
{
  tag_value_t v = 0;

  BEGIN();

  TEST(t_scan(tag_a, NULL, NULL, &v), -1); /* Invalid case */

  TEST(t_scan(tag_a, NULL, "foo", &v), 1);
  TEST_S((char *)v, "foo");
  su_free(NULL, (void *)v);

  TEST(t_scan(tag_i, NULL, "", &v), -1); /* Invalid case */
  TEST(t_scan(tag_i, NULL, "kukkuu-reset", &v), -1); /* Invalid case */

  TEST(t_scan(tag_i, NULL, "-1234", &v), 1);
  TEST(v, -1234);

  TEST(t_scan(tag_n, NULL, "", &v), -1); /* Invalid case */

  TEST(t_scan(tag_n, NULL, "3243432", &v), 1);
  TEST(v, 3243432);

  TEST(t_scan(tag_p, NULL, "kukkuu-reset", &v), -1); /* Invalid case */
  TEST(t_scan(tag_p, NULL, "", &v), -1); /* Invalid case */

  TEST(t_scan(tag_p, NULL, "true", &v), 1);
  TEST(v, 1);

  TEST(t_scan(tag_p, NULL, "true ", &v), 1);
  TEST(v, 1);

  TEST(t_scan(tag_p, NULL, "  134  ", &v), 1);
  TEST(v, 1);

  TEST(t_scan(tag_p, NULL, "false   \n\t", &v), 1);
  TEST(v, 0);

  TEST(t_scan(tag_p, NULL, "  000.000  ", &v), 1);
  TEST(v, 0);

619 620 621
  TEST(t_scan(tag_null, NULL, "NULL", &v), -2);
  TEST(t_scan(tag_skip, NULL, "SKIP", &v), -2);
  TEST(t_scan(tag_any, NULL, "any", &v), -2);
Pekka Pessi's avatar
Pekka Pessi committed
622 623 624 625 626 627 628 629

  TEST(t_scan(NULL, NULL, "-1234", &v), -1);
  TEST(t_scan(tag_a, NULL, NULL, &v), -1);
  TEST(t_scan(tag_a, NULL, "-1234", NULL), -1);

  END();
}

630
void usage(int exitcode)
631
{
632 633
  fprintf(stderr,
	  "usage: %s [-v] [-a]\n",
634
	  name);
635
  exit(exitcode);
636 637 638 639 640 641 642 643 644 645 646 647
}

int main(int argc, char *argv[])
{
  int retval = 0;
  int i;

  init_tags();

  for (i = 1; argv[i]; i++) {
    if (strcmp(argv[i], "-v") == 0)
      tstflags |= tst_verbatim;
648 649
    else if (strcmp(argv[i], "-a") == 0)
      tstflags |= tst_abort;
650
    else
651
      usage(1);
652 653
  }

654 655 656 657
#if HAVE_OPEN_C
  tstflags |= tst_verbatim;
#endif

658 659 660 661 662 663 664 665 666 667 668
  retval |= test_assumptions();
  retval |= test_stackargs(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
  retval |= test_dup();
  retval |= test_filters();
  retval |= test_print();
  retval |= test_tagargs();
  retval |= test_gets();
  retval |= test_scan();

  return retval;
}