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 package android.hardware.cts.helpers.sensorverification; 18 19 import junit.framework.TestCase; 20 21 import android.hardware.cts.helpers.SensorStats; 22 import android.hardware.cts.helpers.TestSensorEnvironment; 23 import android.hardware.cts.helpers.TestSensorEvent; 24 25 import java.util.ArrayList; 26 import java.util.Collection; 27 28 /** 29 * Tests for {@link StandardDeviationVerification}. 30 */ 31 public class StandardDeviationVerificationTest extends TestCase { 32 33 /** 34 * Test {@link StandardDeviationVerification#verify(TestSensorEnvironment, SensorStats)}. 35 */ 36 public void testVerify() { 37 // Stddev should be {sqrt(2.5), sqrt(2.5), sqrt(2.5)} 38 float[][] values = { 39 {0, 1, 0}, 40 {1, 2, 2}, 41 {2, 3, 4}, 42 {3, 4, 6}, 43 {4, 5, 8}, 44 }; 45 float[] standardDeviations = { 46 (float) Math.sqrt(2.5), (float) Math.sqrt(2.5), (float) Math.sqrt(10.0) 47 }; 48 49 float[] threshold = {2, 2, 4}; 50 runVerification(threshold, values, true, standardDeviations); 51 52 threshold = new float[]{1, 2, 4}; 53 runVerification(threshold, values, false, standardDeviations); 54 55 threshold = new float[]{2, 1, 4}; 56 runVerification(threshold, values, false, standardDeviations); 57 58 threshold = new float[]{2, 2, 3}; 59 runVerification(threshold, values, false, standardDeviations); 60 } 61 62 private void runVerification(float[] threshold, float[][] values, boolean pass, 63 float[] standardDeviations) { 64 SensorStats stats = new SensorStats(); 65 StandardDeviationVerification verification = getVerification(threshold, values); 66 if (pass) { 67 verification.verify(stats); 68 } else { 69 boolean failed = false; 70 try { 71 verification.verify(stats); 72 } catch (AssertionError e) { 73 // Expected; 74 failed = true; 75 } 76 assertTrue("Expected an AssertionError", failed); 77 } 78 assertEquals(pass, stats.getValue(StandardDeviationVerification.PASSED_KEY)); 79 float[] actual = (float[]) stats.getValue(SensorStats.STANDARD_DEVIATION_KEY); 80 for (int i = 0; i < standardDeviations.length; i++) { 81 assertEquals(standardDeviations[i], actual[i], 0.1); 82 } 83 } 84 85 private static StandardDeviationVerification getVerification( 86 float[] threshold, 87 float[] ... values) { 88 Collection<TestSensorEvent> events = new ArrayList<>(values.length); 89 for (float[] value : values) { 90 events.add(new TestSensorEvent(null, 0, 0, value)); 91 } 92 StandardDeviationVerification verification = new StandardDeviationVerification(threshold); 93 verification.addSensorEvents(events); 94 return verification; 95 } 96 } 97