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  * @enablePreview
 38  * @run testng CompositeLookupTest
 39  */
 40 public class CompositeLookupTest {
 41 
 42     @Test(dataProvider = "testCases")
 43     public void testLookups(SymbolLookup lookup, List<Result> results) {
 44         for (Result result : results) {
 45             switch (result) {
 46                 case Success(String name, long expectedLookupId) -> {
 47                     Optional<MemorySegment> symbol = lookup.find(name);
 48                     assertTrue(symbol.isPresent());
 49                     assertEquals(symbol.get().address(), expectedLookupId);
 50                 }
 51                 case Failure(String name) -> {
 52                     Optional<MemorySegment> symbol = lookup.find(name);
 53                     assertFalse(symbol.isPresent());
 54                 }
 55             }
 56         }
 57     }
 58 
 59     static class TestLookup implements SymbolLookup {
 60 
 61         private Set<String> symbols;
 62         private long id;
 63 
 64         public TestLookup(long id, String... symbols) {
 65             this.id = id;
 66             this.symbols = Set.of(symbols);
 67         }
 68 
 69         @Override
 70         public Optional<MemorySegment> find(String name) {
 71             return symbols.contains(name) ?
 72                     Optional.of(MemorySegment.ofAddress(id)) : Optional.empty();
 73         }
 74     }
 75 
 76     sealed interface Result { }
 77     record Success(String name, long expectedLookupId) implements Result { }
 78     record Failure(String name) implements Result { }
 79 
 80     @DataProvider(name = "testCases")
 81     public Object[][] testCases() {
 82         return new Object[][]{
 83                 {
 84                     new TestLookup(1, "a", "b", "c")
 85                             .or(new TestLookup(2,"d", "e", "f"))
 86                             .or(new TestLookup(3,"g", "h", "i")),
 87                     List.of(
 88                             new Success("a", 1),
 89                             new Success("b", 1),
 90                             new Success("c", 1),
 91                             new Success("d", 2),
 92                             new Success("e", 2),
 93                             new Success("f", 2),
 94                             new Success("g", 3),
 95                             new Success("h", 3),
 96                             new Success("i", 3),
 97                             new Failure("j")
 98                     )
 99                 },
100                 {
101                         new TestLookup(1, "a", "b", "c")
102                                 .or(new TestLookup(2,"a", "b", "c"))
103                                 .or(new TestLookup(3,"a", "b", "c")),
104                         List.of(
105                                 new Success("a", 1),
106                                 new Success("b", 1),
107                                 new Success("c", 1),
108                                 new Failure("d")
109                         )
110                 },
111                 {
112                         new TestLookup(1 )
113                                 .or(new TestLookup(2))
114                                 .or(new TestLookup(3,"a", "b", "c")),
115                         List.of(
116                                 new Success("a", 3),
117                                 new Success("b", 3),
118                                 new Success("c", 3),
119                                 new Failure("d")
120                         )
121                 },
122                 {
123                         new TestLookup(1, "a", "b", "c")
124                                 .or(new TestLookup(2,"d")
125                                         .or(new TestLookup(3,"e"))
126                                         .or(new TestLookup(4,"f")))
127                                 .or(new TestLookup(5,"g")
128                                         .or(new TestLookup(6,"h"))
129                                         .or(new TestLookup(7,"i"))),
130                         List.of(
131                                 new Success("a", 1),
132                                 new Success("b", 1),
133                                 new Success("c", 1),
134                                 new Success("d", 2),
135                                 new Success("e", 3),
136                                 new Success("f", 4),
137                                 new Success("g", 5),
138                                 new Success("h", 6),
139                                 new Success("i", 7),
140                                 new Failure("j")
141                         )
142                 },
143         };
144     }
145 }