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.operator.aggregation;
 
 
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static org.testng.Assert.assertEquals;
 
 public class TestStateCompiler
 {
     @Test
     {
         StateCompiler compiler = new StateCompiler();
 
         NullableBigintState state = factory.createSingleState();
         NullableBigintState deserializedState = factory.createSingleState();
 
         state.setLong(2);
         state.setNull(false);
 
         BlockBuilder builder = ..createBlockBuilder(new BlockBuilderStatus());
         serializer.serialize(statebuilder);
         state.setNull(true);
         serializer.serialize(statebuilder);
 
         Block block = builder.build();
 
         assertEquals(.getLong(block, 0), state.getLong());
         serializer.deserialize(block, 0, deserializedState);
         assertEquals(deserializedState.getLong(), state.getLong());
 
         assertEquals(block.isNull(1), state.isNull());
         serializer.deserialize(block, 1, deserializedState);
         assertEquals(deserializedState.isNull(), state.isNull());
     }
 
     @Test
     public void testPrimitiveLongSerialization()
     {
         StateCompiler compiler = new StateCompiler();
 
         AccumulatorStateFactory<LongStatefactory = compiler.generateStateFactory(LongState.class);
         AccumulatorStateSerializer<LongStateserializer = compiler.generateStateSerializer(LongState.class);
         LongState state = factory.createSingleState();
         LongState deserializedState = factory.createSingleState();
 
         state.setLong(2);
 
         BlockBuilder builder = ..createBlockBuilder(new BlockBuilderStatus());
         serializer.serialize(statebuilder);
 
         Block block = builder.build();
 
         assertEquals(.getLong(block, 0), state.getLong());
         serializer.deserialize(block, 0, deserializedState);
         assertEquals(deserializedState.getLong(), state.getLong());
     }
 
     @Test
     public void testGetSerializedType()
     {
         StateCompiler compiler = new StateCompiler();
         AccumulatorStateSerializer<LongStateserializer = compiler.generateStateSerializer(LongState.class);
         assertEquals(serializer.getSerializedType(), .);
     }
 
     @Test
     public void testPrimitiveBooleanSerialization()
     {
         StateCompiler compiler = new StateCompiler();
 
        AccumulatorStateFactory<BooleanStatefactory = compiler.generateStateFactory(BooleanState.class);
        AccumulatorStateSerializer<BooleanStateserializer = compiler.generateStateSerializer(BooleanState.class);
        BooleanState state = factory.createSingleState();
        BooleanState deserializedState = factory.createSingleState();
        state.setBoolean(true);
        serializer.serialize(statebuilder);
        Block block = builder.build();
        serializer.deserialize(block, 0, deserializedState);
        assertEquals(deserializedState.getBoolean(), state.getBoolean());
    }
    @Test
    public void testPrimitiveByteSerialization()
    {
        StateCompiler compiler = new StateCompiler();
        AccumulatorStateFactory<ByteStatefactory = compiler.generateStateFactory(ByteState.class);
        AccumulatorStateSerializer<ByteStateserializer = compiler.generateStateSerializer(ByteState.class);
        ByteState state = factory.createSingleState();
        ByteState deserializedState = factory.createSingleState();
        state.setByte((byte) 3);
        serializer.serialize(statebuilder);
        Block block = builder.build();
        serializer.deserialize(block, 0, deserializedState);
        assertEquals(deserializedState.getByte(), state.getByte());
    }
    @Test
    public void testVarianceStateSerialization()
    {
        StateCompiler compiler = new StateCompiler();
        AccumulatorStateFactory<VarianceStatefactory = compiler.generateStateFactory(VarianceState.class);
        AccumulatorStateSerializer<VarianceStateserializer = compiler.generateStateSerializer(VarianceState.class);
        VarianceState singleState = factory.createSingleState();
        VarianceState deserializedState = factory.createSingleState();
        singleState.setMean(1);
        singleState.setCount(2);
        singleState.setM2(3);
        serializer.serialize(singleStatebuilder);
        Block block = builder.build();
        serializer.deserialize(block, 0, deserializedState);
        assertEquals(deserializedState.getCount(), singleState.getCount());
        assertEquals(deserializedState.getMean(), singleState.getMean());
        assertEquals(deserializedState.getM2(), singleState.getM2());
    }
    @Test
    public void testComplexSerialization()
    {
        StateCompiler compiler = new StateCompiler();
        TestComplexState singleState = factory.createSingleState();
        TestComplexState deserializedState = factory.createSingleState();
        singleState.setBoolean(true);
        singleState.setLong(1);
        singleState.setDouble(2.0);
        singleState.setByte((byte) 3);
        serializer.serialize(singleStatebuilder);
        Block block = builder.build();
        serializer.deserialize(block, 0, deserializedState);
        assertEquals(deserializedState.getBoolean(), singleState.getBoolean());
        assertEquals(deserializedState.getLong(), singleState.getLong());
        assertEquals(deserializedState.getDouble(), singleState.getDouble());
        assertEquals(deserializedState.getByte(), singleState.getByte());
    }
    public interface TestComplexState
            extends AccumulatorState
    {
        double getDouble();
        void setDouble(double value);
        boolean getBoolean();
        void setBoolean(boolean value);
        long getLong();
        void setLong(long value);
        byte getByte();
        void setByte(byte value);
    }
    public interface BooleanState
            extends AccumulatorState
    {
        boolean getBoolean();
        void setBoolean(boolean value);
    }
    public interface ByteState
            extends AccumulatorState
    {
        byte getByte();
        void setByte(byte value);
    }
New to GrepCode? Check out our FAQ X