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 testConstructBoundaries()38 public void testConstructBoundaries() { 39 List<Double> buckets = Arrays.asList(0.0, 1.0, 2.0); 40 List<Double> expectedBuckets = Arrays.asList(1.0, 2.0); 41 BucketBoundaries bucketBoundaries = BucketBoundaries.create(buckets); 42 assertThat(bucketBoundaries.getBoundaries()).isEqualTo(expectedBuckets); 43 } 44 45 @Test testConstructBoundaries_IgnoreNegativeBounds()46 public void testConstructBoundaries_IgnoreNegativeBounds() { 47 List<Double> buckets = Arrays.asList(-5.0, -1.0, 1.0, 2.0); 48 List<Double> expectedBuckets = Arrays.asList(1.0, 2.0); 49 BucketBoundaries bucketBoundaries = BucketBoundaries.create(buckets); 50 assertThat(bucketBoundaries.getBoundaries()).isEqualTo(expectedBuckets); 51 } 52 53 @Test testConstructBoundaries_IgnoreZeroAndNegativeBounds()54 public void testConstructBoundaries_IgnoreZeroAndNegativeBounds() { 55 List<Double> buckets = Arrays.asList(-5.0, -2.0, -1.0, 0.0); 56 BucketBoundaries bucketBoundaries = BucketBoundaries.create(buckets); 57 assertThat(bucketBoundaries.getBoundaries()).isEmpty(); 58 } 59 60 @Test testBoundariesDoesNotChangeWithOriginalList()61 public void testBoundariesDoesNotChangeWithOriginalList() { 62 List<Double> original = new ArrayList<Double>(); 63 original.add(0.0); 64 original.add(1.0); 65 original.add(2.0); 66 BucketBoundaries bucketBoundaries = BucketBoundaries.create(original); 67 original.set(2, 3.0); 68 original.add(4.0); 69 List<Double> expected = Arrays.asList(1.0, 2.0); 70 assertThat(bucketBoundaries.getBoundaries()).isNotEqualTo(original); 71 assertThat(bucketBoundaries.getBoundaries()).isEqualTo(expected); 72 } 73 74 @Test testNullBoundaries()75 public void testNullBoundaries() throws Exception { 76 thrown.expect(NullPointerException.class); 77 BucketBoundaries.create(null); 78 } 79 80 @Test testUnsortedBoundaries()81 public void testUnsortedBoundaries() throws Exception { 82 List<Double> buckets = Arrays.asList(0.0, 1.0, 1.0); 83 thrown.expect(IllegalArgumentException.class); 84 BucketBoundaries.create(buckets); 85 } 86 87 @Test testNoBoundaries()88 public void testNoBoundaries() { 89 List<Double> buckets = Arrays.asList(); 90 BucketBoundaries bucketBoundaries = BucketBoundaries.create(buckets); 91 assertThat(bucketBoundaries.getBoundaries()).isEqualTo(buckets); 92 } 93 94 @Test testBucketBoundariesEquals()95 public void testBucketBoundariesEquals() { 96 new EqualsTester() 97 .addEqualityGroup( 98 BucketBoundaries.create(Arrays.asList(-1.0, 2.0)), 99 BucketBoundaries.create(Arrays.asList(-1.0, 2.0))) 100 .addEqualityGroup(BucketBoundaries.create(Arrays.asList(-1.0))) 101 .testEquals(); 102 } 103 } 104