1 /*
   2  * Copyright (c) 2018, 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 java.foreign.Libraries;
  25 import java.foreign.Library;
  26 import java.foreign.NativeTypes;
  27 import java.foreign.Scope;
  28 import java.foreign.memory.Array;
  29 import java.foreign.memory.Pointer;
  30 import java.lang.invoke.MethodHandles;
  31 import java.util.Arrays;
  32 import org.testng.annotations.BeforeTest;
  33 import org.testng.annotations.Test;
  34 import test.arrayparam.funcArrayParam;
  35 import test.arrayparam.funcArrayParam.FPPtrFieldStruct;
  36 
  37 import static org.testng.Assert.assertEquals;
  38 
  39 /*
  40  * @test
  41  * @bug 8209016
  42  * @library ..
  43  * @run driver JtregJextract -t test.arrayparam -- funcArrayParam.h
  44  * @run testng FuncArrayParamTest
  45  */
  46 public class FuncArrayParamTest {
  47     private funcArrayParam fap;
  48 
  49     @BeforeTest
  50     public void init() {
  51         Library lib = Libraries.loadLibrary(MethodHandles.lookup(), "FuncArrayParam");
  52         fap = Libraries.bind(funcArrayParam.class, lib);
  53     }
  54 
  55     private static int[] jarr = new int[] { 34, 66, 23, 53, 345 };
  56 
  57     @Test
  58     public void testFuncArrayParam() {
  59         try (Scope scope = Scope.globalScope().fork()) {
  60             Array<Integer> carr = scope.allocateArray(NativeTypes.INT32, jarr.length);
  61             for (int i = 0; i < jarr.length; i++) {
  62                 carr.set(i, jarr[i]);
  63             }
  64             int sum = Arrays.stream(jarr).sum();
  65             assertEquals(fap.f(carr.elementPointer(), jarr.length), sum);
  66             assertEquals(fap.g(carr.elementPointer(), jarr.length), sum);
  67             assertEquals(fap.k(carr.elementPointer(), jarr.length), sum);
  68 
  69             assertEquals(fap.map_sum(carr.elementPointer(), jarr.length, scope.allocateCallback((arr, idx, val)->2*val)), 2*sum);
  70 
  71             FPPtrFieldStruct s = scope.allocateStruct(FPPtrFieldStruct.class);
  72             s.map$set(scope.allocateCallback((arr, idx, val) -> -val));
  73             assertEquals(fap.map_sum2(carr.elementPointer(), jarr.length, s), -sum);
  74         }
  75     }
  76 }