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.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 }