1 /* 2 * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 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 }