-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedBlackTreeTest.java
69 lines (55 loc) · 2.81 KB
/
RedBlackTreeTest.java
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
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
import kala.collection.immutable.ImmutableSeq;
import kala.collection.immutable.primitive.ImmutableIntSeq;
import org.aya.normalize.Normalizer;
import org.aya.syntax.compile.JitCon;
import org.aya.syntax.compile.JitData;
import org.aya.syntax.compile.JitFn;
import org.aya.syntax.core.term.Term;
import org.aya.syntax.core.term.call.DataCall;
import org.aya.syntax.core.term.repr.IntegerTerm;
import org.aya.syntax.core.term.repr.ListTerm;
import org.aya.syntax.literate.CodeOptions;
import org.aya.syntax.ref.ModulePath;
import org.aya.syntax.ref.QPath;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.Random;
import java.util.function.Function;
import java.util.function.IntFunction;
import static org.aya.compiler.NameSerializer.getClassName;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class RedBlackTreeTest {
@Test public void test1() throws IOException {
var stream = RedBlackTreeTest.class.getResourceAsStream("/TreeSort.aya");
assert stream != null;
var code = new String(stream.readAllBytes());
stream.close();
var result = CompileTest.tyck(code);
var tester = new CompileTester(CompileTest.serializeFrom(result));
tester.compile();
var baka = QPath.fileLevel(ModulePath.of("baka"));
JitData List = tester.loadInstance(getClassName(baka, "List"));
JitCon nil = tester.loadInstance(getClassName(baka.derive("List"), "[]"));
JitCon cons = tester.loadInstance(getClassName(baka.derive("List"), ":>"));
JitData Nat = tester.loadInstance(getClassName(baka, "Nat"));
JitCon O = tester.loadInstance(getClassName(baka.derive("Nat"), "O"));
JitCon S = tester.loadInstance(getClassName(baka.derive("Nat"), "S"));
JitFn tree_sortNat = tester.loadInstance(getClassName(baka, "tree_sortNat"));
var NatCall = new DataCall(Nat, 0, ImmutableSeq.empty());
var ListNatCall = new DataCall(List, 0, ImmutableSeq.of(NatCall));
IntFunction<Term> mkInt = i -> new IntegerTerm(i, O, S, NatCall);
Function<ImmutableIntSeq, Term> mkList = xs -> new ListTerm(xs.mapToObj(mkInt), nil, cons, ListNatCall);
var seed = 114514L;
var random = new Random(seed);
var largeList = mkList.apply(ImmutableIntSeq.fill(300, () -> random.nextInt(200)));
var args = ImmutableSeq.of(largeList);
var normalizer = new Normalizer(result.info().makeTyckState());
var sortResult = normalizer.normalize(tree_sortNat.invoke(null, args), CodeOptions.NormalizeMode.FULL);
assertNotNull(sortResult);
Profiler.profileMany(5, () ->
normalizer.normalize(tree_sortNat.invoke(null, args), CodeOptions.NormalizeMode.FULL));
System.out.println(sortResult.debuggerOnlyToString());
}
}