Use spaces for indentation, not tabs.
[gnulib.git] / tests / test-times.c
1 /* Test of times function.
2    Copyright (C) 2008 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 Simon Josefsson <simon@josefsson.org>, 2008.  */
18
19 #include <config.h>
20
21 #include <sys/times.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include <unistd.h>
26 #include <math.h>
27
28 static int
29 doublecmp (const void *p, const void *q)
30 {
31   double a = *(double *) p;
32   double b = *(double *) q;
33
34   return a < b;
35 }
36
37 int
38 main (int argc, char *argv[])
39 {
40   struct tms tms;
41   clock_t t;
42 #ifndef _SC_CLK_TCK
43   clock_t clk_tck = CLK_TCK;
44 #else
45   clock_t clk_tck = sysconf (_SC_CLK_TCK);
46 #endif
47
48   t = times (&tms);
49   if (t == (clock_t) -1)
50     {
51       perror ("times");
52       return EXIT_FAILURE;
53     }
54
55   if (argc > 1)
56     {
57       printf ("clk_tck %ld\n", (long int) clk_tck);
58
59       printf ("t %ld\n", t);
60       printf ("tms.tms_utime %ldms\n", (tms.tms_utime * 1000) / clk_tck);
61       printf ("tms.tms_stime %ldms\n", (tms.tms_stime * 1000) / clk_tck);
62       printf ("tms.tms_cutime %ldms\n", (tms.tms_cutime * 1000) / clk_tck);
63       printf ("tms.tms_cstime %ldms\n", (tms.tms_cstime * 1000) / clk_tck);
64     }
65
66   if (argc > 1)
67     {
68       size_t size = atoi (argv[1]);
69       double *base;
70       size_t i;
71
72       base = malloc (size * sizeof (double));
73
74       for (i = 0; i < size; i++)
75         base[i] = i * i;
76
77       qsort (base, size, sizeof (double), doublecmp);
78
79       free (base);
80     }
81
82   t = times (&tms);
83   if (t == (clock_t) -1)
84     {
85       perror ("times");
86       return EXIT_FAILURE;
87     }
88
89   if (argc > 1)
90     {
91       printf ("clk_tck %ld\n", (long int) clk_tck);
92
93       printf ("t %ld\n", t);
94       printf ("tms.tms_utime %ldms\n", (tms.tms_utime * 1000) / clk_tck);
95       printf ("tms.tms_stime %ldms\n", (tms.tms_stime * 1000) / clk_tck);
96       printf ("tms.tms_cutime %ldms\n", (tms.tms_cutime * 1000) / clk_tck);
97       printf ("tms.tms_cstime %ldms\n", (tms.tms_cstime * 1000) / clk_tck);
98     }
99
100   return 0;
101 }