wavepacket-lib/tool/testResources/test.cpp

Go to the documentation of this file.
00001 /*
00002  * test.cpp
00003  *
00004  * Copyright (C) 2010  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 doTest
00022 (
00023 void
00024 )
00025 {
00026         DPRINTF("Walking registry...");
00027         int nNamespaces = getResourceNamespaceCount();
00028         DPRINTF("Found %d namespaces", nNamespaces);
00029 
00030         for (int i = 0; i < nNamespaces; ++i) {
00031                 const char * nameSpace = getResourceNamespaceName(i);
00032                 DPRINTF("namespace[%d] = '%s'", i, nameSpace);
00033                 ASSERT_THROW(nameSpace, "null");
00034 
00035                 int nStrings = getStringResourceCount(nameSpace);
00036                 DPRINTF("  namespace '%s' contains %d string resources",
00037                     nameSpace, nStrings);
00038 
00039                 for (int j = 0; j < nStrings; ++j) {
00040                         const char * name = getStringResourceName(nameSpace, j);
00041                         DPRINTF("    string resource name %d: '%s'", j, name);
00042                         ASSERT_THROW(name, "null resource name");
00043                         const char * value = getStringResource(nameSpace, name);
00044                         DPRINTF("      %s: '%s'", name, value);
00045                         ASSERT_THROW(value, "null resource value");
00046                 }
00047         }
00048 
00049         // example of hard-coded constants that the code expects to be there
00050         DPRINTF("Verifying hard-coded lookups...");
00051         ASSERT_THROW(getStringResource("foo", "example1.txt"),
00052             "Should have found registered string");
00053         ASSERT_THROW(getStringResource("foo", "example2.txt"),
00054             "Should have found registered string");
00055         ASSERT_THROW(getStringResource("bar", "example1.txt"),
00056             "Should have found registered string");
00057 
00058         DPRINTF("Verifying lookup failures...");
00059         ASSERT_THROW(!getStringResource("bar", "example2.txt"),
00060             "Only namespace 'foo' should have resource 'example2.txt'");
00061 
00062         // shouldn't find this one
00063         const char * nameSpace = "baz";
00064         const char * name = "example3";
00065         ASSERT_THROW(!getStringResource(nameSpace, name),
00066             "Expected null result for unregistered string");
00067 
00068         DPRINTF("Verifying runtime registration...");
00069         registerStringResource(nameSpace, name, "bogus string example");
00070         ASSERT_THROW(getStringResource(nameSpace, name),
00071             "Should have found string now that we registered it");
00072 }
00073 
00074 
00075 
00076 ////////////////////////////////////////////////////////////////////////////////
00077 //
00078 //      entry point
00079 //
00080 ////////////////////////////////////////////////////////////////////////////////
00081 
00082 int
00083 main
00084 (
00085 IN int argc,
00086 IN const char * argv[]
00087 )
00088 {
00089         int result = 0;
00090 
00091         try {
00092                 doTest();
00093 
00094         } catch (std::exception& e) {
00095                 DPRINTF("Exception: %s", e.what());
00096                 result = 1;
00097         }
00098 
00099         return result;
00100 }
00101