diff --git a/Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloJavaKitArrays.java b/Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloJavaKitArrays.java new file mode 100644 index 000000000..a0495fc71 --- /dev/null +++ b/Samples/JavaKitSampleApp/Sources/JavaKitExample/com/example/swift/HelloJavaKitArrays.java @@ -0,0 +1,59 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2025 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +package com.example.swift; + +public class HelloJavaKitArrays { + + public byte[] getFixedBytes() { + return new byte[] { 1, 2, 3, 4, 5 }; + } + + public byte[] getEmptyBytes() { + return new byte[0]; + } + + public byte[] filledBytes(int size, byte value) { + byte[] result = new byte[size]; + java.util.Arrays.fill(result, value); + return result; + } + + public byte[] reverseBytes(byte[] input) { + byte[] result = new byte[input.length]; + for (int i = 0; i < input.length; i++) { + result[i] = input[input.length - 1 - i]; + } + return result; + } + + public int[] getFixedInts() { + return new int[] { 100, 200, 300 }; + } + + public long[] doubleLongs(long[] input) { + long[] result = new long[input.length * 2]; + System.arraycopy(input, 0, result, 0, input.length); + System.arraycopy(input, 0, result, input.length, input.length); + return result; + } + + public byte[] stringToBytes(String s) { + return s.getBytes(java.nio.charset.StandardCharsets.UTF_8); + } + + public String[] getGreetings() { + return new String[] { "hello", "world", "from", "java" }; + } +} diff --git a/Samples/JavaKitSampleApp/Sources/JavaKitExample/swift-java.config b/Samples/JavaKitSampleApp/Sources/JavaKitExample/swift-java.config index a86b5a4f3..1c8b3c3fc 100644 --- a/Samples/JavaKitSampleApp/Sources/JavaKitExample/swift-java.config +++ b/Samples/JavaKitSampleApp/Sources/JavaKitExample/swift-java.config @@ -2,6 +2,7 @@ "classes" : { "com.example.swift.HelloSwift" : "HelloSwift", "com.example.swift.HelloSubclass" : "HelloSubclass", + "com.example.swift.HelloJavaKitArrays" : "HelloJavaKitArrays", "com.example.swift.JavaKitSampleMain" : "JavaKitSampleMain", "com.example.swift.ThreadSafeHelperClass" : "ThreadSafeHelperClass" } diff --git a/Samples/JavaKitSampleApp/Tests/JavaKitExampleTests/JavaKitArrayRuntimeTests.swift b/Samples/JavaKitSampleApp/Tests/JavaKitExampleTests/JavaKitArrayRuntimeTests.swift new file mode 100644 index 000000000..9b186fc31 --- /dev/null +++ b/Samples/JavaKitSampleApp/Tests/JavaKitExampleTests/JavaKitArrayRuntimeTests.swift @@ -0,0 +1,96 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2025 Apple Inc. and the Swift.org project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of Swift.org project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import JavaKitExample +import SwiftJava +import Testing + +@Suite +struct JavaKitArrayRuntimeTests { + + let jvm = try JavaKitSampleJVM.shared + + @Test + func getFixedBytes() throws { + let env = try jvm.environment() + let arrays = HelloJavaKitArrays(environment: env) + + let bytes: [Int8] = arrays.getFixedBytes() + #expect(bytes == [1, 2, 3, 4, 5]) + } + + @Test + func getEmptyBytes() throws { + let env = try jvm.environment() + let arrays = HelloJavaKitArrays(environment: env) + + let bytes: [Int8] = arrays.getEmptyBytes() + #expect(bytes.isEmpty) + } + + @Test + func filledBytes() throws { + let env = try jvm.environment() + let arrays = HelloJavaKitArrays(environment: env) + + let bytes: [Int8] = arrays.filledBytes(4, 42) + #expect(bytes == [42, 42, 42, 42]) + } + + @Test + func reverseBytes() throws { + let env = try jvm.environment() + let arrays = HelloJavaKitArrays(environment: env) + + let reversed: [Int8] = arrays.reverseBytes([10, 20, 30]) + #expect(reversed == [30, 20, 10]) + } + + @Test + func getFixedInts() throws { + let env = try jvm.environment() + let arrays = HelloJavaKitArrays(environment: env) + + let ints: [Int32] = arrays.getFixedInts() + #expect(ints == [100, 200, 300]) + } + + @Test + func doubleLongs() throws { + let env = try jvm.environment() + let arrays = HelloJavaKitArrays(environment: env) + + let longs: [Int64] = arrays.doubleLongs([1, 2, 3]) + #expect(longs == [1, 2, 3, 1, 2, 3]) + } + + @Test + func stringToBytes() throws { + let env = try jvm.environment() + let arrays = HelloJavaKitArrays(environment: env) + + let bytes: [Int8] = arrays.stringToBytes("Hi") + // "Hi" in UTF-8 is [0x48, 0x69] + #expect(bytes == [0x48, 0x69]) + } + + @Test + func getGreetings() throws { + let env = try jvm.environment() + let arrays = HelloJavaKitArrays(environment: env) + + let greetings: [String] = arrays.getGreetings() + #expect(greetings == ["hello", "world", "from", "java"]) + } +}