Update to refactor basic statistics helper functions to be accessible to host-side tests.
This is a precursor to host-side power tests which will use these helper functions.
There should be no code changes other then moving existing code out of one class and into another
that is more commonly accessible.
Added an additional dependency in ont file to account for change
Change-Id: I0feefcc181ad21a0f11965953754167514539f00
diff --git a/libs/commonutil/src/com/android/cts/util/StatisticsUtils.java b/libs/commonutil/src/com/android/cts/util/StatisticsUtils.java
new file mode 100644
index 0000000..d6589af
--- /dev/null
+++ b/libs/commonutil/src/com/android/cts/util/StatisticsUtils.java
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2014 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.cts.util;
+
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Set of static helper methods for CTS tests.
+ */
+public class StatisticsUtils {
+
+
+    /**
+     * Private constructor for static class.
+     */
+    private StatisticsUtils() {}
+
+    /**
+     * Get the value of the 95th percentile using nearest rank algorithm.
+     *
+     * @throws IllegalArgumentException if the collection is null or empty
+     */
+    public static <TValue extends Comparable<? super TValue>> TValue get95PercentileValue(
+            Collection<TValue> collection) {
+        validateCollection(collection);
+
+        List<TValue> arrayCopy = new ArrayList<TValue>(collection);
+        Collections.sort(arrayCopy);
+
+        // zero-based array index
+        int arrayIndex = (int) Math.round(arrayCopy.size() * 0.95 + .5) - 1;
+
+        return arrayCopy.get(arrayIndex);
+    }
+
+    /**
+     * Calculate the mean of a collection.
+     *
+     * @throws IllegalArgumentException if the collection is null or empty
+     */
+    public static <TValue extends Number> double getMean(Collection<TValue> collection) {
+        validateCollection(collection);
+
+        double sum = 0.0;
+        for(TValue value : collection) {
+            sum += value.doubleValue();
+        }
+        return sum / collection.size();
+    }
+
+    /**
+     * Calculate the bias-corrected sample variance of a collection.
+     *
+     * @throws IllegalArgumentException if the collection is null or empty
+     */
+    public static <TValue extends Number> double getVariance(Collection<TValue> collection) {
+        validateCollection(collection);
+
+        double mean = getMean(collection);
+        ArrayList<Double> squaredDiffs = new ArrayList<Double>();
+        for(TValue value : collection) {
+            double difference = mean - value.doubleValue();
+            squaredDiffs.add(Math.pow(difference, 2));
+        }
+
+        double sum = 0.0;
+        for (Double value : squaredDiffs) {
+            sum += value;
+        }
+        return sum / (squaredDiffs.size() - 1);
+    }
+
+    /**
+     * Calculate the bias-corrected standard deviation of a collection.
+     *
+     * @throws IllegalArgumentException if the collection is null or empty
+     */
+    public static <TValue extends Number> double getStandardDeviation(
+            Collection<TValue> collection) {
+        return Math.sqrt(getVariance(collection));
+    }
+
+    /**
+     * Validate that a collection is not null or empty.
+     *
+     * @throws IllegalStateException if collection is null or empty.
+     */
+    private static <T> void validateCollection(Collection<T> collection) {
+        if(collection == null || collection.size() == 0) {
+            throw new IllegalStateException("Collection cannot be null or empty");
+        }
+    }
+
+}
diff --git a/libs/commonutil/src/com/android/cts/util/StatisticsUtilsTest.java b/libs/commonutil/src/com/android/cts/util/StatisticsUtilsTest.java
new file mode 100644
index 0000000..d78ba99
--- /dev/null
+++ b/libs/commonutil/src/com/android/cts/util/StatisticsUtilsTest.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2014 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.cts.util;
+
+import junit.framework.TestCase;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Unit tests for the {@link StatisticsUtils} class.
+ */
+public class StatisticsUtilsTest extends TestCase {
+
+    /**
+     * Test {@link StatisticsUtils#get95PercentileValue(Collection)}.
+     */
+    public void testGet95PercentileValue() {
+        Collection<Integer> values = new HashSet<Integer>();
+        for (int i = 0; i < 100; i++) {
+            values.add(i);
+        }
+        assertEquals(95, (int) StatisticsUtils.get95PercentileValue(values));
+
+        values = new HashSet<Integer>();
+        for (int i = 0; i < 1000; i++) {
+            values.add(i);
+        }
+        assertEquals(950, (int) StatisticsUtils.get95PercentileValue(values));
+
+        values = new HashSet<Integer>();
+        for (int i = 0; i < 100; i++) {
+            values.add(i * i);
+        }
+        assertEquals(95 * 95, (int) StatisticsUtils.get95PercentileValue(values));
+    }
+
+    /**
+     * Test {@link StatisticsUtils#getMean(Collection)}.
+     */
+    public void testGetMean() {
+        List<Integer> values = Arrays.asList(0, 1, 2, 3, 4);
+        double mean = StatisticsUtils.getMean(values);
+        assertEquals(2.0, mean, 0.00001);
+
+        values = Arrays.asList(1, 2, 3, 4, 5);
+        mean = StatisticsUtils.getMean(values);
+        assertEquals(3.0, mean, 0.00001);
+
+        values = Arrays.asList(0, 1, 4, 9, 16);
+        mean = StatisticsUtils.getMean(values);
+        assertEquals(6.0, mean, 0.00001);
+    }
+
+    /**
+     * Test {@link StatisticsUtils#getVariance(Collection)}.
+     */
+    public void testGetVariance() {
+        List<Integer> values = Arrays.asList(0, 1, 2, 3, 4);
+        double variance = StatisticsUtils.getVariance(values);
+        assertEquals(2.5, variance, 0.00001);
+
+        values = Arrays.asList(1, 2, 3, 4, 5);
+        variance = StatisticsUtils.getVariance(values);
+        assertEquals(2.5, variance, 0.00001);
+
+        values = Arrays.asList(0, 2, 4, 6, 8);
+        variance = StatisticsUtils.getVariance(values);
+        assertEquals(10.0, variance, 0.00001);
+    }
+
+    /**
+     * Test {@link StatisticsUtils#getStandardDeviation(Collection)}.
+     */
+    public void testGetStandardDeviation() {
+        List<Integer> values = Arrays.asList(0, 1, 2, 3, 4);
+        double stddev = StatisticsUtils.getStandardDeviation(values);
+        assertEquals(Math.sqrt(2.5), stddev, 0.00001);
+
+        values = Arrays.asList(1, 2, 3, 4, 5);
+        stddev = StatisticsUtils.getStandardDeviation(values);
+        assertEquals(Math.sqrt(2.5), stddev, 0.00001);
+
+        values = Arrays.asList(0, 2, 4, 6, 8);
+        stddev = StatisticsUtils.getStandardDeviation(values);
+        assertEquals(Math.sqrt(10.0), stddev, 0.00001);
+    }
+
+
+}
diff --git a/tests/tests/hardware/Android.mk b/tests/tests/hardware/Android.mk
index 2ad58ba..6c15c36 100644
--- a/tests/tests/hardware/Android.mk
+++ b/tests/tests/hardware/Android.mk
@@ -19,13 +19,15 @@
 include $(CLEAR_VARS)
 
 LOCAL_MODULE := cts-sensors-tests
