Back to index

lightning-sunbird  0.9+nobinonly
TestCSSPropertyLookup.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <stdio.h>
00038 #include "plstr.h"
00039 #include "nsCSSProps.h"
00040 #include "nsCSSKeywords.h"
00041 #include "nsString.h"
00042 
00043 static const char* const kJunkNames[] = {
00044   nsnull,
00045   "",
00046   "123",
00047   "backgroundz",
00048   "zzzzzz",
00049   "#@$&@#*@*$@$#"
00050 };
00051 
00052 int TestProps() {
00053   int rv = 0;
00054   nsCSSProperty id;
00055   nsCSSProperty index;
00056 
00057   // Everything appears to assert if we don't do this first...
00058   nsCSSProps::AddRefTable();
00059 
00060   // First make sure we can find all of the tags that are supposed to
00061   // be in the table. Futz with the case to make sure any case will
00062   // work
00063   extern const char* const kCSSRawProperties[];
00064   const char*const* et = &kCSSRawProperties[0];
00065   const char*const* end = &kCSSRawProperties[eCSSProperty_COUNT];
00066   index = eCSSProperty_UNKNOWN;
00067   while (et < end) {
00068     char tagName[100];
00069     PL_strcpy(tagName, *et);
00070     index = nsCSSProperty(PRInt32(index) + 1);
00071 
00072     id = nsCSSProps::LookupProperty(nsCString(tagName));
00073     if (id == eCSSProperty_UNKNOWN) {
00074       printf("bug: can't find '%s'\n", tagName);
00075       rv = -1;
00076     }
00077     if (id != index) {
00078       printf("bug: name='%s' id=%d index=%d\n", tagName, id, index);
00079       rv = -1;
00080     }
00081 
00082     // fiddle with the case to make sure we can still find it
00083     if (('a' <= tagName[0]) && (tagName[0] <= 'z')) {
00084       tagName[0] = tagName[0] - 32;
00085     }
00086     id = nsCSSProps::LookupProperty(NS_ConvertASCIItoUCS2(tagName));
00087     if (id < 0) {
00088       printf("bug: can't find '%s'\n", tagName);
00089       rv = -1;
00090     }
00091     if (index != id) {
00092       printf("bug: name='%s' id=%d index=%d\n", tagName, id, index);
00093       rv = -1;
00094     }
00095     et++;
00096   }
00097 
00098   // Now make sure we don't find some garbage
00099   for (int i = 0; i < (int) (sizeof(kJunkNames) / sizeof(const char*)); i++) {
00100     const char* const tag = kJunkNames[i];
00101     id = nsCSSProps::LookupProperty(nsCAutoString(tag));
00102     if (id >= 0) {
00103       printf("bug: found '%s'\n", tag ? tag : "(null)");
00104       rv = -1;
00105     }
00106   }
00107 
00108   nsCSSProps::ReleaseTable();
00109   return rv;
00110 }
00111 
00112 int TestKeywords() {
00113   nsCSSKeywords::AddRefTable();
00114 
00115   int rv = 0;
00116   nsCSSKeyword id;
00117   nsCSSKeyword index;
00118 
00119   extern const char* const kCSSRawKeywords[];
00120 
00121   // First make sure we can find all of the tags that are supposed to
00122   // be in the table. Futz with the case to make sure any case will
00123   // work
00124   const char*const*  et = &kCSSRawKeywords[0];
00125   const char*const*  end = &kCSSRawKeywords[eCSSKeyword_COUNT - 1];
00126   index = eCSSKeyword_UNKNOWN;
00127   while (et < end) {
00128     char tagName[512];
00129     char* underscore = &(tagName[0]);
00130 
00131     PL_strcpy(tagName, *et);
00132     while (*underscore) {
00133       if (*underscore == '_') {
00134         *underscore = '-';
00135       }
00136       underscore++;
00137     }
00138     index = nsCSSKeyword(PRInt32(index) + 1);
00139 
00140     id = nsCSSKeywords::LookupKeyword(nsCString(tagName));
00141     if (id <= eCSSKeyword_UNKNOWN) {
00142       printf("bug: can't find '%s'\n", tagName);
00143       rv = -1;
00144     }
00145     if (id != index) {
00146       printf("bug: name='%s' id=%d index=%d\n", tagName, id, index);
00147       rv = -1;
00148     }
00149 
00150     // fiddle with the case to make sure we can still find it
00151     if (('a' <= tagName[0]) && (tagName[0] <= 'z')) {
00152       tagName[0] = tagName[0] - 32;
00153     }
00154     id = nsCSSKeywords::LookupKeyword(nsCString(tagName));
00155     if (id <= eCSSKeyword_UNKNOWN) {
00156       printf("bug: can't find '%s'\n", tagName);
00157       rv = -1;
00158     }
00159     if (id != index) {
00160       printf("bug: name='%s' id=%d index=%d\n", tagName, id, index);
00161       rv = -1;
00162     }
00163     et++;
00164   }
00165 
00166   // Now make sure we don't find some garbage
00167   for (int i = 0; i < (int) (sizeof(kJunkNames) / sizeof(const char*)); i++) {
00168     const char* const tag = kJunkNames[i];
00169     id = nsCSSKeywords::LookupKeyword(nsCAutoString(tag));
00170     if (eCSSKeyword_UNKNOWN < id) {
00171       printf("bug: found '%s'\n", tag ? tag : "(null)");
00172       rv = -1;
00173     }
00174   }
00175 
00176   nsCSSKeywords::ReleaseTable();
00177   return rv;
00178 }
00179 
00180 int main(int argc, char** argv)
00181 {
00182   TestProps();
00183   TestKeywords();
00184   return 0;
00185 }