bsp-test.cpp

Go to the documentation of this file.
00001 /*
00002  * bsp-test.cpp
00003  *
00004  * Copyright (C) 2010  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Quick test for BSP parsing
00032  */
00033 
00034 // includes --------------------------------------------------------------------
00035 #include "quake/bsp/bsp.h"
00036 
00037 #include <iostream>
00038 
00039 #include "perf/perf.h"
00040 #include "util/file.h"
00041 
00042 
00043 
00044 ////////////////////////////////////////////////////////////////////////////////
00045 //
00046 //      static helper methods
00047 //
00048 ////////////////////////////////////////////////////////////////////////////////
00049 
00050 static void
00051 doTest
00052 (
00053 IN const char * filename
00054 )
00055 {
00056         ASSERT(filename, "null");
00057 
00058         // create filesystem manager
00059         std::string parentDir;
00060         GetParentDirectory(filename, parentDir);
00061 
00062         smart_ptr<nstream::Manager> mgr =
00063             nstream::getFilesystemManager(parentDir.c_str());
00064         ASSERT_THROW(mgr, "failed to get filesystem manager");
00065 
00066         const char * name = GetFilename(filename);
00067 
00068         // open stream
00069         smart_ptr<nstream::Stream> stream =
00070             nstream::openNamedStream(mgr, name);
00071         ASSERT_THROW(stream, "failed to open named stream: " << name);
00072 
00073         // now load bsp
00074         smart_ptr<quake::Bsp> bsp = quake::Bsp::load(stream);
00075         ASSERT_THROW(bsp, "failed to load bsp");
00076 
00077         DPRINTF("Just loaded bsp with name: '%s'", bsp->getName());
00078 
00079         // iterate lumps
00080         const quake::vec_lump_type_t& lumps = bsp->getLumps();
00081         DPRINTF("Contains %d lumps", (int) lumps.size());
00082         for (quake::vec_lump_type_t::const_iterator i = lumps.begin();
00083              i != lumps.end(); ++i) {
00084                 quake::eLumpType type = *i;
00085                 const char * name = quake::getLumpName(type);
00086                 ASSERT_THROW(name, "Name not found for type: " << type);
00087                 DPRINTF("Reading lump: %s", name);
00088 
00089                 int nObjects = bsp->getLumpObjectCount(type);
00090                 DPRINTF("  There are %d objects of this type", nObjects);
00091                 if (!nObjects) {
00092                         DPRINTF("  Lump is empty or does not exist!");
00093                         continue;
00094                 }
00095                 if (nObjects < 0) {
00096                         DPRINTF("  Variable number of objects in lump!");
00097                         nObjects = 0x7ffff;
00098                 }
00099 
00100                 // okay, apparently have objects.  Start iteration!
00101                 std::string timerName = "iterateAllLump";
00102                 timerName += name;
00103 
00104                 {
00105                         perf::Timer timer(timerName.c_str());
00106 
00107                         bsp->startLumpIteration(type);
00108                         const quake::lump_object_t * obj = NULL;
00109                         int j = 0;      // index
00110                         while (true) {
00111                                 obj = bsp->getNextLumpObject();
00112                                 if (!obj) {
00113                                         // end of iteration
00114                                         break;
00115                                 }
00116                                 ASSERT_THROW(obj->index == j,
00117                                     "Iteration object has bad index " <<
00118                                     obj->index << " vs. correct index " << j);
00119                                 quake::eLumpType oType = obj->getType();
00120                                 ASSERT_THROW(oType == type,
00121                                     "Iteration object has bad type " << oType
00122                                     << " vs. correct type " << type);
00123                                 ++j;
00124                         }
00125                         DPRINTF("  Read %d total objects", j);
00126                 }
00127         }
00128 }
00129 
00130 
00131 
00132 ////////////////////////////////////////////////////////////////////////////////
00133 //
00134 //      entry point
00135 //
00136 ////////////////////////////////////////////////////////////////////////////////
00137 
00138 int
00139 main
00140 (
00141 IN int argc,
00142 IN const char * argv[]
00143 )
00144 {
00145         ASSERT(2 == argc, "usage: quake-bsp-test <bsp-filename>");
00146         const char * filename = argv[1];
00147 
00148         int retval = 0;
00149         try {
00150                 perf::Timer timer("test -- overall timer");
00151                 doTest(filename);
00152 
00153         } catch (std::exception& e) {
00154                 DPRINTF("Exception: %s", e.what());
00155                 retval = 1;
00156         } catch (...) {
00157                 DPRINTF("Unknown exception!");
00158                 retval = 2;
00159         }
00160         perf::dumpTimingSummary(std::cerr);
00161 
00162         return retval;
00163 }
00164