summaryrefslogtreecommitdiffstats
path: root/jack/tests/com/android/jack/FibonacciThreeAddressTest.java
blob: 3cc829e0f02021dc560b6be752395b6ecc8bd5fb (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.jack;

import com.android.jack.dx.dex.DexOptions;
import com.android.jack.dx.dex.file.ClassDefItem;
import com.android.jack.dx.dex.file.DexFile;
import com.android.jack.ir.ast.JDefinedClassOrInterface;
import com.android.jack.ir.ast.JSession;
import com.android.jack.scheduling.marker.ClassDefItemMarker;

import junit.framework.Assert;

import org.junit.Test;

import java.io.File;

/**
 * JUnit test for compilation of Fibonacci (three-address style).
 */
public class FibonacciThreeAddressTest {

  private static final String CLASS_BINARY_NAME = "com/android/jack/fibonacci/test001/jack/FibonacciThreeAddress";
  private static final String CLASS_SIGNATURE = "L" + CLASS_BINARY_NAME + ";";
  private static final String JAVA_FILENAME = "FibonacciThreeAddress.java";
  private static final File JAVA_FILEPATH = TestTools.getJackTestFromBinaryName(CLASS_BINARY_NAME);

  /**
   * Verifies that FibonacciThreeAddress can be loaded in J-AST.
   */
  @Test
  public void testLoadFiboInJAst() throws Exception {
    JSession session = TestTools.buildJAst(TestTools.buildCommandLineArgs(JAVA_FILEPATH));
    Assert.assertNotNull(session);

    JDefinedClassOrInterface fibo = (JDefinedClassOrInterface) session.getLookup().getType(CLASS_SIGNATURE);
    Assert.assertNotNull(fibo);
  }

  /**
   * Verifies that FibonacciThreeAddress can be compiled into a {@code DexFile} containing
   * {@code ClassDefItem}.
   */
  @Test
  public void testBuildFiboDexFile() throws Exception {
    Options fiboArgs = TestTools.buildCommandLineArgs(JAVA_FILEPATH);
    fiboArgs.addProperty(Options.METHOD_FILTER.getName(), "reject-all-methods");
    JSession session = TestTools.buildSession(fiboArgs);

    JDefinedClassOrInterface fibo = (JDefinedClassOrInterface) session.getLookup().getType(CLASS_SIGNATURE);
    Assert.assertNotNull(fibo);

    ClassDefItemMarker marker = fibo.getMarker(ClassDefItemMarker.class);
    Assert.assertNotNull(marker);

    DexFile dexFile = new DexFile(new DexOptions());
    ClassDefItem cdi = marker.getClassDefItem();
    Assert.assertNotNull(cdi);
    dexFile.add(cdi);
    dexFile.prepare();

    // Check source file
    String sourceFilename = cdi.getSourceFile().getString();
    Assert.assertEquals(JAVA_FILENAME, sourceFilename);
  }

}