/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 jp.co.yahoo.yosegi.blackbox;

import java.io.IOException;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.Arguments;

import jp.co.yahoo.yosegi.config.Configuration;

import jp.co.yahoo.yosegi.message.objects.*;

import jp.co.yahoo.yosegi.spread.expression.*;
import jp.co.yahoo.yosegi.spread.column.filter.*;
import jp.co.yahoo.yosegi.spread.column.*;
import jp.co.yahoo.yosegi.binary.*;
import jp.co.yahoo.yosegi.binary.maker.*;

public class TestBytePrimitiveColumn {

  public static Stream<Arguments> data1() throws IOException{
    return Stream.of(
      arguments( "jp.co.yahoo.yosegi.binary.maker.UnsafeOptimizeDumpLongColumnBinaryMaker" ),
      arguments( "jp.co.yahoo.yosegi.binary.maker.UnsafeOptimizeLongColumnBinaryMaker" ),
      arguments( "jp.co.yahoo.yosegi.binary.maker.RleLongColumnBinaryMaker" ) ,
      arguments( "jp.co.yahoo.yosegi.binary.maker.OptimizedNullArrayLongColumnBinaryMaker" ),
      arguments( "jp.co.yahoo.yosegi.binary.maker.OptimizedNullArrayDumpLongColumnBinaryMaker" )
    );
  }

  public IColumn createTestColumn( final String targetClassName , final byte[] valueArray ) throws IOException {
    IColumn column = new PrimitiveColumn( ColumnType.BYTE , "column" );
    for ( int i = 0 ; i < valueArray.length ; i++ ) {
      column.add( ColumnType.BYTE , new ByteObj( valueArray[i] ) , i );
    }

    IColumnBinaryMaker maker = FindColumnBinaryMaker.get( targetClassName );
    ColumnBinaryMakerConfig defaultConfig = new ColumnBinaryMakerConfig();
    ColumnBinaryMakerCustomConfigNode configNode = new ColumnBinaryMakerCustomConfigNode( "root" , defaultConfig );
    ColumnBinary columnBinary = maker.toBinary( defaultConfig , null , new CompressResultNode() , column );
    return FindColumnBinaryMaker.get( columnBinary.makerClassName ).toColumn( columnBinary );
  }

