maint: update copyright
[gnulib.git] / tests / test-times.c
1 /* Test of times function.
2    Copyright (C) 2008-2014 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
23 #include "signature.h"
24 SIGNATURE_CHECK (times, clock_t, (struct tms *));
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <unistd.h>
30 #include <math.h>
31
32 static int
33 doublecmp (const void *p, const void *q)
34 {
35   double a = *(double *) p;
36   double b = *(double *) q;
37
38   return a < b;
39 }
40
41 int
42 main (int argc, char *argv[])
43 {
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
51
52   t = times (&tms);
53   if (t == (clock_t) -1)
54     {
55       perror ("times");
56       return EXIT_FAILURE;
57     }
58
59   if (argc > 1)
60     {
61       printf ("clk_tck %ld\n", (long int) clk_tck);
62
63       printf ("t %ld\n", (long int) t);
64       printf ("tms.tms_utime %ldms\n", ((long int) tms.tms_utime * 1000) / clk_tck);
65       printf ("tms.tms_stime %ldms\n", ((long int) tms.tms_stime * 1000) / clk_tck);
66       printf ("tms.tms_cutime %ldms\n", ((long int) tms.tms_cutime * 1000) / clk_tck);
67       printf ("tms.tms_cstime %ldms\n", ((long int) tms.tms_cstime * 1000) / clk_tck);
68     }
69
70   if (argc > 1)
71     {
72       size_t size = atoi (argv[1]);
73       double *base;
74       size_t i;
75
76       base = malloc (size * sizeof (double));
77
78       for (i = 0; i < size; i++)
79         base[i] = i * i;
80
81       qsort (base, size, sizeof (double), doublecmp);
82
83       free (base);
84     }
85
86   t = times (&tms);
87   if (t == (clock_t) -1)
88     {
89       perror ("times");
90       return EXIT_FAILURE;
91     }
92
93   if (argc > 1)
94     {
95       printf ("clk_tck %ld\n", (long int) clk_tck);
96
97       printf ("t %ld\n", (long int) t);
98       printf ("tms.tms_utime %ldms\n", ((long int) tms.tms_utime * 1000) / clk_tck);
99       printf ("tms.tms_stime %ldms\n", ((long int) tms.tms_stime * 1000) / clk_tck);
100       printf ("tms.tms_cutime %ldms\n", ((long int) tms.tms_cutime * 1000) / clk_tck);
101       printf ("tms.tms_cstime %ldms\n", ((long int) tms.tms_cstime * 1000) / clk_tck);
102     }
103
104   return 0;
105 }