8165451: Convert WorkerDataArray_test to GTest
authorbmoloden
Wed, 26 Oct 2016 17:29:47 +0300
changeset 42058 46e3b28fda3b
parent 42057 6a5b8ebcd3f2
child 42060 a915170794e7
8165451: Convert WorkerDataArray_test to GTest Reviewed-by: jwilhelm
hotspot/src/share/vm/gc/g1/workerDataArray.cpp
hotspot/src/share/vm/utilities/internalVMTests.cpp
hotspot/test/native/gc/g1/test_workerDataArray.cpp
--- a/hotspot/src/share/vm/gc/g1/workerDataArray.cpp	Wed Oct 26 14:36:05 2016 +0200
+++ b/hotspot/src/share/vm/gc/g1/workerDataArray.cpp	Wed Oct 26 17:29:47 2016 +0300
@@ -79,126 +79,3 @@
   }
   out->cr();
 }
-
-#ifndef PRODUCT
-
-#include "memory/resourceArea.hpp"
-
-void WorkerDataArray_test_verify_string(const char* expected_string, const char* actual_string) {
-  const size_t expected_len = strlen(expected_string);
-
-  assert(expected_len == strlen(actual_string),
-      "Wrong string length, expected " SIZE_FORMAT " but got " SIZE_FORMAT "(Expected '%s' but got: '%s')",
-      expected_len, strlen(actual_string), expected_string, actual_string);
-
-  // Can't use strncmp here because floating point values use different decimal points for different locales.
-  // Allow strings to differ in "." vs. "," only. This should still catch most errors.
-  for (size_t i = 0; i < expected_len; i++) {
-    char e = expected_string[i];
-    char a = actual_string[i];
-    if (e != a) {
-      if ((e == '.' || e == ',') && (a == '.' || a == ',')) {
-        // Most likely just a difference in locale
-      } else {
-        assert(false, "Expected '%s' but got: '%s'", expected_string, actual_string);
-      }
-    }
-  }
-}
-
-void WorkerDataArray_test_verify_array(WorkerDataArray<size_t>& array, size_t expected_sum, double expected_avg, const char* expected_summary, const char* exected_details) {
-  const double epsilon = 0.0001;
-  assert(array.sum() == expected_sum, "Wrong sum, expected: " SIZE_FORMAT " but got: " SIZE_FORMAT, expected_sum, array.sum());
-  assert(fabs(array.average() - expected_avg) < epsilon, "Wrong average, expected: %f but got: %f", expected_avg, array.average());
-
-  ResourceMark rm;
-  stringStream out;
-  array.print_summary_on(&out);
-  WorkerDataArray_test_verify_string(expected_summary, out.as_string());
-  out.reset();
-  array.print_details_on(&out);
-  WorkerDataArray_test_verify_string(exected_details, out.as_string());
-}
-
-void WorkerDataArray_test_verify_array(WorkerDataArray<double>& array, double expected_sum, double expected_avg, const char* expected_summary, const char* exected_details) {
-  const double epsilon = 0.0001;
-  assert(fabs(array.sum() - expected_sum) < epsilon, "Wrong sum, expected: %f but got: %f", expected_sum, array.sum());
-  assert(fabs(array.average() - expected_avg) < epsilon, "Wrong average, expected: %f but got: %f", expected_avg, array.average());
-
-  ResourceMark rm;
-  stringStream out;
-  array.print_summary_on(&out);
-  WorkerDataArray_test_verify_string(expected_summary, out.as_string());
-  out.reset();
-  array.print_details_on(&out);
-  WorkerDataArray_test_verify_string(exected_details, out.as_string());
-}
-
-void WorkerDataArray_test_basic() {
-  WorkerDataArray<size_t> array(3, "Test array");
-  array.set(0, 5);
-  array.set(1, 3);
-  array.set(2, 7);
-
-  WorkerDataArray_test_verify_array(array, 15, 5.0,
-      "Test array                Min: 3, Avg:  5.0, Max: 7, Diff: 4, Sum: 15, Workers: 3\n",
-      "                           5  3  7\n" );
-}
-
-void WorkerDataArray_test_add() {
-  WorkerDataArray<size_t> array(3, "Test array");
-  array.set(0, 5);
-  array.set(1, 3);
-  array.set(2, 7);
-
-  for (uint i = 0; i < 3; i++) {
-    array.add(i, 1);
-  }
-
-  WorkerDataArray_test_verify_array(array, 18, 6.0,
-      "Test array                Min: 4, Avg:  6.0, Max: 8, Diff: 4, Sum: 18, Workers: 3\n",
-      "                           6  4  8\n" );
-}
-
-void WorkerDataArray_test_with_uninitialized() {
-  WorkerDataArray<size_t> array(3, "Test array");
-  array.set(0, 5);
-  array.set(1, WorkerDataArray<size_t>::uninitialized());
-  array.set(2, 7);
-
-  WorkerDataArray_test_verify_array(array, 12, 6,
-      "Test array                Min: 5, Avg:  6.0, Max: 7, Diff: 2, Sum: 12, Workers: 2\n",
-      "                           5 -  7\n" );
-}
-
-void WorkerDataArray_test_uninitialized() {
-  WorkerDataArray<size_t> array(3, "Test array");
-  array.set(0, WorkerDataArray<size_t>::uninitialized());
-  array.set(1, WorkerDataArray<size_t>::uninitialized());
-  array.set(2, WorkerDataArray<size_t>::uninitialized());
-
-  WorkerDataArray_test_verify_array(array, 0, 0.0,
-      "Test array                skipped\n",
-      "                          - - -\n" );
-}
-
-void WorkerDataArray_test_double_with_uninitialized() {
-  WorkerDataArray<double> array(3, "Test array");
-  array.set(0, 5.1 / MILLIUNITS);
-  array.set(1, WorkerDataArray<double>::uninitialized());
-  array.set(2, 7.2 / MILLIUNITS);
-
-  WorkerDataArray_test_verify_array(array, 12.3 / MILLIUNITS, 6.15 / MILLIUNITS,
-      "Test array                Min:  5.1, Avg:  6.1, Max:  7.2, Diff:  2.1, Sum: 12.3, Workers: 2\n",
-      "                           5.1 -  7.2\n" );
-}
-
-void WorkerDataArray_test() {
-  WorkerDataArray_test_basic();
-  WorkerDataArray_test_add();
-  WorkerDataArray_test_with_uninitialized();
-  WorkerDataArray_test_uninitialized();
-  WorkerDataArray_test_double_with_uninitialized();
-}
-
-#endif
--- a/hotspot/src/share/vm/utilities/internalVMTests.cpp	Wed Oct 26 14:36:05 2016 +0200
+++ b/hotspot/src/share/vm/utilities/internalVMTests.cpp	Wed Oct 26 17:29:47 2016 +0300
@@ -58,7 +58,6 @@
 #endif
 #if INCLUDE_ALL_GCS
   run_unit_test(TestBufferingOopClosure_test);
