1+ //===----------------------------------------------------------------------===//
2+ //
3+ // This source file is part of the Swift.org open source project
4+ //
5+ // Copyright (c) 2024 Apple Inc. and the Swift.org project authors
6+ // Licensed under Apache License v2.0
7+ //
8+ // See LICENSE.txt for license information
9+ // See CONTRIBUTORS.txt for the list of Swift.org project authors
10+ //
11+ // SPDX-License-Identifier: Apache-2.0
12+ //
13+ //===----------------------------------------------------------------------===//
14+
15+ import JExtractSwiftLib
16+ import SwiftJavaConfigurationShared
17+ import Testing
18+
19+ final class ByteArrayTests {
20+ let text =
21+ """
22+ public func acceptArray(array: [UInt8])
23+ """
24+
25+
26+ @Test (
27+ " Import: accept [UInt8] array " ,
28+ arguments: [
29+ // TODO: implement JNI mode here
30+ (
31+ JExtractGenerationMode . ffm,
32+ /* expected Java chunks */
33+ [
34+ """
35+ /**
36+ * {@snippet lang=c :
37+ * void swiftjava_SwiftModule_acceptArray_array(const void *array_pointer, ptrdiff_t array_count)
38+ * }
39+ */
40+ private static class swiftjava_SwiftModule_acceptArray_array {
41+ private static final FunctionDescriptor DESC = FunctionDescriptor.ofVoid(
42+ /* array_pointer: */SwiftValueLayout.SWIFT_POINTER,
43+ /* array_count: */SwiftValueLayout.SWIFT_INT
44+ );
45+ """ ,
46+ """
47+ /**
48+ * Downcall to Swift:
49+ * {@snippet lang=swift :
50+ * public func acceptArray(array: [UInt8])
51+ * }
52+ */
53+ public static void acceptArray(byte[] array) {
54+ try(var arena$ = Arena.ofConfined()) {
55+ swiftjava_SwiftModule_acceptArray_array.call(arena$.allocateFrom(ValueLayout.JAVA_BYTE, array), array.length);
56+ }
57+ }
58+ """
59+ ] ,
60+ /* expected Swift chunks */
61+ [
62+ """
63+ @_cdecl( " swiftjava_SwiftModule_acceptArray_array " )
64+ public func swiftjava_SwiftModule_acceptArray_array(_ array_pointer: UnsafeRawPointer, _ array_count: Int) {
65+ acceptArray(array: [UInt8](UnsafeRawBufferPointer(start: array_pointer, count: array_count)))
66+ }
67+ """
68+ ]
69+ )
70+ ]
71+ )
72+ func func_accept_array_uint8( mode: JExtractGenerationMode , expectedJavaChunks: [ String ] , expectedSwiftChunks: [ String ] ) throws {
73+ try assertOutput (
74+ input: text,
75+ mode, . java,
76+ expectedChunks: expectedJavaChunks)
77+
78+ try assertOutput (
79+ input: text,
80+ mode, . swift,
81+ expectedChunks: expectedSwiftChunks)
82+ }
83+ }
0 commit comments