00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00033 #include <cstdarg>
00034 #include <iostream>
00035 #include <iomanip>
00036 #include <string>
00037 #include <sstream>
00038 #include <memory>
00039 #include <map>
00040 #include <vector>
00041 #include <set>
00042 #include <dae.h>
00043 #include <dom/domConstants.h>
00044 #include <dom/domCOLLADA.h>
00045 #include <dom/domProfile_common.h>
00046 #include <dae/daeSIDResolver.h>
00047 #include <dom/domInstance_controller.h>
00048 #include <dae/domAny.h>
00049 #include <dae/daeErrorHandler.h>
00050 #include <dae/daeUtils.h>
00051 #include <dom/domImage.h>
00052 #include <modules/stdErrPlugin.h>
00053 #include <dom/domEllipsoid.h>
00054 #include <dom/domInput_global.h>
00055 #include <dom/domAsset.h>
00056 #include <dom/domLimits_sub.h>
00057 #include "domTest.h"
00058
00059 #define PFLN {printf("DEBUG PRINT FILE %s LINE %d\n",__FILE__,__LINE__);}
00060 using namespace ColladaDOM150;
00061 using namespace cdom;
00062
00063 void my_addGeometry(daeElement* root);
00064 void my_addVisualScene(daeElement* root);
00065 void my_addEffect(daeElement* root);
00066 void my_addMaterial(daeElement* root);
00067 void my_addImage(daeElement* root);
00068 void my_addSource(daeElement* mesh,
00069 const std::string& srcID,
00070 const std::string& paramNames,
00071 domFloat values[],
00072 int valueCount);
00073
00074 template<typename T>
00075 daeTArray<T> rawArrayToDaeArray(T rawArray[], size_t count) {
00076 daeTArray<T> result;
00077 for (size_t i = 0; i < count; i++)
00078 result.append(rawArray[i]);
00079 return result;
00080 }
00081
00082 namespace fs = boost::filesystem;
00083 using namespace std;
00084 using namespace cdom;
00085
00086 float toFloat(const string& s) {
00087 istringstream stream(s);
00088 float f;
00089 stream >> f;
00090 return f;
00091 }
00092
00093 #define CheckResultWithMsg(val, msg) \
00094 if (!(val)) { \
00095 return testResult(false, __FILE__, __LINE__, msg); \
00096 }
00097
00098 #define CompareDocs(dae, file1, file2) \
00099 { \
00100 domCOLLADA *root1 = (dae).getRoot(file1), \
00101 *root2 = (dae).getRoot(file2); \
00102 daeElement::compareResult result = daeElement::compareWithFullResult(*root1, *root2); \
00103 if (result.compareValue != 0) { \
00104 return testResult(false, __FILE__, __LINE__, result.format()); \
00105 } \
00106 }
00107
00108 map<string, domTest*>& registeredTests() {
00109 static map<string, domTest*> tests;
00110 return tests;
00111 }
00112
00113 fs::path& dataPath() {
00114 static fs::path dataPath_;
00115 return dataPath_;
00116 }
00117
00118 fs::path& tmpPath() {
00119 static fs::path tmpPath_;
00120 return tmpPath_;
00121 }
00122
00123 #define RunTest(testName) \
00124 { \
00125 map<string, domTest*>::iterator iter = registeredTests().find(#testName); \
00126 CheckResult(iter != registeredTests().end()); \
00127 CheckResult(iter->second->run()); \
00128 }
00129
00130
00131 string lookupTestFile(const string& fileName) {
00132 return (dataPath() / fileName).native_file_string();
00133 }
00134
00135 string getTmpFile(const string& fileName) {
00136 return (tmpPath() / fileName).native_file_string();
00137 }
00138
00139
00140 string chopWS(const string& s) {
00141 string ws = " \t\n\r";
00142 size_t beginPos = s.find_first_not_of(ws);
00143 size_t endPos = s.find_last_not_of(ws);
00144 if (beginPos == string::npos)
00145 return "";
00146 return s.substr(beginPos, endPos-beginPos+1);
00147 }
00148
00149 DefineTest(chopWS) {
00150 CheckResult(chopWS("") == "");
00151 CheckResult(chopWS("") == "");
00152 CheckResult(chopWS(" ") == "");
00153 CheckResult(chopWS(" test") == "test");
00154 CheckResult(chopWS("test ") == "test");
00155 CheckResult(chopWS(" test ") == "test");
00156 CheckResult(chopWS(" a ") == "a");
00157 return testResult(true);
00158 }
00159
00160
00161 DefineTest(utils) {
00162 CheckResult(replace("abc", "abc", "def") == "def");
00163 CheckResult(replace("abc", "a", "1") == "1bc");
00164 CheckResult(replace("abc", "c", "1") == "ab1");
00165 CheckResult(replace("abc123", "bc12", "b") == "ab3");
00166 CheckResult(replace("abracadabra", "a", "") == "brcdbr");
00167
00168 CheckResult(tokenize("1|2|3|4", "|") == makeStringList("1", "2", "3", "4", 0));
00169 CheckResult(tokenize("|1|", "|") == makeStringList("1", 0));
00170 CheckResult(tokenize("1|||2||3|", "|") == makeStringList("1", "2", "3", 0));
00171 CheckResult(tokenize("1|||2||3|", "|", true) ==
00172 makeStringList("1", "|", "|", "|", "2", "|", "|", "3", "|", 0));
00173 CheckResult(tokenize("this/is some#text", "/#", true) ==
00174 makeStringList("this", "/", "is some", "#", "text", 0));
00175 CheckResult(tokenize("this/is some#text", "/# ", false) ==
00176 makeStringList("this", "is", "some", "text", 0));
00177
00178 CheckResult(toString(5) == "5");
00179 CheckResult(toFloat(toString(4.0f)) == 4.0f);
00180
00181 return testResult(true);
00182 }
00183
00184
00185 DefineTest(elementAddFunctions) {
00186 DAE dae;
00187 const char* uri = "file.dae";
00188
00189
00190 daeElement* root = dae.add(uri);
00191 CheckResult(root);
00192 daeElement* geomLib = root->add("library_geometries");
00193 CheckResult(geomLib);
00194 daeElement* effectLib = root->add("library_effects", 0);
00195 CheckResult(effectLib && root->getChildren()[0] == effectLib);
00196 root->addBefore(geomLib, effectLib);
00197 CheckResult(root->getChildren()[0] == geomLib);
00198 CheckResult(root->removeChildElement(geomLib));
00199 root->addAfter(geomLib, effectLib);
00200 CheckResult(root->getChildren()[1] == geomLib);
00201 CheckResult(root->removeChildElement(geomLib));
00202 root->add(geomLib);
00203 CheckResult(root->getDescendant("library_geometries"));
00204 daeElement* instanceGeom = root->add("library_nodes node instance_geometry");
00205 CheckResult(instanceGeom && instanceGeom->typeID() == domInstance_geometry::ID());
00206 CheckResult(root->add("library_materials material blah") == NULL);
00207 CheckResult(root->getDescendant("library_materials") == NULL);
00208
00209
00210 dae.close(uri);
00211 root = dae.add(uri);
00212 CheckResult(root);
00213 geomLib = root->createAndPlace("library_geometries");
00214 CheckResult(geomLib);
00215 effectLib = root->createAndPlaceAt(0, "library_effects");
00216 CheckResult(effectLib && root->getChildren()[0] == effectLib);
00217 root->placeElementBefore(effectLib, geomLib);
00218 CheckResult(root->getChildren()[0] == geomLib);
00219 CheckResult(root->removeChildElement(geomLib));
00220 root->placeElementAfter(effectLib, geomLib);
00221 CheckResult(root->getChildren()[1] == geomLib);
00222 CheckResult(root->removeChildElement(geomLib));
00223 root->placeElement(geomLib);
00224 CheckResult(root->getDescendant("library_geometries"));
00225
00226 return testResult(true);
00227 }
00228
00229
00230 DefineTest(loadClipPlane) {
00231 DAE dae;
00232 CheckResult(dae.open(lookupTestFile("clipPlane.dae")));
00233 return testResult(true);
00234 }
00235
00236
00237 DefineTest(renderStates) {
00238 string memoryUri = "renderStates_create.dae";
00239 string file = getTmpFile("renderStates.dae");
00240
00241 DAE dae;
00242 daeElement* root = dae.add(memoryUri);
00243 CheckResult(root);
00244 daeElement* pass = root->add("library_effects effect profile_CG technique pass");
00245 CheckResult(pass);
00246
00247 domCg_pass::domEvaluate* evaluate = daeSafeCast<domCg_pass::domEvaluate>(pass->add("evaluate"));
00248 evaluate->add("color_clear")->setCharData("0 0 0 0");
00249
00250 domCg_pass::domStates* states = daeSafeCast<domCg_pass::domStates>(pass->add("states"));
00251 states->add("depth_mask")->setAttribute("value", "true");
00252 states->add("cull_face_enable")->setAttribute("value", "true");
00253 states->add("blend_enable")->setAttribute("value", "true");
00254 states->add("blend_func_separate")->setAttribute("value", "true");
00255 states->add("cull_face")->setAttribute("value", "FRONT");
00256 states->add("polygon_offset_fill_enable")->setAttribute("value", "true");
00257
00258
00259 CheckResult(dae.writeTo(memoryUri, file));
00260
00261
00262 root = dae.open(file);
00263 CheckResult(root);
00264 CompareDocs(dae, memoryUri, file);
00265
00266
00267 CheckResult(root->getDescendant("depth_mask")->isAttributeSet("value") == false);
00268 CheckResult(root->getDescendant("color_clear")->getCharData() != "");
00269 CheckResult(root->getDescendant("polygon_offset_fill_enable")->isAttributeSet("value"));
00270
00271 return testResult(true);
00272 }
00273
00274
00275 DefineTest(compareElements) {
00276 string memoryUri = "file.dae";
00277
00278 DAE dae;
00279 daeElement* root = dae.add(memoryUri);
00280 CheckResult(root);
00281
00282 daeElement* technique = root->add("extra technique");
00283 CheckResult(technique);
00284
00285
00286 daeElement* elt1 = technique->add("elt");
00287 daeElement* elt2 = technique->add("elt");
00288 CheckResult(elt1 && elt2);
00289
00290 elt1->setAttribute("attr1", "val1");
00291 elt1->setAttribute("attr2", "val2");
00292 elt2->setAttribute("attr2", "val2");
00293 elt2->setAttribute("attr1", "val1");
00294
00295 CheckResult(daeElement::compare(*elt1, *elt2) == 0);
00296
00297
00298 elt2->setAttribute("attr3", "val3");
00299 CheckResult(daeElement::compare(*elt1, *elt2) < 0);
00300
00301 return testResult(true);
00302 }
00303
00304
00305 DefineTest(writeCamera) {
00306 string memoryUri = "camera_create.dae";
00307 string file = getTmpFile("camera.dae");
00308
00309 DAE dae;
00310 daeElement* elt = dae.add(memoryUri);
00311 CheckResult(elt);
00312 elt = elt->add("library_cameras camera optics technique_common perspective xfov");
00313 CheckResult(elt);
00314 elt->setCharData("1.0");
00315
00316 CheckResult(dae.writeTo(memoryUri, file));
00317 domCOLLADA* root = dae.open(file);
00318 CheckResult(root);
00319 CompareDocs(dae, memoryUri, file);
00320 CheckResult(toFloat(root->getDescendant("xfov")->getCharData()) == 1.0f);
00321
00322 return testResult(true);
00323 }
00324
00325
00326 string getRoundTripFile(const string& name) {
00327 return getTmpFile(fs::basename(fs::path(name)) + "_roundTrip.dae");
00328 }
00329
00330 bool roundTrip(const string& file) {
00331 DAE dae;
00332 if (!dae.open(file))
00333 return false;
00334 return dae.writeTo(file, getRoundTripFile(file));
00335 }
00336
00337 DefineTest(roundTripSeymour) {
00338 string file1 = lookupTestFile("Seymour.dae"),
00339 file2 = getRoundTripFile(file1);
00340 DAE dae;
00341 CheckResult(dae.open(file1));
00342 CheckResult(dae.writeTo(file1, file2));
00343 CheckResult(dae.open(file2));
00344 CompareDocs(dae, file1, file2);
00345 return testResult(true);
00346 }
00347
00348
00349 DefineTest(rawSupport) {
00350 string seymourOrig = lookupTestFile("Seymour.dae"),
00351 seymourRaw = getTmpFile("Seymour_raw.dae");
00352 DAE dae;
00353
00354 CheckResult(dae.open(seymourOrig));
00355 dae.getIOPlugin()->setOption("saveRawBinary", "true");
00356 CheckResult(dae.writeTo(seymourOrig, seymourRaw));
00357 dae.clear();
00358
00359
00360 CheckResult(fs::exists(fs::path(seymourRaw + ".raw")));
00361
00362 daeElement* seymourRawRoot = dae.open(seymourRaw);
00363 CheckResult(seymourRawRoot);
00364 CheckResult(dae.getDatabase()->idLookup("l_hip_rotateY_l_hip_rotateY_ANGLE-input",
00365 seymourRawRoot->getDocument()));
00366 domAccessor* accessor = dae.getDatabase()->typeLookup<domAccessor>().at(0);
00367 daeURI& uri = accessor->getSource();
00368 CheckResult(uri.pathExt().find(".raw") != string::npos);
00369 CheckResult(uri.getElement());
00370
00371 return testResult(true);
00372 }
00373
00374 DefineTest(extraTypeTest) {
00375 DAE dae;
00376 string file = lookupTestFile("extraTest.dae");
00377 daeElement* root = dae.open(file);
00378 CheckResult(root);
00379
00380 daeElement *technique = root->getDescendant("technique"),
00381 *expectedTypesElt = root->getDescendant("expected_types");
00382 CheckResult(technique && expectedTypesElt);
00383
00384
00385 istringstream expectedTypesStream(expectedTypesElt->getCharData());
00386 vector<string> expectedTypes;
00387 string tmp;
00388 while (expectedTypesStream >> tmp)
00389 expectedTypes.push_back(tmp);
00390
00391 daeElementRefArray elements = technique->getChildren();
00392
00393
00394 CheckResult(expectedTypes.size() == elements.getCount()-1);
00395 for (size_t i = 0; i < elements.getCount()-1; i++) {
00396 ostringstream msg;
00397 msg << "Actual type - " << elements[i]->getTypeName() << ", Expected type - " << expectedTypes[i];
00398 CheckResultWithMsg(expectedTypes[i] == elements[i]->getTypeName(), msg.str());
00399 }
00400
00401 return testResult(true);
00402 }
00403
00404 #if defined(TINYXML)
00405 #include <dae/daeTinyXMLPlugin.h>
00406 DefineTest(tinyXmlLoad) {
00407 string seymourOrig = lookupTestFile("Seymour.dae"),
00408 seymourTinyXml = getTmpFile("Seymour_tinyXml.dae");
00409
00410
00411
00412 DAE dae;
00413 CheckResult(dae.open(seymourOrig));
00414 auto_ptr<daeTinyXMLPlugin> tinyXmlPlugin(new daeTinyXMLPlugin);
00415 dae.setIOPlugin(tinyXmlPlugin.get());
00416 CheckResult(dae.writeTo(seymourOrig, seymourTinyXml));
00417 CheckResult(dae.open(seymourTinyXml));
00418 CompareDocs(dae, seymourOrig, seymourTinyXml);
00419
00420 return testResult(true);
00421 }
00422 #endif
00423
00424
00425 string resolveResultToString(const string& sidRef, daeElement* refElt) {
00426 daeSidRef::resolveData rd = daeSidRef(sidRef, refElt).resolve();
00427 if (rd.scalar) return "scalar";
00428 else if (rd.array) return "array";
00429 else if (rd.elt) return "element";
00430 else return "failed";
00431 }
00432
00433 DefineTest(sidResolve) {
00434 DAE dae;
00435 daeElement* root = dae.open(lookupTestFile("sidResolveTest.dae"));
00436 CheckResult(root);
00437 daeDatabase& database = *dae.getDatabase();
00438 daeDocument* doc = root->getDocument();
00439
00440 daeElement *effect = database.idLookup("myEffect", doc),
00441 *effectExtra = database.idLookup("effectExtra", doc);
00442 CheckResult(effect && effectExtra);
00443
00444 istringstream stream(effectExtra->getCharData());
00445 string sidRef, expectedResult;
00446 while (stream >> sidRef >> expectedResult) {
00447 string result = resolveResultToString(sidRef, effect);
00448 CheckResultWithMsg(result == expectedResult,
00449 string("sid ref=") + sidRef + ", expectedResult=" + expectedResult + ", actualResult=" + result);
00450 }
00451
00452 daeElement* nodeSidRefExtra = database.idLookup("nodeSidRefExtra", doc);
00453 CheckResult(nodeSidRefExtra);
00454
00455 stream.clear();
00456 stream.str(nodeSidRefExtra->getCharData());
00457 while (stream >> sidRef >> expectedResult) {
00458 string result = resolveResultToString(sidRef, root);
00459 CheckResultWithMsg(result == expectedResult,
00460 string("sid ref=") + sidRef + ", expectedResult=" + expectedResult + ", actualResult=" + result);
00461 }
00462
00463 nodeSidRefExtra = database.idLookup("nodeSidRefExtra2", doc);
00464 CheckResult(nodeSidRefExtra);
00465
00466 stream.clear();
00467 stream.str(nodeSidRefExtra->getCharData());
00468 while (stream >> sidRef >> expectedResult) {
00469 daeElement* elt = daeSidRef(sidRef, root).resolve().elt;
00470 string result = elt ? elt->getAttribute("id") : "failed";
00471 CheckResultWithMsg(result == expectedResult,
00472 string("sid ref=") + sidRef + ", expectedResult=" + expectedResult + ", actualResult=" + result);
00473 }
00474
00475 nodeSidRefExtra = database.idLookup("nodeSidRefExtra3", doc);
00476 CheckResult(nodeSidRefExtra);
00477
00478 stream.clear();
00479 stream.str(nodeSidRefExtra->getCharData());
00480 string profile;
00481 while (stream >> sidRef >> profile >> expectedResult) {
00482 daeElement* elt = daeSidRef(sidRef, root, profile).resolve().elt;
00483 string result = elt ? elt->getAttribute("id") : "failed";
00484 CheckResultWithMsg(result == expectedResult,
00485 string("sid ref=") + sidRef + ", profile=" + profile +
00486 ", expectedResult=" + expectedResult + ", actualResult=" + result);
00487 }
00488
00489
00490 return testResult(true);
00491 }
00492
00493 daeElement* findChildByName(daeElement* el, daeString name) {
00494 if (!el)
00495 return 0;
00496
00497 daeElementRefArray children = el->getChildren();
00498 for (size_t i = 0; i < children.getCount(); i++)
00499 if (strcmp(children[i]->getElementName(), name) == 0)
00500 return children[i];
00501
00502 return 0;
00503 }
00504
00505 daeElement* findAncestorByType(daeElement* el, daeString type) {
00506 if (el == 0 || strcmp(el->getTypeName(), type) == 0)
00507 return el;
00508 return findAncestorByType(el->getParentElement(), type);
00509 }
00510
00511 daeElement* resolveID(daeString id, daeDocument& document) {
00512 return document.getDatabase()->idLookup(id, &document);
00513 }
00514
00515 daeElement* resolveSid(const string& sid, daeElement& refElt) {
00516 return daeSidRef(sid, &refElt).resolve().elt;
00517 }
00518
00519 string getCharData(daeElement* el) {
00520 return el ? el->getCharData() : "";
00521 }
00522
00523 daeURI* getTextureUri(const string& samplerSid, daeElement& effect) {
00524 daeElement* sampler = findChildByName(resolveSid(samplerSid, effect), "sampler2D");
00525 daeElement* instanceImage = findChildByName(sampler, "instance_image");
00526 xsAnyURI imageUrl = daeSafeCast<domInstance_image>(instanceImage)->getUrl();
00527 domImage* image = daeSafeCast<domImage>(imageUrl.getElement());
00528 if (image && image->getInit_from() && image->getInit_from()->getRef())
00529 return &image->getInit_from()->getRef()->getValue();
00530 return 0;
00531 }
00532
00533 DefineTest(getTexture) {
00534 DAE dae;
00535 CheckResult(dae.open(lookupTestFile("Seymour.dae")));
00536
00537 daeElement* effect = dae.getDatabase()->idLookup("face-fx").at(0);
00538 daeElement* texture = effect->getDescendant("texture");
00539 CheckResult(texture);
00540
00541 daeURI* uri = getTextureUri(texture->getAttribute("texture"), *effect);
00542 CheckResult(uri);
00543 CheckResult(uri->pathFile() == "boy_10.tga");
00544
00545 return testResult(true);
00546 }
00547
00548
00549 DefineTest(removeElement) {
00550 DAE dae;
00551 daeElement* collada = dae.open(lookupTestFile("Seymour.dae"));
00552 CheckResult(collada);
00553
00554 daeElement *animLib = dae.getDatabase()->typeLookup(domLibrary_animations::ID()).at(0),
00555 *asset = dae.getDatabase()->typeLookup(domAsset::ID()).at(0);
00556
00557 collada->removeChildElement(asset);
00558 daeElement::removeFromParent(animLib);
00559
00560 CheckResult(collada->getDescendant("asset") == NULL);
00561 CheckResult(collada->getDescendant("library_animations") == NULL);
00562
00563 CheckResult(dae.writeTo(lookupTestFile("Seymour.dae"),
00564 getTmpFile("Seymour_removeElements.dae")));
00565 return testResult(true);
00566 }
00567
00568 void nameArraySet(domList_of_names& names, size_t index, const char* name) {
00569 *(daeStringRef*)&names[index] = name;
00570 }
00571
00572 void nameArrayAppend(domList_of_names& names, const char* name) {
00573 names.append(NULL);
00574 nameArraySet(names, names.getCount()-1, name);
00575 }
00576
00577 DefineTest(nameArray) {
00578 domList_of_names names;
00579 for (int i = 0; i < 10; i++)
00580 nameArrayAppend(names, (string("name") + toString(i)).c_str());
00581 for (int i = 0; i < 10; i++) {
00582 CheckResult(string("name") + toString(i) == string(names[i]));
00583 }
00584
00585 return testResult(true);
00586 }
00587
00588 daeTArray<int> makeIntArray(int i, ...) {
00589 va_list args;
00590 va_start(args, i);
00591 daeTArray<int> result;
00592 while (i != INT_MAX) {
00593 result.append(i);
00594 i = va_arg(args, int);
00595 }
00596 va_end(args);
00597 return result;
00598 }
00599
00600 DefineTest(arrayOps) {
00601 daeTArray<int> zeroToFour = makeIntArray(0, 1, 2, 3, 4, INT_MAX);
00602
00603
00604 daeTArray<int> array = zeroToFour;
00605 array.removeIndex(2);
00606 CheckResult(array == makeIntArray(0, 1, 3, 4, INT_MAX));
00607
00608
00609 array = zeroToFour;
00610 array.insert(3, 5, 9);
00611 CheckResult(array == makeIntArray(0, 1, 2, 9, 9, 9, 9, 9, 3, 4, INT_MAX));
00612
00613
00614 array = zeroToFour;
00615 array.insert(7, 2, 5);
00616 CheckResult(array == makeIntArray(0, 1, 2, 3, 4, 5, 5, 5, 5, INT_MAX));
00617
00618 return testResult(true);
00619 }
00620
00621
00622 void printMemoryToStringResult(daeAtomicType& type, daeMemoryRef value) {
00623 ostringstream buffer;
00624 type.memoryToString(value, buffer);
00625 cout << buffer.str() << endl;
00626 }
00627
00628 string toString(daeAtomicType& type, daeMemoryRef value) {
00629 ostringstream buffer;
00630 type.memoryToString(value, buffer);
00631 return buffer.str();
00632 }
00633
00634 DefineTest(atomicTypeOps) {
00635 DAE dae;
00636 daeUIntType UIntType(dae);
00637 daeIntType IntType(dae);
00638 daeLongType LongType(dae);
00639 daeShortType ShortType(dae);
00640 daeULongType ULongType(dae);
00641 daeFloatType FloatType(dae);
00642 daeDoubleType DoubleType(dae);
00643 daeStringRefType StringRefType(dae);
00644 daeElementRefType ElementRefType(dae);
00645 daeEnumType EnumType(dae);
00646 daeResolverType ResolverType(dae);
00647 daeIDResolverType IDResolverType(dae);
00648 daeBoolType BoolType(dae);
00649 daeTokenType TokenType(dae);
00650
00651 EnumType._values = new daeEnumArray;
00652 EnumType._strings = new daeStringRefArray;
00653 EnumType._values->append(0);
00654 EnumType._strings->append("myEnumValue");
00655
00656 daeUInt UInt(1);
00657 daeInt Int(2);
00658 daeLong Long(3);
00659 daeShort Short(4);
00660 daeULong ULong(5);
00661 daeFloat Float(6.123f);
00662 daeDouble Double(7.456);
00663 daeStringRef StringRef("StringRef");
00664
00665 daeEnum Enum(0);
00666 daeURI uri(dae, "http://www.example.com/#fragment");
00667 daeIDRef IDRef("sampleID");
00668 daeBool Bool(false);
00669 daeStringRef Token("token");
00670
00671
00672 CheckResult(toString(UIntType, (daeMemoryRef)&UInt) == "1");
00673 CheckResult(toString(IntType, (daeMemoryRef)&Int) == "2");
00674 CheckResult(toString(LongType, (daeMemoryRef)&Long) == "3");
00675 CheckResult(toString(ShortType, (daeMemoryRef)&Short) == "4");
00676 CheckResult(toString(ULongType, (daeMemoryRef)&ULong) == "5");
00677 CheckResult(toString(FloatType, (daeMemoryRef)&Float) == "6.123");
00678 CheckResult(toString(DoubleType, (daeMemoryRef)&Double) == "7.456");
00679 CheckResult(toString(StringRefType, (daeMemoryRef)&StringRef) == "StringRef");
00680
00681 CheckResult(toString(EnumType, (daeMemoryRef)&Enum) == "myEnumValue");
00682 CheckResult(toString(ResolverType, (daeMemoryRef)&uri) == "http://www.example.com/#fragment");
00683 CheckResult(toString(IDResolverType, (daeMemoryRef)&IDRef) == "sampleID");
00684 CheckResult(toString(BoolType, (daeMemoryRef)&Bool) == "false");
00685 CheckResult(toString(TokenType, (daeMemoryRef)&Token) == "token");
00686
00687 return testResult(true);
00688 }
00689
00690
00691 DefineTest(clone) {
00692 DAE dae;
00693 CheckResult(dae.open(lookupTestFile("Seymour.dae")));
00694
00695 daeElement* el = dae.getDatabase()->idLookup("l_ulna").at(0);
00696 daeElementRef clone = el->clone("-foo", "-bar");
00697 el->getParentElement()->placeElement(clone);
00698
00699 CheckResult(dae.writeTo(lookupTestFile("Seymour.dae"), getTmpFile("cloneTest.dae")));
00700
00701 return testResult(true);
00702 }
00703
00704
00705 DefineTest(genericOps) {
00706 string file = lookupTestFile("cube.dae");
00707 DAE dae;
00708 CheckResult(dae.open(file));
00709 daeDatabase& database = *dae.getDatabase();
00710
00711
00712 daeElement* el = database.idLookup("box-lib-positions-array").at(0);
00713
00714 CheckResult(el->hasAttribute("digits"));
00715 CheckResult(el->getAttribute("count") == "24");
00716 CheckResult(el->setAttribute("blah", "hey") == false);
00717 CheckResult(el->setAttribute("magnitude", "30"));
00718
00719 el = database.idLookup("Blue-fx").at(0);
00720 CheckResult(el->hasAttribute("name"));
00721 CheckResult(el->isAttributeSet("name") == false);
00722 CheckResult(el->isAttributeSet("hello") == false);
00723
00724
00725 el = database.typeLookup(domAsset::domUp_axis::ID()).at(0);
00726
00727 CheckResult(el->getCharData() == "Y_UP");
00728 el->setCharData("X_UP");
00729
00730 el = database.idLookup("PerspCamera").at(0);
00731 CheckResult(!el->hasCharData());
00732
00733
00734 el = database.idLookup("my_test_element").at(0);
00735 daeElementRef clone = el->clone("-clone", "-clone");
00736
00737 CheckResult(el->getAttribute("attr1") == "value1" &&
00738 el->getAttribute("attr2") == "value2");
00739 CheckResult(el->setAttribute("attr1", "value_1"));
00740 CheckResult(el->setAttribute("attr3", "value3"));
00741
00742 CheckResult(chopWS(el->getCharData()) == "this is some text");
00743 el->setCharData("reset text");
00744
00745
00746 el->getParentElement()->placeElementAfter(el, clone);
00747 domAny* any = (domAny*)clone.cast();
00748 CheckResult(any);
00749 CheckResult(any->getAttributeCount() == 3);
00750 CheckResult(string(any->getAttributeName(0)) == "id");
00751 CheckResult(string(any->getAttributeValue(1)) == "value1");
00752 CheckResult(chopWS(any->getValue()) == "this is some text");
00753 any->setValue("reset text 2");
00754
00755
00756 for (size_t i = 0; i < 50; i++) {
00757 ostringstream name, value;
00758 name << "attr" << static_cast<unsigned int>(i);
00759 value << "value" << static_cast<unsigned int>(i);
00760 any->setAttribute(name.str().c_str(), value.str().c_str());
00761 }
00762
00763 CheckResult(dae.writeTo(file, getTmpFile(fs::basename(fs::path(file)) + "_genericOps.dae")));
00764
00765 return testResult(true);
00766 }
00767
00768
00769 daeArray* getSkewArray(daeElement* node, const string& sid) {
00770 if (!node)
00771 return NULL;
00772
00773 daeElement* skew = resolveSid(sid, *node);
00774 if (!skew || skew->getElementType() != COLLADA_TYPE::SKEW)
00775 return NULL;
00776
00777 return (daeArray*)skew->getCharDataObject()->get(skew);
00778 }
00779
00780 DefineTest(badSkew) {
00781 DAE dae;
00782 CheckResult(dae.open(lookupTestFile("badSkew.dae")));
00783
00784 daeElement* node = dae.getDatabase()->idLookup("my-node").at(0);
00785
00786 daeArray* array1 = getSkewArray(node, "tooFew");
00787 daeArray* array2 = getSkewArray(node, "justRight");
00788 daeArray* array3 = getSkewArray(node, "tooMany");
00789 CheckResult(array1 && array2 && array3);
00790
00791 CheckResult(array1->getCount() == 4);
00792 CheckResult(array2->getCount() == 7);
00793 CheckResult(array3->getCount() == 11);
00794
00795 return testResult(true);
00796 }
00797
00798
00799 DefineTest(stringTable) {
00800 daeStringTable stringTable;
00801 stringTable.allocString("hello");
00802
00803 stringTable.clear();
00804 stringTable.allocString("goodbye");
00805 return testResult(true);
00806 }
00807
00808
00809
00810 #if 0
00811 DefineTest(sidResolveSpeed) {
00812 DAE dae;
00813 string file = lookupTestFile("crankarm.dae");
00814 domCOLLADA* root = dae.open(file);
00815 CheckResult(root);
00816
00817 vector<domSIDREF_array*> sidRefArrays = dae.getDatabase()->typeLookup<domSIDREF_array>();
00818 for (size_t i = 0; i < sidRefArrays.size(); i++) {
00819 domListOfNames& sidRefs = sidRefArrays[i]->getValue();
00820 for (size_t j = 0; j < sidRefs.getCount(); j++) {
00821 CheckResult(resolveSid(sidRefs[i], root));
00822 }
00823 }
00824
00825 return testResult(true);
00826 }
00827 #endif
00828
00829
00830 DefineTest(seymourSidResolve) {
00831 DAE dae;
00832 string file = lookupTestFile("Seymour.dae");
00833 CheckResult(dae.open(file));
00834
00835 vector<daeElement*> nodes = dae.getDatabase()->typeLookup(domNode::ID());
00836 for (size_t i = 0; i < nodes.size(); i++) {
00837 daeElementRefArray children = nodes[i]->getChildren();
00838 for (size_t j = 0; j < children.getCount(); j++) {
00839 string sid = children[j]->getAttribute("sid");
00840 if (!sid.empty()) {
00841 CheckResult(daeSidRef(sid, nodes[i]).resolve().elt);
00842 }
00843 }
00844 }
00845
00846 return testResult(true);
00847 }
00848
00849
00850 vector<string> getChildNames(daeElement* elt) {
00851 vector<string> result;
00852 if (!elt)
00853 return result;
00854
00855 daeElementRefArray children = elt->getChildren();
00856 for (size_t i = 0; i < children.getCount(); i++)
00857 result.push_back(children[i]->getElementName());
00858
00859 return result;
00860 }
00861
00862 DefineTest(placeElement) {
00863 DAE dae;
00864 CheckResult(dae.open(lookupTestFile("cube.dae")));
00865
00866 daeElement* node = dae.getDatabase()->idLookup("Box").at(0);
00867
00868 CheckResult(getChildNames(node) == makeStringArray(
00869 "rotate", "rotate", "rotate", "instance_geometry", 0));
00870
00871
00872
00873 node->placeElementAfter(node->getChildren()[0], node->createElement("translate"));
00874 CheckResult(getChildNames(node) == makeStringArray(
00875 "rotate", "translate", "rotate", "rotate", "instance_geometry", 0));
00876
00877 node->placeElementBefore(node->getChildren()[0], node->createElement("scale"));
00878 CheckResult(getChildNames(node) == makeStringArray(
00879 "scale", "rotate", "translate", "rotate", "rotate", "instance_geometry", 0));
00880
00881 return testResult(true);
00882 };
00883
00884
00885 DefineTest(nativePathConversion) {
00886
00887 CheckResult(cdom::nativePathToUri("C:\\myFolder\\myFile.dae", cdom::Windows) == "/C:/myFolder/myFile.dae");
00888 CheckResult(cdom::nativePathToUri("\\myFolder\\myFile.dae", cdom::Windows) == "/myFolder/myFile.dae");
00889 CheckResult(cdom::nativePathToUri("..\\myFolder\\myFile.dae", cdom::Windows) == "../myFolder/myFile.dae");
00890 CheckResult(cdom::nativePathToUri("\\\\otherComputer\\myFile.dae", cdom::Windows) == "//otherComputer/myFile.dae");
00891
00892
00893 CheckResult(cdom::nativePathToUri("/myFolder/myFile.dae", cdom::Posix) == "/myFolder/myFile.dae");
00894 CheckResult(cdom::nativePathToUri("../myFolder/myFile.dae", cdom::Posix) == "../myFolder/myFile.dae");
00895 CheckResult(cdom::nativePathToUri("/my folder/my file.dae", cdom::Posix) == "/my%20folder/my%20file.dae");
00896
00897
00898 CheckResult(cdom::uriToNativePath("../folder/file.dae", cdom::Windows) == "..\\folder\\file.dae");
00899 CheckResult(cdom::uriToNativePath("/C:/folder/file.dae", cdom::Windows) == "C:\\folder\\file.dae");
00900 CheckResult(cdom::uriToNativePath("file:///C:/folder/file.dae", cdom::Windows) == "C:\\folder\\file.dae");
00901 CheckResult(cdom::uriToNativePath("//otherComputer/file.dae", cdom::Windows) == "\\\\otherComputer\\file.dae");
00902 CheckResult(cdom::uriToNativePath("file://///otherComputer/file.dae", cdom::Windows) == "\\\\otherComputer\\file.dae");
00903 CheckResult(cdom::uriToNativePath("http://www.slashdot.org", cdom::Windows) == "");
00904
00905
00906 CheckResult(cdom::uriToNativePath("../folder/file.dae", cdom::Posix) == "../folder/file.dae");
00907 CheckResult(cdom::uriToNativePath("/folder/file.dae", cdom::Posix) == "/folder/file.dae");
00908 CheckResult(cdom::uriToNativePath("file:///folder/file.dae", cdom::Posix) == "/folder/file.dae");
00909 CheckResult(cdom::uriToNativePath("http://www.slashdot.org", cdom::Posix) == "");
00910
00911 return testResult(true);
00912 }
00913
00914
00915 DefineTest(libxmlUriBugWorkaround) {
00916 if (cdom::getSystemType() == cdom::Posix) {
00917
00918 CheckResult(cdom::fixUriForLibxml("file:/folder/file.dae") == "file:///folder/file.dae");
00919 }
00920 else if (cdom::getSystemType() == cdom::Windows) {
00921
00922 CheckResult(cdom::fixUriForLibxml("file:/c:/folder/file.dae") == "file:///c:/folder/file.dae");
00923
00924 CheckResult(cdom::fixUriForLibxml("file://otherComputer/file.dae") == "file://///otherComputer/file.dae");
00925
00926
00927 CheckResult(cdom::fixUriForLibxml("file:/folder/file.dae") == "file:////folder/file.dae");
00928 }
00929
00930 return testResult(true);
00931 }
00932
00933
00934
00935
00936
00937
00938 #if 0
00939 DefineTest(uriOpen) {
00940 DAE dae;
00941 CheckResult(dae.open("file:/c:/models/cube.dae"));
00942 CheckResult(dae.open("/c:/models/cube.dae"));
00943 CheckResult(dae.open("/models/cube.dae"));
00944 CheckResult(dae.open("file:////models/cube.dae"));
00945 CheckResult(dae.open("file://isis/sceard/COLLADA/forsteve/cube.dae"));
00946 CheckResult(dae.open("file://///isis/sceard/COLLADA/forsteve/cube.dae"));
00947 return testResult(true);
00948 }
00949 #endif
00950
00951
00952 DefineTest(uriOps) {
00953 DAE dae;
00954
00955
00956 CheckResult(daeURI(dae, "file:///home/sthomas/file.txt").str() == "file:/home/sthomas/file.txt");
00957 CheckResult(daeURI(dae, "http://www.example.com/path").str() == "http://www.example.com/path");
00958 CheckResult(daeURI(dae, "file:home/sthomas/file.txt").str() == "file:home/sthomas/file.txt");
00959 CheckResult(daeURI(dae, "file:file.txt#fragment", true).str() == "file:file.txt");
00960
00961
00962 {
00963 daeURI base(dae, "file:/home/sthomas/file.txt?baseQuery#baseFragment");
00964 CheckResult(base.str() == "file:/home/sthomas/file.txt?baseQuery#baseFragment");
00965 CheckResult(daeURI(base, "file:/home/sthomas").str() == "file:/home/sthomas");
00966 CheckResult(daeURI(base, "//authority").str() == "file://authority");
00967 CheckResult(daeURI(base, "//authority/path").str() == "file://authority/path");
00968 CheckResult(daeURI(base, "/home/johnny").str() == "file:/home/johnny");
00969 CheckResult(daeURI(base, "myFile.txt").str() == "file:/home/sthomas/myFile.txt");
00970 CheckResult(daeURI(base, "?query#fragment").str() == "file:/home/sthomas/file.txt?query#fragment");
00971 CheckResult(daeURI(base, "?query").str() == "file:/home/sthomas/file.txt?query");
00972 CheckResult(daeURI(base, "").str() == "file:/home/sthomas/file.txt?baseQuery");
00973 CheckResult(daeURI(daeURI(dae, "http://www.example.com/path"), "myFolder/file.txt").str() == "http://www.example.com/myFolder/file.txt");
00974 CheckResult(daeURI(daeURI(dae, "http://www.example.com/path/"), "myFolder/file.txt").str() == "http://www.example.com/path/myFolder/file.txt");
00975 CheckResult(daeURI(daeURI(dae, "http://www.example.com"), "myFolder/file.txt").str() == "http://www.example.com/myFolder/file.txt");
00976 }
00977
00978
00979 {
00980 daeURI base(dae, "http://a/b/c/d;p?q");
00981
00982 CheckResult(daeURI(base, "g:h").str() == "g:h");
00983 CheckResult(daeURI(base, "g").str() == "http://a/b/c/g");
00984 CheckResult(daeURI(base, "./g").str() == "http://a/b/c/g");
00985 CheckResult(daeURI(base, "g/").str() == "http://a/b/c/g/");
00986 CheckResult(daeURI(base, "/g").str() == "http://a/g");
00987 CheckResult(daeURI(base, "//g").str() == "http://g");
00988 CheckResult(daeURI(base, "?y").str() == "http://a/b/c/d;p?y");
00989 CheckResult(daeURI(base, "g?y").str() == "http://a/b/c/g?y");
00990 CheckResult(daeURI(base, "#s").str() == "http://a/b/c/d;p?q#s");
00991 CheckResult(daeURI(base, "g#s").str() == "http://a/b/c/g#s");
00992 CheckResult(daeURI(base, "g?y#s").str() == "http://a/b/c/g?y#s");
00993 CheckResult(daeURI(base, ";x").str() == "http://a/b/c/;x");
00994 CheckResult(daeURI(base, "g;x").str() == "http://a/b/c/g;x");
00995 CheckResult(daeURI(base, "g;x?y#s").str() == "http://a/b/c/g;x?y#s");
00996 CheckResult(daeURI(base, "").str() == "http://a/b/c/d;p?q");
00997 CheckResult(daeURI(base, ".").str() == "http://a/b/c/");
00998 CheckResult(daeURI(base, "./").str() == "http://a/b/c/");
00999 CheckResult(daeURI(base, "..").str() == "http://a/b/");
01000 CheckResult(daeURI(base, "../").str() == "http://a/b/");
01001 CheckResult(daeURI(base, "../g").str() == "http://a/b/g");
01002 CheckResult(daeURI(base, "../..").str() == "http://a/");
01003 CheckResult(daeURI(base, "../../").str() == "http://a/");
01004 CheckResult(daeURI(base, "../../g").str() == "http://a/g");
01005
01006 CheckResult(daeURI(base, "../../../g").str() == "http://a/g");
01007 CheckResult(daeURI(base, "../../../../g").str() == "http://a/g");
01008 CheckResult(daeURI(base, "/./g").str() == "http://a/g");
01009 CheckResult(daeURI(base, "/../g").str() == "http://a/g");
01010 CheckResult(daeURI(base, "g.").str() == "http://a/b/c/g.");
01011 CheckResult(daeURI(base, ".g").str() == "http://a/b/c/.g");
01012 CheckResult(daeURI(base, "g..").str() == "http://a/b/c/g..");
01013 CheckResult(daeURI(base, "..g").str() == "http://a/b/c/..g");
01014
01015 CheckResult(daeURI(base, "./../g").str() == "http://a/b/g");
01016 CheckResult(daeURI(base, "./g/.").str() == "http://a/b/c/g/");
01017 CheckResult(daeURI(base, "g/./h").str() == "http://a/b/c/g/h");
01018 CheckResult(daeURI(base, "g/../h").str() == "http://a/b/c/h");
01019 CheckResult(daeURI(base, "g;x=1/./y").str() == "http://a/b/c/g;x=1/y");
01020 CheckResult(daeURI(base, "g;x=1/../y").str() == "http://a/b/c/y");
01021
01022
01023 CheckResult(daeURI(base, "g?y/./x").str() == "http://a/b/c/g?y/./x");
01024 CheckResult(daeURI(base, "g?y/../x").str() == "http://a/b/c/g?y/../x");
01025 CheckResult(daeURI(base, "g#s/./x").str() == "http://a/b/c/g#s/./x");
01026 CheckResult(daeURI(base, "g#s/../x").str() == "http://a/b/c/g#s/../x");
01027
01028 CheckResult(daeURI(base, "http:g").str() == "http:g");
01029 }
01030
01031
01032 CheckResult(daeURI(dae, "relPath/file.txt").originalStr() == "relPath/file.txt");
01033
01034
01035 {
01036 daeURI uri(dae);
01037 uri.set("file:/path/file.txt");
01038 CheckResult(uri.str() == "file:/path/file.txt");
01039 uri.set("http", "www.example.com", "/path", "q", "f");
01040 CheckResult(uri.str() == "http://www.example.com/path?q#f");
01041 }
01042
01043
01044 CheckResult(daeURI(dae, "file:/home/sthomas/file.txt").scheme() == "file");
01045 CheckResult(daeURI(dae, "http://www.example.com").authority() == "www.example.com");
01046 CheckResult(daeURI(dae, "file:/home/sthomas/file.txt").path() == "/home/sthomas/file.txt");
01047 CheckResult(daeURI(dae, "file:/home/sthomas/file.txt?query").query() == "query");
01048 CheckResult(daeURI(dae, "file:/home/sthomas/file.txt?query#fragment").fragment() == "fragment");
01049 CheckResult(daeURI(dae, "file:/home/sthomas/file.txt?query#fragment").id() == "fragment");
01050
01051
01052 {
01053 daeURI uri(dae);
01054 uri.scheme("file");
01055 uri.authority("myAuth");
01056 uri.path("/home/sthomas/file.txt");
01057 uri.query("q");
01058 uri.fragment("f");
01059 CheckResult(uri.str() == "file://myAuth/home/sthomas/file.txt?q#f");
01060 uri.id("id");
01061 CheckResult(uri.str() == "file://myAuth/home/sthomas/file.txt?q#id");
01062 }
01063
01064
01065 {
01066 daeURI uri(dae, "file:/home/sthomas/file.txt");
01067 CheckResult(uri.str() == "file:/home/sthomas/file.txt");
01068 string dir, base, ext;
01069 uri.pathComponents(dir, base, ext);
01070 CheckResult(dir == "/home/sthomas/");
01071 CheckResult(base == "file");
01072 CheckResult(ext == ".txt");
01073 CheckResult(uri.pathDir() == "/home/sthomas/");
01074 CheckResult(uri.pathFileBase() == "file");
01075 CheckResult(uri.pathExt() == ".txt");
01076 CheckResult(uri.pathFile() == "file.txt");
01077 }
01078
01079
01080 {
01081 daeURI uri(dae, "file:");
01082 CheckResult(uri.str() == "file:");
01083 uri.path("/home/sthomas/", "file", ".txt");
01084 CheckResult(uri.str() == "file:/home/sthomas/file.txt");
01085 uri.pathDir("/home/johnny");
01086 uri.pathFileBase("otherFile");
01087 uri.pathExt(".dae");
01088 CheckResult(uri.str() == "file:/home/johnny/otherFile.dae");
01089 uri.pathFile("file.txt");
01090 CheckResult(uri.str() == "file:/home/johnny/file.txt");
01091 }
01092
01093
01094 CheckResult(daeURI(dae, "file:/d1/d2/d3/../../d4/./file.txt").str() == "file:/d1/d4/file.txt");
01095
01096
01097 CheckResult(strcmp(daeURI(dae, "file:/dir/file.txt").getURI(), "file:/dir/file.txt") == 0);
01098 CheckResult(strcmp(daeURI(dae, "dir/file.txt").getOriginalURI(), "dir/file.txt") == 0);
01099 {
01100 daeURI uri(dae), base(dae);
01101 base.setURI("http://www.example.com");
01102 uri.setURI("dir/file.txt", &base);
01103 CheckResult(uri.str() == "http://www.example.com/dir/file.txt");
01104 uri.setURI("http://www.example.com/dir/file.txt?q#f");
01105 CheckResult(strcmp(uri.getScheme(), "http") == 0);
01106 CheckResult(strcmp(uri.getProtocol(), "http") == 0);
01107 CheckResult(strcmp(uri.getAuthority(), "www.example.com") == 0);
01108 CheckResult(strcmp(uri.getPath(), "/dir/file.txt") == 0);
01109 CheckResult(strcmp(uri.getQuery(), "q") == 0);
01110 CheckResult(strcmp(uri.getFragment(), "f") == 0);
01111 CheckResult(strcmp(uri.getID(), "f") == 0);
01112 char buffer1[4], buffer2[32];
01113 CheckResult(!uri.getPath(buffer1, sizeof(buffer1)));
01114 CheckResult(uri.getPath(buffer2, sizeof(buffer2)));
01115 CheckResult(strcmp(buffer2, "/dir/file.txt") == 0);
01116 }
01117
01118
01119 {
01120 daeURI base(dae, "file:/home/sthomas/");
01121 daeURI uri1(base, "folder1/file.dae");
01122 daeURI uri2(base, "folder2/file.dae");
01123 uri2.makeRelativeTo(&uri1);
01124 CheckResult(uri2.originalStr() == "../folder2/file.dae");
01125 CheckResult(uri2.str() == "file:/home/sthomas/folder2/file.dae");
01126 }
01127
01128
01129
01130 {
01131 string scheme, authority, path, query, fragment;
01132 cdom::parseUriRef("file:////models/cube.dae", scheme, authority, path, query, fragment);
01133 CheckResult(cdom::assembleUri(scheme, authority, path, query, fragment) == "file:////models/cube.dae");
01134 }
01135
01136 return testResult(true);
01137 }
01138
01139
01140 DefineTest(uriBase) {
01141 DAE dae;
01142 daeURI uri(dae, cdom::nativePathToUri(lookupTestFile("uri.dae")));
01143 CheckResult(dae.open(uri.str()));
01144 domImage::domInit_from* initFrom = dae.getDatabase()->typeLookup<domImage::domInit_from>().at(0);
01145 CheckResult(initFrom->getRef()->getValue().pathDir() == uri.pathDir());
01146 return testResult(true);
01147 }
01148
01149
01150 DefineTest(xmlNavigation) {
01151 DAE dae;
01152 string file = lookupTestFile("cube.dae");
01153 domCOLLADA* root = dae.open(file);
01154 CheckResult(root);
01155
01156 CheckResult(root->getChild("library_cameras"));
01157 CheckResult(root->getChild("contributor") == 0);
01158 CheckResult(root->getDescendant("steveT") == 0);
01159 daeElement* upAxis = root->getDescendant("up_axis");
01160 CheckResult(upAxis);
01161 CheckResult(upAxis->getParent());
01162 CheckResult(upAxis->getAncestor("asset"));
01163 CheckResult(upAxis->getAncestor("library_geometries") == 0);
01164
01165 CheckResult(root->getChild(daeElement::matchType(domLibrary_cameras::ID())));
01166 CheckResult(root->getChild(daeElement::matchType(domAsset::domContributor::ID())) == 0);
01167 CheckResult(root->getDescendant(daeElement::matchType(-10)) == 0);
01168 upAxis = root->getDescendant(daeElement::matchType(domAsset::domUp_axis::ID()));
01169 CheckResult(upAxis);
01170 CheckResult(upAxis->getParent());
01171 CheckResult(upAxis->getAncestor(daeElement::matchType(domAsset::ID())));
01172 CheckResult(upAxis->getAncestor(daeElement::matchType(domLibrary_geometries::ID())) == 0);
01173
01174 return testResult(true);
01175 }
01176
01177
01178 DefineTest(multipleDae) {
01179
01180
01181 DAE dae1;
01182 DAE dae2;
01183 CheckResult(dae2.open(lookupTestFile("cube.dae")));
01184 CheckResult(dae1.open(lookupTestFile("duck.dae")));
01185 return testResult(true);
01186 }
01187
01188
01189 DefineTest(unusedTypeCheck) {
01190 DAE dae;
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201 set<int> expectedUnusedTypes;
01202 expectedUnusedTypes.insert(domEllipsoid::ID());
01203 expectedUnusedTypes.insert(domEllipsoid::domSize::ID());
01204 expectedUnusedTypes.insert(domInput_global::ID());
01205 expectedUnusedTypes.insert(domAny::ID());
01206
01207
01208 expectedUnusedTypes.insert(domImage_source::ID());
01209 expectedUnusedTypes.insert(domFx_sampler::ID());
01210 expectedUnusedTypes.insert(domFx_rendertarget::ID());
01211 expectedUnusedTypes.insert(domGles2_newparam::ID());
01212 expectedUnusedTypes.insert(domLimits_sub::ID());
01213 expectedUnusedTypes.insert(domTargetable_float4::ID());
01214 expectedUnusedTypes.insert(domCommon_int_or_param::ID());
01215
01216
01217 set<int> actualUnusedTypes;
01218 const daeMetaElementRefArray &metas = dae.getAllMetas();
01219 for (size_t i = 0; i < metas.getCount(); i++)
01220 if (!metas[i])
01221 actualUnusedTypes.insert((int)i);
01222
01223
01224 return testResult(expectedUnusedTypes == actualUnusedTypes);
01225 }
01226
01227
01228 DefineTest(domFx_common_transparent) {
01229
01230 DAE dae;
01231 CheckResult(dae.open(lookupTestFile("cube.dae")));
01232
01233 domFx_common_transparent* transparent =
01234 dae.getDatabase()->typeLookup<domFx_common_transparent>().at(0);
01235
01236 CheckResult(transparent->getColor() != NULL);
01237 CheckResult(transparent->getParam() == NULL);
01238 CheckResult(transparent->getTexture() == NULL);
01239 CheckResult(transparent->getOpaque() == FX_OPAQUE_A_ONE);
01240
01241 return testResult(true);
01242 };
01243
01244
01245 DefineTest(autoResolve) {
01246
01247
01248 DAE dae;
01249 daeDatabase& database = *dae.getDatabase();
01250 CheckResult(dae.open(lookupTestFile("Seymour.dae")));
01251
01252 {
01253
01254 xsIDREFS& idRefs = database.typeLookup<domIdref_array>().at(0)->getValue();
01255 for (size_t i = 0; i < idRefs.getCount(); i++) {
01256 CheckResult(idRefs[i].getElement());
01257 }
01258
01259 domInstance_controller& ic = *database.typeLookup<domInstance_controller>().at(0);
01260 CheckResult(ic.getUrl().getElement());
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270
01271
01272
01273 }
01274
01275
01276
01277 dae.clear();
01278 domCOLLADA* root = dae.add("tmp.dae");
01279 CheckResult(root);
01280
01281
01282 CheckResult(root->add("library_geometries geometry mesh source IDREF_array"));
01283 daeElement* geom = root->getDescendant("geometry");
01284 geom->setAttribute("id", "myGeom");
01285 xsIDREFS& idRefs = database.typeLookup<domIdref_array>().at(0)->getValue();
01286 idRefs.append(daeIDRef("myGeom"));
01287
01288
01289 daeElement* node1 = root->add("library_nodes node");
01290 node1->setAttribute("id", "myNode");
01291
01292
01293 daeElement* node2 = root->getDescendant("library_nodes")->add("node");
01294 domInstance_node& instanceNode = *daeSafeCast<domInstance_node>(node2->add("instance_node"));
01295 domInstance_geometry& instanceGeom = *daeSafeCast<domInstance_geometry>(
01296 node2->add("instance_geometry"));
01297 instanceNode.setUrl("#myNode");
01298 instanceGeom.setUrl("#myGeom");
01299
01300
01301 domImage_source::domRef* ref = daeSafeCast<domImage_source::domRef>(
01302 root->add("library_images image init_from ref"));
01303 ref->setValue("myGeom");
01304
01305
01306 CheckResult(idRefs[0].getElement() == geom);
01307 CheckResult(instanceGeom.getUrl().getElement() == geom);
01308
01309
01310 CheckResult(instanceNode.getUrl().getElement() == node1);
01311
01312 return testResult(true);
01313 }
01314
01315
01316 DefineTest(baseURI) {
01317 DAE dae1, dae2;
01318 dae1.setBaseURI("http://www.example.com/");
01319 daeURI uri1(dae1, "myFolder/myFile.dae");
01320 daeURI uri2(dae2, "myFolder/myFile.dae");
01321 CheckResult(uri1.str() != uri2.str());
01322 CheckResult(uri1.str() == "http://www.example.com/myFolder/myFile.dae");
01323 return testResult(true);
01324 }
01325
01326
01327 DefineTest(databaseLookup) {
01328 DAE dae;
01329 CheckResult(dae.open(lookupTestFile("cube.dae")));
01330 daeDatabase& database = *dae.getDatabase();
01331 daeDocument* doc = database.getDoc(0);
01332 CheckResult(doc);
01333
01334
01335 CheckResult(database.idLookup("light-lib").size() == 1);
01336 CheckResult(database.idLookup("light-lib", doc));
01337 CheckResult(database.typeLookup(domNode::ID()).size() == 5);
01338 vector<daeElement*> elts;
01339 database.typeLookup(domRotate::ID(), elts, doc);
01340 CheckResult(elts.size() == 15);
01341 CheckResult(database.typeLookup<domNode>().size() == 5);
01342 vector<domRotate*> rotateElts;
01343 database.typeLookup(rotateElts);
01344 CheckResult(rotateElts.size() == 15);
01345
01346
01347 CheckResult(database.getElementCount("light-lib") == 1);
01348 daeElement* elt = NULL;
01349 database.getElement(&elt, 0, "light-lib", NULL, doc->getDocumentURI()->getURI());
01350 CheckResult(elt);
01351 CheckResult(database.getElementCount(NULL, "node") == 5);
01352 database.getElement(&elt, 8, NULL, "rotate");
01353 CheckResult(elt);
01354
01355 return testResult(true);
01356 }
01357
01358
01359 DefineTest(fileExtension) {
01360
01361
01362
01363 DAE dae;
01364 CheckResult(dae.open(lookupTestFile("cube.cstm")));
01365 CheckResult(dae.getDatabase()->typeLookup<domAccessor>().at(0)->getSource().getElement());
01366 CheckResult(dae.writeTo(lookupTestFile("cube.cstm"), getTmpFile("cube_roundTrip.cstm")));
01367 return testResult(true);
01368 }
01369
01370
01371 DefineTest(zipFile) {
01372
01373 DAE dae;
01374 CheckResult(dae.open(lookupTestFile("cube.dae.gz")));
01375 CheckResult(dae.getDatabase()->typeLookup(domAsset::ID()).size() == 1);
01376 return testResult(true);
01377 }
01378
01379
01380 DefineTest(charEncoding) {
01381
01382 string file = getTmpFile("charEncoding.dae");
01383 DAE dae;
01384 dae.setCharEncoding(DAE::Latin1);
01385 daeElement* elt = dae.add(file)->add("asset contributor comments");
01386 CheckResult(elt);
01387 elt->setCharData("æ ø å ü ä ö");
01388 CheckResult(dae.writeAll());
01389 dae.clear();
01390 CheckResult(dae.open(file));
01391 return testResult(true);
01392 }
01393
01394
01395 DefineTest(getElementBug) {
01396 DAE dae;
01397 CheckResult(dae.open(lookupTestFile("cube.dae")));
01398
01399
01400 domInstance_geometry* geomInst = dae.getDatabase()->typeLookup<domInstance_geometry>().at(0);
01401 CheckResult(geomInst->getUrl().getElement());
01402 daeElement::removeFromParent(geomInst->getUrl().getElement());
01403 CheckResult(geomInst->getUrl().getElement() == 0);
01404
01405
01406 daeIDRef idRef(*geomInst);
01407 idRef.setID("PerspCamera");
01408 CheckResult(idRef.getElement());
01409 daeElement::removeFromParent(idRef.getElement());
01410 CheckResult(idRef.getElement() == 0);
01411
01412
01413 daeSidRefCache& cache = dae.getSidRefCache();
01414 daeElement* effect = dae.getDatabase()->typeLookup(domEffect::ID()).at(0);
01415 daeSidRef sidRef("common", effect);
01416
01417 CheckResult(cache.empty() && cache.hits() == 0 && cache.misses() == 0);
01418 daeElement* technique = sidRef.resolve().elt;
01419 CheckResult(technique && cache.misses() == 1 && !cache.empty());
01420 sidRef.resolve();
01421 CheckResult(cache.misses() == 1 && cache.hits() == 1);
01422 daeElement::removeFromParent(technique);
01423 CheckResult(cache.empty() && cache.misses() == 0 && cache.hits() == 0);
01424 CheckResult(sidRef.resolve().elt == NULL);
01425 CheckResult(cache.empty() && cache.misses() == 1);
01426
01427 return testResult(true);
01428 }
01429
01430
01431 DefineTest(externalRef) {
01432 DAE dae;
01433 CheckResult(dae.open(lookupTestFile("externalRef.dae")));
01434 domInstance_geometry* geomInst = dae.getDatabase()->typeLookup<domInstance_geometry>().at(0);
01435 daeURI& uri = geomInst->getUrl();
01436 CheckResult(uri.isExternalReference() == true);
01437 CheckResult(uri.getReferencedDocument() == NULL);
01438 CheckResult(uri.getElement());
01439 CheckResult(uri.getReferencedDocument());
01440 return testResult(true);
01441 }
01442
01443
01444 DefineTest(charEncodingSetting) {
01445 DAE dae;
01446 dae.setGlobalCharEncoding(DAE::Utf8);
01447 CheckResult(dae.getCharEncoding() == DAE::Utf8);
01448 dae.setCharEncoding(DAE::Latin1);
01449 CheckResult(dae.getCharEncoding() == DAE::Latin1);
01450 DAE dae2;
01451 CheckResult(dae2.getCharEncoding() == DAE::Utf8);
01452 return testResult(true);
01453 }
01454
01455
01456 DefineTest(uriCopy) {
01457 DAE dae;
01458 CheckResult(dae.open(lookupTestFile("cube.dae")));
01459 domInstance_geometry* geomInst = dae.getDatabase()->typeLookup<domInstance_geometry>().at(0);
01460 daeURI& uri = geomInst->getUrl();
01461 CheckResult(uri.getElement());
01462 daeURI uriCopy = geomInst->getUrl();
01463 CheckResult(uriCopy.getElement());
01464 return testResult(true);
01465 }
01466
01467
01468 DefineTest(badFileLoad) {
01469 DAE dae;
01470 CheckResult(!dae.open(lookupTestFile("badFile.dae")));
01471 return testResult(true);
01472 }
01473
01474
01475 DefineTest(spuriousQuotes) {
01476 DAE dae;
01477 CheckResult(!dae.open(lookupTestFile("quotesProblem.dae")));
01478 return testResult(true);
01479 }
01480
01481
01482 DefineTest(zaeLoading) {
01483 DAE dae;
01484
01485
01486 std::string testFile = lookupTestFile("duck.zae");
01487 domCOLLADA* root = dae.open(testFile);
01488 CheckResult(root);
01489
01490
01491 domInstance_with_extra* instanceVisualScene = daeSafeCast<domInstance_with_extra>(root->getDescendant("instance_visual_scene"));
01492 CheckResult(instanceVisualScene);
01493 daeURI visualSceneURI = instanceVisualScene->getUrl();
01494 domVisual_scene* visualScene = daeSafeCast<domVisual_scene>(visualSceneURI.getElement());
01495 CheckResult(visualScene);
01496
01497
01498 domInstance_image* instanceImage = daeSafeCast<domInstance_image>(visualScene->getDocument()->getDomRoot()->getDescendant("instance_image"));
01499 CheckResult(instanceImage);
01500 daeURI imageURI = instanceImage->getUrl();
01501 domImage* image = daeSafeCast<domImage>(imageURI.getElement());
01502 CheckResult(image);
01503
01504
01505 domImage_source::domRef* ref = daeSafeCast<domImage_source::domRef>(image->getDescendant("ref"));
01506 xsAnyURI imageFileURI = ref->getValue();
01507 bool imageFileExists = boost::filesystem::exists( cdom::uriToNativePath( imageFileURI.str() ) );
01508 CheckResult(imageFileExists);
01509
01510
01511 domInstance_geometry* instanceGeometry = daeSafeCast<domInstance_geometry>(visualScene->getDocument()->getDomRoot()->getDescendant("instance_geometry"));
01512 CheckResult(instanceGeometry);
01513 daeURI geometryURI = instanceGeometry->getUrl();
01514 domGeometry* geometry = daeSafeCast<domGeometry>(geometryURI.getElement());
01515 CheckResult(geometry);
01516
01517 return testResult(true);
01518 }
01519
01520 DefineTest(zaeIllegalArchive) {
01521 DAE dae;
01522
01523
01524 std::string testFile = lookupTestFile("illegal_archive.zae");
01525 domCOLLADA* root = dae.open(testFile);
01526 CheckResult(!root);
01527
01528 return testResult(true);
01529 }
01530
01531
01532
01533
01534
01535
01536
01537
01538
01539 bool checkTests(const set<string>& tests) {
01540 bool invalidTestFound = false;
01541 for (set<string>::const_iterator iter = tests.begin(); iter != tests.end(); iter++) {
01542 if (registeredTests().find(*iter) == registeredTests().end()) {
01543 if (!invalidTestFound)
01544 cout << "Invalid arguments:\n";
01545 cout << " " << *iter << endl;
01546 invalidTestFound = true;
01547 }
01548 }
01549
01550 return !invalidTestFound;
01551 }
01552
01553
01554 map<string, testResult> runTests(const set<string>& tests) {
01555 map<string, testResult> failedTests;
01556 for (set<string>::const_iterator iter = tests.begin(); iter != tests.end(); iter++) {
01557 testResult result = registeredTests()[*iter]->run();
01558 if (!result.passed)
01559 failedTests[*iter] = result;
01560 }
01561 return failedTests;
01562 }
01563
01564
01565
01566 bool printTestResults(const map<string, testResult>& failedTests) {
01567 if (!failedTests.empty()) {
01568 cout << "Failed tests:\n";
01569 for (map<string, testResult>::const_iterator iter = failedTests.begin();
01570 iter != failedTests.end();
01571 iter++) {
01572 cout << " " << iter->first;
01573 if (!iter->second.file.empty()) {
01574 cout << " (file " << fs::path(iter->second.file).leaf();
01575 if (iter->second.line != -1)
01576 cout << ", line " << iter->second.line << ")";
01577 else
01578 cout << ")";
01579 }
01580 cout << endl;
01581 if (!iter->second.msg.empty())
01582 cout << " " << replace(iter->second.msg, "\n", "\n ") << "\n";
01583 }
01584 return false;
01585 }
01586 else {
01587 cout << "All tests passed.\n";
01588 return true;
01589 }
01590 }
01591
01592 struct tmpDir {
01593 fs::path path;
01594 bool deleteWhenDone;
01595
01596 tmpDir(fs::path& path, bool deleteWhenDone)
01597 : path(path),
01598 deleteWhenDone(deleteWhenDone) {
01599 fs::create_directories(path);
01600 }
01601
01602 ~tmpDir() {
01603 if (deleteWhenDone)
01604 fs::remove_all(path);
01605 }
01606 };
01607
01608
01609 int main(int argc, char* argv[]) {
01610
01611
01612 DAE dae;
01613 PFLN
01614 daeElement* root = dae.add("valid.dae");
01615 PFLN
01616 daeElement* asset = root->add("asset");
01617 daeElement* contributor = asset->add("contributor");
01618 daeElement* created = asset->add("created");
01619 daeElement* modified = asset->add("modified");
01620 const char* date = "2008-04-08T13:07:52-08:00";
01621 daeBool res = created->setCharData(date);
01622 printf("result date %d\n", res);
01623 modified->setCharData(date);
01624
01625 my_addGeometry(root);
01626
01627
01628
01629
01630
01631
01632 daeElement* effectLib = root->add("library_effects");
01633 daeElement* effect = effectLib->add("effect");
01634 effect->setAttribute("id", "cubeEffect");
01635 daeElement* profile = effect->add("profile_COMMON");
01636 daeElement* technique = profile->add("technique");
01637 technique->setAttribute("sid", "phong1");
01638 daeElement* phong = technique->add("phong");
01639 daeElement* emission = phong->add("emission color");
01640 emission->setCharData("0.5 0.5 0.0 1.0");
01641 daeElement* ambient = phong->add("ambient color");
01642 ambient->setCharData("0.5 0.5 0.0 1.0");
01643 PFLN
01644 daeElement* diffuse = phong->add("diffuse color");
01645 diffuse->setCharData("0.5 0.5 0.0 1.0");
01646 PFLN
01647 daeElement* specular = phong->add("specular color");
01648 specular->setCharData("0.5 0.5 0.0 1.0");
01649 PFLN
01650 daeElement* shininess = phong->add("shininess float");
01651 PFLN
01652 shininess->setCharData("20.0");
01653 daeElement* reflective = phong->add("reflective color");
01654 PFLN
01655 reflective->setCharData("0.5 0.5 0.0 1.0");
01656 PFLN
01657 daeElement* reflectivity = phong->add("reflectivity float");
01658 PFLN
01659 reflectivity->setCharData("0.5");
01660 PFLN
01661 daeElement* transparent = phong->add("transparent color");
01662 PFLN
01663 transparent->setCharData("1.0 1.0 1.0 1.0");
01664 PFLN
01665 daeElement* transparency = phong->add("transparency float");
01666 PFLN
01667 transparency->setCharData("1.0");
01668 PFLN
01669
01670
01671
01672 daeElement* materialLib = root->add("library_materials");
01673 daeElement* material = materialLib->add("material");
01674 material->setAttribute("id", "cubeMaterial");
01675 daeElement* instance_effect = material->add("instance_effect");
01676 instance_effect->setAttribute("url", "#cubeEffect");
01677
01678
01679 daeElement* visualSceneLib = root->add("library_visual_scenes");
01680 daeElement* visualScene = visualSceneLib->add("visual_scene");
01681 visualScene->setAttribute("id", "cubeScene");
01682
01683
01684 daeElement* node = visualScene->add("node");
01685 node->setAttribute("id", "cubeNode");
01686 node->add("rotate")->setCharData("1 0 0 45");
01687 node->add("translate")->setCharData("0 10 0");
01688 node->add("scale")->setCharData("1 1 1");
01689
01690
01691 daeElement* instanceGeom = node->add("instance_geometry");
01692 instanceGeom->setAttribute("url", makeUriRef("cubeGeom").c_str());
01693
01694
01695 daeElement* instanceMaterial = instanceGeom->add("bind_material technique_common instance_material");
01696 instanceMaterial->setAttribute("symbol", "mtl");
01697 instanceMaterial->setAttribute("target", makeUriRef("cubeMaterial").c_str());
01698
01699
01700
01701
01702
01703
01704
01705 root->add("scene instance_visual_scene")->setAttribute("url", makeUriRef("cubeScene").c_str());
01706
01707 printf("write all\n");
01708 dae.writeAll();
01709 printf("returning\n");
01710 return 0;
01711
01712
01713
01714
01715
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728
01729 #if defined _MSC_VER && defined _DEBUG
01730 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_EVERY_1024_DF);
01731 #endif
01732
01733 if (argc == 1) {
01734 cout << "Usage:\n"
01735 " -printTests - Print the names of all available tests\n"
01736 " -all - Run all tests\n"
01737 " -leaveTmpFiles - Don't delete the tmp folder containing the generated test files\n"
01738 " test1 test2 ... - Run the named tests\n";
01739 return 0;
01740 }
01741
01742 bool printTests = false;
01743 bool allTests = false;
01744 bool leaveTmpFiles = false;
01745 set<string> tests;
01746 for (int i = 1; i < argc; i++) {
01747 if (string(argv[i]) == "-printTests")
01748 printTests = true;
01749 else if (string(argv[i]) == "-all")
01750 allTests = true;
01751 else if (string(argv[i]) == "-leaveTmpFiles")
01752 leaveTmpFiles = true;
01753 else
01754 tests.insert(argv[i]);
01755 }
01756
01757 #ifdef __CELLOS_LV2__
01758
01759
01760
01761 leaveTmpFiles = true;
01762 #endif
01763
01764
01765 daeErrorHandler::setErrorHandler(&quietErrorHandler::getInstance());
01766
01767 dataPath() = (fs::path(argv[0]).branch_path()/"domTestData/").normalize();
01768 if (!fs::exists(dataPath()))
01769 dataPath() = (fs::path(argv[0]).branch_path()/"../../test/1.5/data/").normalize();
01770 tmpPath() = dataPath() / "tmp";
01771 tmpDir tmp(tmpPath(), !leaveTmpFiles);
01772
01773 if (checkTests(tests) == false)
01774 return 0;
01775
01776
01777 if (printTests) {
01778 map<string, domTest*>::iterator iter;
01779 for (iter = registeredTests().begin(); iter != registeredTests().end(); iter++)
01780 cout << iter->second->name << endl;
01781 return 0;
01782 }
01783
01784
01785 if (allTests) {
01786 map<string, domTest*>::iterator iter;
01787 for (iter = registeredTests().begin(); iter != registeredTests().end(); iter++)
01788 tests.insert(iter->first);
01789 }
01790
01791
01792 return printTestResults(runTests(tests)) ? 0 : 1;
01793 }