2013-10-16 21:59:46 +00:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2013-10-05 05:32:05 +00:00
|
|
|
#pragma once
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sstream>
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/slice.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "util/random.h"
|
2013-12-07 01:11:09 +00:00
|
|
|
#include "util/stack_trace.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2011-03-18 22:37:00 +00:00
|
|
|
namespace test {
|
|
|
|
|
2011-08-16 01:21:01 +00:00
|
|
|
// Run some of the tests registered by the TEST() macro. If the
|
2013-10-05 05:32:05 +00:00
|
|
|
// environment variable "ROCKSDB_TESTS" is not set, runs all tests.
|
2011-08-16 01:21:01 +00:00
|
|
|
// Otherwise, runs only the tests whose name contains the value of
|
2013-10-05 05:32:05 +00:00
|
|
|
// "ROCKSDB_TESTS" as a substring. E.g., suppose the tests are:
|
2011-08-16 01:21:01 +00:00
|
|
|
// TEST(Foo, Hello) { ... }
|
|
|
|
// TEST(Foo, World) { ... }
|
2013-10-05 05:32:05 +00:00
|
|
|
// ROCKSDB_TESTS=Hello will run the first test
|
|
|
|
// ROCKSDB_TESTS=o will run both tests
|
|
|
|
// ROCKSDB_TESTS=Junk will run no tests
|
2011-08-16 01:21:01 +00:00
|
|
|
//
|
2011-03-18 22:37:00 +00:00
|
|
|
// Returns 0 if all tests pass.
|
|
|
|
// Dies or returns a non-zero value if some test fails.
|
|
|
|
extern int RunAllTests();
|
|
|
|
|
|
|
|
// Return the directory to use for temporary storage.
|
|
|
|
extern std::string TmpDir();
|
|
|
|
|
|
|
|
// Return a randomization seed for this run. Typically returns the
|
|
|
|
// same number on repeated invocations of this binary, but automated
|
|
|
|
// runs may be able to vary the seed.
|
|
|
|
extern int RandomSeed();
|
|
|
|
|
|
|
|
// An instance of Tester is allocated to hold temporary state during
|
|
|
|
// the execution of an assertion.
|
|
|
|
class Tester {
|
|
|
|
private:
|
|
|
|
bool ok_;
|
|
|
|
const char* fname_;
|
|
|
|
int line_;
|
|
|
|
std::stringstream ss_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Tester(const char* f, int l)
|
|
|
|
: ok_(true), fname_(f), line_(l) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~Tester() {
|
|
|
|
if (!ok_) {
|
|
|
|
fprintf(stderr, "%s:%d:%s\n", fname_, line_, ss_.str().c_str());
|
2013-12-07 01:11:09 +00:00
|
|
|
PrintStack(2);
|
2011-03-18 22:37:00 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Tester& Is(bool b, const char* msg) {
|
|
|
|
if (!b) {
|
|
|
|
ss_ << " Assertion failure " << msg;
|
|
|
|
ok_ = false;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Tester& IsOk(const Status& s) {
|
|
|
|
if (!s.ok()) {
|
|
|
|
ss_ << " " << s.ToString();
|
|
|
|
ok_ = false;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define BINARY_OP(name,op) \
|
|
|
|
template <class X, class Y> \
|
|
|
|
Tester& name(const X& x, const Y& y) { \
|
|
|
|
if (! (x op y)) { \
|
|
|
|
ss_ << " failed: " << x << (" " #op " ") << y; \
|
|
|
|
ok_ = false; \
|
|
|
|
} \
|
|
|
|
return *this; \
|
|
|
|
}
|
|
|
|
|
|
|
|
BINARY_OP(IsEq, ==)
|
|
|
|
BINARY_OP(IsNe, !=)
|
|
|
|
BINARY_OP(IsGe, >=)
|
|
|
|
BINARY_OP(IsGt, >)
|
|
|
|
BINARY_OP(IsLe, <=)
|
|
|
|
BINARY_OP(IsLt, <)
|
|
|
|
#undef BINARY_OP
|
|
|
|
|
|
|
|
// Attach the specified value to the error message if an error has occurred
|
|
|
|
template <class V>
|
|
|
|
Tester& operator<<(const V& value) {
|
|
|
|
if (!ok_) {
|
|
|
|
ss_ << " " << value;
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
#define ASSERT_TRUE(c) ::rocksdb::test::Tester(__FILE__, __LINE__).Is((c), #c)
|
|
|
|
#define ASSERT_OK(s) ::rocksdb::test::Tester(__FILE__, __LINE__).IsOk((s))
|
|
|
|
#define ASSERT_EQ(a,b) ::rocksdb::test::Tester(__FILE__, __LINE__).IsEq((a),(b))
|
|
|
|
#define ASSERT_NE(a,b) ::rocksdb::test::Tester(__FILE__, __LINE__).IsNe((a),(b))
|
|
|
|
#define ASSERT_GE(a,b) ::rocksdb::test::Tester(__FILE__, __LINE__).IsGe((a),(b))
|
|
|
|
#define ASSERT_GT(a,b) ::rocksdb::test::Tester(__FILE__, __LINE__).IsGt((a),(b))
|
|
|
|
#define ASSERT_LE(a,b) ::rocksdb::test::Tester(__FILE__, __LINE__).IsLe((a),(b))
|
|
|
|
#define ASSERT_LT(a,b) ::rocksdb::test::Tester(__FILE__, __LINE__).IsLt((a),(b))
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
#define TCONCAT(a,b) TCONCAT1(a,b)
|
|
|
|
#define TCONCAT1(a,b) a##b
|
|
|
|
|
|
|
|
#define TEST(base,name) \
|
|
|
|
class TCONCAT(_Test_,name) : public base { \
|
|
|
|
public: \
|
|
|
|
void _Run(); \
|
|
|
|
static void _RunIt() { \
|
|
|
|
TCONCAT(_Test_,name) t; \
|
|
|
|
t._Run(); \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
bool TCONCAT(_Test_ignored_,name) = \
|
2013-10-04 04:49:15 +00:00
|
|
|
::rocksdb::test::RegisterTest(#base, #name, &TCONCAT(_Test_,name)::_RunIt); \
|
2011-03-18 22:37:00 +00:00
|
|
|
void TCONCAT(_Test_,name)::_Run()
|
|
|
|
|
|
|
|
// Register the specified test. Typically not used directly, but
|
|
|
|
// invoked via the macro expansion of TEST.
|
|
|
|
extern bool RegisterTest(const char* base, const char* name, void (*func)());
|
|
|
|
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace test
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|