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.Addressable; 25 import jdk.incubator.foreign.MemoryAddress; 26 import jdk.incubator.foreign.MemorySegment; 27 import jdk.incubator.foreign.ResourceScope; 28 import org.testng.annotations.Test; 29 import test.jextract.test8246400.*; 30 import static org.testng.Assert.assertEquals; 31 import static org.testng.Assert.assertTrue; 32 import static test.jextract.test8246400.test8246400_h.*; 33 34 /* 35 * @test id=classes 36 * @bug 8246400 37 * @summary jextract should generate a utility to manage mutliple MemorySegments 38 * @library .. 39 * @modules jdk.incubator.jextract 40 * @run driver JtregJextract -l Test8246400 -t test.jextract.test8246400 -- test8246400.h 41 * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibTest8246400Test 42 */ 43 /* 44 * @test id=sources 45 * @bug 8246400 46 * @summary jextract should generate a utility to manage mutliple MemorySegments 47 * @library .. 48 * @modules jdk.incubator.jextract 49 * @run driver JtregJextractSources -l Test8246400 -t test.jextract.test8246400 -- test8246400.h 50 * @run testng/othervm --enable-native-access=jdk.incubator.jextract,ALL-UNNAMED LibTest8246400Test 51 */ 52 public class LibTest8246400Test { 53 @Test 54 public void testSegmentRegister() { 55 MemorySegment sum = null; 56 try (ResourceScope scope = ResourceScope.newConfinedScope()) { 57 var v1 = Vector.allocate(scope); 58 Vector.x$set(v1, 1.0); 59 Vector.y$set(v1, 0.0); 60 61 var v2 = Vector.allocate(scope); 62 Vector.x$set(v2, 0.0); 63 Vector.y$set(v2, 1.0); 64 65 sum = add(scope, v1, v2); 66 67 assertEquals(Vector.x$get(sum), 1.0, 0.1); 68 assertEquals(Vector.y$get(sum), 1.0, 0.1); 69 70 Addressable callback = cosine_similarity$dot.allocate((a, b) -> { 71 return (Vector.x$get(a) * Vector.x$get(b)) + 72 (Vector.y$get(a) * Vector.y$get(b)); 73 }, scope); 74 75 var value = cosine_similarity(v1, v2, callback); 76 assertEquals(value, 0.0, 0.1); 77 78 value = cosine_similarity(v1, v1, callback); 79 assertEquals(value, 1.0, 0.1); 80 } 81 assertTrue(!sum.scope().isAlive()); 82 } 83 }