value_nan.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <common/config.h>
  17. #include <core/perfmodel/perfmodel.h>
  18. #include "../helper.h"
  19. #include <unistd.h>
  20. #ifdef STARPU_HAVE_WINDOWS
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #endif
  24. /*
  25. * Test that _starpu_write_double and _starpu_read_double properly manipulate
  26. * NaN values
  27. */
  28. #define STRING "booh"
  29. static
  30. int _check_number(double val, int checknan)
  31. {
  32. char *tmp = "starpu_XXXXXX";
  33. char filename[100];
  34. strcpy(filename, tmp);
  35. #ifdef STARPU_HAVE_WINDOWS
  36. _mktemp(filename);
  37. #else
  38. {
  39. int id = mkstemp(filename);
  40. /* fail */
  41. if (id < 0)
  42. {
  43. FPRINTF(stderr, "Error when creating temp file\n");
  44. return 1;
  45. }
  46. }
  47. #endif
  48. /* write the double value in the file followed by a predefined string */
  49. FILE *f = fopen(filename, "w");
  50. if (!f)
  51. {
  52. FPRINTF(stderr, "Error when opening file %s\n", filename);
  53. return 1;
  54. }
  55. // A double is written with the format %e ...
  56. _starpu_write_double(f, "%e", val);
  57. fprintf(f, " %s\n", STRING);
  58. fclose(f);
  59. /* read the double value and the string back from the file */
  60. f = fopen(filename, "r");
  61. if (!f)
  62. {
  63. FPRINTF(stderr, "Error when opening file %s\n", filename);
  64. return 1;
  65. }
  66. double lat;
  67. char str[10];
  68. // ... but is read with the format %le
  69. int x = _starpu_read_double(f, "%le", &lat);
  70. int y = fscanf(f, " %9s", str);
  71. fclose(f);
  72. unlink(filename);
  73. /* check that what has been read is identical to what has been written */
  74. int pass;
  75. pass = (x == 1) && (y == 1);
  76. pass = pass && strcmp(str, STRING) == 0;
  77. if (checknan)
  78. pass = pass && isnan(val) && isnan(lat);
  79. else
  80. pass = pass && (int)lat == (int)val;
  81. return pass?0:1;
  82. }
  83. int main(void)
  84. {
  85. int ret1, ret2;
  86. double nanvalue = nan("");
  87. ret1 = _check_number(42.0, 0);
  88. FPRINTF(stderr, "%s when reading %e\n", ret1==0?"Success":"Error", 42.0);
  89. ret2 = _check_number(nanvalue, 1);
  90. FPRINTF(stderr, "%s when reading %e\n", ret2==0?"Success":"Error", nanvalue);
  91. return ret1+ret2;
  92. }