forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for Resource Scope in Java (apache#12955)
- Loading branch information
1 parent
94f3665
commit 58d4efb
Showing
2 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
scala-package/core/src/test/java/org/apache/mxnet/javaapi/ResourceScopeTestSuite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
|
||
package org.apache.mxnet.javaapi; | ||
|
||
import org.apache.mxnet.NativeResourceRef; | ||
import org.apache.mxnet.ResourceScope; | ||
import org.junit.Test; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.Callable; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class ResourceScopeTestSuite { | ||
|
||
/** | ||
* This is a placeholder class to test out whether NDArray References get collected or not when using | ||
* try-with-resources in Java. | ||
* | ||
*/ | ||
class TestNDArray { | ||
NDArray selfArray; | ||
|
||
public TestNDArray(Context context, int[] shape) { | ||
this.selfArray = NDArray.ones(context, shape); | ||
} | ||
|
||
public boolean verifyIsDisposed() { | ||
return this.selfArray.nd().isDisposed(); | ||
} | ||
|
||
public NativeResourceRef getNDArrayReference() { | ||
return this.selfArray.nd().ref(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testNDArrayAutoRelease() { | ||
TestNDArray test = null; | ||
|
||
try (ResourceScope scope = new ResourceScope()) { | ||
test = new TestNDArray(Context.cpu(), new int[]{100, 100}); | ||
} | ||
|
||
assertTrue(test.verifyIsDisposed()); | ||
} | ||
|
||
@Test | ||
public void testObjectReleaseFromList() { | ||
List<TestNDArray> list = new ArrayList<>(); | ||
|
||
try (ResourceScope scope = new ResourceScope()) { | ||
for (int i = 0;i < 10; i++) { | ||
list.add(new TestNDArray(Context.cpu(), new int[] {100, 100})); | ||
} | ||
} | ||
|
||
assertEquals(list.size() , 10); | ||
list.forEach(n -> assertTrue(n.verifyIsDisposed())); | ||
} | ||
|
||
@Test | ||
public void testObjectReleaseFromMap() { | ||
Map<String, TestNDArray> stringToNDArrayMap = new HashMap<>(); | ||
|
||
try (ResourceScope scope = new ResourceScope()) { | ||
for (int i = 0;i < 10; i++) { | ||
stringToNDArrayMap.put(String.valueOf(i),new TestNDArray(Context.cpu(), new int[] {i, i})); | ||
} | ||
} | ||
|
||
assertEquals(stringToNDArrayMap.size(), 10); | ||
stringToNDArrayMap.forEach((key, value) -> assertTrue(value.verifyIsDisposed())); | ||
|
||
Map<TestNDArray, String> ndArrayToStringMap = new HashMap<>(); | ||
|
||
try (ResourceScope scope = new ResourceScope()) { | ||
for (int i = 0;i < 10; i++) { | ||
ndArrayToStringMap.put(new TestNDArray(Context.cpu(), new int[] {i, i}), String.valueOf(i)); | ||
} | ||
} | ||
|
||
assertEquals(ndArrayToStringMap.size(), 10); | ||
ndArrayToStringMap.forEach((key, value) -> assertTrue(key.verifyIsDisposed())); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters