Refactor common unit test code.

TEST=compiler-unittests
BUG=v8:3489
LOG=n
R=svenpanne@chromium.org

Review URL: https://codereview.chromium.org/540823002

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23673 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
This commit is contained in:
bmeurer@chromium.org 2014-09-04 08:44:03 +00:00
parent 33e56ef889
commit 31718c550e
17 changed files with 297 additions and 175 deletions

View File

@ -1,4 +0,0 @@
# TODO(bmeurer): Temporary work-around, will be fixed soonish.
include_rules = [
"+include/libplatform/libplatform.h"
]

View File

@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "src/compiler/change-lowering.h"
#include "src/compiler/compiler-test-utils.h"
#include "src/compiler/graph-unittest.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/node-properties-inl.h"

View File

@ -3,8 +3,8 @@
// found in the LICENSE file.
#include "src/compiler/common-operator.h"
#include "src/compiler/compiler-unittests.h"
#include "src/compiler/operator-properties-inl.h"
#include "src/test/test-utils.h"
namespace v8 {
namespace internal {
@ -12,7 +12,7 @@ namespace compiler {
namespace {
class CommonOperatorTest : public CompilerTest {
class CommonOperatorTest : public TestWithZone {
public:
CommonOperatorTest() : common_(zone()) {}
virtual ~CommonOperatorTest() {}

View File

@ -2,20 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_COMPILER_UNITTESTS_COMPILER_UNITTESTS_H_
#define V8_COMPILER_UNITTESTS_COMPILER_UNITTESTS_H_
#ifndef V8_COMPILER_COMPILER_TEST_UTILS_H_
#define V8_COMPILER_COMPILER_TEST_UTILS_H_
#include "include/v8.h"
#include "src/zone.h"
#include "testing/gtest-support.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace v8 {
namespace internal {
// Forward declarations.
class Factory;
namespace compiler {
// The TARGET_TEST(Case, Name) macro works just like
@ -57,34 +50,8 @@ namespace compiler {
#define TARGET_TYPED_TEST(Case, Name) TYPED_TEST(Case, DISABLED_##Name)
#endif
class CompilerTest : public ::testing::Test {
public:
CompilerTest();
virtual ~CompilerTest();
Factory* factory() const;
Isolate* isolate() const { return reinterpret_cast<Isolate*>(isolate_); }
Zone* zone() { return &zone_; }
static void SetUpTestCase();
static void TearDownTestCase();
private:
static v8::Isolate* isolate_;
v8::Isolate::Scope isolate_scope_;
v8::HandleScope handle_scope_;
v8::Context::Scope context_scope_;
Zone zone_;
};
template <typename T>
class CompilerTestWithParam : public CompilerTest,
public ::testing::WithParamInterface<T> {};
} // namespace compiler
} // namespace internal
} // namespace v8
#endif // V8_COMPILER_UNITTESTS_COMPILER_UNITTESTS_H_
#endif // V8_COMPILER_COMPILER_TEST_UTILS_H_

View File

@ -1,90 +0,0 @@
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "include/libplatform/libplatform.h"
#include "src/compiler/compiler-unittests.h"
#include "src/isolate-inl.h"
#include "testing/gmock/include/gmock/gmock.h"
using testing::IsNull;
using testing::NotNull;
namespace v8 {
namespace internal {
namespace compiler {
// static
v8::Isolate* CompilerTest::isolate_ = NULL;
CompilerTest::CompilerTest()
: isolate_scope_(isolate_),
handle_scope_(isolate_),
context_scope_(v8::Context::New(isolate_)),
zone_(isolate()) {}
CompilerTest::~CompilerTest() {}
Factory* CompilerTest::factory() const { return isolate()->factory(); }
// static
void CompilerTest::SetUpTestCase() {
Test::SetUpTestCase();
EXPECT_THAT(isolate_, IsNull());
isolate_ = v8::Isolate::New();
ASSERT_THAT(isolate_, NotNull());
}
// static
void CompilerTest::TearDownTestCase() {
ASSERT_THAT(isolate_, NotNull());
isolate_->Dispose();
isolate_ = NULL;
Test::TearDownTestCase();
}
} // namespace compiler
} // namespace internal
} // namespace v8
namespace {
class CompilerTestEnvironment FINAL : public ::testing::Environment {
public:
CompilerTestEnvironment() : platform_(NULL) {}
~CompilerTestEnvironment() {}
virtual void SetUp() OVERRIDE {
EXPECT_THAT(platform_, IsNull());
platform_ = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform_);
ASSERT_TRUE(v8::V8::Initialize());
}
virtual void TearDown() OVERRIDE {
ASSERT_THAT(platform_, NotNull());
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete platform_;
platform_ = NULL;
}
private:
v8::Platform* platform_;
};
} // namespace
int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
testing::AddGlobalTestEnvironment(new CompilerTestEnvironment);
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
return RUN_ALL_TESTS();
}

View File

@ -12,9 +12,7 @@
'target_name': 'compiler-unittests',
'type': 'executable',
'dependencies': [
'../../testing/gmock.gyp:gmock',
'../../testing/gtest.gyp:gtest',
'../../tools/gyp/v8.gyp:v8_libplatform',
'../test/test.gyp:run-all-unittests',
],
'include_dirs': [
'../..',
@ -22,8 +20,7 @@
'sources': [ ### gcmole(all) ###
'change-lowering-unittest.cc',
'common-operator-unittest.cc',
'compiler-unittests.cc',
'compiler-unittests.h',
'compiler-test-utils.h',
'graph-unittest.cc',
'graph-unittest.h',
'instruction-selector-unittest.cc',
@ -53,29 +50,6 @@
'x64/instruction-selector-x64-unittest.cc',
],
}],
['component=="shared_library"', {
# compiler-unittests can't be built against a shared library, so we
# need to depend on the underlying static target in that case.
'conditions': [
['v8_use_snapshot=="true"', {
'dependencies': ['../../tools/gyp/v8.gyp:v8_snapshot'],
},
{
'dependencies': [
'../../tools/gyp/v8.gyp:v8_nosnapshot',
],
}],
],
}, {
'dependencies': ['../../tools/gyp/v8.gyp:v8'],
}],
['os_posix == 1', {
# TODO(svenpanne): This is a temporary work-around to fix the warnings
# that show up because we use -std=gnu++0x instead of -std=c++11.
'cflags!': [
'-pedantic',
],
}],
],
},
],

