value_nan.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Centre National de la Recherche Scientifique
  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. #ifdef STARPU_HAVE_WINDOWS
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #endif
  23. #define STRING "booh"
  24. int _check_number(double val, int checknan)
  25. {
  26. char *tmp = "starpu_XXXXXX";
  27. char filename[100];
  28. int id;
  29. strcpy(filename, tmp);
  30. #ifdef STARPU_HAVE_WINDOWS
  31. _mktemp(filename);
  32. id = open(filename, _O_RDWR);
  33. #else
  34. id = mkstemp(filename);
  35. #endif
  36. /* fail */
  37. if (id < 0)
  38. {
  39. return 1;
  40. }
  41. /* write the double value in the file followed by a predefined string */
  42. FILE *f = fopen(filename, "w");
  43. if (!f)
  44. {
  45. FPRINTF(stderr, "Error when opening file %s\n", filename);
  46. return 1;
  47. }
  48. fprintf(f, "%lf %s\n", val, STRING);
  49. fclose(f);
  50. /* read the double value and the string back from the file */
  51. f = fopen(filename, "r");
  52. if (!f)
  53. {
  54. FPRINTF(stderr, "Error when opening file %s\n", filename);
  55. return 1;
  56. }
  57. double lat;
  58. char str[10];
  59. int x = _starpu_read_double(f, "%lf", &lat);
  60. int y = fscanf(f, "%s", str);
  61. fclose(f);
  62. /* check that what has been read is identical to what has been written */
  63. int pass;
  64. pass = (x == 1) && (y == 1);
  65. pass = pass && strcmp(str, STRING) == 0;
  66. if (checknan)
  67. pass = pass && isnan(val) && isnan(lat);
  68. else
  69. pass = pass && lat == val;
  70. return pass?0:1;
  71. }
  72. int main(int argc, char **argv)
  73. {
  74. int ret;
  75. ret = _check_number(42.0, 0);
  76. FPRINTF(stderr, "%s when reading %lf\n", ret==0?"Success":"Error", 42.0);
  77. if (ret==0)
  78. {
  79. ret = _check_number(NAN, 1);
  80. FPRINTF(stderr, "%s when reading %lf\n", ret==0?"Success":"Error", NAN);
  81. }
  82. return ret;
  83. }