-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathSwiftDictionaryMapTest.java
More file actions
95 lines (82 loc) · 3.65 KB
/
SwiftDictionaryMapTest.java
File metadata and controls
95 lines (82 loc) · 3.65 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
//===----------------------------------------------------------------------===//
//
// 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 java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.swift.swiftkit.core.collections.SwiftDictionaryMap;
import org.swift.swiftkit.core.SwiftArena;
import static org.junit.jupiter.api.Assertions.*;
public class SwiftDictionaryMapTest {
@Test
void makeStringToLongDictionary() {
try (var arena = SwiftArena.ofConfined()) {
SwiftDictionaryMap<String, Long> dict = MySwiftLibrary.makeStringToLongDictionary(arena);
assertEquals(2, dict.size());
assertEquals(1L, dict.get("hello"));
assertEquals(2L, dict.get("world"));
assertTrue(dict.containsKey("hello"));
assertFalse(dict.containsKey("missing"));
assertNull(dict.get("missing"));
}
}
@Test
void stringToLongDictionaryRoundtrip() {
try (var arena = SwiftArena.ofConfined()) {
SwiftDictionaryMap<String, Long> original = MySwiftLibrary.makeStringToLongDictionary(arena);
SwiftDictionaryMap<String, Long> roundtripped = MySwiftLibrary.stringToLongDictionary(original, arena);
assertEquals(original.size(), roundtripped.size());
assertEquals(original.get("hello"), roundtripped.get("hello"));
assertEquals(original.get("world"), roundtripped.get("world"));
}
}
@Test
void insertIntoStringToLongDictionary() {
try (var arena = SwiftArena.ofConfined()) {
SwiftDictionaryMap<String, Long> original = MySwiftLibrary.makeStringToLongDictionary(arena);
assertEquals(2, original.size());
// Insert a new key by passing the dictionary through Swift
SwiftDictionaryMap<String, Long> modified =
MySwiftLibrary.insertIntoStringToLongDictionary(original, "swift", 42L, arena);
// The modified dictionary has the new key
assertEquals(3, modified.size());
assertEquals(1L, modified.get("hello"));
assertEquals(2L, modified.get("world"));
assertEquals(42L, modified.get("swift"));
// The original dictionary is unchanged (Swift value semantics — it's a copy)
assertEquals(2, original.size());
assertNull(original.get("swift"));
}
}
@Test
void toJavaMap() {
Map<String, Long> javaMap;
try (var arena = SwiftArena.ofConfined()) {
SwiftDictionaryMap<String, Long> dict = MySwiftLibrary.makeStringToLongDictionary(arena);
javaMap = dict.toJavaMap();
// The copy has the same contents as the original
assertEquals(2, javaMap.size());
assertEquals(1L, javaMap.get("hello"));
assertEquals(2L, javaMap.get("world"));
assertTrue(javaMap.containsKey("hello"));
assertFalse(javaMap.containsKey("missing"));
// It's a plain HashMap, not the native-backed map
assertInstanceOf(HashMap.class, javaMap);
}
// The Java map copy survives arena closure
assertEquals(2, javaMap.size());
assertEquals(1L, javaMap.get("hello"));
assertEquals(2L, javaMap.get("world"));
}
}