-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathTupleTest.java
More file actions
137 lines (122 loc) · 4.97 KB
/
TupleTest.java
File metadata and controls
137 lines (122 loc) · 4.97 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//===----------------------------------------------------------------------===//
//
// 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;
import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.SwiftArena;
import org.swift.swiftkit.core.tuple.Tuple2;
import org.swift.swiftkit.core.tuple.Tuple3;
import org.swift.swiftkit.core.tuple.Tuple16;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Optional;
import java.util.OptionalLong;
public class TupleTest {
@Test
void returnPair() {
Tuple2<Long, String> result = MySwiftLibrary.returnPair();
assertEquals(42L, result.$0);
assertEquals("hello", result.$1);
}
@Test
void takePair() {
String result = MySwiftLibrary.takePair(new Tuple2<>(99L, "world"));
assertEquals("99:world", result);
}
@Test
void labeledTuple() {
var result = MySwiftLibrary.labeledTuple();
// Access via named accessors
assertEquals(10, result.x());
assertEquals(20, result.y());
// Positional access still works (inherited from Tuple2)
assertEquals(10, result.$0);
assertEquals(20, result.$1);
// The labelled tuple is a subclass of Tuple2
assertInstanceOf(Tuple2.class, result);
// And the generic types match positionally as well
@SuppressWarnings("unused")
Tuple2<Integer, Integer> check = result;
}
@Test
void echoTriple() {
Tuple3<Boolean, Double, Long> input = new Tuple3<>(true, 3.14, 100L);
Tuple3<Boolean, Double, Long> result = MySwiftLibrary.echoTriple(input);
assertEquals(true, result.$0);
assertEquals(3.14, result.$1, 0.001);
assertEquals(100L, result.$2);
}
@Test
void echoOptionalTriple() {
try (var arena = SwiftArena.ofConfined()) {
Tuple3<OptionalLong, Optional<String>, Optional<Alignment>> input = new Tuple3<>(
OptionalLong.of(100L),
Optional.of("hello"),
Optional.of(Alignment.horizontal(arena))
);
Tuple3<OptionalLong, Optional<String>, Optional<Alignment>> result = MySwiftLibrary.echoOptionalTriple(input, arena);
assertEquals(input.$0, result.$0);
assertEquals(input.$1, result.$1);
assertEquals(input.$2.map(Alignment::getDiscriminator), result.$2.map(Alignment::getDiscriminator));
}
try (var arena = SwiftArena.ofConfined()) {
Tuple3<OptionalLong, Optional<String>, Optional<Alignment>> input = new Tuple3<>(
OptionalLong.empty(),
Optional.empty(),
Optional.empty()
);
Tuple3<OptionalLong, Optional<String>, Optional<Alignment>> result = MySwiftLibrary.echoOptionalTriple(input, arena);
assertEquals(input.$0, result.$0);
assertEquals(input.$1, result.$1);
assertEquals(input.$2.map(Alignment::getDiscriminator), result.$2.map(Alignment::getDiscriminator));
}
}
@Test
void makeBigTuple() {
Tuple16<Boolean, Byte, Short, Character,
Integer, Long, Float, Double,
String, Boolean, Byte, Short,
Character, Integer, Long, Float> result = MySwiftLibrary.makeBigTuple();
assertEquals(true, result.$0);
assertEquals((byte) 1, result.$1);
assertEquals((short) 2, result.$2);
assertEquals((char) 3, result.$3);
assertEquals(4, result.$4);
assertEquals(5L, result.$5);
assertEquals(6.0f, result.$6);
assertEquals(7.0, result.$7);
assertEquals("eight", result.$8);
assertEquals(false, result.$9);
assertEquals((byte) 9, result.$10);
assertEquals((short) 10, result.$11);
assertEquals((char) 11, result.$12);
assertEquals(12, result.$13);
assertEquals(13L, result.$14);
assertEquals(14.0f, result.$15);
}
@Test
void namedByteArrayTuple() {
var result = MySwiftLibrary.namedByteArrayTuple();
assertArrayEquals(new byte[]{1, 2, 3}, result.name());
assertArrayEquals(new byte[]{4, 5}, result.another());
assertArrayEquals(new byte[]{1, 2, 3}, (byte[]) result.$0);
assertArrayEquals(new byte[]{4, 5}, (byte[]) result.$1);
}
@Test
void genericTypeTuple() {
try (var arena = SwiftArena.ofConfined()) {
var result = MySwiftLibrary.genericTypeTuple(arena);
assertEquals("1.23", result.$0.getDescription());
assertEquals(Alignment.Discriminator.HORIZONTAL, result.$1.getDiscriminator());
}
}
}