Back to index

lightning-sunbird  0.9+nobinonly
OJITestLoader.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
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  * Sun Microsystems, Inc.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
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 the GNU General Public License Version 2 or later (the "GPL"), or
00026  * 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 "OJITestLoader.h"
00038 //#include <io.h>
00039 #include "nsReadableUtils.h"
00040 
00041 static NS_DEFINE_IID(kISupportsIID,    NS_ISUPPORTS_IID);
00042 static NS_DEFINE_IID(kIOJITestLoaderIID, OJITESTLOADER_IID);
00043 
00044 NS_IMPL_ISUPPORTS1(OJITestLoader, OJITestLoader)
00045 
00046 OJITestLoader::OJITestLoader(void) 
00047 {
00048 
00049   TestResult* res = NULL;
00050   char** testCase = loadTestList();
00051   int i = 0;
00052   TEST_TYPE testType;
00053 
00054   if (!testCase) {
00055        fprintf(stderr, "ERROR: Can't load test list !\n");
00056        return;
00057   }
00058   if (!(fdResFile = PR_Open(OJI_TEST_RESULTS, PR_CREATE_FILE | PR_WRONLY, PR_IRWXU))) {
00059     fprintf(stderr, "ERROR: Can't open test results file !\n");
00060   }
00061   for(i=0; testCase[i]; i++) {
00062     res = NULL;
00063     switch(testType = getTestType(testCase[i])) {
00064     case(JNI):
00065       res = runTest(testCase[i], OJI_JNI_TESTS);
00066       break;
00067     case(JM):
00068       res = runTest(testCase[i], OJI_JM_TESTS);
00069       break;
00070     case(TM):
00071       res = runTest(testCase[i], OJI_TM_TESTS);
00072       break;
00073     case(LCM):
00074       res = runTest(testCase[i], OJI_LCM_TESTS);
00075       break;
00076     case(JMPTI):
00077       res = runTest(testCase[i], OJI_JMPTI_TESTS);
00078       break;
00079     default:
00080       fprintf(stderr, "Can't determine test type (%s)\n", testCase[i]);
00081     }  
00082     if (res)
00083       registerRes(res, testCase[i]);
00084     else
00085       registerRes(TestResult::FAIL("Test execution failed"), testCase[i]);
00086   }
00087 
00088 
00089 }
00090 
00091 
00092 TestResult* OJITestLoader::runTest(const char* testCase, const char* libName) {
00093 
00094   PRLibrary* lib = PR_LoadLibrary(libName);
00095   OJI_TESTPROC testProc = NULL; 
00096 
00097   if (lib) {
00098        testProc = (OJI_TESTPROC)PR_FindSymbol(lib, testCase);
00099        if (testProc) {
00100               return testProc();
00101        } else {
00102               fprintf(stderr, "WARNING: Can't find %s method in %s library\n", testCase, libName);
00103        }
00104   } else {
00105        fprintf(stderr, "WARNING: Can't load %s library\n", libName);
00106   }
00107   return NULL;
00108 }
00109 
00110 
00111 void OJITestLoader::registerRes(TestResult* res, char* tc){
00112        char *outBuf = (char*)calloc(1, res->comment.Length() + PL_strlen(tc) + 100);
00113        
00114        sprintf(outBuf, "%s: %s (%s)\n", tc, res->status?"PASS":"FAILED", NS_LossyConvertUCS2toASCII(res->comment).get());
00115        if (fdResFile) {     
00116               printf("%s", outBuf);       
00117               if (PR_Write(fdResFile, outBuf, PL_strlen(outBuf)) < PL_strlen(outBuf))
00118                      fprintf(stderr, "WARNING: Can't write entire result message. Possibly not enough free disk space !\n");
00119        } else {
00120               printf("%s", outBuf);
00121        }
00122        free(outBuf);
00123 }
00124 
00125 
00126 OJITestLoader::~OJITestLoader() 
00127 {
00128   if(fdResFile)
00129     PR_Close(fdResFile);
00130 }
00131 
00132 
00133 char** OJITestLoader::loadTestList() {
00134        struct stat st;
00135        FILE *file = NULL;
00136        char *content;
00137        int nRead, nTests, count = 0;
00138        char *pos, *pos1;
00139        char **testList;
00140 
00141        if (stat(OJI_TESTS_LIST, &st) < 0) {
00142               fprintf(stderr, "ERROR: can't get stat from file %s\n", OJI_TESTS_LIST);
00143               return NULL;
00144        }
00145        content = (char*)calloc(1, st.st_size+1);
00146        if ((file = fopen(OJI_TESTS_LIST, "r")) == NULL) {
00147               fprintf(stderr, "ERROR: can't open file %s\n", OJI_TESTS_LIST);
00148               return NULL;
00149        }
00150        if ((nRead = fread(content, 1, st.st_size, file)) < st.st_size) {
00151               fprintf(stderr, "WARNING: can't read entire file in text mode (%d of %d) !\n", nRead, st.st_size);
00152               //return;
00153        }
00154        content[nRead] = 0;
00155        //printf("File content: %s\n", content);
00156        fclose(file);
00157 
00158        //allocate maximal possible size
00159        nTests = countChars(content, '\n') + 2;
00160        printf("nTests = %d\n", nTests);
00161        testList = (char**)calloc(sizeof(char*), nTests);
00162        testList[0] = 0;
00163        pos = content;
00164        while((pos1 = PL_strchr(pos, '\n'))) {
00165               *pos1 = 0;
00166               if(*pos && *pos != '#') {
00167                 //printf("First char: %c\n", *pos);
00168                      testList[count++] = PL_strdup(pos);
00169               }
00170               pos = pos1+1;
00171               if (!(*pos)) {
00172                      printf("Parser done: %d .. ", count);
00173                      testList[count] = 0;
00174                      printf("ok\n");
00175                      break;
00176               }
00177        }
00178        //If there is no \n after last line
00179        if (*pos && *pos != '#') {
00180               testList[count++] = PL_strdup(pos);
00181               testList[count] = 0;
00182        }
00183        //free(content);
00184        return testList;
00185        
00186 }
00187 
00188 
00189 int OJITestLoader::countChars(char* buf, char ch) {
00190        char *pos = buf;
00191        int count = 0;
00192 
00193        while((pos = PL_strchr(pos, ch))) { 
00194               pos++;
00195               count++;
00196        }
00197        return count;
00198 } 
00199 
00200 
00201 
00202 TEST_TYPE OJITestLoader::getTestType(char *tc) {
00203        char *buf = PL_strdup(tc);
00204        char *pos = PL_strchr(buf, '_');
00205        if (!pos)
00206               return (TEST_TYPE)-1;
00207        *pos = 0;
00208        for(int i=0; test_types[i]; i++) {
00209               if (!PL_strcmp(buf, test_types[i])) {
00210                      free(buf);
00211                      return (TEST_TYPE)i;
00212               }
00213        }
00214        free(buf);
00215        return (TEST_TYPE)-1;
00216 }
00217 
00218 NS_METHOD OJITestLoader::Create(nsISupports* outer, const nsIID& aIID, void* *aInstancePtr) {
00219        OJITestLoader *tl = new OJITestLoader();
00220        if (!aInstancePtr)
00221               return NS_ERROR_NULL_POINTER;
00222        *aInstancePtr = nsnull;
00223        if (aIID.Equals(kISupportsIID)) 
00224               *aInstancePtr  = (void*)(nsISupports*)tl;
00225        if (aIID.Equals(kIOJITestLoaderIID))
00226               *aInstancePtr = (void*)tl;
00227        if(!(*aInstancePtr))
00228               return NS_ERROR_NO_INTERFACE;
00229        return NS_OK;
00230 }