|
| 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 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.swift.swiftkit.core.collections.SwiftDictionaryMap; |
| 21 | +import org.swift.swiftkit.core.SwiftArena; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.*; |
| 24 | + |
| 25 | +public class SwiftDictionaryMapTest { |
| 26 | + @Test |
| 27 | + void makeStringToLongDictionary() { |
| 28 | + try (var arena = SwiftArena.ofConfined()) { |
| 29 | + SwiftDictionaryMap<String, Long> dict = MySwiftLibrary.makeStringToLongDictionary(arena); |
| 30 | + assertEquals(2, dict.size()); |
| 31 | + assertEquals(1L, dict.get("hello")); |
| 32 | + assertEquals(2L, dict.get("world")); |
| 33 | + assertTrue(dict.containsKey("hello")); |
| 34 | + assertFalse(dict.containsKey("missing")); |
| 35 | + assertNull(dict.get("missing")); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void stringToLongDictionaryRoundtrip() { |
| 41 | + try (var arena = SwiftArena.ofConfined()) { |
| 42 | + SwiftDictionaryMap<String, Long> original = MySwiftLibrary.makeStringToLongDictionary(arena); |
| 43 | + SwiftDictionaryMap<String, Long> roundtripped = MySwiftLibrary.stringToLongDictionary(original, arena); |
| 44 | + assertEquals(original.size(), roundtripped.size()); |
| 45 | + assertEquals(original.get("hello"), roundtripped.get("hello")); |
| 46 | + assertEquals(original.get("world"), roundtripped.get("world")); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void insertIntoStringToLongDictionary() { |
| 52 | + try (var arena = SwiftArena.ofConfined()) { |
| 53 | + SwiftDictionaryMap<String, Long> original = MySwiftLibrary.makeStringToLongDictionary(arena); |
| 54 | + assertEquals(2, original.size()); |
| 55 | + |
| 56 | + // Insert a new key by passing the dictionary through Swift |
| 57 | + SwiftDictionaryMap<String, Long> modified = |
| 58 | + MySwiftLibrary.insertIntoStringToLongDictionary(original, "swift", 42L, arena); |
| 59 | + |
| 60 | + // The modified dictionary has the new key |
| 61 | + assertEquals(3, modified.size()); |
| 62 | + assertEquals(1L, modified.get("hello")); |
| 63 | + assertEquals(2L, modified.get("world")); |
| 64 | + assertEquals(42L, modified.get("swift")); |
| 65 | + |
| 66 | + // The original dictionary is unchanged (Swift value semantics — it's a copy) |
| 67 | + assertEquals(2, original.size()); |
| 68 | + assertNull(original.get("swift")); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void toJavaMap() { |
| 74 | + Map<String, Long> javaMap; |
| 75 | + try (var arena = SwiftArena.ofConfined()) { |
| 76 | + SwiftDictionaryMap<String, Long> dict = MySwiftLibrary.makeStringToLongDictionary(arena); |
| 77 | + javaMap = dict.toJavaMap(); |
| 78 | + |
| 79 | + // The copy has the same contents as the original |
| 80 | + assertEquals(2, javaMap.size()); |
| 81 | + assertEquals(1L, javaMap.get("hello")); |
| 82 | + assertEquals(2L, javaMap.get("world")); |
| 83 | + assertTrue(javaMap.containsKey("hello")); |
| 84 | + assertFalse(javaMap.containsKey("missing")); |
| 85 | + |
| 86 | + // It's a plain HashMap, not the native-backed map |
| 87 | + assertInstanceOf(HashMap.class, javaMap); |
| 88 | + } |
| 89 | + |
| 90 | + // The Java map copy survives arena closure |
| 91 | + assertEquals(2, javaMap.size()); |
| 92 | + assertEquals(1L, javaMap.get("hello")); |
| 93 | + assertEquals(2L, javaMap.get("world")); |
| 94 | + } |
| 95 | +} |
0 commit comments