/*
 * 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
 *
 *     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 org.apache.lucene.search;

import org.apache.lucene.index.DocValuesSkipper;
import org.apache.lucene.util.FixedBitSet;

public class TestSkipBlockRangeIterator extends BaseDocValuesSkipperTests {

  public void testSkipBlockRangeIterator() throws Exception {

    DocValuesSkipper skipper = docValuesSkipper(10, 20, true);
    SkipBlockRangeIterator it = new SkipBlockRangeIterator(skipper, 10, 20);

    assertEquals(0, it.nextDoc());
    // Level-1 block [0,127] is dense and in range: docIDRunEnd expands
    assertEquals(128, it.docIDRunEnd());
    assertEquals(100, it.advance(100));
    assertEquals(512, it.advance(300));
    assertEquals(513, it.docIDRunEnd());
    // Block [1024,1087] is dense YES, but level-1 [1024,1151] is sparse: can't expand
    assertEquals(1024, it.advance(1024));
    assertEquals(1088, it.docIDRunEnd());
    assertEquals(1100, it.advance(1100));
    assertEquals(1101, it.docIDRunEnd());
    assertEquals(1536, it.advance(1500));
    assertEquals(1537, it.docIDRunEnd());
    assertEquals(DocIdSetIterator.NO_MORE_DOCS, it.advance(2050));
  }

  public void testIntoBitSet() throws Exception {
    DocValuesSkipper skipper = docValuesSkipper(10, 20, true);
    SkipBlockRangeIterator it = new SkipBlockRangeIterator(skipper, 10, 20);
    assertEquals(512, it.advance(300));
    FixedBitSet bitSet = new FixedBitSet(2048);
    it.intoBitSet(1500, bitSet, 512);

    FixedBitSet expected = new FixedBitSet(2048);
    expected.set(0, 640);

    assertEquals(expected, bitSet);
  }
}
