From f54d81e0e2d97798b928ba32f5edab5519622f28 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Tue, 10 Jun 2014 10:54:57 +0100 Subject: Tests for Runtime.*Memory() The docs describe a condition we do not test for. The freeMemory / totalMemory tests have been consolidated into a single test and joined by an additional assertion for maxMemory(). The test for freeMemory from OldRuntimeTest has been tidied up and moved to RuntimeTest. Bug: 15507122 Change-Id: I4343215fb7f6b441613e25d3844d67812f5b5e94 --- .../harmony/tests/java/lang/RuntimeTest.java | 36 ++++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/RuntimeTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/RuntimeTest.java index 08a4fa9..17f822d 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/RuntimeTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/RuntimeTest.java @@ -19,6 +19,8 @@ package org.apache.harmony.tests.java.lang; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; import java.util.Vector; public class RuntimeTest extends junit.framework.TestCase { @@ -82,14 +84,6 @@ public class RuntimeTest extends junit.framework.TestCase { } /** - * java.lang.Runtime#freeMemory() - */ - public void test_freeMemory() { - // Test for method long java.lang.Runtime.freeMemory() - assertTrue("freeMemory returned nonsense value", r.freeMemory() > 0); - } - - /** * java.lang.Runtime#gc() */ public void test_gc() { @@ -142,12 +136,28 @@ public class RuntimeTest extends junit.framework.TestCase { } /** - * java.lang.Runtime#totalMemory() + * java.lang.Runtime#freeMemory() / java.lang.Runtime#totalMemory() / + * java.lang.Runtime#maxMemory() */ - public void test_totalMemory() { - // Test for method long java.lang.Runtime.totalMemory() - assertTrue("totalMemory returned nonsense value", r.totalMemory() >= r - .freeMemory()); + public void test_memory() { + assertTrue("freeMemory <= 0", r.freeMemory() > 0); + assertTrue("totalMemory() < freeMemory()", r.totalMemory() >= r.freeMemory()); + assertTrue("maxMemory() < totalMemory()", r.maxMemory() >= r.totalMemory()); + } + + public void test_freeMemory() { + // Heap might grow or do GC at any time, so we can't really test a lot. Hence we are just + // doing some basic sanity checks here. + long freeBefore = r.freeMemory(); + List arrays = new ArrayList(); + for (int i = 1; i < 10; i++) { + arrays.add(new byte[10000]); + } + long freeAfter = r.freeMemory(); + + // If totalMemory() has grown/shrunk freeMemory() might have gone down or up, but the + // freeMemory is unlikely to stay the same. + assertTrue("free memory must change with allocations", freeAfter != freeBefore); } public RuntimeTest() { -- cgit v1.1