/**
 * Copyright (C) 2014-2015 LinkedIn Corp. (pinot-core@linkedin.com)
 *
 * 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.linkedin.pinot.core.realtime.impl.dictionary;

import org.testng.Assert;
import org.testng.annotations.Test;
import com.linkedin.pinot.common.data.DimensionFieldSpec;
import com.linkedin.pinot.common.data.FieldSpec;
import com.linkedin.pinot.core.io.readerwriter.impl.FixedByteSingleColumnMultiValueReaderWriter;


public class MultiValueDictionaryTest {
  private static final String COL_NAME = "greek";
  private static int NROWS = 10000;
  private static int MAX_N_VALUES = 999;

  @Test
  public void testMultiValueIndexing() throws Exception {
    final FieldSpec mvIntFs = new DimensionFieldSpec(COL_NAME, FieldSpec.DataType.LONG, false);
    final LongMutableDictionary dict = new LongMutableDictionary(mvIntFs);
    final FixedByteSingleColumnMultiValueReaderWriter indexer = new FixedByteSingleColumnMultiValueReaderWriter(NROWS,
        Integer.SIZE / 8, FixedByteSingleColumnMultiValueReaderWriter.DEFAULT_MAX_NUMBER_OF_MULTIVALUES);

    // Insert rows into the indexer and dictionary
    for (int row = 0; row < NROWS; row++) {
      int nValues = (row % MAX_N_VALUES) + 1;
      Long[] val = new Long[nValues];
      for (int i = 0; i < nValues; i++) {
        val[i] = (long)row * i;
      }
      dict.index(val);
      int dictIds[] = new int[nValues];
      for (int i = 0; i < nValues; i++) {
        dictIds[i] = dict.indexOf(val[i]);
      }
      indexer.setIntArray(row, dictIds);
    }

    // Read back rows and make sure that the values are good.
    final int[] dictIds = new int[MAX_N_VALUES];
    for (int row = 0; row < NROWS; row++) {
      int nValues = indexer.getIntArray(row, dictIds);
      Assert.assertEquals(nValues, (row % MAX_N_VALUES)+1, "Mismatching number of values");

      for (int i = 0; i < nValues; i++) {
        Long val = dict.getLongValue(dictIds[i]);
        Assert.assertEquals(val.longValue(), (long)row * i, "Value mismatch at row " + row);
      }
    }
  }
}

@@ -690,16 +690,41 @@
 onary;%0A%0A
+import java.util.Random;%0A
 import o
@@ -1086,24 +1086,30 @@
 vate static 
+final 
 int NROWS = 
@@ -1112,17 +1112,16 @@
 S = 1000
-0
 ;%0A  priv
@@ -1131,16 +1131,22 @@
  static 
+final 
 int MAX_
@@ -1160,11 +1160,146 @@
 S = 
-999
+FixedByteSingleColumnMultiValueReaderWriter.DEFAULT_MAX_NUMBER_OF_MULTIVALUES;%0A  private static final long RANDOM_SEED = System.nanoTime()
 ;%0A%0A 
@@ -1343,16 +1343,22 @@
 dexing()
+%0A     
  throws 
@@ -1603,16 +1603,24 @@
 ndexer =
+%0A       
  new Fix
@@ -1666,24 +1666,16 @@
 r(NROWS,
-%0A       
  Integer
@@ -1689,79 +1689,14 @@
  8, 
-FixedByteSingleColumnMultiValueReaderWriter.DEFAULT_MAX_NUMBER_OF_MULTI
+MAX_N_
 VALU
@@ -1752,16 +1752,61 @@
 tionary%0A
+    Random random = new Random(RANDOM_SEED);%0A
     for 
@@ -1861,20 +1861,42 @@
 alues = 
-(row
+Math.abs(random.nextInt())
  %25 MAX_N
@@ -1906,13 +1906,8 @@
 LUES
-) + 1
 ;%0A  
@@ -2001,29 +2001,33 @@
 al%5Bi%5D = 
-(long)row * i
+random.nextLong()
 ;%0A      
@@ -2294,16 +2294,54 @@
 e good.%0A
+    random = new Random(RANDOM_SEED);%0A
     fina
@@ -2515,20 +2515,42 @@
 Values, 
-(row
+Math.abs(random.nextInt())
  %25 MAX_N
@@ -2560,12 +2560,19 @@
 LUES
-)+1,
+,%0A         
  %22Mi
@@ -2597,17 +2597,49 @@
 f values
-%22
+, random seed is: %22 + RANDOM_SEED
 );%0A%0A    
@@ -2775,22 +2775,38 @@
 (), 
-(long)row * i,
+random.nextLong(),%0A           
  %22Va
@@ -2832,16 +2832,53 @@
  %22 + row
+ + %22, random seed is: %22 + RANDOM_SEED
 );%0A     

Modify MultiValueDictionaryTest to use random, reduce the tested row numbers