*printf-posix: Avoid test failures. Make tests work on MacOS X, Cygwin.
[gnulib.git] / tests / test-dprintf-posix2.c
1 /* Test of POSIX compatible dprintf() function.
2    Copyright (C) 2009-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 of the License, or
7    (at your option) 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 Bruno Haible <bruno@clisp.org>, 2009.  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27
28 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT
29 # include <sys/types.h>
30 # include <sys/time.h>
31 # include <sys/resource.h>
32 #endif
33
34 #include "resource-ext.h"
35
36 /* Test against a memory leak in the fprintf replacement.  */
37
38 /* Number of iterations across the loop.  */
39 #define NUM_ROUNDS 1000
40
41 /* Number of bytes that are allowed to escape per round.  */
42 #define MAX_ALLOC_ROUND 10000
43
44 /* Number of bytes that are allowed to escape in total.
45    This should be at least 10 MB, since it includes the normal memory
46    or address space of the test program.  */
47 #define MAX_ALLOC_TOTAL (NUM_ROUNDS * MAX_ALLOC_ROUND)
48
49 int
50 main (int argc, char *argv[])
51 {
52   uintptr_t initial_rusage_as;
53   int arg;
54   int result;
55
56   /* Limit the amount of malloc()ed memory to MAX_ALLOC_TOTAL or less.  */
57
58   /* On BSD systems, malloc() is limited by RLIMIT_DATA.  */
59 #if HAVE_GETRLIMIT && HAVE_SETRLIMIT && defined RLIMIT_DATA
60   {
61     struct rlimit limit;
62
63     if (getrlimit (RLIMIT_DATA, &limit) >= 0)
64       {
65         if (limit.rlim_max == RLIM_INFINITY || limit.rlim_max > MAX_ALLOC_TOTAL)
66           limit.rlim_max = MAX_ALLOC_TOTAL;
67         limit.rlim_cur = limit.rlim_max;
68         (void) setrlimit (RLIMIT_DATA, &limit);
69       }
70   }
71 #endif
72   /* On Linux systems, malloc() is limited by RLIMIT_AS.
73      On some systems, setrlimit of RLIMIT_AS doesn't work but get_rusage_as ()
74      does.  Allow the address space size to grow by at most MAX_ALLOC_TOTAL.  */
75   initial_rusage_as = get_rusage_as ();
76   if (initial_rusage_as == 0)
77     return 77;
78
79   arg = atoi (argv[1]);
80   if (arg == 0)
81     {
82       void *memory = malloc (MAX_ALLOC_TOTAL);
83       if (memory == NULL)
84         return 1;
85       memset (memory, 17, MAX_ALLOC_TOTAL);
86       result = 78;
87     }
88   else
89     {
90       /* Perform the test and test whether it triggers a permanent memory
91          allocation of more than MAX_ALLOC_TOTAL bytes.  */
92       int repeat;
93
94       for (repeat = 0; repeat < NUM_ROUNDS; repeat++)
95         {
96           /* This may produce a temporary memory allocation of 11000 bytes.
97              but should not result in a permanent memory allocation.  */
98           if (dprintf (STDOUT_FILENO, "%011000d\n", 17) == -1
99               && errno == ENOMEM)
100             return 1;
101         }
102
103       result = 0;
104     }
105
106   if (get_rusage_as () > initial_rusage_as + MAX_ALLOC_TOTAL)
107     return 1;
108
109   return result;
110 }