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 package android.sample.cts;
17 
18 import android.sample.SampleDeviceActivity;
19 
20 import androidx.test.platform.app.InstrumentationRegistry;
21 import androidx.test.rule.ActivityTestRule;
22 import androidx.test.runner.AndroidJUnit4;
23 
24 import org.junit.Assert;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 import com.android.compatibility.common.util.DeviceReportLog;
30 import com.android.compatibility.common.util.MeasureRun;
31 import com.android.compatibility.common.util.MeasureTime;
32 import com.android.compatibility.common.util.ResultType;
33 import com.android.compatibility.common.util.ResultUnit;
34 import com.android.compatibility.common.util.Stat;
35 
36 import java.util.Arrays;
37 import java.util.Random;
38 
39 /**
40  * A simple compatibility test which includes results in the report.
41  *
42  * This test measures the time taken to run a workload and adds in the report.
43  */
44 @RunWith(AndroidJUnit4.class)
45 public class SampleDeviceResultTest {
46 
47     /**
48      * Name of the report log to store test metrics.
49      */
50     private static final String REPORT_LOG_NAME = "CtsSampleDeviceTestCases";
51 
52     /**
53      * The number of times to repeat the test.
54      */
55     private static final int REPEAT = 5;
56 
57     /**
58      * A {@link Random} to generate random integers to test the sort.
59      */
60     private static final Random random = new Random(12345);
61 
62     /**
63      * Measures the time taken to sort an array.
64      */
65     @Test
testSort()66     public void testSort() throws Exception {
67         // MeasureTime runs the workload N times and records the time taken by each run.
68         double[] result = MeasureTime.measure(REPEAT, new MeasureRun() {
69             /**
70              * The size of the array to sort.
71              */
72             private static final int ARRAY_SIZE = 100000;
73             private int[] array;
74             @Override
75             public void prepare(int i) throws Exception {
76                 array = createArray(ARRAY_SIZE);
77             }
78             @Override
79             public void run(int i) throws Exception {
80                 Arrays.sort(array);
81                 Assert.assertTrue("Array not sorted", isSorted(array));
82             }
83         });
84         // Compute the stats.
85         Stat.StatResult stat = Stat.getStat(result);
86         // Create a new report to hold the metrics.
87         String streamName = "test_sort";
88         DeviceReportLog reportLog = new DeviceReportLog(REPORT_LOG_NAME, streamName);
89         // Add the results to the report.
90         reportLog.addValues("times", result, ResultType.LOWER_BETTER, ResultUnit.MS);
91         reportLog.addValue("min", stat.mMin, ResultType.LOWER_BETTER, ResultUnit.MS);
92         reportLog.addValue("max", stat.mMax, ResultType.LOWER_BETTER, ResultUnit.MS);
93         // Set a summary.
94         reportLog.setSummary("average", stat.mAverage, ResultType.LOWER_BETTER, ResultUnit.MS);
95         // Submit the report to the given instrumentation.
96         reportLog.submit(InstrumentationRegistry.getInstrumentation());
97     }
98 
99     /**
100      * Creates an array filled with random numbers of the given size.
101      */
createArray(int size)102     private static int[] createArray(int size) {
103         int[] array = new int[size];
104         for (int i = 0; i < size; i++) {
105             array[i] = random.nextInt();
106         }
107         return array;
108     }
109 
110     /**
111      * Tests an array is sorted.
112      */
isSorted(int[] array)113     private static boolean isSorted(int[] array) {
114         int len = array.length;
115         for (int i = 0, j = 1; j < len; i++, j++) {
116             if (array[i] > array[j]) {
117                 return false;
118             }
119         }
120         return true;
121     }
122 }
123