forked from swiftlang/swift-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProtocolCallbacksTest.java
More file actions
117 lines (97 loc) · 3.5 KB
/
ProtocolCallbacksTest.java
File metadata and controls
117 lines (97 loc) · 3.5 KB
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 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;
import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.SwiftArena;
import org.swift.swiftkit.core.annotations.Unsigned;
import java.util.Optional;
import java.util.OptionalLong;
import static org.junit.jupiter.api.Assertions.*;
public class ProtocolCallbacksTest {
static class JavaCallbacks implements CallbackProtocol {
@Override
public boolean withBool(boolean input) {
return input;
}
@Override
public byte withInt8(byte input) {
return input;
}
@Override
public @Unsigned char withUInt16(char input) {
return input;
}
@Override
public short withInt16(short input) {
return input;
}
@Override
public int withInt32(int input) {
return input;
}
@Override
public long withInt64(long input) {
return input;
}
@Override
public float withFloat(float input) {
return input;
}
@Override
public double withDouble(double input) {
return input;
}
@Override
public String withString(String input) {
return input;
}
@Override
public void withVoid() {}
@Override
public MySwiftClass withObject(MySwiftClass input, SwiftArena swiftArena$) {
return input;
}
@Override
public OptionalLong withOptionalInt64(OptionalLong input) {
return input;
}
@Override
public Optional<MySwiftClass> withOptionalObject(Optional<MySwiftClass> input, SwiftArena swiftArena$) {
return input;
}
}
@Test
void primitiveCallbacks() {
try (var arena = SwiftArena.ofConfined()) {
JavaCallbacks callbacks = new JavaCallbacks();
var object = MySwiftClass.init(5, 3, arena);
var optionalObject = Optional.of(MySwiftClass.init(10, 10, arena));
var output = MySwiftLibrary.outputCallbacks(callbacks, true, (byte) 1, (char) 16, (short) 16, (int) 32, 64L, 1.34f, 1.34, "Hello from Java!", object, OptionalLong.empty(), optionalObject, arena);
assertEquals(1, output.getInt8());
assertEquals(16, output.getUint16());
assertEquals(16, output.getInt16());
assertEquals(32, output.getInt32());
assertEquals(64, output.getInt64());
assertEquals(1.34f, output.get_float());
assertEquals(1.34, output.get_double());
assertEquals("Hello from Java!", output.getString());
assertFalse(output.getOptionalInt64().isPresent());
assertEquals(5, output.getObject(arena).getX());
assertEquals(3, output.getObject(arena).getY());
var optionalObjectOutput = output.getOptionalObject(arena);
assertTrue(optionalObjectOutput.isPresent());
assertEquals(10, optionalObjectOutput.get().getX());
}
}
}