Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-xmemdup0.c
1 /* Test of xmemdup0() function.
2    Copyright (C) 2008-2009 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 3, 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 Eric Blake <ebb9@byu.net>, 2008.  */
19
20 #include <config.h>
21
22 #include "xmemdup0.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include "progname.h"
29
30 #define ASSERT(expr) \
31   do                                                                         \
32     {                                                                        \
33       if (!(expr))                                                           \
34         {                                                                    \
35           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
36           fflush (stderr);                                                   \
37           abort ();                                                          \
38         }                                                                    \
39     }                                                                        \
40   while (0)
41
42 int
43 main (int argc, char **argv)
44 {
45   char buffer[10] = { 'a', 'b', 'c', 'd', '\0',
46                       'f', 'g', 'h', 'i', 'j'   };
47
48   set_program_name (argv[0]);
49
50   /* Empty string.  */
51   {
52     char *result = xmemdup0 (NULL, 0);
53     ASSERT (result);
54     ASSERT (!*result);
55     free (result);
56   }
57   {
58     char *result = xmemdup0 ("", 0);
59     ASSERT (result);
60     ASSERT (!*result);
61     free (result);
62   }
63
64   /* Various buffer lengths.  */
65   {
66     char *result = xmemdup0 (buffer, 4);
67     ASSERT (result);
68     ASSERT (strcmp (result, buffer) == 0);
69     free (result);
70   }
71   {
72     char *result = xmemdup0 (buffer, 5);
73     ASSERT (result);
74     ASSERT (strcmp (result, buffer) == 0);
75     ASSERT (result[5] == '\0');
76     free (result);
77   }
78   {
79     char *result = xmemdup0 (buffer, 9);
80     ASSERT (result);
81     ASSERT (memcmp (result, buffer, 9) == 0);
82     ASSERT (result[9] == '\0');
83     free (result);
84   }
85   {
86     char *result = xmemdup0 (buffer, 10);
87     ASSERT (result);
88     ASSERT (memcmp (result, buffer, 10) == 0);
89     ASSERT (result[10] == '\0');
90     free (result);
91   }
92
93   return 0;
94 }