maint: update copyright
[gnulib.git] / tests / test-xmemdup0.c
1 /* Test of xmemdup0() function.
2    Copyright (C) 2008-2014 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake <ebb9@byu.net>, 2008.  */
18
19 #include <config.h>
20
21 #include "xmemdup0.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "progname.h"
27 #include "macros.h"
28
29 int
30 main (int argc, char **argv)
31 {
32   char buffer[10] = { 'a', 'b', 'c', 'd', '\0',
33                       'f', 'g', 'h', 'i', 'j'   };
34
35   set_program_name (argv[0]);
36
37   /* Empty string.  */
38   {
39     char *result = xmemdup0 (NULL, 0);
40     ASSERT (result);
41     ASSERT (!*result);
42     free (result);
43   }
44   {
45     char *result = xmemdup0 ("", 0);
46     ASSERT (result);
47     ASSERT (!*result);
48     free (result);
49   }
50
51   /* Various buffer lengths.  */
52   {
53     char *result = xmemdup0 (buffer, 4);
54     ASSERT (result);
55     ASSERT (strcmp (result, buffer) == 0);
56     free (result);
57   }
58   {
59     char *result = xmemdup0 (buffer, 5);
60     ASSERT (result);
61     ASSERT (strcmp (result, buffer) == 0);
62     ASSERT (result[5] == '\0');
63     free (result);
64   }
65   {
66     char *result = xmemdup0 (buffer, 9);
67     ASSERT (result);
68     ASSERT (memcmp (result, buffer, 9) == 0);
69     ASSERT (result[9] == '\0');
70     free (result);
71   }
72   {
73     char *result = xmemdup0 (buffer, 10);
74     ASSERT (result);
75     ASSERT (memcmp (result, buffer, 10) == 0);
76     ASSERT (result[10] == '\0');
77     free (result);
78   }
79
80   return 0;
81 }