~ubuntu-branches/ubuntu/gutsy/m4/gutsy

« back to all changes in this revision

Viewing changes to tests/test-xvasprintf.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-08-01 14:00:55 UTC
  • mfrom: (1.5.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070801140055-ob48jo517shsyvgk
Tags: 1.4.10-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Test of xvasprintf() and xasprintf() functions.
 
2
   Copyright (C) 2007 Free Software Foundation, Inc.
 
3
 
 
4
   This program is free software; you can redistribute it and/or modify
 
5
   it under the terms of the GNU General Public License as published by
 
6
   the Free Software Foundation; either version 2, or (at your option)
 
7
   any later version.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; if not, write to the Free Software Foundation,
 
16
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
 
17
 
 
18
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
 
19
 
 
20
#include <config.h>
 
21
 
 
22
#include "xvasprintf.h"
 
23
 
 
24
#include <stdarg.h>
 
25
#include <stdio.h>
 
26
#include <stdlib.h>
 
27
#include <string.h>
 
28
 
 
29
#include "progname.h"
 
30
 
 
31
#define ASSERT(expr) \
 
32
  do                                                                         \
 
33
    {                                                                        \
 
34
      if (!(expr))                                                           \
 
35
        {                                                                    \
 
36
          fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
 
37
          abort ();                                                          \
 
38
        }                                                                    \
 
39
    }                                                                        \
 
40
  while (0)
 
41
 
 
42
static char *
 
43
my_xasprintf (const char *format, ...)
 
44
{
 
45
  va_list args;
 
46
  char *ret;
 
47
 
 
48
  va_start (args, format);
 
49
  ret = xvasprintf (format, args);
 
50
  va_end (args);
 
51
  return ret;
 
52
}
 
53
 
 
54
static void
 
55
test_xvasprintf ()
 
56
{
 
57
  int repeat;
 
58
 
 
59
  for (repeat = 0; repeat <= 8; repeat++)
 
60
    {
 
61
      char *result = my_xasprintf ("%d", 12345);
 
62
      ASSERT (result != NULL);
 
63
      ASSERT (strcmp (result, "12345") == 0);
 
64
      free (result);
 
65
    }
 
66
}
 
67
 
 
68
static void
 
69
test_xasprintf ()
 
70
{
 
71
  int repeat;
 
72
 
 
73
  for (repeat = 0; repeat <= 8; repeat++)
 
74
    {
 
75
      char *result = xasprintf ("%d", 12345);
 
76
      ASSERT (result != NULL);
 
77
      ASSERT (strcmp (result, "12345") == 0);
 
78
      free (result);
 
79
    }
 
80
}
 
81
 
 
82
int
 
83
main (int argc, char *argv[])
 
84
{
 
85
  set_program_name (argv[0]);
 
86
 
 
87
  test_xvasprintf ();
 
88
  test_xasprintf ();
 
89
 
 
90
  return 0;
 
91
}