testharness.cc
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 <stdlib.h> 8 #include <sys/stat.h> 9 #include <sys/types.h> 10 11 #include <string> 12 #include <vector> 13 14 #include "leveldb/env.h" 15 16 namespace leveldb { 17 namespace test { 18 19 namespace { 20 struct Test { 21 const char* base; 22 const char* name; 23 void (*func)(); 24 }; 25 std::vector<Test>* tests; 26 } // namespace 27 28 bool RegisterTest(const char* base, const char* name, void (*func)()) { 29 if (tests == nullptr) { 30 tests = new std::vector<Test>; 31 } 32 Test t; 33 t.base = base; 34 t.name = name; 35 t.func = func; 36 tests->push_back(t); 37 return true; 38 } 39 40 int RunAllTests() { 41 const char* matcher = getenv("LEVELDB_TESTS"); 42 43 int num = 0; 44 if (tests != nullptr) { 45 for (size_t i = 0; i < tests->size(); i++) { 46 const Test& t = (*tests)[i]; 47 if (matcher != nullptr) { 48 std::string name = t.base; 49 name.push_back('.'); 50 name.append(t.name); 51 if (strstr(name.c_str(), matcher) == nullptr) { 52 continue; 53 } 54 } 55 fprintf(stderr, "==== Test %s.%s\n", t.base, t.name); 56 (*t.func)(); 57 ++num; 58 } 59 } 60 fprintf(stderr, "==== PASSED %d tests\n", num); 61 return 0; 62 } 63 64 std::string TmpDir() { 65 std::string dir; 66 Status s = Env::Default()->GetTestDirectory(&dir); 67 ASSERT_TRUE(s.ok()) << s.ToString(); 68 return dir; 69 } 70 71 int RandomSeed() { 72 const char* env = getenv("TEST_RANDOM_SEED"); 73 int result = (env != nullptr ? atoi(env) : 301); 74 if (result <= 0) { 75 result = 301; 76 } 77 return result; 78 } 79 80 } // namespace test 81 } // namespace leveldb