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 = HBaseClassTestRule.forClass(TestSize.class);
035
036  @Test
037  public void testConversion() {
038    Size kbSize = new Size(1024D, Size.Unit.MEGABYTE);
039    assertEquals(1D, kbSize.get(Size.Unit.GIGABYTE), 0);
040    assertEquals(1024D, kbSize.get(), 0);
041    assertEquals(1024D * 1024D, kbSize.get(Size.Unit.KILOBYTE), 0);
042    assertEquals(1024D * 1024D * 1024D, kbSize.get(Size.Unit.BYTE), 0);
043  }
044
045  @Test
046  public void testCompare() {
047    Size size00 = new Size(100D, Size.Unit.GIGABYTE);
048    Size size01 = new Size(100D, Size.Unit.MEGABYTE);
049    Size size02 = new Size(100D, Size.Unit.BYTE);
050    Set<Size> sizes = new TreeSet<>();
051    sizes.add(size00);
052    sizes.add(size01);
053    sizes.add(size02);
054    int count = 0;
055    for (Size s : sizes) {
056      switch (count++) {
057        case 0:
058          assertEquals(size02, s);
059          break;
060        case 1:
061          assertEquals(size01, s);
062          break;
063        default:
064          assertEquals(size00, s);
065          break;
066      }
067    }
068    assertEquals(3, count);
069  }
070
071  @Test
072  public void testEqual() {
073    assertEquals(new Size(1024D, Size.Unit.TERABYTE), new Size(1D, Size.Unit.PETABYTE));
074    assertEquals(new Size(1024D, Size.Unit.GIGABYTE), new Size(1D, Size.Unit.TERABYTE));
075    assertEquals(new Size(1024D, Size.Unit.MEGABYTE), new Size(1D, Size.Unit.GIGABYTE));
076    assertEquals(new Size(1024D, Size.Unit.KILOBYTE), new Size(1D, Size.Unit.MEGABYTE));
077    assertEquals(new Size(1024D, Size.Unit.BYTE), new Size(1D, Size.Unit.KILOBYTE));
078  }
079
080}