From 55c66d365f76f3e5ecc6b850ba81c84b320f6772 Mon Sep 17 00:00:00 2001
From: Shivaram Venkataraman <shivaram@eecs.berkeley.edu>
Date: Mon, 7 Jan 2013 15:19:33 -0800
Subject: [PATCH] Use a dummy string class in Size Estimator tests to make it
 resistant to jdk versions

---
 .../test/scala/spark/SizeEstimatorSuite.scala | 33 ++++++++++++-------
 1 file changed, 21 insertions(+), 12 deletions(-)

diff --git a/core/src/test/scala/spark/SizeEstimatorSuite.scala b/core/src/test/scala/spark/SizeEstimatorSuite.scala
index 17f366212b..bf3b2e1eed 100644
--- a/core/src/test/scala/spark/SizeEstimatorSuite.scala
+++ b/core/src/test/scala/spark/SizeEstimatorSuite.scala
@@ -20,6 +20,15 @@ class DummyClass4(val d: DummyClass3) {
   val x: Int = 0
 }
 
+object DummyString {
+  def apply(str: String) : DummyString = new DummyString(str.toArray)
+}
+class DummyString(val arr: Array[Char]) {
+  override val hashCode: Int = 0
+  // JDK-7 has an extra hash32 field http://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/rev/11987e85555f
+  @transient val hash32: Int = 0
+}
+
 class SizeEstimatorSuite
   extends FunSuite with BeforeAndAfterAll with PrivateMethodTester with ShouldMatchers {
 
@@ -50,10 +59,10 @@ class SizeEstimatorSuite
   // http://mail.openjdk.java.net/pipermail/core-libs-dev/2012-May/010257.html
   // Work around to check for either.
   test("strings") {
-    SizeEstimator.estimate("") should (equal (48) or equal (40))
-    SizeEstimator.estimate("a") should (equal (56) or equal (48))
-    SizeEstimator.estimate("ab") should (equal (56) or equal (48))
-    SizeEstimator.estimate("abcdefgh") should (equal(64) or equal(56))
+    SizeEstimator.estimate(DummyString("")) should (equal (48) or equal (40))
+    SizeEstimator.estimate(DummyString("a")) should (equal (56) or equal (48))
+    SizeEstimator.estimate(DummyString("ab")) should (equal (56) or equal (48))
+    SizeEstimator.estimate(DummyString("abcdefgh")) should (equal(64) or equal(56))
   }
 
   test("primitive arrays") {
@@ -105,10 +114,10 @@ class SizeEstimatorSuite
     val initialize = PrivateMethod[Unit]('initialize)
     SizeEstimator invokePrivate initialize()
 
-    expect(40)(SizeEstimator.estimate(""))
-    expect(48)(SizeEstimator.estimate("a"))
-    expect(48)(SizeEstimator.estimate("ab"))
-    expect(56)(SizeEstimator.estimate("abcdefgh"))
+    expect(40)(SizeEstimator.estimate(DummyString("")))
+    expect(48)(SizeEstimator.estimate(DummyString("a")))
+    expect(48)(SizeEstimator.estimate(DummyString("ab")))
+    expect(56)(SizeEstimator.estimate(DummyString("abcdefgh")))
 
     resetOrClear("os.arch", arch)
   }
@@ -124,10 +133,10 @@ class SizeEstimatorSuite
     val initialize = PrivateMethod[Unit]('initialize)
     SizeEstimator invokePrivate initialize()
 
-    SizeEstimator.estimate("") should (equal (64) or equal (56))
-    SizeEstimator.estimate("a") should (equal (72) or equal (64))
-    SizeEstimator.estimate("ab") should (equal (72) or equal (64))
-    SizeEstimator.estimate("abcdefgh") should (equal (80) or equal (72))
+    SizeEstimator.estimate(DummyString("")) should (equal (64) or equal (56))
+    SizeEstimator.estimate(DummyString("a")) should (equal (72) or equal (64))
+    SizeEstimator.estimate(DummyString("ab")) should (equal (72) or equal (64))
+    SizeEstimator.estimate(DummyString("abcdefgh")) should (equal (80) or equal (72))
 
     resetOrClear("os.arch", arch)
     resetOrClear("spark.test.useCompressedOops", oops)
-- 
GitLab