Refactor common unit test code.
authorbmeurer@chromium.org <bmeurer@chromium.org>
Thu, 4 Sep 2014 08:44:03 +0000 (08:44 +0000)
committerbmeurer@chromium.org <bmeurer@chromium.org>
Thu, 4 Sep 2014 08:44:03 +0000 (08:44 +0000)
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

17 files changed:
src/compiler/DEPS [deleted file]
src/compiler/change-lowering-unittest.cc
src/compiler/common-operator-unittest.cc
src/compiler/compiler-test-utils.h [moved from src/compiler/compiler-unittests.h with 63% similarity]
src/compiler/compiler-unittests.cc [deleted file]
src/compiler/compiler.gyp
src/compiler/graph-unittest.h
src/compiler/instruction-selector-unittest.cc
src/compiler/instruction-selector-unittest.h
src/compiler/machine-operator-reducer-unittest.cc
src/compiler/machine-operator-unittest.cc
src/test/DEPS [new file with mode: 0644]
src/test/run-all-unittests.cc [new file with mode: 0644]
src/test/test-utils.cc [new file with mode: 0644]
src/test/test-utils.h [new file with mode: 0644]
src/test/test.gyp [new file with mode: 0644]
testing/gmock.gyp

diff --git a/src/compiler/DEPS b/src/compiler/DEPS
deleted file mode 100644 (file)
index a9d9bb0..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-# TODO(bmeurer): Temporary work-around, will be fixed soonish.
-include_rules = [
-  "+include/libplatform/libplatform.h"
-]
index c624715..4205684 100644 (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"
index 109873b..2738e08 100644 (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() {}
similarity index 63%
rename from src/compiler/compiler-unittests.h
rename to src/compiler/compiler-test-utils.h
index dd4234b..437abd6 100644 (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_
diff --git a/src/compiler/compiler-unittests.cc b/src/compiler/compiler-unittests.cc
deleted file mode 100644 (file)
index b3cbb86..0000000
+++ /dev/null
@@ -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();
-}
index 4713f30..8935c5f 100644 (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',
             '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',
-          ],
-        }],
       ],
     },
   ],
index 96813d0..9eb1553 100644 (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();
index 7b775f8..cc67567 100644 (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) {
index 476733c..c236853 100644 (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_; }
 
index df06110..ab95fb8 100644 (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) {
index e7b9337..5f46be1 100644 (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:
diff --git a/src/test/DEPS b/src/test/DEPS
new file mode 100644 (file)
index 0000000..13855ec
--- /dev/null
@@ -0,0 +1,3 @@
+include_rules = [
+  "+include/libplatform/libplatform.h"
+]
diff --git a/src/test/run-all-unittests.cc b/src/test/run-all-unittests.cc
new file mode 100644 (file)
index 0000000..8c361dd
--- /dev/null
@@ -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();
+}
diff --git a/src/test/test-utils.cc b/src/test/test-utils.cc
new file mode 100644 (file)
index 0000000..1041465
--- /dev/null
@@ -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
diff --git a/src/test/test-utils.h b/src/test/test-utils.h
new file mode 100644 (file)
index 0000000..05d1ea6
--- /dev/null
@@ -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_
diff --git a/src/test/test.gyp b/src/test/test.gyp
new file mode 100644 (file)
index 0000000..f4c6a5e
--- /dev/null
@@ -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'],
+        }],
+      ],
+    },
+  ],
+}
index f423bd0..ba43861 100644 (file)
       'export_dependent_settings': [
         'gtest.gyp:gtest',
       ],
+      'conditions': [
+        ['want_separate_host_toolset==1', {
+          'toolsets': ['host', 'target'],
+        }, {
+          'toolsets': ['target'],
+        }],
+      ],
     },
     {
       'target_name': 'gmock_main',