utImporter.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "UnitTestPCH.h"
  2. #include "../../include/assimp/postprocess.h"
  3. #include "../../include/assimp/scene.h"
  4. #include <assimp/Importer.hpp>
  5. #include <BaseImporter.h>
  6. using namespace std;
  7. using namespace Assimp;
  8. class ImporterTest : public ::testing::Test
  9. {
  10. public:
  11. virtual void SetUp() { pImp = new Importer(); }
  12. virtual void TearDown() { delete pImp; }
  13. protected:
  14. Importer* pImp;
  15. };
  16. #define InputData_BLOCK_SIZE 1310
  17. // test data for Importer::ReadFileFromMemory() - ./test/3DS/CameraRollAnim.3ds
  18. static unsigned char InputData_abRawBlock[1310] = {
  19. 77,77,30,5,0,0,2,0,10,0,0,0,3,0,0,0,61,61,91,3,0,0,62,61,10,0,0,0,3,0,0,0,
  20. 0,1,10,0,0,0,0,0,128,63,0,64,254,2,0,0,66,111,120,48,49,0,0,65,242,2,0,0,16,65,64,1,
  21. 0,0,26,0,102,74,198,193,102,74,198,193,0,0,0,0,205,121,55,66,102,74,198,193,0,0,0,0,102,74,198,193,
  22. 138,157,184,65,0,0,0,0,205,121,55,66,138,157,184,65,0,0,0,0,102,74,198,193,102,74,198,193,90,252,26,66,
  23. 205,121,55,66,102,74,198,193,90,252,26,66,102,74,198,193,138,157,184,65,90,252,26,66,205,121,55,66,138,157,184,65,
  24. 90,252,26,66,102,74,198,193,102,74,198,193,0,0,0,0,205,121,55,66,102,74,198,193,0,0,0,0,205,121,55,66,
  25. 102,74,198,193,90,252,26,66,205,121,55,66,102,74,198,193,90,252,26,66,102,74,198,193,102,74,198,193,90,252,26,66,
  26. 102,74,198,193,102,74,198,193,0,0,0,0,205,121,55,66,138,157,184,65,0,0,0,0,205,121,55,66,102,74,198,193,
  27. 90,252,26,66,205,121,55,66,138,157,184,65,0,0,0,0,102,74,198,193,138,157,184,65,0,0,0,0,102,74,198,193,
  28. 138,157,184,65,90,252,26,66,102,74,198,193,138,157,184,65,90,252,26,66,205,121,55,66,138,157,184,65,90,252,26,66,
  29. 205,121,55,66,138,157,184,65,0,0,0,0,102,74,198,193,138,157,184,65,0,0,0,0,102,74,198,193,102,74,198,193,
  30. 90,252,26,66,102,74,198,193,102,74,198,193,90,252,26,66,102,74,198,193,138,157,184,65,0,0,0,0,64,65,216,0,
  31. 0,0,26,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,63,0,0,128,63,0,0,0,0,
  32. 0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,128,63,
  33. 0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,128,63,0,0,128,63,0,0,128,63,
  34. 0,0,128,63,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,
  35. 0,0,128,63,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,128,63,0,0,128,63,0,0,128,63,
  36. 0,0,128,63,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,63,
  37. 0,0,128,63,0,0,128,63,0,0,128,63,0,0,0,0,0,0,0,0,96,65,54,0,0,0,0,0,128,63,0,0,
  38. 0,0,0,0,0,0,0,0,0,0,0,0,128,63,0,0,0,0,0,0,0,0,0,0,0,0,0,0,128,63,53,169,
  39. 40,65,176,205,90,191,0,0,0,0,32,65,158,0,0,0,12,0,0,0,2,0,3,0,6,0,3,0,1,0,0,0,
  40. 6,0,4,0,5,0,7,0,6,0,7,0,6,0,4,0,6,0,8,0,9,0,10,0,6,0,11,0,12,0,13,0,
  41. 6,0,1,0,14,0,7,0,6,0,7,0,15,0,1,0,6,0,16,0,17,0,18,0,6,0,19,0,20,0,21,0,
  42. 6,0,22,0,0,0,23,0,6,0,24,0,6,0,25,0,6,0,80,65,54,0,0,0,2,0,0,0,2,0,0,0,
  43. 4,0,0,0,4,0,0,0,8,0,0,0,8,0,0,0,16,0,0,0,16,0,0,0,32,0,0,0,32,0,0,0,
  44. 64,0,0,0,64,0,0,0,0,64,67,0,0,0,67,97,109,101,114,97,48,49,0,0,71,52,0,0,0,189,19,25,
  45. 195,136,104,81,64,147,56,182,65,96,233,20,194,67,196,97,190,147,56,182,65,0,0,0,0,85,85,85,66,32,71,14,
  46. 0,0,0,0,0,0,0,0,0,122,68,0,176,179,1,0,0,10,176,21,0,0,0,5,0,77,65,88,83,67,69,78,
  47. 69,0,44,1,0,0,8,176,14,0,0,0,0,0,0,0,44,1,0,0,9,176,10,0,0,0,128,2,0,0,2,176,
  48. 168,0,0,0,48,176,8,0,0,0,0,0,16,176,18,0,0,0,66,111,120,48,49,0,0,64,0,0,255,255,19,176,
  49. 18,0,0,0,0,0,0,128,0,0,0,128,0,0,0,128,32,176,38,0,0,0,0,0,0,0,0,0,0,0,0,0,
  50. 1,0,0,0,0,0,0,0,0,0,53,169,40,65,176,205,90,191,0,0,0,0,33,176,42,0,0,0,0,0,0,0,
  51. 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  52. 34,176,38,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,128,63,0,0,
  53. 128,63,0,0,128,63,3,176,143,0,0,0,48,176,8,0,0,0,1,0,16,176,21,0,0,0,67,97,109,101,114,97,
  54. 48,49,0,0,64,0,0,255,255,32,176,38,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,
  55. 0,0,0,189,19,25,195,136,104,81,64,147,56,182,65,35,176,30,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
  56. 0,0,0,0,0,0,0,0,0,0,0,52,66,36,176,40,0,0,0,0,0,0,0,0,0,120,0,0,0,2,0,0,
  57. 0,0,0,0,0,0,0,120,13,90,189,120,0,0,0,0,0,99,156,154,194,4,176,73,0,0,0,48,176,8,0,0,
  58. 0,2,0,16,176,21,0,0,0,67,97,109,101,114,97,48,49,0,0,64,0,0,255,255,32,176,38,0,0,0,0,0,
  59. 0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,96,233,20,194,67,196,97,190,147,56,182,65,
  60. };
  61. #define AIUT_DEF_ERROR_TEXT "sorry, this is a test"
  62. static const aiImporterDesc desc = {
  63. "UNIT TEST - IMPORTER",
  64. "",
  65. "",
  66. "",
  67. 0,
  68. 0,
  69. 0,
  70. 0,
  71. 0,
  72. "apple mac linux windows"
  73. };
  74. class TestPlugin : public BaseImporter
  75. {
  76. public:
  77. virtual bool CanRead(
  78. const std::string& pFile, IOSystem* /*pIOHandler*/, bool /*test*/) const
  79. {
  80. std::string::size_type pos = pFile.find_last_of('.');
  81. // no file extension - can't read
  82. if( pos == std::string::npos)
  83. return false;
  84. std::string extension = pFile.substr( pos);
  85. // todo ... make case-insensitive
  86. return (extension == ".apple" || extension == ".mac" ||
  87. extension == ".linux" || extension == ".windows" );
  88. }
  89. virtual const aiImporterDesc* GetInfo () const
  90. {
  91. return & desc;
  92. }
  93. virtual void InternReadFile(
  94. const std::string& /*pFile*/, aiScene* /*pScene*/, IOSystem* /*pIOHandler*/)
  95. {
  96. throw DeadlyImportError(AIUT_DEF_ERROR_TEXT);
  97. }
  98. };
  99. // ------------------------------------------------------------------------------------------------
  100. TEST_F(ImporterTest, testMemoryRead)
  101. {
  102. const aiScene* sc = pImp->ReadFileFromMemory(InputData_abRawBlock,InputData_BLOCK_SIZE,
  103. aiProcessPreset_TargetRealtime_Quality,"3ds");
  104. ASSERT_TRUE(sc != NULL);
  105. EXPECT_EQ(aiString("<3DSRoot>"), sc->mRootNode->mName);
  106. EXPECT_EQ(1U, sc->mNumMeshes);
  107. EXPECT_EQ(24U, sc->mMeshes[0]->mNumVertices);
  108. EXPECT_EQ(12U, sc->mMeshes[0]->mNumFaces);
  109. }
  110. // ------------------------------------------------------------------------------------------------
  111. TEST_F(ImporterTest, testIntProperty)
  112. {
  113. bool b = pImp->SetPropertyInteger("quakquak",1503);
  114. EXPECT_FALSE(b);
  115. EXPECT_EQ(1503, pImp->GetPropertyInteger("quakquak",0));
  116. EXPECT_EQ(314159, pImp->GetPropertyInteger("not_there",314159));
  117. b = pImp->SetPropertyInteger("quakquak",1504);
  118. EXPECT_TRUE(b);
  119. }
  120. // ------------------------------------------------------------------------------------------------
  121. TEST_F(ImporterTest, testFloatProperty)
  122. {
  123. bool b = pImp->SetPropertyFloat("quakquak",1503.f);
  124. EXPECT_TRUE(!b);
  125. EXPECT_EQ(1503.f, pImp->GetPropertyFloat("quakquak",0.f));
  126. EXPECT_EQ(314159.f, pImp->GetPropertyFloat("not_there",314159.f));
  127. }
  128. // ------------------------------------------------------------------------------------------------
  129. TEST_F(ImporterTest, testStringProperty)
  130. {
  131. bool b = pImp->SetPropertyString("quakquak","test");
  132. EXPECT_TRUE(!b);
  133. EXPECT_EQ("test", pImp->GetPropertyString("quakquak","weghwekg"));
  134. EXPECT_EQ("ILoveYou", pImp->GetPropertyString("not_there","ILoveYou"));
  135. }
  136. // ------------------------------------------------------------------------------------------------
  137. TEST_F(ImporterTest, testPluginInterface)
  138. {
  139. pImp->RegisterLoader(new TestPlugin());
  140. EXPECT_TRUE(pImp->IsExtensionSupported(".apple"));
  141. EXPECT_TRUE(pImp->IsExtensionSupported(".mac"));
  142. EXPECT_TRUE(pImp->IsExtensionSupported("*.linux"));
  143. EXPECT_TRUE(pImp->IsExtensionSupported("windows"));
  144. EXPECT_TRUE(pImp->IsExtensionSupported(".x")); /* x and 3ds must be available in this Assimp build, of course! */
  145. EXPECT_TRUE(pImp->IsExtensionSupported(".3ds"));
  146. EXPECT_FALSE(pImp->IsExtensionSupported("."));
  147. TestPlugin* p = (TestPlugin*) pImp->GetImporter(".windows");
  148. ASSERT_TRUE(NULL != p);
  149. try {
  150. p->InternReadFile("",0,NULL);
  151. }
  152. catch ( const DeadlyImportError& dead)
  153. {
  154. EXPECT_TRUE(!strcmp(dead.what(),AIUT_DEF_ERROR_TEXT));
  155. // unregister the plugin and delete it
  156. pImp->UnregisterLoader(p);
  157. delete p;
  158. return;
  159. }
  160. EXPECT_TRUE(false); // control shouldn't reach this point
  161. }
  162. // ------------------------------------------------------------------------------------------------
  163. TEST_F(ImporterTest, testExtensionCheck)
  164. {
  165. std::string s;
  166. pImp->GetExtensionList(s);
  167. // TODO
  168. }
  169. // ------------------------------------------------------------------------------------------------
  170. TEST_F(ImporterTest, testMultipleReads)
  171. {
  172. // see http://sourceforge.net/projects/assimp/forums/forum/817654/topic/3591099
  173. // Check whether reading and post-processing multiple times using
  174. // the same objects is *generally* fine. This test doesn't target
  175. // importers. Testing post-processing stability is the main point.
  176. const unsigned int flags =
  177. aiProcess_Triangulate |
  178. aiProcess_JoinIdenticalVertices |
  179. aiProcess_GenSmoothNormals |
  180. aiProcess_ValidateDataStructure |
  181. aiProcess_RemoveRedundantMaterials |
  182. aiProcess_SortByPType |
  183. aiProcess_FindDegenerates |
  184. aiProcess_FindInvalidData |
  185. aiProcess_GenUVCoords |
  186. aiProcess_OptimizeMeshes |
  187. aiProcess_OptimizeGraph;
  188. EXPECT_TRUE(pImp->ReadFile("../test/models/X/test.x",flags));
  189. //EXPECT_TRUE(pImp->ReadFile("../../test/models/X/dwarf.x",flags)); # is in nonbsd
  190. EXPECT_TRUE(pImp->ReadFile("../test/models/X/Testwuson.X",flags));
  191. EXPECT_TRUE(pImp->ReadFile("../test/models/X/anim_test.x",flags));
  192. //EXPECT_TRUE(pImp->ReadFile("../../test/models/X/dwarf.x",flags)); # is in nonbsd
  193. EXPECT_TRUE(pImp->ReadFile("../test/models/X/anim_test.x",flags));
  194. EXPECT_TRUE(pImp->ReadFile("../test/models/X/BCN_Epileptic.X",flags));
  195. //EXPECT_TRUE(pImp->ReadFile("../../test/models/X/dwarf.x",flags)); # is in nonbsd
  196. }