tool/testResources/test.cpp

Go to the documentation of this file.
00001 /*
00002  * test.cpp
00003  *
00004  * Copyright (C) 2010,2011  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  * Simple test of the resource APIs/build
00008  */
00009 
00010 // includes --------------------------------------------------------------------
00011 #include "resources/resources.h"
00012 
00013 
00014 ////////////////////////////////////////////////////////////////////////////////
00015 //
00016 //      static helper methods
00017 //
00018 ////////////////////////////////////////////////////////////////////////////////
00019 
00020 static void
00021 registerString
00022 (
00023 IN const char * nameSpace,
00024 IN const char * name,
00025 IN const char * value
00026 )
00027 {
00028         ASSERT(nameSpace, "null");
00029         ASSERT(name, "null");
00030         ASSERT(value, "null");
00031 
00032         int bytes = strlen(value) + 1;  // add one for null terminator
00033 
00034         registerResource(nameSpace, name, (const byte_t *) value, bytes);
00035 
00036         ASSERT_THROW(getResourceData(nameSpace, name),
00037             "Should have found resource now that we registered it:"
00038             << nameSpace << "/" << name);
00039 }
00040 
00041 
00042 static void
00043 doTest
00044 (
00045 void
00046 )
00047 {
00048         DPRINTF("Walking registry...");
00049         int nNamespaces = getResourceNamespaceCount();
00050         DPRINTF("Found %d namespaces", nNamespaces);
00051 
00052         for (int i = 0; i < nNamespaces; ++i) {
00053                 const char * nameSpace = getResourceNamespaceName(i);
00054                 DPRINTF("namespace[%d] = '%s'", i, nameSpace);
00055                 ASSERT_THROW(nameSpace, "null");
00056 
00057                 int nStrings = getResourceCount(nameSpace);
00058                 DPRINTF("  namespace '%s' contains %d resources",
00059                     nameSpace, nStrings);
00060 
00061                 for (int j = 0; j < nStrings; ++j) {
00062                         const char * name = getResourceName(nameSpace, j);
00063                         ASSERT_THROW(name, "Failed to look up resource: " << j);
00064                         DPRINTF("    string resource name %d: '%s'",
00065                             j, name);
00066                         const byte_t * raw_bytes =
00067                             getResourceData(nameSpace, name);
00068                         ASSERT_THROW(raw_bytes, "null resource value");
00069                         const char * value = (const char *) raw_bytes;
00070                         DPRINTF("      %s: '%s'", name, value);
00071                 }
00072         }
00073 
00074         // example of hard-coded constants that the code expects to be there
00075         DPRINTF("Verifying hard-coded lookups...");
00076         ASSERT_THROW(getResourceData("foo", "example1.txt"),
00077             "Should have found registered string");
00078         ASSERT_THROW(getResourceData("foo", "example2.txt"),
00079             "Should have found registered string");
00080         ASSERT_THROW(getResourceData("bar", "example1.txt"),
00081             "Should have found registered string");
00082 
00083         DPRINTF("Verifying lookup failures...");
00084         ASSERT_THROW(!getResourceData("bar", "example2.txt"),
00085             "Only namespace 'foo' should have resource 'example2.txt'");
00086 
00087         // shouldn't find this one
00088         const char * nameSpace = "baz";
00089         const char * name = "example3";
00090         ASSERT_THROW(!getResourceData(nameSpace, name),
00091             "Expected null result for unregistered string");
00092 
00093         DPRINTF("Verifying runtime registration...");
00094         registerString(nameSpace, name, "bogus string example");
00095 
00096         // try a binary object
00097         DPRINTF("Verifying binary resources...");
00098         const int nLongs = 1024;
00099         long data[nLongs];
00100         for (int i = 0; i < nLongs; ++i) {
00101                 data[i] = rand();
00102         }
00103 
00104         int bytes = nLongs * sizeof(long);
00105         registerResource(nameSpace, name, (const byte_t *) data, bytes);
00106 
00107         const byte_t * rawData = getResourceData(nameSpace, name);
00108         ASSERT_THROW(rawData,
00109             "Should have found binary resource since we just registered it");
00110 
00111         int outBytes = getResourceSize(nameSpace, name);
00112         ASSERT_THROW(outBytes == bytes,
00113             "registered resource bytes (" << outBytes << ") don't match " <<
00114             "what we registered (" << bytes << ")");
00115 
00116         // verify
00117         const long * out = (const long *) rawData;
00118         for (int i = 0; i < nLongs; ++i) {
00119                 ASSERT_THROW(data[i] == out[i],
00120                     "Data mismatch at element " << i);
00121         }
00122 }
00123 
00124 
00125 
00126 ////////////////////////////////////////////////////////////////////////////////
00127 //
00128 //      entry point
00129 //
00130 ////////////////////////////////////////////////////////////////////////////////
00131 
00132 int
00133 main
00134 (
00135 IN int argc,
00136 IN const char * argv[]
00137 )
00138 {
00139         int result = 0;
00140 
00141         try {
00142                 doTest();
00143 
00144         } catch (std::exception& e) {
00145                 DPRINTF("Exception: %s", e.what());
00146                 result = 1;
00147         }
00148 
00149         return result;
00150 }
00151