Home | History | Annotate | Download | only in stats
      1 /*
      2  * Copyright 2017, OpenCensus Authors
      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 io.opencensus.stats;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import com.google.common.testing.EqualsTester;
     22 import java.util.ArrayList;
     23 import java.util.Arrays;
     24 import java.util.List;
     25 import org.junit.Rule;
     26 import org.junit.Test;
     27 import org.junit.rules.ExpectedException;
     28 import org.junit.runner.RunWith;
     29 import org.junit.runners.JUnit4;
     30 
     31 /** Unit tests for {@link io.opencensus.stats.BucketBoundaries}. */
     32 @RunWith(JUnit4.class)
     33 public class BucketBoundariesTest {
     34 
     35   @Rule public final ExpectedException thrown = ExpectedException.none();
     36 
     37   @Test
     38   public void testConstructBoundaries() {
     39     List<Double> buckets = Arrays.asList(0.0, 1.0, 2.0);
     40     BucketBoundaries bucketBoundaries = BucketBoundaries.create(buckets);
     41     assertThat(bucketBoundaries.getBoundaries()).isEqualTo(buckets);
     42   }
     43 
     44   @Test
     45   public void testBoundariesDoesNotChangeWithOriginalList() {
     46     List<Double> original = new ArrayList<Double>();
     47     original.add(0.0);
     48     original.add(1.0);
     49     original.add(2.0);
     50     BucketBoundaries bucketBoundaries = BucketBoundaries.create(original);
     51     original.set(2, 3.0);
     52     original.add(4.0);
     53     List<Double> expected = Arrays.asList(0.0, 1.0, 2.0);
     54     assertThat(bucketBoundaries.getBoundaries()).isNotEqualTo(original);
     55     assertThat(bucketBoundaries.getBoundaries()).isEqualTo(expected);
     56   }
     57 
     58   @Test
     59   public void testNullBoundaries() throws Exception {
     60     thrown.expect(NullPointerException.class);
     61     BucketBoundaries.create(null);
     62   }
     63 
     64   @Test
     65   public void testUnsortedBoundaries() throws Exception {
     66     List<Double> buckets = Arrays.asList(0.0, 1.0, 1.0);
     67     thrown.expect(IllegalArgumentException.class);
     68     BucketBoundaries.create(buckets);
     69   }
     70 
     71   @Test
     72   public void testNoBoundaries() {
     73     List<Double> buckets = Arrays.asList();
     74     BucketBoundaries bucketBoundaries = BucketBoundaries.create(buckets);
     75     assertThat(bucketBoundaries.getBoundaries()).isEqualTo(buckets);
     76   }
     77 
     78   @Test
     79   public void testBucketBoundariesEquals() {
     80     new EqualsTester()
     81         .addEqualityGroup(
     82             BucketBoundaries.create(Arrays.asList(-1.0, 2.0)),
     83             BucketBoundaries.create(Arrays.asList(-1.0, 2.0)))
     84         .addEqualityGroup(BucketBoundaries.create(Arrays.asList(-1.0)))
     85         .testEquals();
     86   }
     87 }
     88