Skip to content

Commit dbc911e

Browse files
authored
javakit: Add more runtime tests for arrays to the sample app (#703)
1 parent a5d78af commit dbc911e

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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+
package com.example.swift;
16+
17+
public class HelloJavaKitArrays {
18+
19+
public byte[] getFixedBytes() {
20+
return new byte[] { 1, 2, 3, 4, 5 };
21+
}
22+
23+
public byte[] getEmptyBytes() {
24+
return new byte[0];
25+
}
26+
27+
public byte[] filledBytes(int size, byte value) {
28+
byte[] result = new byte[size];
29+
java.util.Arrays.fill(result, value);
30+
return result;
31+
}
32+
33+
public byte[] reverseBytes(byte[] input) {
34+
byte[] result = new byte[input.length];
35+
for (int i = 0; i < input.length; i++) {
36+
result[i] = input[input.length - 1 - i];
37+
}
38+
return result;
39+
}
40+
41+
public int[] getFixedInts() {
42+
return new int[] { 100, 200, 300 };
43+
}
44+
45+
public long[] doubleLongs(long[] input) {
46+
long[] result = new long[input.length * 2];
47+
System.arraycopy(input, 0, result, 0, input.length);
48+
System.arraycopy(input, 0, result, input.length, input.length);
49+
return result;
50+
}
51+
52+
public byte[] stringToBytes(String s) {
53+
return s.getBytes(java.nio.charset.StandardCharsets.UTF_8);
54+
}
55+
56+
public String[] getGreetings() {
57+
return new String[] { "hello", "world", "from", "java" };
58+
}
59+
}

Samples/JavaKitSampleApp/Sources/JavaKitExample/swift-java.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"classes" : {
33
"com.example.swift.HelloSwift" : "HelloSwift",
44
"com.example.swift.HelloSubclass" : "HelloSubclass",
5+
"com.example.swift.HelloJavaKitArrays" : "HelloJavaKitArrays",
56
"com.example.swift.JavaKitSampleMain" : "JavaKitSampleMain",
67
"com.example.swift.ThreadSafeHelperClass" : "ThreadSafeHelperClass"
78
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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 JavaKitExample
16+
import SwiftJava
17+
import Testing
18+
19+
@Suite
20+
struct JavaKitArrayRuntimeTests {
21+
22+
let jvm = try JavaKitSampleJVM.shared
23+
24+
@Test
25+
func getFixedBytes() throws {
26+
let env = try jvm.environment()
27+
let arrays = HelloJavaKitArrays(environment: env)
28+
29+
let bytes: [Int8] = arrays.getFixedBytes()
30+
#expect(bytes == [1, 2, 3, 4, 5])
31+
}
32+
33+
@Test
34+
func getEmptyBytes() throws {
35+
let env = try jvm.environment()
36+
let arrays = HelloJavaKitArrays(environment: env)
37+
38+
let bytes: [Int8] = arrays.getEmptyBytes()
39+
#expect(bytes.isEmpty)
40+
}
41+
42+
@Test
43+
func filledBytes() throws {
44+
let env = try jvm.environment()
45+
let arrays = HelloJavaKitArrays(environment: env)
46+
47+
let bytes: [Int8] = arrays.filledBytes(4, 42)
48+
#expect(bytes == [42, 42, 42, 42])
49+
}
50+
51+
@Test
52+
func reverseBytes() throws {
53+
let env = try jvm.environment()
54+
let arrays = HelloJavaKitArrays(environment: env)
55+
56+
let reversed: [Int8] = arrays.reverseBytes([10, 20, 30])
57+
#expect(reversed == [30, 20, 10])
58+
}
59+
60+
@Test
61+
func getFixedInts() throws {
62+
let env = try jvm.environment()
63+
let arrays = HelloJavaKitArrays(environment: env)
64+
65+
let ints: [Int32] = arrays.getFixedInts()
66+
#expect(ints == [100, 200, 300])
67+
}
68+
69+
@Test
70+
func doubleLongs() throws {
71+
let env = try jvm.environment()
72+
let arrays = HelloJavaKitArrays(environment: env)
73+
74+
let longs: [Int64] = arrays.doubleLongs([1, 2, 3])
75+
#expect(longs == [1, 2, 3, 1, 2, 3])
76+
}
77+
78+
@Test
79+
func stringToBytes() throws {
80+
let env = try jvm.environment()
81+
let arrays = HelloJavaKitArrays(environment: env)
82+
83+
let bytes: [Int8] = arrays.stringToBytes("Hi")
84+
// "Hi" in UTF-8 is [0x48, 0x69]
85+
#expect(bytes == [0x48, 0x69])
86+
}
87+
88+
@Test
89+
func getGreetings() throws {
90+
let env = try jvm.environment()
91+
let arrays = HelloJavaKitArrays(environment: env)
92+
93+
let greetings: [String] = arrays.getGreetings()
94+
#expect(greetings == ["hello", "world", "from", "java"])
95+
}
96+
}

0 commit comments

Comments
 (0)