-  run_unit_test(WorkerDataArray_test);
   run_unit_test(ParallelCompact_test);
 #endif
   tty->print_cr("All internal VM tests passed");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hotspot/test/native/gc/g1/test_workerDataArray.cpp	Wed Oct 26 17:29:47 2016 +0300
@@ -0,0 +1,282 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+#include "precompiled.hpp"
+#include "gc/g1/workerDataArray.inline.hpp"
+#include "memory/resourceArea.hpp"
+#include "unittest.hpp"
+#include "utilities/ostream.hpp"
+
+static const double epsilon = 0.0001;
+
+template<typename T>
+class WorkerDataArrayTest : public ::testing::Test {
+ protected:
+  WorkerDataArrayTest() :
+    title("Test array"),
+    array(3, title) {
+  }
+
+  const char* print_summary() {
+    stringStream out;
+    array.print_summary_on(&out);
+    return out.as_string();
+  }
+
+  const char* print_details() {
+    stringStream out;
+    array.print_details_on(&out);
+    return out.as_string();
+  }
+
+  const char* print_expected_summary() {
+    return prepend_with(title, expected_summary());
+  }
+
+  const char* print_expected_details() {
+    return prepend_with("", expected_details());
+  }
+
+  // returns expected summary for array without uninitialized elements
+  // used it because string representation of double depends on locale
+  static const char* format_summary(
+    T min, double avg, T max, T diff, T sum, size_t workers);
+
+  const char* title;
+  WorkerDataArray<T> array;
+
+ private:
+  virtual const char* expected_summary() = 0;
+  virtual const char* expected_details() = 0;
+
+  static const char* prepend_with(const char* str, const char* orig) {
+    stringStream out;
+    out.print("%-25s", str);
+    out.print("%s", orig);
+    return out.as_string();
+  }
+
+  ResourceMark rm;
+};
+
+template<>
+const char* WorkerDataArrayTest<size_t>::format_summary(
+  size_t min, double avg, size_t max, size_t diff, size_t sum, size_t workers) {
+
+  stringStream out;
+  out.print(" Min: " SIZE_FORMAT
+            ", Avg: %4.1lf, Max: " SIZE_FORMAT
+            ", Diff: " SIZE_FORMAT ", Sum: " SIZE_FORMAT
+            ", Workers: " SIZE_FORMAT "\n",
+            min, avg, max, diff, sum, workers);
+  return out.as_string();
+}
+
+template<>
+const char* WorkerDataArrayTest<double>::format_summary(
+  double min, double avg, double max, double diff, double sum, size_t workers) {
+
+  stringStream out;
+  out.print(" Min: %4.1lf"
+            ", Avg: %4.1lf, Max: %4.1lf"
+            ", Diff: %4.1lf, Sum: %4.1lf"
+            ", Workers: " SIZE_FORMAT "\n",
+            min, avg, max, diff, sum, workers);
+  return out.as_string();
+}
+
+class BasicWorkerDataArrayTest : public WorkerDataArrayTest<size_t> {
+ protected:
+  BasicWorkerDataArrayTest() {
+    array.set(0, 5);
+    array.set(1, 3);
+    array.set(2, 7);
+  }
+
+ private:
+  virtual const char* expected_summary() {
+    return format_summary(3, 5.0, 7, 4, 15, 3);
+  }
+
+  virtual const char* expected_details() {
+    return "  5  3  7\n";
+  }
+};
+
+TEST_F(BasicWorkerDataArrayTest, sum_test) {
+  ASSERT_EQ(15u, array.sum());
+}
+
+TEST_F(BasicWorkerDataArrayTest, average_test) {
+  ASSERT_NEAR(5.0, array.average(), epsilon);
+}
+
+TEST_F(BasicWorkerDataArrayTest, print_summary_on_test) {
+  ASSERT_STREQ(print_expected_summary(), print_summary());
+}
+
+TEST_F(BasicWorkerDataArrayTest, print_details_on_test) {
+  ASSERT_STREQ(print_expected_details(), print_details());
+}
+
+class AddWorkerDataArrayTest : public WorkerDataArrayTest<size_t> {
+ protected:
+  AddWorkerDataArrayTest() {
+    array.set(0, 5);
+    array.set(1, 3);
+    array.set(2, 7);
+
+    for (uint i = 0; i < 3; i++) {
+      array.add(i, 1);
+    }
+  }
+
+ private:
+  virtual const char* expected_summary() {
+    return format_summary(4, 6.0, 8, 4, 18, 3);
+  }
+
+  virtual const char* expected_details() {
+    return "  6  4  8\n";
+  }
+};
+
+TEST_F(AddWorkerDataArrayTest, sum_test) {
+  ASSERT_EQ(18u, array.sum());
+}
+
+TEST_F(AddWorkerDataArrayTest, average_test) {
+  ASSERT_NEAR(6.0, array.average(), epsilon);
+}
+
+TEST_F(AddWorkerDataArrayTest, print_summary_on_test) {
+  ASSERT_STREQ(print_expected_summary(), print_summary());
+}
+
+TEST_F(AddWorkerDataArrayTest, print_details_on_test) {
+  ASSERT_STREQ(print_expected_details(), print_details());
+}
+
+class UninitializedElementWorkerDataArrayTest : public WorkerDataArrayTest<size_t> {
+ protected:
+  UninitializedElementWorkerDataArrayTest() {
+    array.set(0, 5);
+    array.set(1, WorkerDataArray<size_t>::uninitialized());
+    array.set(2, 7);
+  }
+
+ private:
+  virtual const char* expected_summary() {
+    return format_summary(5, 6.0, 7, 2, 12, 2);
+  }
+
+  virtual const char* expected_details() {
+    return "  5 -  7\n";
+  }
+};
+
+TEST_F(UninitializedElementWorkerDataArrayTest, sum_test) {
+  ASSERT_EQ(12u, array.sum());
+}
+
+TEST_F(UninitializedElementWorkerDataArrayTest, average_test) {
+  ASSERT_NEAR(6.0, array.average(), epsilon);
+}
+
+TEST_F(UninitializedElementWorkerDataArrayTest, print_summary_on_test) {
+  ASSERT_STREQ(print_expected_summary(), print_summary());
+}
+
+TEST_F(UninitializedElementWorkerDataArrayTest, print_details_on_test) {
+  ASSERT_STREQ(print_expected_details(), print_details());
+}
+
+class UninitializedWorkerDataArrayTest : public WorkerDataArrayTest<size_t> {
+ protected:
+  UninitializedWorkerDataArrayTest() {
+    array.set(0, WorkerDataArray<size_t>::uninitialized());
+    array.set(1, WorkerDataArray<size_t>::uninitialized());
+    array.set(2, WorkerDataArray<size_t>::uninitialized());
+  }
+
+ private:
+  virtual const char* expected_summary() {
+    return " skipped\n";
+  }
+
+  virtual const char* expected_details() {
+    return " - - -\n";
+  }
+};
+
+TEST_F(UninitializedWorkerDataArrayTest, sum_test) {
+  ASSERT_EQ(0u, array.sum());
+}
+
+TEST_F(UninitializedWorkerDataArrayTest, average_test) {
+  ASSERT_NEAR(0.0, array.average(), epsilon);
+}
+
+TEST_F(UninitializedWorkerDataArrayTest, print_summary_on_test) {
+  ASSERT_STREQ(print_expected_summary(), print_summary());
+}
+
+TEST_F(UninitializedWorkerDataArrayTest, print_details_on_test) {
+  ASSERT_STREQ(print_expected_details(), print_details());
+}
+
+class UninitializedDoubleElementWorkerDataArrayTest : public WorkerDataArrayTest<double> {
+ protected:
+  UninitializedDoubleElementWorkerDataArrayTest() {
+    array.set(0, 5.1 / MILLIUNITS);
+    array.set(1, WorkerDataArray<double>::uninitialized());
+    array.set(2, 7.2 / MILLIUNITS);
+  }
+
+ private:
+  virtual const char* expected_summary() {
+    return format_summary(5.1, 6.1, 7.2, 2.1, 12.3, 2);
+  }
+
+  virtual const char* expected_details() {
+    stringStream out;
+    out.print(" %4.1lf - %4.1lf\n", 5.1, 7.2);
+    return out.as_string();
+  }
+};
+
+TEST_F(UninitializedDoubleElementWorkerDataArrayTest, sum_test) {
+  ASSERT_NEAR(12.3 / MILLIUNITS, array.sum(), epsilon);
+}
+
+TEST_F(UninitializedDoubleElementWorkerDataArrayTest, average_test) {
+  ASSERT_NEAR(6.15 / MILLIUNITS, array.average(), epsilon);
+}
+
+TEST_F(UninitializedDoubleElementWorkerDataArrayTest, print_summary_on_test) {
+  ASSERT_STREQ(print_expected_summary(), print_summary());
+}
+
+TEST_F(UninitializedDoubleElementWorkerDataArrayTest, print_details_on_test) {
+  ASSERT_STREQ(print_expected_details(), print_details());
+}