testUtils.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #ifndef _TEST_UTILS_H_
  2. #define _TEST_UTILS_H_ 1
  3. #include "irrlicht.h"
  4. #include <assert.h>
  5. // Small hack. Some newer X11 systems can't handle switching drivers too fast (causing BadWindow errors in X_ChangeWindowAttributes).
  6. // Could be they don't like when Windows with different Visuals are created very quickly (it always happened after creating a new Window with different Visual to previous one).
  7. // timeMs value set by try&error
  8. #ifdef _IRR_POSIX_API_
  9. #include <time.h>
  10. #define SLOW_SWITCH \
  11. do { \
  12. struct timespec ts; \
  13. const int timeMs = 250; \
  14. ts.tv_sec = (time_t) (timeMs / 1000); \
  15. ts.tv_nsec = (long) (timeMs % 1000) * 1000000; \
  16. nanosleep(&ts, NULL);\
  17. } while (false)
  18. #else
  19. #define SLOW_SWITCH
  20. #endif
  21. #define TestWithAllDrivers(X) \
  22. logTestString("Running test " #X "\n"); \
  23. for (u32 i=1; i<video::EDT_COUNT; ++i) \
  24. if (video::E_DRIVER_TYPE(i) != video::DEPRECATED_EDT_DIRECT3D8_NO_LONGER_EXISTS && irr::IrrlichtDevice::isDriverSupported((irr::video::E_DRIVER_TYPE)i)) \
  25. {\
  26. SLOW_SWITCH; \
  27. result &= X(video::E_DRIVER_TYPE(i));\
  28. }
  29. #define TestWithAllHWDrivers(X) \
  30. SLOW_SWITCH; \
  31. logTestString("Running test " #X "\n"); \
  32. for (u32 i=video::EDT_DIRECT3D9; i<video::EDT_COUNT; ++i) \
  33. if (irr::IrrlichtDevice::isDriverSupported((irr::video::E_DRIVER_TYPE)i)) \
  34. {\
  35. SLOW_SWITCH; \
  36. result &= X(video::E_DRIVER_TYPE(i));\
  37. }
  38. // replacement for assert which does log the lines instead
  39. #define assert_log(X) \
  40. do { \
  41. if ( !(X) ) \
  42. { \
  43. logTestString("ASSERT in %s:%d: %s\n", __FILE__, __LINE__, #X); \
  44. } \
  45. } while (false)
  46. //! Compare two files
  47. /** \param fileName1 The first file for comparison.
  48. \param fileName2 The second file for comparison.
  49. \return true if the files are identical, false on any error or difference. */
  50. extern bool binaryCompareFiles(const char * fileName1, const char * fileName2);
  51. //! Compare two xml-files (which can have different types of text-encoding)
  52. /** \param fs Filesystem which should be used.
  53. \param fileName1 The first file for comparison.
  54. \param fileName2 The second file for comparison.
  55. \return true if the files are identical, false on any error or difference. */
  56. extern bool xmlCompareFiles(irr::io::IFileSystem * fs, const char * fileName1, const char * fileName2);
  57. //! Compare two images, returning the degree to which they match.
  58. /** \param driver The Irrlicht video driver.
  59. \param fileName1 The first image to compare.
  60. \param fileName2 The second image to compare.
  61. \return The match, from 0.f to 100.f */
  62. extern float fuzzyCompareImages(irr::video::IVideoDriver * driver,
  63. const char * fileName1, const char * fileName2);
  64. //! Take a screenshot and compare it against a reference screenshot in the tests/media subdirectory
  65. /** \param driver The Irrlicht video driver.
  66. \param fileName The unique filename suffix that will be appended to the name of the video driver.
  67. \param requiredMatch The degree to which the screenshot needs to match the reference image
  68. in order to be considered a match.
  69. \return true if the screenshot was taken and is identical to the reference image of the same name
  70. in the tests/media directory, false on any error or difference. */
  71. extern bool takeScreenshotAndCompareAgainstReference(irr::video::IVideoDriver * driver,
  72. const char * fileName,
  73. irr::f32 requiredMatch = 99.f);
  74. //! Stabilize the screen background eg. eliminate problems like an aero transparency effects etc.
  75. /** \param driver The Irrlicht video driver.
  76. \return true if required color is the same as a window background color. */
  77. extern void stabilizeScreenBackground(irr::video::IVideoDriver * driver,
  78. irr::video::SColor color = irr::video::SColor(255, 255, 255, 255));
  79. //! Opens a test log file, deleting any existing contents.
  80. /** \param startNewLog true to create a new log file, false to append to an
  81. existing one.
  82. \param filename The filename to open
  83. \return true if the test log file was opened, false on error. */
  84. extern bool openTestLog(bool startNewLog, const char * filename = "tests.log");
  85. //! Close the test log file opened with openTestLog()
  86. extern void closeTestLog();
  87. //! Log a string to the console and the test log file created by openTestLog().
  88. /** \param format The format string
  89. \... optional parameters */
  90. extern void logTestString(const char * format, ...);
  91. //! Return a drivername for the driver which is useable in filenames
  92. extern irr::core::stringc shortDriverName(irr::video::IVideoDriver * driver);
  93. #endif // _TEST_UTILS_H_