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