CLOSED TREE: TraceMonkey merge head. (a=blockers)
[mozilla-central.git] / tools / testy / Testy.cpp
blobb87ab7f2a1a5377877647764a3d89f6dccf5060c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include "prlink.h"
6 #include "prio.h"
7 #include "plstr.h"
9 #include "TestySupport.h"
11 #define LOG(args) printf args
13 #ifdef XP_WIN
14 static const char kPathSep = '\\';
15 #else
16 static const char kPathSep = '/';
17 #endif
18 static const char kTestsDirectory[] = "tch_tests";
20 typedef int (* EntryPoint)(void);
21 static const char kInitMethod[] = "Testy_Init";
22 static const char kTestMethod[] = "Testy_RunTest";
23 static const char kShutdownMethod[] = "Testy_Shutdown";
25 static void
26 ProcessModule(const char *modulesDir, const char *fileName)
28 int dLen = strlen(modulesDir);
29 int fLen = strlen(fileName);
31 char *buf = (char *) malloc(dLen + 1 + fLen + 1);
32 memcpy(buf, modulesDir, dLen);
33 buf[dLen] = kPathSep;
34 memcpy(buf + dLen + 1, fileName, fLen);
35 buf[dLen + 1 + fLen] = '\0';
37 PRLibrary *lib = PR_LoadLibrary(buf);
38 if (lib) {
39 EntryPoint initFunc = (EntryPoint) PR_FindFunctionSymbol(lib, kInitMethod);
40 EntryPoint testFunc = (EntryPoint) PR_FindFunctionSymbol(lib, kTestMethod);
41 EntryPoint shutdownFunc = (EntryPoint) PR_FindFunctionSymbol(lib, kShutdownMethod);
43 if (testFunc) {
44 int rv = 0;
45 if (initFunc)
46 rv = initFunc();
47 // don't run test case if init fails.
48 if (rv == 0)
49 testFunc();
50 if (shutdownFunc)
51 shutdownFunc();
53 PR_UnloadLibrary(lib);
56 free(buf);
59 static void
60 RunTests(const char *exePath)
62 if (!(exePath && *exePath))
63 return;
66 // load test modules
68 char *p = strrchr(exePath, kPathSep);
69 if (p == NULL) {
70 LOG(("unexpected exe path\n"));
71 return;
74 int baseLen = p - exePath;
75 int finalLen = baseLen + 1 + sizeof(kTestsDirectory);
77 // build full path to ipc modules
78 char *modulesDir = (char*) malloc(finalLen);
79 memcpy(modulesDir, exePath, baseLen);
80 modulesDir[baseLen] = kPathSep;
81 memcpy(modulesDir + baseLen + 1, kTestsDirectory, sizeof(kTestsDirectory));
83 LOG(("loading libraries in %s\n", modulesDir));
84 //
85 // scan directory for IPC modules
87 PRDir *dir = PR_OpenDir(modulesDir);
88 if (dir) {
89 PRDirEntry *ent;
90 while ((ent = PR_ReadDir(dir, PR_SKIP_BOTH)) != NULL) {
91 //
92 // locate extension, and check if dynamic library
94 char *p = strrchr(ent->name, '.');
95 if (p && PL_strcasecmp(p, MOZ_DLL_SUFFIX) == 0)
96 ProcessModule(modulesDir, ent->name);
98 PR_CloseDir(dir);
101 free(modulesDir);
104 int main(int argc, char **argv)
106 Testy_LogInit("tch.log");
108 RunTests(argv[0]);
110 Testy_LogShutdown();
111 return 0;