< prev index next >

test/jdk/tools/jextract/test8246400/LibTest8246400Test.java

Print this page

 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 

24 import jdk.incubator.foreign.MemorySegment;
25 import org.testng.annotations.Test;
26 import test.jextract.test8246400.*;
27 import static org.testng.Assert.assertEquals;
28 import static org.testng.Assert.assertTrue;
29 import static test.jextract.test8246400.test8246400_h.*;
30 import static test.jextract.test8246400.RuntimeHelper.*;
31 
32 /*
33  * @test
34  * @library ..
35  * @modules jdk.incubator.jextract
36  * @bug 8246400
37  * @summary jextract should generate a utility to manage mutliple MemorySegments
38  * @run driver JtregJextract -l Test8246400 -t test.jextract.test8246400 -- test8246400.h
39  * @run testng/othervm -Dforeign.restricted=permit LibTest8246400Test
40  */
41 public class LibTest8246400Test {
42     @Test
43     public void testSegmentRegister() {
44         MemorySegment sum = null, callback = null;

45         try (var scope = new CScope()) {
46             var v1 = CVector.allocate(scope);
47             CVector.x$set(v1, 1.0);
48             CVector.y$set(v1, 0.0);
49 
50             var v2 = CVector.allocate(scope);
51             CVector.x$set(v2, 0.0);
52             CVector.y$set(v2, 1.0);
53 
54             sum = add(v1.segment(), v2.segment());
55             scope.register(sum);
56 
57             assertEquals(CVector.x$get(sum.baseAddress()), 1.0, 0.1);
58             assertEquals(CVector.y$get(sum.baseAddress()), 1.0, 0.1);
59 
60             callback = cosine_similarity$dot.allocate((a, b) -> {
61                 return (CVector.x$get(a.baseAddress()) * CVector.x$get(b.baseAddress())) +
62                     (CVector.y$get(a.baseAddress()) * CVector.y$get(b.baseAddress()));
63             });
64             scope.register(callback);
65 
66             var value = cosine_similarity(v1.segment(), v2.segment(), callback.baseAddress());
67             assertEquals(value, 0.0, 0.1);
68 
69             value = cosine_similarity(v1.segment(), v1.segment(), callback.baseAddress());
70             assertEquals(value, 1.0, 0.1);
71         }
72         assertTrue(!sum.isAlive());
73         assertTrue(!callback.isAlive());
74     }
75 }

 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import jdk.incubator.foreign.MemoryAddress;
25 import jdk.incubator.foreign.MemorySegment;
26 import org.testng.annotations.Test;
27 import test.jextract.test8246400.*;
28 import static org.testng.Assert.assertEquals;
29 import static org.testng.Assert.assertTrue;
30 import static test.jextract.test8246400.test8246400_h.*;
31 import static test.jextract.test8246400.RuntimeHelper.*;
32 
33 /*
34  * @test
35  * @library ..
36  * @modules jdk.incubator.jextract
37  * @bug 8246400
38  * @summary jextract should generate a utility to manage mutliple MemorySegments
39  * @run driver JtregJextract -l Test8246400 -t test.jextract.test8246400 -- test8246400.h
40  * @run testng/othervm -Dforeign.restricted=permit LibTest8246400Test
41  */
42 public class LibTest8246400Test {
43     @Test
44     public void testSegmentRegister() {
45         MemorySegment sum = null;
46         MemoryAddress callback = null;
47         try (var scope = new CScope()) {
48             var v1 = CVector.allocate(scope);
49             CVector.x$set(v1, 1.0);
50             CVector.y$set(v1, 0.0);
51 
52             var v2 = CVector.allocate(scope);
53             CVector.x$set(v2, 0.0);
54             CVector.y$set(v2, 1.0);
55 
56             sum = add(v1.segment(), v2.segment());
57             scope.register(sum);
58 
59             assertEquals(CVector.x$get(sum.baseAddress()), 1.0, 0.1);
60             assertEquals(CVector.y$get(sum.baseAddress()), 1.0, 0.1);
61 
62             callback = cosine_similarity$dot.allocate((a, b) -> {
63                 return (CVector.x$get(a.baseAddress()) * CVector.x$get(b.baseAddress())) +
64                     (CVector.y$get(a.baseAddress()) * CVector.y$get(b.baseAddress()));
65             }, scope);

66 
67             var value = cosine_similarity(v1.segment(), v2.segment(), callback);
68             assertEquals(value, 0.0, 0.1);
69 
70             value = cosine_similarity(v1.segment(), v1.segment(), callback);
71             assertEquals(value, 1.0, 0.1);
72         }
73         assertTrue(!sum.isAlive());
74         assertTrue(!callback.segment().isAlive());
75     }
76 }
< prev index next >