File: ArgumentsWrappersMarshalTest.java

package info (click to toggle)
libjna-java 5.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 11,828 kB
  • sloc: java: 90,222; ansic: 4,994; xml: 3,713; makefile: 433; sh: 299
file content (178 lines) | stat: -rw-r--r-- 6,408 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Copyright (c) 2018 Matthias Bläsing, All Rights Reserved
 *
 * The contents of this file is dual-licensed under 2
 * alternative Open Source/Free licenses: LGPL 2.1 or later and
 * Apache License 2.0. (starting with JNA version 4.0.0).
 *
 * You can freely decide which license you want to apply to
 * the project.
 *
 * You may obtain a copy of the LGPL License at:
 *
 * http://www.gnu.org/licenses/licenses.html
 *
 * A copy is also included in the downloadable source code package
 * containing JNA, in file "LGPL2.1".
 *
 * You may obtain a copy of the Apache License at:
 *
 * http://www.apache.org/licenses/
 *
 * A copy is also included in the downloadable source code package
 * containing JNA, in file "AL2.0".
 */
package com.sun.jna;


import junit.framework.TestCase;

/**
 * Test native calls where the parameters and return types are bound as wrappers
 */
public class ArgumentsWrappersMarshalTest extends TestCase {

    public static interface TestLibrary extends Library {
        Boolean returnBooleanArgument(Boolean arg);
        Byte returnInt8Argument(Byte arg);
        Character returnWideCharArgument(Character arg);
        Short returnInt16Argument(Short arg);
        Integer returnInt32Argument(Integer i);
        Long returnInt64Argument(Long l);
        Float returnFloatArgument(Float f);
        Double returnDoubleArgument(Double d);
        Long checkInt64ArgumentAlignment(Integer i, Long j, Integer i2, Long j2);
        Double checkDoubleArgumentAlignment(Float i, Double j, Float i2, Double j2);
    }

    TestLibrary lib;
    @Override
    protected void setUp() {
        lib = Native.load("testlib", TestLibrary.class);
    }

    @Override
    protected void tearDown() {
        lib = null;
    }

    public void testBooleanArgument() {
        assertTrue("True argument should be returned",
                   lib.returnBooleanArgument(true));
        assertFalse("False argument should be returned",
                    lib.returnBooleanArgument(false));
    }

    public void testInt8Argument() {
        Byte b = 0;
        assertEquals("Wrong value returned",
                     b, lib.returnInt8Argument(b));
        b = 127;
        assertEquals("Wrong value returned",
                     b, lib.returnInt8Argument(b));
        b = -128;
        assertEquals("Wrong value returned",
                     b, lib.returnInt8Argument(b));
    }

    public void testWideCharArgument() {
        Character c = 0;
        assertEquals("Wrong value returned",
                     c, lib.returnWideCharArgument(c));
        c = 0xFFFF;
        assertEquals("Wrong value returned",
                     c, lib.returnWideCharArgument(c));
        c = 0x7FFF;
        assertEquals("Wrong value returned",
                     c, lib.returnWideCharArgument(c));
    }

    public void testInt16Argument() {
        Short v = 0;
        assertEquals("Wrong value returned",
                     v, lib.returnInt16Argument(v));
        v = 32767;
        assertEquals("Wrong value returned",
                     v, lib.returnInt16Argument(v));
        v = -32768;
        assertEquals("Wrong value returned",
                     v, lib.returnInt16Argument(v));
    }

    public void testIntArgument() {
        Integer value = 0;
        assertEquals("Should return 32-bit argument",
                     value, lib.returnInt32Argument(value));
        value = 1;
        assertEquals("Should return 32-bit argument",
                     value, lib.returnInt32Argument(value));
        value = 0x7FFFFFFF;
        assertEquals("Should return 32-bit argument",
                     value, lib.returnInt32Argument(value));
        value = 0x80000000;
        assertEquals("Should return 32-bit argument",
                     value, lib.returnInt32Argument(value));
    }

    public void testLongArgument() {
        Long value = 0L;
        assertEquals("Should return 64-bit argument",
                     value, lib.returnInt64Argument(value));
        value = 1L;
        assertEquals("Should return 64-bit argument",
                     value, lib.returnInt64Argument(value));
        value = 0x7FFFFFFFL;
        assertEquals("Should return 64-bit argument",
                     value, lib.returnInt64Argument(value));
        value = 0x80000000L;
        assertEquals("Should return 64-bit argument",
                     value, lib.returnInt64Argument(value));
        value = 0x7FFFFFFF00000000L;
        assertEquals("Should return 64-bit argument",
                     value, lib.returnInt64Argument(value));
        value = 0x8000000000000000L;
        assertEquals("Should return 64-bit argument",
                     value, lib.returnInt64Argument(value));
    }

    public void testFloatArgument() {
        Float value = 0f;
        assertEquals("Should return float argument",
                     value, lib.returnFloatArgument(value));
        value = 4223.4223f;
        assertEquals("Should return float argument",
                     value, lib.returnFloatArgument(value));
        value = -4223.4223f;
        assertEquals("Should return float argument",
                     value, lib.returnFloatArgument(value));
    }

    public void testDoubleArgument() {
        Double value = 0d;
        assertEquals("Should return double argument",
                     value, lib.returnDoubleArgument(value));
        value = 4223.4223d;
        assertEquals("Should return double argument",
                     value, lib.returnDoubleArgument(value));
        value = -4223.4223d;
        assertEquals("Should return double argument",
                     value, lib.returnDoubleArgument(value));
    }

    public void testInt64ArgumentAlignment() {
        long value = lib.checkInt64ArgumentAlignment(0x10101010, 0x1111111111111111L,
                                                     0x01010101, 0x2222222222222222L);
        assertEquals("Improper handling of interspersed int32/int64",
                     0x3333333344444444L, value);
    }

    public void testDoubleArgumentAlignment() {
        Double value = lib.checkDoubleArgumentAlignment(1f, 2d, 3f, 4d);
        assertEquals("Improper handling of interspersed float/double",
                     10d, value, 0);
    }

    public static void main(java.lang.String[] argList) {
        junit.textui.TestRunner.run(ArgumentsWrappersMarshalTest.class);
    }

}