value_nan.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013, 2015 CNRS
  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 <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. #define STRING "booh"
  25. static
  26. int _check_number(double val, int checknan)
  27. {
  28. char *tmp = "starpu_XXXXXX";
  29. char filename[100];
  30. strcpy(filename, tmp);
  31. #ifdef STARPU_HAVE_WINDOWS
  32. _mktemp(filename);
  33. #else
  34. {
  35. int id = mkstemp(filename);
  36. /* fail */
  37. if (id < 0)
  38. {
  39. FPRINTF(stderr, "Error when creating temp file\n");
  40. return 1;
  41. }
  42. }
  43. #endif
  44. /* write the double value in the file followed by a predefined string */
  45. FILE *f = fopen(filename, "w");
  46. if (!f)
  47. {
  48. FPRINTF(stderr, "Error when opening file %s\n", filename);
  49. return 1;
  50. }
  51. // A double is written with the format %e ...
  52. _starpu_write_double(f, "%e", val);
  53. fprintf(f, " %s\n", STRING);
  54. fclose(f);
  55. /* read the double value and the string back from the file */
  56. f = fopen(filename, "r");
  57. if (!f)
  58. {
  59. FPRINTF(stderr, "Error when opening file %s\n", filename);
  60. return 1;
  61. }
  62. double lat;
  63. char str[10];
  64. // ... but is read with the format %le
  65. int x = _starpu_read_double(f, "%le", &lat);
  66. int y = fscanf(f, " %s", str);
  67. fclose(f);
  68. unlink(filename);
  69. /* check that what has been read is identical to what has been written */
  70. int pass;
  71. pass = (x == 1) && (y == 1);
  72. pass = pass && strcmp(str, STRING) == 0;
  73. if (checknan)
  74. pass = pass && isnan(val) && isnan(lat);
  75. else
  76. pass = pass && (int)lat == (int)val;
  77. return pass?0:1;
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. int ret1, ret2;
  82. double nanvalue = nan("");
  83. ret1 = _check_number(42.0, 0);
  84. FPRINTF(stderr, "%s when reading %e\n", ret1==0?"Success":"Error", 42.0);
  85. ret2 = _check_number(nanvalue, 1);
  86. FPRINTF(stderr, "%s when reading %e\n", ret2==0?"Success":"Error", nanvalue);
  87. return ret1+ret2;
  88. }