1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * 
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * The contents of this file are subject to the terms of either the Universal Permissive License
   7  * v 1.0 as shown at http://oss.oracle.com/licenses/upl
   8  *
   9  * or the following license:
  10  *
  11  * Redistribution and use in source and binary forms, with or without modification, are permitted
  12  * provided that the following conditions are met:
  13  * 
  14  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions
  15  * and the following disclaimer.
  16  * 
  17  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
  18  * conditions and the following disclaimer in the documentation and/or other materials provided with
  19  * the distribution.
  20  * 
  21  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to
  22  * endorse or promote products derived from this software without specific prior written permission.
  23  * 
  24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  31  * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32  */
  33 package org.openjdk.jmc.common.test;
  34 
  35 import static org.openjdk.jmc.common.unit.BinaryPrefix.NOBI;
  36 import static org.junit.Assert.assertEquals;
  37 import static org.junit.Assert.assertNotNull;
  38 import static org.junit.Assert.assertSame;
  39 
  40 import org.junit.Assert;
  41 import org.junit.Test;
  42 
  43 import org.openjdk.jmc.common.unit.BinaryPrefix;
  44 
  45 public class BinaryPrefixTest extends MCTestCase {
  46 
  47         private void assertAlignmentLog2(int expectedLog2, long value) throws Exception {
  48                 Assert.assertEquals(expectedLog2, BinaryPrefix.getAlignmentLog2(value));
  49         }
  50 
  51         private void assertAlignmentLog2(int expectedLog2, double value) throws Exception {
  52                 Assert.assertEquals(expectedLog2, BinaryPrefix.getAlignmentLog2(value));
  53         }
  54 
  55         @Test
  56         public void testZero() {
  57                 assertEquals(0, BinaryPrefix.getFloorLog1024(0));
  58                 assertEquals(NOBI, BinaryPrefix.getFloorPrefix(0));
  59         }
  60 
  61         @Test
  62         public void testZeroDouble() {
  63                 assertEquals(0, BinaryPrefix.getFloorLog1024(0.0));
  64                 assertEquals(NOBI, BinaryPrefix.getFloorPrefix(0.0));
  65         }
  66 
  67         @Test
  68         public void testPositive() {
  69                 int oldLog1024 = -1;
  70                 BinaryPrefix oldPrefix = null;
  71                 int i = 0;
  72                 for (long val = 1; val != 0; val <<= 1) {
  73                         int log1024 = BinaryPrefix.getFloorLog1024(val);
  74                         BinaryPrefix prefix = BinaryPrefix.getFloorPrefix(val);
  75 
  76                         if ((i++ % 10) == 0) {
  77                                 assertGreaterThan("For value " + val, oldLog1024, log1024);
  78                                 if (oldPrefix == null) {
  79                                         assertNotNull(prefix);
  80                                 } else {
  81                                         assertGreaterThan(oldPrefix, prefix);
  82                                 }
  83                         } else {
  84                                 assertEquals("For value " + val, oldLog1024, log1024);
  85                                 assertSame(oldPrefix, prefix);
  86                         }
  87 
  88                         oldLog1024 = log1024;
  89                         oldPrefix = prefix;
  90                 }
  91         }
  92 
  93         @Test
  94         public void testPositiveDouble() {
  95                 int oldLog1024 = -1;
  96                 BinaryPrefix oldPrefix = null;
  97                 int i = 0;
  98                 for (int powOf2 = 0; powOf2 < 90; powOf2++) {
  99                         double val = Math.scalb(1.0, powOf2);
 100                         int log1024 = BinaryPrefix.getFloorLog1024(val);
 101                         BinaryPrefix prefix = BinaryPrefix.getFloorPrefix(val);
 102 
 103                         if ((i++ % 10) == 0) {
 104                                 assertGreaterThan("For value " + val, oldLog1024, log1024);
 105                                 if (oldPrefix == null) {
 106                                         assertNotNull(prefix);
 107                                 } else {
 108                                         assertGreaterThan(oldPrefix, prefix);
 109                                 }
 110                         } else {
 111                                 assertEquals("For value " + val, oldLog1024, log1024);
 112                                 assertSame(oldPrefix, prefix);
 113                         }
 114 
 115                         oldLog1024 = log1024;
 116                         oldPrefix = prefix;
 117                 }
 118         }
 119 
 120         @Test
 121         public void testNegative() {
 122                 int oldLog1024 = -1;
 123                 BinaryPrefix oldPrefix = null;
 124                 int i = 0;
 125                 for (long val = -1; val != 0; val <<= 1) {
 126                         int log1024 = BinaryPrefix.getFloorLog1024(val);
 127                         BinaryPrefix prefix = BinaryPrefix.getFloorPrefix(val);
 128 
 129                         if ((i++ % 10) == 0) {
 130                                 assertGreaterThan("For value " + val, oldLog1024, log1024);
 131                                 if (oldPrefix == null) {
 132                                         assertNotNull(prefix);
 133                                 } else {
 134                                         assertGreaterThan(oldPrefix, prefix);
 135                                 }
 136                         } else {
 137                                 assertEquals("For value " + val, oldLog1024, log1024);
 138                                 assertSame(oldPrefix, prefix);
 139                         }
 140 
 141                         oldLog1024 = log1024;
 142                         oldPrefix = prefix;
 143                 }
 144         }
 145 
 146         @Test
 147         public void testNegativeDouble() {
 148                 int oldLog1024 = -1;
 149                 BinaryPrefix oldPrefix = null;
 150                 int i = 0;
 151                 for (int powOf2 = 0; powOf2 < 90; powOf2++) {
 152                         double val = -Math.scalb(1.0, powOf2);
 153                         int log1024 = BinaryPrefix.getFloorLog1024(val);
 154                         BinaryPrefix prefix = BinaryPrefix.getFloorPrefix(val);
 155 
 156                         if ((i++ % 10) == 0) {
 157                                 assertGreaterThan("For value " + val, oldLog1024, log1024);
 158                                 if (oldPrefix == null) {
 159                                         assertNotNull(prefix);
 160                                 } else {
 161                                         assertGreaterThan(oldPrefix, prefix);
 162                                 }
 163                         } else {
 164                                 assertEquals("For value " + val, oldLog1024, log1024);
 165                                 assertSame(oldPrefix, prefix);
 166                         }
 167 
 168                         oldLog1024 = log1024;
 169                         oldPrefix = prefix;
 170                 }
 171         }
 172 
 173         @Test
 174         public void testBinaryLongAlignment() throws Exception {
 175                 assertAlignmentLog2(0, 1);
 176                 assertAlignmentLog2(0, 17);
 177                 assertAlignmentLog2(0, 1027);
 178                 assertAlignmentLog2(1, 2);
 179                 assertAlignmentLog2(1, 42);
 180                 assertAlignmentLog2(1, 1030);
 181                 assertAlignmentLog2(2, 4);
 182                 assertAlignmentLog2(3, 8);
 183                 assertAlignmentLog2(4, 16);
 184                 assertAlignmentLog2(10, 1024);
 185         }
 186 
 187         @Test
 188         public void testBinaryDoubleAlignment() throws Exception {
 189                 assertAlignmentLog2(0, 1.0);
 190                 assertAlignmentLog2(0, 17.0);
 191                 assertAlignmentLog2(0, 1027.0);
 192                 assertAlignmentLog2(1, 2.0);
 193                 assertAlignmentLog2(1, 42.0);
 194                 assertAlignmentLog2(1, 1030.0);
 195                 assertAlignmentLog2(2, 4.0);
 196                 assertAlignmentLog2(3, 8.0);
 197                 assertAlignmentLog2(4, 16.0);
 198                 assertAlignmentLog2(10, 1024.0);
 199 
 200                 assertAlignmentLog2(-1, 0.5);
 201                 assertAlignmentLog2(-1, 1.5);
 202                 assertAlignmentLog2(-1, 17.5);
 203                 assertAlignmentLog2(-1, 16777216.5);
 204 
 205                 assertAlignmentLog2(-2, 0.25);
 206                 assertAlignmentLog2(-2, 1.25);
 207                 assertAlignmentLog2(-2, 17.25);
 208                 assertAlignmentLog2(-2, 16777216.25);
 209         }
 210 }