+
 LOCAL_MODULE_TAGS := tests
 
+LOCAL_STATIC_JAVA_LIBRARIES := ctsdeviceutil
+
 LOCAL_SRC_FILES := $(call all-java-files-under, src/android/hardware/cts/helpers)
 
 include $(BUILD_STATIC_JAVA_LIBRARY)
 
-
 # CtsHardwareTestCases package
 
 include $(CLEAR_VARS)
diff --git a/tests/tests/hardware/src/android/hardware/cts/helpers/SensorCtsHelper.java b/tests/tests/hardware/src/android/hardware/cts/helpers/SensorCtsHelper.java
index ed55b01..25ca5a7 100644
--- a/tests/tests/hardware/src/android/hardware/cts/helpers/SensorCtsHelper.java
+++ b/tests/tests/hardware/src/android/hardware/cts/helpers/SensorCtsHelper.java
@@ -38,70 +38,6 @@
      */
     private SensorCtsHelper() {}
 
-    /**
-     * Get the value of the 95th percentile using nearest rank algorithm.
-     *
-     * @throws IllegalArgumentException if the collection is null or empty
-     */
-    public static <TValue extends Comparable<? super TValue>> TValue get95PercentileValue(
-            Collection<TValue> collection) {
-        validateCollection(collection);
-
-        List<TValue> arrayCopy = new ArrayList<TValue>(collection);
-        Collections.sort(arrayCopy);
-
-        // zero-based array index
-        int arrayIndex = (int) Math.round(arrayCopy.size() * 0.95 + .5) - 1;
-
-        return arrayCopy.get(arrayIndex);
-    }
-
-    /**
-     * Calculate the mean of a collection.
-     *
-     * @throws IllegalArgumentException if the collection is null or empty
-     */
-    public static <TValue extends Number> double getMean(Collection<TValue> collection) {
-        validateCollection(collection);
-
-        double sum = 0.0;
-        for(TValue value : collection) {
-            sum += value.doubleValue();
-        }
-        return sum / collection.size();
-    }
-
-    /**
-     * Calculate the bias-corrected sample variance of a collection.
-     *
-     * @throws IllegalArgumentException if the collection is null or empty
-     */
-    public static <TValue extends Number> double getVariance(Collection<TValue> collection) {
-        validateCollection(collection);
-
-        double mean = getMean(collection);
-        ArrayList<Double> squaredDiffs = new ArrayList<Double>();
-        for(TValue value : collection) {
-            double difference = mean - value.doubleValue();
-            squaredDiffs.add(Math.pow(difference, 2));
-        }
-
-        double sum = 0.0;
-        for (Double value : squaredDiffs) {
-            sum += value;
-        }
-        return sum / (squaredDiffs.size() - 1);
-    }
-
-    /**
-     * Calculate the bias-corrected standard deviation of a collection.
-     *
-     * @throws IllegalArgumentException if the collection is null or empty
-     */
-    public static <TValue extends Number> double getStandardDeviation(
-            Collection<TValue> collection) {
-        return Math.sqrt(getVariance(collection));
-    }
 
     /**
      * Get the default sensor for a given type.
diff --git a/tests/tests/hardware/src/android/hardware/cts/helpers/SensorCtsHelperTest.java b/tests/tests/hardware/src/android/hardware/cts/helpers/SensorCtsHelperTest.java
index 6f99692..daac8d1 100644
--- a/tests/tests/hardware/src/android/hardware/cts/helpers/SensorCtsHelperTest.java
+++ b/tests/tests/hardware/src/android/hardware/cts/helpers/SensorCtsHelperTest.java
@@ -30,80 +30,6 @@
 public class SensorCtsHelperTest extends TestCase {
 
     /**
-     * Test {@link SensorCtsHelper#get95PercentileValue(Collection)}.
-     */
-    public void testGet95PercentileValue() {
-        Collection<Integer> values = new HashSet<Integer>();
-        for (int i = 0; i < 100; i++) {
-            values.add(i);
-        }
-        assertEquals(95, (int) SensorCtsHelper.get95PercentileValue(values));
-
-        values = new HashSet<Integer>();
-        for (int i = 0; i < 1000; i++) {
-            values.add(i);
-        }
-        assertEquals(950, (int) SensorCtsHelper.get95PercentileValue(values));
-
-        values = new HashSet<Integer>();
-        for (int i = 0; i < 100; i++) {
-            values.add(i * i);
-        }
-        assertEquals(95 * 95, (int) SensorCtsHelper.get95PercentileValue(values));
-    }
-
-    /**
-     * Test {@link SensorCtsHelper#getMean(Collection)}.
-     */
-    public void testGetMean() {
-        List<Integer> values = Arrays.asList(0, 1, 2, 3, 4);
-        double mean = SensorCtsHelper.getMean(values);
-        assertEquals(2.0, mean, 0.00001);
-
-        values = Arrays.asList(1, 2, 3, 4, 5);
-        mean = SensorCtsHelper.getMean(values);
-        assertEquals(3.0, mean, 0.00001);
-
-        values = Arrays.asList(0, 1, 4, 9, 16);
-        mean = SensorCtsHelper.getMean(values);
-        assertEquals(6.0, mean, 0.00001);
-    }
-
-    /**
-     * Test {@link SensorCtsHelper#getVariance(Collection)}.
-     */
-    public void testGetVariance() {
-        List<Integer> values = Arrays.asList(0, 1, 2, 3, 4);
-        double variance = SensorCtsHelper.getVariance(values);
-        assertEquals(2.5, variance, 0.00001);
-
-        values = Arrays.asList(1, 2, 3, 4, 5);
-        variance = SensorCtsHelper.getVariance(values);
-        assertEquals(2.5, variance, 0.00001);
-
-        values = Arrays.asList(0, 2, 4, 6, 8);
-        variance = SensorCtsHelper.getVariance(values);
-        assertEquals(10.0, variance, 0.00001);
-    }
-
-    /**
-     * Test {@link SensorCtsHelper#getStandardDeviation(Collection)}.
-     */
-    public void testGetStandardDeviation() {
-        List<Integer> values = Arrays.asList(0, 1, 2, 3, 4);
-        double stddev = SensorCtsHelper.getStandardDeviation(values);
-        assertEquals(Math.sqrt(2.5), stddev, 0.00001);
-
-        values = Arrays.asList(1, 2, 3, 4, 5);
-        stddev = SensorCtsHelper.getStandardDeviation(values);
-        assertEquals(Math.sqrt(2.5), stddev, 0.00001);
-
-        values = Arrays.asList(0, 2, 4, 6, 8);
-        stddev = SensorCtsHelper.getStandardDeviation(values);
-        assertEquals(Math.sqrt(10.0), stddev, 0.00001);
-    }
-
-    /**
      * Test {@link SensorCtsHelper#getFrequency(Number, TimeUnit)}.
      */
     public void testGetFrequency() {
diff --git a/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/JitterVerification.java b/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/JitterVerification.java
index 6feceb8..a69e1a0 100644
--- a/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/JitterVerification.java
+++ b/tests/tests/hardware/src/android/hardware/cts/helpers/sensorverification/JitterVerification.java
@@ -21,6 +21,8 @@
 import android.hardware.cts.helpers.SensorStats;
 import android.hardware.cts.helpers.TestSensorEvent;
 
+import com.android.cts.util.StatisticsUtils;
+
 import junit.framework.Assert;
 
 import java.util.ArrayList;
@@ -91,7 +93,7 @@
         }
 
         List<Double> jitters = getJitterValues();
-        double jitter95Percentile = SensorCtsHelper.get95PercentileValue(jitters);
+        double jitter95Percentile = StatisticsUtils.get95PercentileValue(jitters);
         boolean failed = (jitter95Percentile > mExpected * (mThreshold / 100.0));
 
         stats.addValue(PASSED_KEY, !failed);
@@ -127,7 +129,7 @@
         for (int i = 1; i < mTimestamps.size(); i++) {
             deltas.add(mTimestamps.get(i) - mTimestamps.get(i -1));
         }
-        double deltaMean = SensorCtsHelper.getMean(deltas);
+        double deltaMean = StatisticsUtils.getMean(deltas);
         List<Double> jitters = new ArrayList<Double>(deltas.size());
         for (long delta : deltas) {
             jitters.add(Math.abs(delta - deltaMean));