1 /*
  2  * Copyright (c) 2024, 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 /*
 25  * @test
 26  * @modules jdk.incubator.code
 27  * @run junit TestTraverse
 28  */
 29 
 30 import jdk.incubator.code.CodeElement;
 31 import jdk.incubator.code.Reflect;
 32 import jdk.incubator.code.CodeTransformer;
 33 import jdk.incubator.code.Op;
 34 import jdk.incubator.code.analysis.SSA;
 35 import jdk.incubator.code.dialect.core.CoreOp;
 36 import org.junit.jupiter.api.Assertions;
 37 import org.junit.jupiter.api.Test;
 38 
 39 import java.lang.reflect.Method;
 40 import java.util.ArrayList;
 41 import java.util.List;
 42 import java.util.Optional;
 43 import java.util.function.BiFunction;
 44 import java.util.stream.Stream;
 45 
 46 public class TestTraverse {
 47 
 48     @Reflect
 49     private static int f(String s, int i, List<Object> acc) {
 50         char c = s.charAt(i);
 51         int d = (c - '0');
 52         int n = s.length();
 53         while (++i < n) {
 54             c = s.charAt(i);
 55             if (c >= '0' && c <= '9') {
 56                 d = d * 10 + (c - '0');
 57                 continue;
 58             }
 59             break;
 60         }
 61         acc.add(d);
 62         return i;
 63     }
 64 
 65     @Test
 66     public void test() {
 67         CoreOp.FuncOp f = getFuncOp("f");
 68         testTraverse(f);
 69 
 70         f = f.transform(CodeTransformer.LOWERING_TRANSFORMER);
 71         testTraverse(f);
 72 
 73         f = SSA.transform(f);
 74         testTraverse(f);
 75     }
 76 
 77     void testTraverse(Op op) {
 78         List<CodeElement<?, ?>> tl = traverse(new ArrayList<>(), op, (l, e) -> {
 79             l.add(e);
 80             return l;
 81         });
 82         Assertions.assertEquals(op.elements().toList(), tl);
 83 
 84         Assertions.assertEquals(op.elements().limit(2).toList(), tl.subList(0, 2));
 85     }
 86 
 87     static <T> T traverse(T t, CodeElement<?, ?> e, BiFunction<T, CodeElement<?, ?>, T> v) {
 88         t = v.apply(t, e);
 89         for (CodeElement<?, ?> c : e.children()) {
 90             t = traverse(t, c, v);
 91         }
 92 
 93         return t;
 94     }
 95 
 96     static CoreOp.FuncOp getFuncOp(String name) {
 97         Optional<Method> om = Stream.of(TestTraverse.class.getDeclaredMethods())
 98                 .filter(m -> m.getName().equals(name))
 99                 .findFirst();
100 
101         Method m = om.get();
102         return Op.ofMethod(m).get();
103     }
104 }