value_nan.c 2.6 KB

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