81 lines
1.7 KiB

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