001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.Set;
023import java.util.TreeSet;
024import org.apache.hadoop.hbase.testclassification.MiscTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.ClassRule;
027import org.junit.Test;
028import org.junit.experimental.categories.Category;
029
030@Category({MiscTests.class, SmallTests.class})
031public class TestSize {
032
033  @ClassRule
034  public static final HBaseClassTestRule CLASS_RULE =
035      HBaseClassTestRule.forClass(TestSize.class);
036
037  @Test
038  public void testConversion() {
039    Size kbSize = new Size(1024D, Size.Unit.MEGABYTE);
040    assertEquals(1D, kbSize.get(Size.Unit.GIGABYTE), 0);
041    assertEquals(1024D, kbSize.get(), 0);
042    assertEquals(1024D * 1024D, kbSize.get(Size.Unit.KILOBYTE), 0);
043    assertEquals(1024D * 1024D * 1024D, kbSize.get(Size.Unit.BYTE), 0);
044  }
045
046  @Test
047  public void testCompare() {
048    Size size00 = new Size(100D, Size.Unit.GIGABYTE);
049    Size size01 = new Size(100D, Size.Unit.MEGABYTE);
050    Size size02 = new Size(100D, Size.Unit.BYTE);
051    Set<Size> sizes = new TreeSet<>();
052    sizes.add(size00);
053    sizes.add(size01);
054    sizes.add(size02);
055    int count = 0;
056    for (Size s : sizes) {
057      switch (count++) {
058        case 0:
059          assertEquals(size02, s);
060          break;
061        case 1:
062          assertEquals(size01, s);
063          break;
064        default:
065          assertEquals(size00, s);
066          break;
067      }
068    }
069    assertEquals(3, count);
070  }
071
072  @Test
073  public void testEqual() {
074    assertEquals(new Size(1024D, Size.Unit.TERABYTE),
075      new Size(1D, Size.Unit.PETABYTE));
076    assertEquals(new Size(1024D, Size.Unit.GIGABYTE),
077      new Size(1D, Size.Unit.TERABYTE));
078    assertEquals(new Size(1024D, Size.Unit.MEGABYTE),
079      new Size(1D, Size.Unit.GIGABYTE));
080    assertEquals(new Size(1024D, Size.Unit.KILOBYTE),
081      new Size(1D, Size.Unit.MEGABYTE));
082    assertEquals(new Size(1024D, Size.Unit.BYTE),
083      new Size(1D, Size.Unit.KILOBYTE));
084  }
085
086}