/libext/libiconv/gnulib/tests/test-times.c

https://gitlab.com/Ntemis/wive-ng-mt · C · 105 lines · 69 code · 20 blank · 16 comment · 8 complexity · 75c4bcd1ad4cadb147647a90d2f318de MD5 · raw file

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