1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 public class Main {
main(String[] args)18   public static void main(String[] args) {
19     assertEquals(4.2f, returnFloat());
20     float[] a = new float[2];
21     a[0] = 42.2f;
22     a[1] = 3.2f;
23     assertEquals(45.4f, returnFloat(a));
24 
25     assertEquals(4.4, returnDouble());
26     double[] b = new double[1];
27     b[0] = 42.4;
28     assertEquals(42.4, returnDouble(b));
29 
30     assertEquals(4.2f, invokeReturnFloat());
31     assertEquals(4.4, invokeReturnDouble());
32     assertEquals(4.2f, takeAFloat(4.2f));
33     assertEquals(3.1, takeADouble(3.1));
34     assertEquals(12.7, takeThreeDouble(3.1, 4.4, 5.2));
35     assertEquals(12.7f, takeThreeFloat(3.1f, 4.4f, 5.2f));
36     assertEquals(4.2f, invokeTakeAFloat(4.2f));
37     assertEquals(3.1, invokeTakeADouble(3.1));
38     assertEquals(12.7, invokeTakeThreeDouble(3.1, 4.4, 5.2));
39     assertEquals(12.7f, invokeTakeThreeFloat(3.1f, 4.4f, 5.2f));
40 
41     testArrayOperations(new float[2], 0, 1.2f, 3.4f);
42     testArrayOperations(new double[2], 0, 4.1, 7.6);
43   }
44 
invokeReturnFloat()45   public static float invokeReturnFloat() {
46     return returnFloat();
47   }
48 
invokeReturnDouble()49   public static double invokeReturnDouble() {
50     return returnDouble();
51   }
52 
returnFloat()53   public static float returnFloat() {
54     return 4.2f;
55   }
56 
returnFloat(float[] a)57   public static float returnFloat(float[] a) {
58     return a[0] + a[1];
59   }
60 
returnDouble()61   public static double returnDouble() {
62     return 4.4;
63   }
64 
returnDouble(double[] a)65   public static double returnDouble(double[] a) {
66     return a[0];
67   }
68 
takeAFloat(float a)69   public static float takeAFloat(float a) {
70     return a;
71   }
72 
takeADouble(double a)73   public static double takeADouble(double a) {
74     return a;
75   }
76 
takeThreeDouble(double a, double b, double c)77   public static double takeThreeDouble(double a, double b, double c) {
78     return a + b + c;
79   }
80 
takeThreeFloat(float a, float b, float c)81   public static float takeThreeFloat(float a, float b, float c) {
82     return a + b + c;
83   }
84 
invokeTakeAFloat(float a)85   public static float invokeTakeAFloat(float a) {
86     return takeAFloat(a);
87   }
88 
invokeTakeADouble(double a)89   public static double invokeTakeADouble(double a) {
90     return takeADouble(a);
91   }
92 
invokeTakeThreeDouble(double a, double b, double c)93   public static double invokeTakeThreeDouble(double a, double b, double c) {
94     return takeThreeDouble(a, b, c);
95   }
96 
invokeTakeThreeFloat(float a, float b, float c)97   public static float invokeTakeThreeFloat(float a, float b, float c) {
98     return takeThreeFloat(a, b, c);
99   }
100 
101   // Test simple operations on a float array to ensure the register allocator works
102   // properly.
testArrayOperations(float[] a, int index, float value1, float value2)103   public static void testArrayOperations(float[] a, int index, float value1, float value2) {
104     a[0] = value1;
105     a[1] = value2;
106     assertEquals(value1 + value2, a[0] + a[1]);
107     a[0] = 0.0f;
108     a[1] = 0.0f;
109     assertEquals(0.0f, a[0] + a[1]);
110     a[index] = value1;
111     a[index + 1] = value2;
112     assertEquals(value1 + value2, a[0] + a[1]);
113   }
114 
115   // Test simple operations on a double array to ensure the register allocator works
116   // properly.
testArrayOperations(double[] a, int index, double value1, double value2)117   public static void testArrayOperations(double[] a, int index, double value1, double value2) {
118     a[0] = value1;
119     a[1] = value2;
120     assertEquals(value1 + value2, a[0] + a[1]);
121     a[0] = 0.0;
122     a[1] = 0.0;
123     assertEquals(0.0, a[0] + a[1]);
124     a[index] = value1;
125     a[index + 1] = value2;
126     assertEquals(value1 + value2, a[0] + a[1]);
127   }
128 
assertEquals(float expected, float actual)129   public static void assertEquals(float expected, float actual) {
130     if (expected != actual) {
131       throw new AssertionError("Expected " + expected + " got " + actual);
132     }
133   }
134 
assertEquals(double expected, double actual)135   public static void assertEquals(double expected, double actual) {
136     if (expected != actual) {
137       throw new AssertionError("Expected " + expected + " got " + actual);
138     }
139   }
140 }
141