1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be 3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors. 4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include "util/testharness.h" 6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 77e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com#include <string> 87e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com#include <stdlib.h> 9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <sys/stat.h> 10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <sys/types.h> 11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb { 13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace test { 14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 15179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace { 16179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstruct Test { 17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const char* base; 18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const char* name; 19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org void (*func)(); 20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org}; 21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstd::vector<Test>* tests; 22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgbool RegisterTest(const char* base, const char* name, void (*func)()) { 25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org if (tests == NULL) { 26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org tests = new std::vector<Test>; 27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org Test t; 29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org t.base = base; 30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org t.name = name; 31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org t.func = func; 32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org tests->push_back(t); 33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return true; 34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgint RunAllTests() { 377e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com const char* matcher = getenv("LEVELDB_TESTS"); 387e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com 39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org int num = 0; 40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org if (tests != NULL) { 414935bf087b28aa308c0a820720b85ef695e236aedgrogan@chromium.org for (size_t i = 0; i < tests->size(); i++) { 42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const Test& t = (*tests)[i]; 437e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com if (matcher != NULL) { 447e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com std::string name = t.base; 457e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com name.push_back('.'); 467e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com name.append(t.name); 477e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com if (strstr(name.c_str(), matcher) == NULL) { 487e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com continue; 497e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com } 507e50a01f8a2820ed7a50c673b5affe0131560ff5gabor@google.com } 51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org fprintf(stderr, "==== Test %s.%s\n", t.base, t.name); 52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org (*t.func)(); 53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ++num; 54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org fprintf(stderr, "==== PASSED %d tests\n", num); 57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return 0; 58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgstd::string TmpDir() { 61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org std::string dir; 62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org Status s = Env::Default()->GetTestDirectory(&dir); 63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org ASSERT_TRUE(s.ok()) << s.ToString(); 64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return dir; 65179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 66179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 67179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgint RandomSeed() { 68179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org const char* env = getenv("TEST_RANDOM_SEED"); 69179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org int result = (env != NULL ? atoi(env) : 301); 70179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org if (result <= 0) { 71179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org result = 301; 72179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org } 73179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org return result; 74179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org} 75179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org 7645b9940be332834440bd5299419f396e38085ebehans@chromium.org} // namespace test 7745b9940be332834440bd5299419f396e38085ebehans@chromium.org} // namespace leveldb 78