malloc/tst-valloc.c: Improve test coverage and use test-skeleton.c.
[platform/upstream/glibc.git] / malloc / tst-valloc.c
1 /* Copyright (C) 2013 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library 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 GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, see
16    <http://www.gnu.org/licenses/>.  */
17
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 static int errors = 0;
25
26 static void
27 merror (const char *msg)
28 {
29   ++errors;
30   printf ("Error: %s\n", msg);
31 }
32
33 static int
34 do_test (void)
35 {
36   void *p;
37   unsigned long pagesize = getpagesize();
38   unsigned long ptrval;
39   int save;
40
41   errno = 0;
42
43   p = valloc (-1);
44
45   save = errno;
46
47   if (p != NULL)
48     merror ("valloc (-1) succeeded.");
49
50   if (p == NULL && save != ENOMEM)
51     merror ("valloc (-1) errno is not set correctly");
52
53   errno = 0;
54
55   p = valloc (-pagesize);
56
57   save = errno;
58
59   if (p != NULL)
60     merror ("valloc (-pagesize) succeeded.");
61
62   if (p == NULL && save != ENOMEM)
63     merror ("valloc (-pagesize) errno is not set correctly");
64
65   p = valloc (0);
66
67   if (p == NULL)
68     merror ("valloc (0) failed.");
69
70   free (p);
71
72   p = valloc (32);
73
74   if (p == NULL)
75     merror ("valloc (32) failed.");
76
77   ptrval = (unsigned long)p;
78
79   if (p == NULL)
80     merror ("valloc (32) failed.");
81
82   if ((ptrval & (pagesize - 1)) != 0)
83     merror ("returned pointer is not page aligned.");
84
85   free (p);
86
87   return errors != 0;
88 }
89
90 #define TEST_FUNCTION do_test ()
91 #include "../test-skeleton.c"