usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-times.c
blob75f35b2965e773b1e0bf4d3f4782f030bb687b1a
1 /* Test of times function.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
19 #include <config.h>
21 #include <sys/times.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (times, clock_t, (struct tms *));
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <unistd.h>
30 #include <math.h>
32 static int
33 doublecmp (const void *p, const void *q)
35 double a = *(double *) p;
36 double b = *(double *) q;
38 return a < b ? -1 : a > b ? 1 : 0;
41 int
42 main (int argc, char *argv[])
44 struct tms tms;
45 clock_t t;
46 #ifndef _SC_CLK_TCK
47 clock_t clk_tck = CLK_TCK;
48 #else
49 clock_t clk_tck = sysconf (_SC_CLK_TCK);
50 #endif
52 t = times (&tms);
53 if (t == (clock_t) -1)
55 perror ("times");
56 return EXIT_FAILURE;
59 if (argc > 1)
61 printf ("clk_tck %ld\n", (long int) clk_tck);
63 printf ("t %ld\n", (long int) t);
64 printf ("tms.tms_utime %ldms\n", ((long int) tms.tms_utime * 1000) / (long int) clk_tck);
65 printf ("tms.tms_stime %ldms\n", ((long int) tms.tms_stime * 1000) / (long int) clk_tck);
66 printf ("tms.tms_cutime %ldms\n", ((long int) tms.tms_cutime * 1000) / (long int) clk_tck);
67 printf ("tms.tms_cstime %ldms\n", ((long int) tms.tms_cstime * 1000) / (long int) clk_tck);
70 if (argc > 1)
72 size_t size = atoi (argv[1]);
73 double *base;
74 size_t i;
76 base = malloc (size * sizeof (double));
78 for (i = 0; i < size; i++)
79 base[i] = i * i;
81 qsort (base, size, sizeof (double), doublecmp);
83 free (base);
86 t = times (&tms);
87 if (t == (clock_t) -1)
89 perror ("times");
90 return EXIT_FAILURE;
93 if (argc > 1)
95 printf ("clk_tck %ld\n", (long int) clk_tck);
97 printf ("t %ld\n", (long int) t);
98 printf ("tms.tms_utime %ldms\n", ((long int) tms.tms_utime * 1000) / (long int) clk_tck);
99 printf ("tms.tms_stime %ldms\n", ((long int) tms.tms_stime * 1000) / (long int) clk_tck);
100 printf ("tms.tms_cutime %ldms\n", ((long int) tms.tms_cutime * 1000) / (long int) clk_tck);
101 printf ("tms.tms_cstime %ldms\n", ((long int) tms.tms_cstime * 1000) / (long int) clk_tck);
104 return 0;