annotate tests/test-times.c @ 10736:0a0a6357e314

Add files missing from last commit.
author Simon Josefsson <simon@josefsson.org>
date Tue, 28 Oct 2008 18:22:28 +0100
parents
children e8d2c6fc33ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10736
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
1 /* Test of times function.
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
2 Copyright (C) 2008 Free Software Foundation, Inc.
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
3
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
7 (at your option) any later version.
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
8
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
12 GNU General Public License for more details.
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
13
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
16
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
17 /* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
18
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
19 #include <config.h>
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
20
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
21 #include <sys/times.h>
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
22 #include <stdio.h>
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
23 #include <stdlib.h>
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
24 #include <stdint.h>
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
25 #include <unistd.h>
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
26 #include <math.h>
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
27
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
28 static int
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
29 doublecmp (const void *p, const void *q)
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
30 {
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
31 double a = *(double *) p;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
32 double b = *(double *) q;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
33
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
34 return a < b;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
35 }
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
36
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
37 int
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
38 main (int argc, char *argv[])
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
39 {
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
40 struct tms tms;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
41 clock_t t;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
42 #ifndef _SC_CLK_TCK
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
43 clock_t clk_tck = CLK_TCK;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
44 #else
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
45 clock_t clk_tck = sysconf (_SC_CLK_TCK);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
46 #endif
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
47
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
48 t = times (&tms);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
49 if (t == (clock_t) -1)
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
50 {
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
51 perror ("times");
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
52 return EXIT_FAILURE;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
53 }
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
54
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
55 if (argc > 1)
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
56 {
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
57 printf ("clk_tck %ld\n", (long int) clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
58
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
59 printf ("t %ld\n", t);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
60 printf ("tms.tms_utime %ldms\n", (tms.tms_utime * 1000) / clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
61 printf ("tms.tms_stime %ldms\n", (tms.tms_stime * 1000) / clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
62 printf ("tms.tms_cutime %ldms\n", (tms.tms_cutime * 1000) / clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
63 printf ("tms.tms_cstime %ldms\n", (tms.tms_cstime * 1000) / clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
64 }
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
65
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
66 if (argc > 1)
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
67 {
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
68 size_t size = atoi (argv[1]);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
69 double *base;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
70 size_t i;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
71
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
72 base = malloc (size * sizeof (double));
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
73
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
74 for (i = 0; i < size; i++)
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
75 base[i] = i * i;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
76
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
77 qsort (base, size, sizeof (double), doublecmp);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
78
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
79 free (base);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
80 }
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
81
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
82 t = times (&tms);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
83 if (t == (clock_t) -1)
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
84 {
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
85 perror ("times");
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
86 return EXIT_FAILURE;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
87 }
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
88
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
89 if (argc > 1)
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
90 {
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
91 printf ("clk_tck %ld\n", (long int) clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
92
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
93 printf ("t %ld\n", t);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
94 printf ("tms.tms_utime %ldms\n", (tms.tms_utime * 1000) / clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
95 printf ("tms.tms_stime %ldms\n", (tms.tms_stime * 1000) / clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
96 printf ("tms.tms_cutime %ldms\n", (tms.tms_cutime * 1000) / clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
97 printf ("tms.tms_cstime %ldms\n", (tms.tms_cstime * 1000) / clk_tck);
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
98 }
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
99
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
100 return 0;
0a0a6357e314 Add files missing from last commit.
Simon Josefsson <simon@josefsson.org>
parents:
diff changeset
101 }