posix_spawn_file_actions_* tests: Fix module dependencies.
[gnulib.git] / tests / test-nonblocking-misc.h
bloba5ecbcdd005d7ff6d40665328ac019d9cb2ff175
1 /* Test for nonblocking read and write.
3 Copyright (C) 2011-2021 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 /* Whether to print debugging messages. */
19 #define ENABLE_DEBUGGING 0
21 /* Delay (in microseconds) to sleep when write() or read() returned -1 with
22 errno = EAGAIN. */
23 #define SMALL_DELAY 10000
25 /* Return a memory area, filled with the data to be transferred. */
26 static unsigned char *
27 init_data (size_t data_block_size)
29 unsigned char *data;
30 unsigned int i;
32 data = (unsigned char *) malloc (2 * data_block_size);
33 ASSERT (data != NULL);
35 for (i = 0; i < 2 * data_block_size; i++)
36 data[i] = (unsigned char) (i * i + (7 * i) % 61 + 4);
38 return data;
41 #if ENABLE_DEBUGGING
42 # include <stdarg.h>
43 static int dbgfprintf (FILE *fp, const char *format, ...)
44 _GL_ATTRIBUTE_FORMAT_PRINTF_STANDARD (2, 3);
45 static int
46 dbgfprintf (FILE *fp, const char *format, ...)
48 /* Accumulate the entire line in a buffer, so that the output on fp
49 is done atomically. */
50 char line[1024];
51 size_t line_len;
52 struct timeval current_time;
53 va_list args;
54 int ret;
56 line_len = 0;
57 gettimeofday (&current_time, NULL);
58 ret = snprintf (line, sizeof (line), "%.6f ",
59 current_time.tv_sec + (double) current_time.tv_usec * 1e-6);
60 if (ret < 0)
61 return -1;
62 line_len = strlen (line);
64 va_start (args, format);
65 ret = vsnprintf (line + line_len, sizeof (line) - line_len, format, args);
66 va_end (args);
67 if (ret < 0)
68 return -1;
69 line_len += strlen (line + line_len);
71 ret = fwrite (line, 1, line_len, fp);
73 /* Make sure the debugging information is output, so that the order of the
74 messages reflects the timeline of events, and so that the output is not
75 lost if the program crashes afterwards (relevant on mingw). */
76 fflush (fp);
77 return ret;
79 #else
80 # define dbgfprintf if (1) ; else fprintf
81 #endif
83 /* Return a textual description of the error code ERR, if FAILED is true.
84 Return an empty string if FAILED is false. */
85 static const char *
86 dbgstrerror (bool failed, int err)
88 static char buf[256];
89 if (failed)
91 sprintf (buf, " %d %s", err, strerror (err));
92 return buf;
94 else
95 return "";
98 #define TIMING_DECLS \
99 struct timeval before_time; \
100 struct timeval after_time; \
101 double spent_time;
102 #define START_TIMING \
103 gettimeofday (&before_time, NULL);
104 #define END_TIMING \
105 gettimeofday (&after_time, NULL); \
106 spent_time = \
107 (after_time.tv_sec - before_time.tv_sec) \
108 + ((double) after_time.tv_usec - (double) before_time.tv_usec) * 1e-6;