1 /* 2 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import org.testng.annotations.Test; 25 26 import java.lang.foreign.*; 27 import java.util.List; 28 import java.util.Optional; 29 import java.util.Set; 30 31 import org.testng.annotations.*; 32 33 import static org.testng.Assert.*; 34 35 /* 36 * @test 37 * @run testng CompositeLookupTest 38 */ 39 public class CompositeLookupTest { 40 41 @Test(dataProvider = "testCases") 42 public void testLookups(SymbolLookup lookup, List<Result> results) { 43 for (Result result : results) { 44 switch (result) { 45 case Success(String name, long expectedLookupId) -> { 46 Optional<MemorySegment> symbol = lookup.find(name); 47 assertTrue(symbol.isPresent()); 48 assertEquals(symbol.get().address(), expectedLookupId); 49 } 50 case Failure(String name) -> { 51 Optional<MemorySegment> symbol = lookup.find(name); 52 assertFalse(symbol.isPresent()); 53 } 54 } 55 } 56 } 57 58 static class TestLookup implements SymbolLookup { 59 60 private Set<String> symbols; 61 private long id; 62 63 public TestLookup(long id, String... symbols) { 64 this.id = id; 65 this.symbols = Set.of(symbols); 66 } 67 68 @Override 69 public Optional<MemorySegment> find(String name) { 70 return symbols.contains(name) ? 71 Optional.of(MemorySegment.ofAddress(id)) : Optional.empty(); 72 } 73 } 74 75 sealed interface Result { } 76 record Success(String name, long expectedLookupId) implements Result { } 77 record Failure(String name) implements Result { } 78 79 @DataProvider(name = "testCases") 80 public Object[][] testCases() { 81 return new Object[][]{ 82 { 83 new TestLookup(1, "a", "b", "c") 84 .or(new TestLookup(2,"d", "e", "f")) 85 .or(new TestLookup(3,"g", "h", "i")), 86 List.of( 87 new Success("a", 1), 88 new Success("b", 1), 89 new Success("c", 1), 90 new Success("d", 2), 91 new Success("e", 2), 92 new Success("f", 2), 93 new Success("g", 3), 94 new Success("h", 3), 95 new Success("i", 3), 96 new Failure("j") 97 ) 98 }, 99 { 100 new TestLookup(1, "a", "b", "c") 101 .or(new TestLookup(2,"a", "b", "c")) 102 .or(new TestLookup(3,"a", "b", "c")), 103 List.of( 104 new Success("a", 1), 105 new Success("b", 1), 106 new Success("c", 1), 107 new Failure("d") 108 ) 109 }, 110 { 111 new TestLookup(1 ) 112 .or(new TestLookup(2)) 113 .or(new TestLookup(3,"a", "b", "c")), 114 List.of( 115 new Success("a", 3), 116 new Success("b", 3), 117 new Success("c", 3), 118 new Failure("d") 119 ) 120 }, 121 { 122 new TestLookup(1, "a", "b", "c") 123 .or(new TestLookup(2,"d") 124 .or(new TestLookup(3,"e")) 125 .or(new TestLookup(4,"f"))) 126 .or(new TestLookup(5,"g") 127 .or(new TestLookup(6,"h")) 128 .or(new TestLookup(7,"i"))), 129 List.of( 130 new Success("a", 1), 131 new Success("b", 1), 132 new Success("c", 1), 133 new Success("d", 2), 134 new Success("e", 3), 135 new Success("f", 4), 136 new Success("g", 5), 137 new Success("h", 6), 138 new Success("i", 7), 139 new Failure("j") 140 ) 141 }, 142 }; 143 } 144 }