  public IColumn createNotNullColumn( final String targetClassName ) throws IOException{
    IColumn column = new PrimitiveColumn( ColumnType.BYTE , "column" );
    column.add( ColumnType.BYTE , new ByteObj( Byte.MAX_VALUE ) , 0 );
    column.add( ColumnType.BYTE , new ByteObj( Byte.MIN_VALUE ) , 1 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)-2 ) , 2 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)-3 ) , 3 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)-4 ) , 4 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)-5 ) , 5 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)-6 ) , 6 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)7 ) , 7 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)8 ) , 8 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)9 ) , 9 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)0 ) , 10 );

    IColumnBinaryMaker maker = FindColumnBinaryMaker.get( targetClassName );
    ColumnBinaryMakerConfig defaultConfig = new ColumnBinaryMakerConfig();
    ColumnBinaryMakerCustomConfigNode configNode = new ColumnBinaryMakerCustomConfigNode( "root" , defaultConfig );
    ColumnBinary columnBinary = maker.toBinary( defaultConfig , null , new CompressResultNode() , column );
    return FindColumnBinaryMaker.get( columnBinary.makerClassName ).toColumn( columnBinary );
  }

  public IColumn createNullColumn( final String targetClassName ) throws IOException{
    IColumn column = new PrimitiveColumn( ColumnType.BYTE , "column" );

    IColumnBinaryMaker maker = FindColumnBinaryMaker.get( targetClassName );
    ColumnBinaryMakerConfig defaultConfig = new ColumnBinaryMakerConfig();
    ColumnBinaryMakerCustomConfigNode configNode = new ColumnBinaryMakerCustomConfigNode( "root" , defaultConfig );
    ColumnBinary columnBinary = maker.toBinary( defaultConfig , null , new CompressResultNode() , column );
    return  FindColumnBinaryMaker.get( columnBinary.makerClassName ).toColumn( columnBinary );
  }

  public IColumn createHasNullColumn( final String targetClassName ) throws IOException{
    IColumn column = new PrimitiveColumn( ColumnType.BYTE , "column" );
    column.add( ColumnType.BYTE , new ByteObj( (byte)0 ) , 0 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)4 ) , 4 );
    column.add( ColumnType.BYTE , new ByteObj( (byte)8 ) , 8 );

    IColumnBinaryMaker maker = FindColumnBinaryMaker.get( targetClassName );
    ColumnBinaryMakerConfig defaultConfig = new ColumnBinaryMakerConfig();
    ColumnBinaryMakerCustomConfigNode configNode = new ColumnBinaryMakerCustomConfigNode( "root" , defaultConfig );
    ColumnBinary columnBinary = maker.toBinary( defaultConfig , null , new CompressResultNode() , column );
    return FindColumnBinaryMaker.get( columnBinary.makerClassName ).toColumn( columnBinary );
  }

  public IColumn createLastCellColumn( final String targetClassName ) throws IOException{
    IColumn column = new PrimitiveColumn( ColumnType.BYTE , "column" );
    column.add( ColumnType.BYTE , new ByteObj( Byte.MAX_VALUE ) , 10000 );

    IColumnBinaryMaker maker = FindColumnBinaryMaker.get( targetClassName );
    ColumnBinaryMakerConfig defaultConfig = new ColumnBinaryMakerConfig();
    ColumnBinaryMakerCustomConfigNode configNode = new ColumnBinaryMakerCustomConfigNode( "root" , defaultConfig );
    ColumnBinary columnBinary = maker.toBinary( defaultConfig , null , new CompressResultNode() , column );
    return FindColumnBinaryMaker.get( columnBinary.makerClassName ).toColumn( columnBinary );
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_notNull_1( final String targetClassName ) throws IOException{
    IColumn column = createNotNullColumn( targetClassName );
    assertEquals( ( (PrimitiveObject)( column.get(0).getRow() ) ).getByte() , Byte.MAX_VALUE );
    assertEquals( ( (PrimitiveObject)( column.get(1).getRow() ) ).getByte() , Byte.MIN_VALUE );
    assertEquals( ( (PrimitiveObject)( column.get(2).getRow() ) ).getByte() , (byte)-2 );
    assertEquals( ( (PrimitiveObject)( column.get(3).getRow() ) ).getByte() , (byte)-3 );
    assertEquals( ( (PrimitiveObject)( column.get(4).getRow() ) ).getByte() , (byte)-4 );
    assertEquals( ( (PrimitiveObject)( column.get(5).getRow() ) ).getByte() , (byte)-5 );
    assertEquals( ( (PrimitiveObject)( column.get(6).getRow() ) ).getByte() , (byte)-6 );
    assertEquals( ( (PrimitiveObject)( column.get(7).getRow() ) ).getByte() , (byte)7 );
    assertEquals( ( (PrimitiveObject)( column.get(8).getRow() ) ).getByte() , (byte)8 );
    assertEquals( ( (PrimitiveObject)( column.get(9).getRow() ) ).getByte() , (byte)9 );
    assertEquals( ( (PrimitiveObject)( column.get(10).getRow() ) ).getByte() , (byte)0 );
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_null_1( final String targetClassName ) throws IOException{
    IColumn column = createNullColumn( targetClassName );
    assertNull( column.get(0).getRow() );
    assertNull( column.get(1).getRow() );
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_hasNull_1( final String targetClassName ) throws IOException{
    IColumn column = createHasNullColumn( targetClassName );
    assertEquals( ( (PrimitiveObject)( column.get(0).getRow() ) ).getByte() , (byte)0 );
    assertNull( column.get(1).getRow() );
    assertNull( column.get(2).getRow() );
    assertNull( column.get(3).getRow() );
    assertEquals( ( (PrimitiveObject)( column.get(4).getRow() ) ).getByte() , (byte)4 );
    assertNull( column.get(5).getRow() );
    assertNull( column.get(6).getRow() );
    assertNull( column.get(7).getRow() );
    assertEquals( ( (PrimitiveObject)( column.get(8).getRow() ) ).getByte() , (byte)8 );
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_lastCell_1( final String targetClassName ) throws IOException{
    IColumn column = createLastCellColumn( targetClassName );
    for( int i = 0 ; i < 10000 ; i++ ){
      assertNull( column.get(i).getRow() );
    }
    assertEquals( ( (PrimitiveObject)( column.get(10000).getRow() ) ).getByte() , Byte.MAX_VALUE );
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_encodeAndDecode_equalsSetValue_withIntBit0( final String targetClassName ) throws IOException{
    byte[] valueArray = new byte[]{
      (byte)0,
      (byte)0,
      (byte)0,
      (byte)0,
      (byte)0,
      (byte)0,
      (byte)0,
      (byte)0,
      (byte)0,
      (byte)0
    };
    IColumn column = createTestColumn( targetClassName , valueArray );
    assertEquals( column.size() , 10 );
    for ( int i = 0 ; i < 10 ; i++ ) {
      assertEquals( ( (PrimitiveObject)( column.get(i).getRow() ) ).getByte() , valueArray[i] );
    }
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_encodeAndDecode_equalsSetValue_withInt1( final String targetClassName ) throws IOException{
    byte[] valueArray = new byte[]{
      (byte)0,
      (byte)0,
      (byte)1,
      (byte)1,
      (byte)0,
      (byte)0,
      (byte)1,
      (byte)1,
      (byte)0,
      (byte)0
    };
    IColumn column = createTestColumn( targetClassName , valueArray );
    assertEquals( column.size() , 10 );
    for ( int i = 0 ; i < 10 ; i++ ) {
      assertEquals( ( (PrimitiveObject)( column.get(i).getRow() ) ).getByte() , valueArray[i] );
    }
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_encodeAndDecode_equalsSetValue_withInt2( final String targetClassName ) throws IOException{
    byte[] valueArray = new byte[]{
      (byte)0,
      (byte)0,
      (byte)1,
      (byte)1,
      (byte)2,
      (byte)2,
      (byte)3,
      (byte)3,
      (byte)0,
      (byte)0
    };
    IColumn column = createTestColumn( targetClassName , valueArray );
    assertEquals( column.size() , 10 );
    for ( int i = 0 ; i < 10 ; i++ ) {
      assertEquals( ( (PrimitiveObject)( column.get(i).getRow() ) ).getByte() , valueArray[i] );
    }
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_encodeAndDecode_equalsSetValue_withInt4( final String targetClassName ) throws IOException{
    byte[] valueArray = new byte[]{
      (byte)0,
      (byte)0,
      (byte)8,
      (byte)8,
      (byte)15,
      (byte)15,
      (byte)1,
      (byte)2,
      (byte)3,
      (byte)4
    };
    IColumn column = createTestColumn( targetClassName , valueArray );
    assertEquals( column.size() , 10 );
    for ( int i = 0 ; i < 10 ; i++ ) {
      assertEquals( ( (PrimitiveObject)( column.get(i).getRow() ) ).getByte() , valueArray[i] );
    }
  }

  @ParameterizedTest
  @MethodSource( "data1" )
  public void T_encodeAndDecode_equalsSetValue_withInt8( final String targetClassName ) throws IOException{
    byte[] valueArray = new byte[]{
      (byte)Byte.MAX_VALUE,
      (byte)Byte.MIN_VALUE,
      (byte)0,
      (byte)0,
      (byte)64,
      (byte)-64,
      (byte)32,
      (byte)-32,
      (byte)16,
      (byte)-16
    };
    IColumn column = createTestColumn( targetClassName , valueArray );
    assertEquals( column.size() , 10 );
    for ( int i = 0 ; i < 10 ; i++ ) {
      assertEquals( ( (PrimitiveObject)( column.get(i).getRow() ) ).getByte() , valueArray[i] );
    }
  }

}