Home | History | Annotate | Download | only in sensorverification
      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.Assert;
     20 
     21 import android.hardware.cts.helpers.TestSensorEvent;
     22 
     23 /**
     24  * Abstract class that calculates of the mean event values.
     25  */
     26 public abstract class AbstractMeanVerification extends AbstractSensorVerification {
     27     private float[] mSums = null;
     28     private int mCount = 0;
     29 
     30     /**
     31      * {@inheritDoc}
     32      */
     33     @Override
     34     protected void addSensorEventInternal(TestSensorEvent event) {
     35         if (mSums == null) {
     36             mSums = new float[event.values.length];
     37         }
     38         Assert.assertEquals(mSums.length, event.values.length);
     39         for (int i = 0; i < mSums.length; i++) {
     40             mSums[i] += event.values[i];
     41         }
     42         mCount++;
     43     }
     44 
     45     /**
     46      * Return the number of events.
     47      */
     48     protected int getCount() {
     49         return mCount;
     50     }
     51 
     52     /**
     53      * Return the means of the event values.
     54      */
     55     protected float[] getMeans() {
     56         if (mCount < 0) {
     57             return null;
     58         }
     59 
     60         float[] means = new float[mSums.length];
     61         for (int i = 0; i < mSums.length; i++) {
     62             means[i] = mSums[i] / mCount;
     63         }
     64         return means;
     65     }
     66 }
     67