View File

@ -6,9 +6,9 @@
#define V8_COMPILER_GRAPH_UNITTEST_H_
#include "src/compiler/common-operator.h"
#include "src/compiler/compiler-unittests.h"
#include "src/compiler/graph.h"
#include "src/compiler/machine-operator.h"
#include "src/test/test-utils.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace v8 {
@ -24,7 +24,7 @@ namespace compiler {
using ::testing::Matcher;
class GraphTest : public CompilerTest {
class GraphTest : public TestWithContext, public TestWithZone {
public:
explicit GraphTest(int parameters = 1);
virtual ~GraphTest();

View File

@ -4,6 +4,7 @@
#include "src/compiler/instruction-selector-unittest.h"
#include "src/compiler/compiler-test-utils.h"
#include "src/flags.h"
namespace v8 {
@ -20,6 +21,9 @@ typedef RawMachineAssembler::Label MLabel;
InstructionSelectorTest::InstructionSelectorTest() : rng_(FLAG_random_seed) {}
InstructionSelectorTest::~InstructionSelectorTest() {}
InstructionSelectorTest::Stream InstructionSelectorTest::StreamBuilder::Build(
InstructionSelector::Features features,
InstructionSelectorTest::StreamBuilderMode mode) {

View File

@ -9,18 +9,18 @@
#include <set>
#include "src/base/utils/random-number-generator.h"
#include "src/compiler/compiler-unittests.h"
#include "src/compiler/instruction-selector.h"
#include "src/compiler/raw-machine-assembler.h"
#include "src/test/test-utils.h"
namespace v8 {
namespace internal {
namespace compiler {
class InstructionSelectorTest : public CompilerTest {
class InstructionSelectorTest : public TestWithContext, public TestWithZone {
public:
InstructionSelectorTest();
virtual ~InstructionSelectorTest() {}
virtual ~InstructionSelectorTest();
base::RandomNumberGenerator* rng() { return &rng_; }

View File

@ -16,7 +16,6 @@ class MachineOperatorReducerTest : public GraphTest {
public:
explicit MachineOperatorReducerTest(int num_parameters = 2)
: GraphTest(num_parameters), machine_(zone()) {}
virtual ~MachineOperatorReducerTest() {}
protected:
Reduction Reduce(Node* node) {

View File

@ -2,31 +2,32 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/compiler/compiler-unittests.h"
#include "src/compiler/machine-operator.h"
#include "src/compiler/operator-properties-inl.h"
#include "testing/gmock/include/gmock/gmock.h"
using testing::IsNull;
#include "src/test/test-utils.h"
namespace v8 {
namespace internal {
namespace compiler {
class MachineOperatorCommonTest : public CompilerTestWithParam<MachineType> {
class MachineOperatorCommonTest
: public TestWithZone,
public ::testing::WithParamInterface<MachineType> {
public:
MachineOperatorCommonTest() : machine_(NULL) {}
virtual ~MachineOperatorCommonTest() { EXPECT_THAT(machine_, IsNull()); }
virtual ~MachineOperatorCommonTest() { EXPECT_EQ(NULL, machine_); }
virtual void SetUp() OVERRIDE {
CompilerTestWithParam<MachineType>::SetUp();
TestWithZone::SetUp();
EXPECT_EQ(NULL, machine_);
machine_ = new MachineOperatorBuilder(zone(), GetParam());
}
virtual void TearDown() OVERRIDE {
ASSERT_TRUE(machine_ != NULL);
delete machine_;
machine_ = NULL;
CompilerTestWithParam<MachineType>::TearDown();
TestWithZone::TearDown();
}
protected:

3
src/test/DEPS Normal file
View File

@ -0,0 +1,3 @@
include_rules = [
"+include/libplatform/libplatform.h"
]

View File

@ -0,0 +1,45 @@
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
#include "src/base/compiler-specific.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace {
class DefaultPlatformEnvironment FINAL : public ::testing::Environment {
public:
DefaultPlatformEnvironment() : platform_(NULL) {}
~DefaultPlatformEnvironment() {}
virtual void SetUp() OVERRIDE {
EXPECT_EQ(NULL, platform_);
platform_ = v8::platform::CreateDefaultPlatform();
ASSERT_TRUE(platform_ != NULL);
v8::V8::InitializePlatform(platform_);
ASSERT_TRUE(v8::V8::Initialize());
}
virtual void TearDown() OVERRIDE {
ASSERT_TRUE(platform_ != NULL);
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete platform_;
platform_ = NULL;
}
private:
v8::Platform* platform_;
};
} // namespace
int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
testing::AddGlobalTestEnvironment(new DefaultPlatformEnvironment);
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
return RUN_ALL_TESTS();
}

58
src/test/test-utils.cc Normal file
View File

@ -0,0 +1,58 @@
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/test/test-utils.h"
#include "src/isolate-inl.h"
namespace v8 {
// static
Isolate* TestWithIsolate::isolate_ = NULL;
TestWithIsolate::TestWithIsolate()
: isolate_scope_(isolate()), handle_scope_(isolate()) {}
TestWithIsolate::~TestWithIsolate() {}
// static
void TestWithIsolate::SetUpTestCase() {
Test::SetUpTestCase();
EXPECT_EQ(NULL, isolate_);
isolate_ = v8::Isolate::New();
EXPECT_TRUE(isolate_ != NULL);
}
// static
void TestWithIsolate::TearDownTestCase() {
ASSERT_TRUE(isolate_ != NULL);
isolate_->Dispose();
isolate_ = NULL;
Test::TearDownTestCase();
}
TestWithContext::TestWithContext()
: context_(Context::New(isolate())), context_scope_(context_) {}
TestWithContext::~TestWithContext() {}
namespace internal {
TestWithIsolate::~TestWithIsolate() {}
Factory* TestWithIsolate::factory() const { return isolate()->factory(); }
TestWithZone::~TestWithZone() {}
} // namespace internal
} // namespace v8

86
src/test/test-utils.h Normal file
View File

@ -0,0 +1,86 @@
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_TEST_TEST_UTILS_H_
#define V8_TEST_TEST_UTILS_H_
#include "include/v8.h"
#include "src/base/macros.h"
#include "src/zone.h"
#include "testing/gtest-support.h"
namespace v8 {
class TestWithIsolate : public ::testing::Test {
public:
TestWithIsolate();
virtual ~TestWithIsolate();
Isolate* isolate() const { return isolate_; }
static void SetUpTestCase();
static void TearDownTestCase();
private:
static Isolate* isolate_;
Isolate::Scope isolate_scope_;
HandleScope handle_scope_;
DISALLOW_COPY_AND_ASSIGN(TestWithIsolate);
};
class TestWithContext : public virtual TestWithIsolate {
public:
TestWithContext();
virtual ~TestWithContext();
const Local<Context>& context() const { return context_; }
private:
Local<Context> context_;
Context::Scope context_scope_;
DISALLOW_COPY_AND_ASSIGN(TestWithContext);
};
namespace internal {
// Forward declarations.
class Factory;
class TestWithIsolate : public virtual ::v8::TestWithIsolate {
public:
TestWithIsolate() {}
virtual ~TestWithIsolate();
Factory* factory() const;
Isolate* isolate() const {
return reinterpret_cast<Isolate*>(::v8::TestWithIsolate::isolate());
}
private:
DISALLOW_COPY_AND_ASSIGN(TestWithIsolate);
};
class TestWithZone : public TestWithIsolate {
public:
TestWithZone() : zone_(isolate()) {}
virtual ~TestWithZone();
Zone* zone() { return &zone_; }
private:
Zone zone_;
DISALLOW_COPY_AND_ASSIGN(TestWithZone);
};
} // namespace internal
} // namespace v8
#endif // V8_TEST_TEST_UTILS_H_

71
src/test/test.gyp Normal file
View File

@ -0,0 +1,71 @@
# Copyright 2014 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
{
'variables': {
'v8_code': 1,
},
'includes': ['../../build/toolchain.gypi', '../../build/features.gypi'],
'targets': [
{
'target_name': 'run-all-unittests',
'type': 'static_library',
'variables': {
'optimize': 'max',
},
'dependencies': [
'../../testing/gmock.gyp:gmock',
'../../testing/gtest.gyp:gtest',
'../../tools/gyp/v8.gyp:v8_libplatform',
],
'include_dirs': [
'../..',
],
'sources': [ ### gcmole(all) ###
'run-all-unittests.cc',
'test-utils.h',
'test-utils.cc',
],
'export_dependent_settings': [
'../../testing/gmock.gyp:gmock',
'../../testing/gtest.gyp:gtest',
],
'conditions': [
['component=="shared_library"', {
# compiler-unittests can't be built against a shared library, so we
# need to depend on the underlying static target in that case.
'conditions': [
['v8_use_snapshot=="true"', {
'dependencies': ['../../tools/gyp/v8.gyp:v8_snapshot'],
},
{
'dependencies': [
'../../tools/gyp/v8.gyp:v8_nosnapshot',
],
}],
],
}, {
'dependencies': ['../../tools/gyp/v8.gyp:v8'],
}],
['os_posix == 1', {
# TODO(svenpanne): This is a temporary work-around to fix the warnings
# that show up because we use -std=gnu++0x instead of -std=c++11.
'cflags!': [
'-pedantic',
],
'direct_dependent_settings': {
'cflags!': [
'-pedantic',
],
},
}],
['want_separate_host_toolset==1', {
'toolsets': ['host', 'target'],
}, {
'toolsets': ['target'],
}],
],
},
],
}

View File

@ -47,6 +47,13 @@
'export_dependent_settings': [
'gtest.gyp:gtest',
],
'conditions': [
['want_separate_host_toolset==1', {
'toolsets': ['host', 'target'],
}, {
'toolsets': ['target'],
}],
],
},
{
'target_name': 'gmock_main',