|
| 1 | +/* Copyright (c) 2022 Carlos Ballesteros, All Rights Reserved |
| 2 | + * |
| 3 | + * The contents of this file is dual-licensed under 2 |
| 4 | + * alternative Open Source/Free licenses: LGPL 2.1 or later and |
| 5 | + * Apache License 2.0. (starting with JNA version 4.0.0). |
| 6 | + * |
| 7 | + * You can freely decide which license you want to apply to |
| 8 | + * the project. |
| 9 | + * |
| 10 | + * You may obtain a copy of the LGPL License at: |
| 11 | + * |
| 12 | + * http://www.gnu.org/licenses/licenses.html |
| 13 | + * |
| 14 | + * A copy is also included in the downloadable source code package |
| 15 | + * containing JNA, in file "LGPL2.1". |
| 16 | + * |
| 17 | + * You may obtain a copy of the Apache License at: |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/ |
| 20 | + * |
| 21 | + * A copy is also included in the downloadable source code package |
| 22 | + * containing JNA, in file "AL2.0". |
| 23 | + */ |
| 24 | +package com.sun.jna; |
| 25 | + |
| 26 | +import junit.framework.TestCase; |
| 27 | + |
| 28 | +import java.util.Collections; |
| 29 | + |
| 30 | +public class NativeCustomSymbolProviderTest extends TestCase implements Paths { |
| 31 | + public static void main(java.lang.String[] argList) { |
| 32 | + junit.textui.TestRunner.run(NativeCustomSymbolProviderTest.class); |
| 33 | + } |
| 34 | + |
| 35 | + interface MathInterfaceWithSymbolProvider extends Library { |
| 36 | + |
| 37 | + double sin(double x); |
| 38 | + double cos(double x); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + static class MathLibraryWithSymbolProvider { |
| 43 | + |
| 44 | + public static native double sin(double x); |
| 45 | + public static native double cos(double x); |
| 46 | + |
| 47 | + static { |
| 48 | + Native.register(MathLibraryWithSymbolProvider.class, NativeLibrary.getInstance(Platform.MATH_LIBRARY_NAME, Collections.singletonMap( |
| 49 | + Library.OPTION_SYMBOL_PROVIDER, |
| 50 | + new SymbolProvider() { |
| 51 | + @Override |
| 52 | + public long getSymbolAddress(long handle, String name, SymbolProvider parent) { |
| 53 | + if (name.equals("sin")) { |
| 54 | + return parent.getSymbolAddress(handle, "cos", null); |
| 55 | + } else { |
| 56 | + return parent.getSymbolAddress(handle, "sin", null); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + ))); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + MathInterfaceWithSymbolProvider lib; |
| 65 | + MathInterfaceWithSymbolProvider libCustom; |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void setUp() { |
| 69 | + lib = Native.load(Platform.MATH_LIBRARY_NAME, MathInterfaceWithSymbolProvider.class); |
| 70 | + libCustom = Native.load( |
| 71 | + Platform.MATH_LIBRARY_NAME, |
| 72 | + MathInterfaceWithSymbolProvider.class, Collections.singletonMap( |
| 73 | + Library.OPTION_SYMBOL_PROVIDER, |
| 74 | + new SymbolProvider() { |
| 75 | + @Override |
| 76 | + public long getSymbolAddress(long handle, String name, SymbolProvider parent) { |
| 77 | + if (name.equals("sin")) { |
| 78 | + return parent.getSymbolAddress(handle, "cos", null); |
| 79 | + } else { |
| 80 | + return parent.getSymbolAddress(handle, "sin", null); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + ) |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + protected void tearDown() { |
| 90 | + lib = null; |
| 91 | + libCustom = null; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + public void testDirectMappingSymbolProvider() { |
| 96 | + assertEquals(lib.cos(0.0), MathLibraryWithSymbolProvider.sin(0.0)); |
| 97 | + assertEquals(lib.sin(0.0), MathLibraryWithSymbolProvider.cos(0.0)); |
| 98 | + } |
| 99 | + |
| 100 | + public void testInterfaceCustomSymbolProvider() { |
| 101 | + assertEquals(lib.cos(0.0), libCustom.sin(0.0)); |
| 102 | + assertEquals(lib.sin(0.0), libCustom.cos(0.0)); |
| 103 | + } |
| 104 | +} |
0 commit comments