relocatable-prog: fix link error
[gnulib.git] / tests / test-xmemdup0.c
1 /* Test of xmemdup0() function.
2    Copyright (C) 2008-2011 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 <stdlib.h>
25 #include <string.h>
26
27 #include "progname.h"
28 #include "macros.h"
29
30 int
31 main (int argc, char **argv)
32 {
33   char buffer[10] = { 'a', 'b', 'c', 'd', '\0',
34                       'f', 'g', 'h', 'i', 'j'   };
35
36   set_program_name (argv[0]);
37
38   /* Empty string.  */
39   {
40     char *result = xmemdup0 (NULL, 0);
41     ASSERT (result);
42     ASSERT (!*result);
43     free (result);
44   }
45   {
46     char *result = xmemdup0 ("", 0);
47     ASSERT (result);
48     ASSERT (!*result);
49     free (result);
50   }
51
52   /* Various buffer lengths.  */
53   {
54     char *result = xmemdup0 (buffer, 4);
55     ASSERT (result);
56     ASSERT (strcmp (result, buffer) == 0);
57     free (result);
58   }
59   {
60     char *result = xmemdup0 (buffer, 5);
61     ASSERT (result);
62     ASSERT (strcmp (result, buffer) == 0);
63     ASSERT (result[5] == '\0');
64     free (result);
65   }
66   {
67     char *result = xmemdup0 (buffer, 9);
68     ASSERT (result);
69     ASSERT (memcmp (result, buffer, 9) == 0);
70     ASSERT (result[9] == '\0');
71     free (result);
72   }
73   {
74     char *result = xmemdup0 (buffer, 10);
75     ASSERT (result);
76     ASSERT (memcmp (result, buffer, 10) == 0);
77     ASSERT (result[10] == '\0');
78     free (result);
79   }
80
81   return 0;
82 }