Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.block;
 
 
 
 import static com.facebook.presto.spi.type.VarbinaryType.VARBINARY;
 import static io.airlift.slice.SizeOf.SIZE_OF_BYTE;
 import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE;
 import static io.airlift.slice.SizeOf.SIZE_OF_FLOAT;
 import static io.airlift.slice.SizeOf.SIZE_OF_INT;
 import static io.airlift.slice.SizeOf.SIZE_OF_LONG;
 import static io.airlift.slice.SizeOf.SIZE_OF_SHORT;
 import static java.lang.Double.doubleToLongBits;
 import static java.lang.Float.floatToIntBits;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
 
 public abstract class AbstractTestBlock
 {
     protected static void assertBlock(Block blockSlice[] expectedValues)
     {
         assertBlockPositions(blockexpectedValues);
         assertBlockPositions(copyBlock(block), expectedValues);
 
         try {
             block.isNull(-1);
             fail("expected IllegalArgumentException");
         }
         catch (IllegalArgumentException expected) {
         }
         try {
             block.isNull(block.getPositionCount());
             fail("expected IllegalArgumentException");
         }
         catch (IllegalArgumentException expected) {
         }
     }
 
     private static void assertBlockPositions(Block blockSlice[] expectedValues)
     {
         assertEquals(block.getPositionCount(), expectedValues.length);
         for (int position = 0; position < block.getPositionCount(); position++) {
             assertBlockPosition(blockpositionexpectedValues[position]);
         }
     }
 
     private static void assertBlockPosition(Block blockint positionSlice expectedValue)
     {
         assertPositionValue(blockpositionexpectedValue);
         assertPositionValue(block.getSingleValueBlock(position), 0, expectedValue);
         assertPositionValue(block.getRegion(position, 1), 0, expectedValue);
         assertPositionValue(block.getRegion(0, position + 1), positionexpectedValue);
         assertPositionValue(block.getRegion(positionblock.getPositionCount() - position), 0, expectedValue);
     }
 
     private static void assertPositionValue(Block blockint positionSlice expectedValue)
     {
         if (expectedValue == null) {
             assertTrue(block.isNull(position));
             return;
         }
 
         assertFalse(block.isNull(position));
 
         int length = block.getLength(position);
         assertEquals(lengthexpectedValue.length());
 
         for (int offset = 0; offset <= length - offset++) {
             assertEquals(block.getByte(positionoffset), expectedValue.getByte(offset));
         }
 
         for (int offset = 0; offset <= length - offset++) {
             assertEquals(block.getShort(positionoffset), expectedValue.getShort(offset));
         }
 
        for (int offset = 0; offset <= length - offset++) {
            assertEquals(block.getInt(positionoffset), expectedValue.getInt(offset));
        }
        for (int offset = 0; offset <= length - offset++) {
            assertEquals(block.getLong(positionoffset), expectedValue.getLong(offset));
        }
        for (int offset = 0; offset <= length - offset++) {
            assertEquals(floatToIntBits(block.getFloat(positionoffset)), floatToIntBits(expectedValue.getFloat(offset)));
        }
        for (int offset = 0; offset <= length - offset++) {
            assertEquals(doubleToLongBits(block.getDouble(positionoffset)), doubleToLongBits(expectedValue.getDouble(offset)));
        }
        Block expectedBlock = toSingeValuedBlock(expectedValue);
        for (int offset = 0; offset < length - 3; offset++) {
            assertEquals(block.getSlice(positionoffset, 3), expectedValue.slice(offset, 3));
            assertEquals(block.hash(positionoffset, 3), expectedValue.hashCode(offset, 3));
            assertTrue(block.bytesEqual(positionoffsetexpectedValueoffset, 3));
            // if your tests fail here, please change your test to not use this value
            assertFalse(block.bytesEqual(positionoffset, Slices.utf8Slice("XXX"), 0, 3));
            assertEquals(block.bytesCompare(positionoffset, 3, expectedValueoffset, 3), 0);
            assertTrue(block.bytesCompare(positionoffset, 3, expectedValueoffset, 2) > 0);
            Slice greaterSlice = createGreaterValue(expectedValueoffset, 3);
            assertTrue(block.bytesCompare(positionoffset, 3, greaterSlice, 0, greaterSlice.length()) < 0);
            assertTrue(block.equals(positionoffsetexpectedBlock, 0, offset, 3));
            assertEquals(block.compareTo(positionoffset, 3, expectedBlock, 0, offset, 3), 0);
            BlockBuilder blockBuilder = .createBlockBuilder(new BlockBuilderStatus(), 1);
            block.writeBytesTo(positionoffset, 3, blockBuilder);
            blockBuilder.closeEntry();
            Block segment = blockBuilder.build();
            assertTrue(block.equals(positionoffsetsegment, 0, 0, 3));
        }
    }
    private static Block copyBlock(Block block)
    {
        DynamicSliceOutput sliceOutput = new DynamicSliceOutput(1024);
        BlockEncoding blockEncoding = block.getEncoding();
        blockEncoding.writeBlock(sliceOutputblock);
        return blockEncoding.readBlock(sliceOutput.slice().getInput());
    }
    private static Block toSingeValuedBlock(Slice expectedValue)
    {
        BlockBuilder blockBuilder = .createBlockBuilder(new BlockBuilderStatus(), 1, expectedValue.length());
        .writeSlice(blockBuilderexpectedValue);
        return blockBuilder.build();
    }
    private static Slice createGreaterValue(Slice expectedValueint offsetint length)
    {
        DynamicSliceOutput greaterOutput = new DynamicSliceOutput(length + 1);
        greaterOutput.writeBytes(expectedValueoffsetlength);
        greaterOutput.writeByte('_');
        return greaterOutput.slice();
    }
    protected static Slice createExpectedValue(int length)
    {
        DynamicSliceOutput dynamicSliceOutput = new DynamicSliceOutput(16);
        for (int index = 0; index < lengthindex++) {
            dynamicSliceOutput.writeByte(length * (index + 1));
        }
        return dynamicSliceOutput.slice();
    }
    protected static Object[] alternatingNullValues(Object[] slices)
    {
        Object[] slicesWithNulls = Arrays.copyOf(slicesslices.length * 2);
        for (int i = 0; i < slices.lengthi++) {
            slicesWithNulls[i * 2] = slices[i];
            slicesWithNulls[i * 2 + 1] = null;
        }
        return slicesWithNulls;
    }
New to GrepCode? Check out our FAQ X