testUtils.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. #include <memory.h>
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <stdarg.h>
  8. #if defined(_MSC_VER) || defined(_IRR_WINDOWS_API_)
  9. #define _CRT_SECURE_NO_WARNINGS 1
  10. #include <windows.h> // For OutputDebugString()
  11. #endif // _MSC_VER || _IRR_WINDOWS_API_
  12. #ifdef _MSC_VER
  13. #pragma warning( disable: 4996)
  14. #endif
  15. using namespace irr;
  16. bool binaryCompareFiles(const char * fileName1, const char * fileName2)
  17. {
  18. assert(fileName1);
  19. assert(fileName2);
  20. if (!fileName1 || !fileName2)
  21. return false;
  22. FILE * file1 = fopen(fileName1, "rb");
  23. if (!file1)
  24. {
  25. logTestString("binaryCompareFiles: File '%s' cannot be opened\n", fileName1);
  26. assert(file1);
  27. return false;
  28. }
  29. FILE * file2 = fopen(fileName2, "rb");
  30. if (!file2)
  31. {
  32. logTestString("binaryCompareFiles: File '%s' cannot be opened\n", fileName2);
  33. (void)fclose(file1);
  34. assert(file2);
  35. return false;
  36. }
  37. (void)fseek(file1, 0, SEEK_END);
  38. (void)fseek(file2, 0, SEEK_END);
  39. const size_t file1Size = ftell(file1);
  40. const size_t file2Size = ftell(file2);
  41. if (file1Size != file2Size)
  42. {
  43. logTestString("binaryCompareFiles: Files are different sizes: %d vs %d\n",
  44. file1Size, file2Size);
  45. (void)fclose(file1);
  46. (void)fclose(file2);
  47. return false;
  48. }
  49. (void)fseek(file1, 0, SEEK_SET);
  50. (void)fseek(file2, 0, SEEK_SET);
  51. char file1Buffer[8196];
  52. char file2Buffer[8196];
  53. while (!feof(file1))
  54. {
  55. if (feof(file2)
  56. ||(fread(file1Buffer, sizeof(file1Buffer), 1, file1) !=
  57. fread(file2Buffer, sizeof(file2Buffer), 1, file2)))
  58. {
  59. logTestString("binaryCompareFiles: Error during file reading\n");
  60. break;
  61. }
  62. if (memcmp(file1Buffer, file2Buffer, sizeof(file1Buffer)))
  63. {
  64. logTestString("binaryCompareFiles: files are different\n");
  65. break;
  66. }
  67. }
  68. bool filesAreIdentical = feof(file1) && feof(file2);
  69. (void)fclose(file1);
  70. (void)fclose(file2);
  71. return filesAreIdentical;
  72. }
  73. bool xmlCompareFiles(irr::io::IFileSystem * fs, const char * fileName1, const char * fileName2)
  74. {
  75. if (!fileName1 || !fileName2)
  76. return false;
  77. io::IXMLReaderUTF8* reader1 = fs->createXMLReaderUTF8(fileName1);
  78. if (!reader1)
  79. {
  80. logTestString("xmlCompareFiles: Could not create a XML reader for '%s'\n", fileName1);
  81. return false;
  82. }
  83. io::IXMLReaderUTF8* reader2 = fs->createXMLReaderUTF8(fileName2);
  84. if (!reader2)
  85. {
  86. logTestString("xmlCompareFiles: Could not create a XML reader for '%s'\n", fileName2);
  87. reader1->drop();
  88. return false;
  89. }
  90. bool different = false;
  91. bool read1 = reader1->read();
  92. bool read2 = reader2->read();
  93. if ( !read1 && !read2 )
  94. {
  95. logTestString("xmlCompareFiles: Both files have no nodes: '%s' %s - is this ok?\n", fileName1, fileName2);
  96. reader1->drop();
  97. reader2->drop();
  98. return true;
  99. }
  100. while (read1 && read2)
  101. {
  102. io::EXML_NODE type1 = reader1->getNodeType();
  103. io::EXML_NODE type2 = reader2->getNodeType();
  104. if ( type1 != type2 )
  105. {
  106. const c8* name1 = reader1->getNodeName();
  107. const c8* name2 = reader2->getNodeName();
  108. logTestString("xmlCompareFiles: file '%s' has different nodes than %s in nodes \"%s\" and \"%s\"\n"
  109. , fileName1, fileName2, name1 ? name1 : "NULL", name2 ? name2 : "NULL");
  110. different = true;
  111. break;
  112. }
  113. if ( reader1->isEmptyElement() != reader2->isEmptyElement() )
  114. {
  115. logTestString("xmlCompareFiles: file '%s' has different empty elements than %s\n", fileName1, fileName2);
  116. different = true;
  117. break;
  118. }
  119. switch ( type1 )
  120. {
  121. case io::EXN_NONE:
  122. case io::EXN_ELEMENT_END:
  123. break;
  124. case io::EXN_ELEMENT:
  125. {
  126. core::stringc name1(reader1->getNodeName());
  127. core::stringc name2(reader2->getNodeName());
  128. if ( name1 != name2 )
  129. {
  130. logTestString("xmlCompareFiles: %s has node %s where %s has node %s\n"
  131. , fileName1, name1.c_str(), fileName2, name2.c_str() );
  132. different = true;
  133. break;
  134. }
  135. unsigned int numAttributes1 = reader1->getAttributeCount();
  136. unsigned int numAttributes2 = reader2->getAttributeCount();
  137. if ( numAttributes1 != numAttributes2 )
  138. {
  139. logTestString("xmlCompareFiles: %s node %s has %d attributes where %s node %s has %d attributes\n"
  140. , fileName1, name1.c_str(), numAttributes1
  141. , fileName2, name2.c_str(), numAttributes2);
  142. different = true;
  143. break;
  144. }
  145. for ( unsigned int i=0; i < numAttributes1; ++i )
  146. {
  147. core::stringc attribName1(reader1->getAttributeName(int(i)));
  148. core::stringc attribName2(reader2->getAttributeName(int(i)));
  149. if ( attribName1 != attribName2 )
  150. {
  151. logTestString("xmlCompareFiles: %s node %s has attribute-name \"%s\" where %s node %s has name \"%s\"\n"
  152. , fileName1, name1.c_str(), attribName1.c_str()
  153. , fileName2, name2.c_str(), attribName2.c_str());
  154. different = true;
  155. break;
  156. }
  157. core::stringc attribVal1(reader1->getAttributeValue(int(i)));
  158. core::stringc attribVal2(reader2->getAttributeValue(int(i)));
  159. if ( attribName1 != attribName2 )
  160. {
  161. logTestString("xmlCompareFiles: %s node %s has attribute-value \"%s\" where %s node %s has value \"%s\"\n"
  162. , fileName1, name1.c_str(), attribVal1.c_str()
  163. , fileName2, name2.c_str(), attribVal2.c_str());
  164. different = true;
  165. break;
  166. }
  167. }
  168. break;
  169. }
  170. case io::EXN_TEXT:
  171. case io::EXN_UNKNOWN:
  172. case io::EXN_COMMENT:
  173. case io::EXN_CDATA:
  174. {
  175. core::stringc nodeData1( reader1->getNodeData() );
  176. core::stringc nodeData2( reader1->getNodeData() );
  177. // removeChars('\r') needed because irrXML doesn't do that (as it probably should)
  178. nodeData1.removeChars(core::stringc('\r'));
  179. nodeData2.removeChars(core::stringc('\r'));
  180. if ( nodeData1 != nodeData2 )
  181. {
  182. logTestString("xmlCompareFiles: %s has data \"%s\" where %s has data \"%s\"\n"
  183. , fileName1, nodeData1.c_str()
  184. , fileName2, nodeData2.c_str());
  185. different = true;
  186. }
  187. break;
  188. }
  189. }
  190. if ( different )
  191. break;
  192. read1 = reader1->read();
  193. read2 = reader2->read();
  194. }
  195. if ( !different && !read1 && !read2 )
  196. {
  197. reader1->drop();
  198. reader2->drop();
  199. return true;
  200. }
  201. if ( !different && read1 )
  202. {
  203. logTestString("xmlCompareFiles: file '%s' has more nodes than %s\n", fileName1, fileName2);
  204. }
  205. if ( !different && read2 )
  206. {
  207. logTestString("xmlCompareFiles: file '%s' has more nodes than %s\n", fileName2, fileName1);
  208. }
  209. reader1->drop();
  210. reader2->drop();
  211. return false;
  212. }
  213. //! Compare two images, returning the degree to which they match.
  214. /** \param image1 The first image to compare.
  215. \param image2 The second image to compare.
  216. \return The match, from 0.f to 100.f */
  217. static float fuzzyCompareImages(irr::video::IImage * image1,
  218. irr::video::IImage * image2)
  219. {
  220. assert(image1);
  221. assert(image2);
  222. if (!image1 || !image2)
  223. return 0.f;
  224. if (image1->getDimension() != image2->getDimension())
  225. {
  226. logTestString("fuzzyCompareImages: images are different sizes\n");
  227. return 0.f;
  228. }
  229. video::ECOLOR_FORMAT format1 = image1->getColorFormat();
  230. if (video::ECF_A8R8G8B8 != format1 && video::ECF_R8G8B8 != format1)
  231. {
  232. logTestString("fuzzyCompareImages: image 1 must be ECF_AR8G8B8 or ECF_R8G8B8\n");
  233. return 0.f;
  234. }
  235. video::ECOLOR_FORMAT format2 = image2->getColorFormat();
  236. if (video::ECF_A8R8G8B8 != format2 && video::ECF_R8G8B8 != format2)
  237. {
  238. logTestString("fuzzyCompareImages: image 2 must be ECF_AR8G8B8 or ECF_R8G8B8\n");
  239. return 0.f;
  240. }
  241. u8 * image1Data = (u8*)image1->getData();
  242. u8 * image2Data = (u8*)image2->getData();
  243. const u32 pixels = (image1->getPitch() * image1->getDimension().Height) / 4;
  244. u32 mismatchedColours = 0;
  245. for(u32 pixel = 0; pixel < pixels; ++pixel)
  246. {
  247. if (video::ECF_A8R8G8B8 == format1)
  248. image1Data++;
  249. const u8 r1 = *(image1Data++);
  250. const u8 g1 = *(image1Data++);
  251. const u8 b1 = *(image1Data++);
  252. if (video::ECF_A8R8G8B8 == format2)
  253. image2Data++;
  254. const u8 r2 = *(image2Data++);
  255. const u8 g2 = *(image2Data++);
  256. const u8 b2 = *(image2Data++);
  257. mismatchedColours += abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2);
  258. }
  259. const u32 totalColours = pixels * 255*3;
  260. return 100.f * (totalColours - mismatchedColours) / totalColours;
  261. }
  262. //! Compare two images, returning the degree to which they match.
  263. /** \param image1 The first image to compare.
  264. \param image2 The second image to compare.
  265. \return The match, from 0.f to 100.f */
  266. float fuzzyCompareImages(irr::video::IVideoDriver * driver,
  267. const char * fileName1, const char * fileName2)
  268. {
  269. assert(fileName1);
  270. assert(fileName2);
  271. irr::video::IImage * img1 = driver->createImageFromFile(fileName1);
  272. if (!img1)
  273. return 0;
  274. irr::video::IImage * img2 = driver->createImageFromFile(fileName2);
  275. const float result = fuzzyCompareImages(img1, img2);
  276. logTestString("Image match: %f%%\n", result);
  277. img1->drop();
  278. if (img2)
  279. img2->drop();
  280. return result;
  281. }
  282. void stabilizeScreenBackground(irr::video::IVideoDriver * driver,
  283. irr::video::SColor color)
  284. {
  285. for(int i = 0; i < 10000; ++i)
  286. {
  287. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, color);
  288. driver->endScene();
  289. irr::video::IImage * screenshot = driver->createScreenShot();
  290. if (!screenshot)
  291. return;
  292. const video::ECOLOR_FORMAT format = screenshot->getColorFormat();
  293. if (format != video::ECF_R8G8B8)
  294. {
  295. irr::video::IImage * fixedScreenshot = driver->createImage(video::ECF_R8G8B8, screenshot->getDimension());
  296. screenshot->copyTo(fixedScreenshot);
  297. screenshot->drop();
  298. if (!fixedScreenshot)
  299. return;
  300. screenshot = fixedScreenshot;
  301. }
  302. u8 * image1Data = (u8*)screenshot->getData();
  303. const u32 pixels = (screenshot->getPitch() * screenshot->getDimension().Height) / 4;
  304. bool status = true;
  305. for(u32 pixel = 0; pixel < pixels; ++pixel)
  306. {
  307. const u8 r = *(image1Data++);
  308. const u8 g = *(image1Data++);
  309. const u8 b = *(image1Data++);
  310. if (r != color.getRed() || g != color.getGreen() || b != color.getBlue())
  311. {
  312. status = false;
  313. break;
  314. }
  315. }
  316. if (status)
  317. {
  318. screenshot->drop();
  319. return;
  320. }
  321. screenshot->drop();
  322. }
  323. logTestString("stabilizeScreenBackground failed\n");
  324. }
  325. irr::core::stringc shortDriverName(irr::video::IVideoDriver * driver)
  326. {
  327. irr::core::stringc driverName = driver->getName();
  328. // For OpenGL and Burning, chop the version number out. Other drivers have more stable version numbers.
  329. // TA: Sorry Rogerborg. burnings video also has the version number inside;-)
  330. // maybe you sould take the getDriverType Info for this
  331. if (driverName.find("OpenGL") > -1)
  332. driverName = "OpenGL";
  333. else if (driverName.find("Burning's Video") > -1)
  334. driverName = "Burning's Video";
  335. return driverName;
  336. }
  337. bool takeScreenshotAndCompareAgainstReference(irr::video::IVideoDriver * driver,
  338. const char * fileName,
  339. irr::f32 requiredMatch)
  340. {
  341. irr::video::IImage * screenshot = driver->createScreenShot();
  342. if (!screenshot)
  343. {
  344. logTestString("Failed to take screenshot\n");
  345. assert(false);
  346. return false;
  347. }
  348. const video::ECOLOR_FORMAT format = screenshot->getColorFormat();
  349. if (format != video::ECF_R8G8B8)
  350. {
  351. irr::video::IImage * fixedScreenshot = driver->createImage(video::ECF_R8G8B8, screenshot->getDimension());
  352. screenshot->copyTo(fixedScreenshot);
  353. screenshot->drop();
  354. if (!fixedScreenshot)
  355. {
  356. logTestString("Failed to convert screenshot to ECF_A8R8G8B8\n");
  357. assert(false);
  358. return false;
  359. }
  360. screenshot = fixedScreenshot;
  361. }
  362. irr::core::stringc driverName = shortDriverName(driver);
  363. irr::core::stringc referenceFilename = "media/";
  364. referenceFilename += driverName;
  365. referenceFilename += fileName;
  366. irr::video::IImage * reference = driver->createImageFromFile(referenceFilename.c_str());
  367. if (!reference)
  368. {
  369. logTestString("\n*** Failed to load reference image '%s'\n*** Creating from screenshot - please check this image.\n\n",
  370. referenceFilename.c_str());
  371. (void)driver->writeImageToFile(screenshot, referenceFilename.c_str());
  372. screenshot->drop();
  373. return false;
  374. }
  375. const float match = fuzzyCompareImages(screenshot, reference);
  376. logTestString("Image match: %f%%\n", match);
  377. if (match < requiredMatch)
  378. {
  379. irr::core::stringc mismatchFilename = "results/";
  380. mismatchFilename += driverName;
  381. mismatchFilename += fileName;
  382. logTestString("Writing mismatched image to '%s'\n", mismatchFilename.c_str());
  383. (void)driver->writeImageToFile(screenshot, mismatchFilename.c_str());
  384. }
  385. screenshot->drop();
  386. reference->drop();
  387. return (match >= requiredMatch);
  388. }
  389. static FILE * logFile = 0;
  390. bool openTestLog(bool startNewLog, const char * filename)
  391. {
  392. closeTestLog();
  393. if (startNewLog)
  394. logFile = fopen(filename, "w");
  395. else
  396. logFile = fopen(filename, "a");
  397. assert(logFile);
  398. if (!logFile)
  399. logTestString("\nWARNING: unable to open the test log file %s\n", filename);
  400. return (logFile != 0);
  401. }
  402. void closeTestLog(void)
  403. {
  404. if (logFile)
  405. {
  406. (void)fclose(logFile);
  407. logFile = 0;
  408. }
  409. }
  410. void logTestString(const char * format, ...)
  411. {
  412. char logString[1024];
  413. va_list arguments;
  414. va_start(arguments, format);
  415. vsprintf(logString, format, arguments);
  416. va_end(arguments);
  417. #if defined(_IRR_WINDOWS_API_)
  418. #if defined (_WIN32_WCE )
  419. core::stringw tmp(logString);
  420. tmp += L"\n";
  421. OutputDebugStringW(tmp.c_str());
  422. #else
  423. OutputDebugStringA(logString);
  424. OutputDebugStringA("\n");
  425. #endif
  426. #endif
  427. (void)printf(logString);
  428. if (logFile)
  429. {
  430. (void)fprintf(logFile, logString);
  431. (void)fflush(logFile);
  432. }
  433. #if defined(TESTING_ON_WINDOWS)
  434. OutputDebugStringA(logString);
  435. #endif // #if defined(TESTING_ON_WINDOWS)
  436. }