1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5 #include "util/testharness.h"
6
7 #include <string>
8 #include <stdlib.h>
9 #include <sys/stat.h>
10 #include <sys/types.h>
11
12 namespace leveldb {
13 namespace test {
14
15 namespace {
16 struct Test {
17 const char* base;
18 const char* name;
19 void (*func)();
20 };
21 std::vector<Test>* tests;
22 }
23
RegisterTest(const char * base,const char * name,void (* func)())24 bool RegisterTest(const char* base, const char* name, void (*func)()) {
25 if (tests == NULL) {
26 tests = new std::vector<Test>;
27 }
28 Test t;
29 t.base = base;
30 t.name = name;
31 t.func = func;
32 tests->push_back(t);
33 return true;
34 }
35
RunAllTests()36 int RunAllTests() {
37 const char* matcher = getenv("LEVELDB_TESTS");
38
39 int num = 0;
40 if (tests != NULL) {
41 for (size_t i = 0; i < tests->size(); i++) {
42 const Test& t = (*tests)[i];
43 if (matcher != NULL) {
44 std::string name = t.base;
45 name.push_back('.');
46 name.append(t.name);
47 if (strstr(name.c_str(), matcher) == NULL) {
48 continue;
49 }
50 }
51 fprintf(stderr, "==== Test %s.%s\n", t.base, t.name);
52 (*t.func)();
53 ++num;
54 }
55 }
56 fprintf(stderr, "==== PASSED %d tests\n", num);
57 return 0;
58 }
59
TmpDir()60 std::string TmpDir() {
61 std::string dir;
62 Status s = Env::Default()->GetTestDirectory(&dir);
63 ASSERT_TRUE(s.ok()) << s.ToString();
64 return dir;
65 }
66
RandomSeed()67 int RandomSeed() {
68 const char* env = getenv("TEST_RANDOM_SEED");
69 int result = (env != NULL ? atoi(env) : 301);
70 if (result <= 0) {
71 result = 301;
72 }
73 return result;
74 }
75
76 } // namespace test
77 } // namespace leveldb
78