/*
 * Copyright 2011 - 2015 Metamarkets Group Inc.
 *
 * 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.metamx.collections.bitmap;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import junit.framework.Assert;
import org.junit.Test;
import org.roaringbitmap.IntIterator;

import java.util.Arrays;

public class RoaringBitmapFactoryTest
{
    
  // testing https://github.com/metamx/bytebuffer-collections/issues/26
  @Test
  public void testIssue26() throws Exception
  {
    printBitMap(new ConciseBitmapFactory());
    printBitMap(new RoaringBitmapFactory());
  }

  // used by issue 26
  private void printBitMap(BitmapFactory bitmapFactory) throws Exception
  {
    int numRow = 5104234;
    ImmutableBitmap bitmap = bitmapFactory.complement(bitmapFactory.makeEmptyImmutableBitmap(), numRow);
    ImmutableBitmap notBitmap = bitmapFactory.complement(bitmap,numRow);
    if(notBitmap.size() != 0) throw new RuntimeException("should be empty");
    IntIterator intIter = notBitmap.iterator();
    while(intIter.hasNext()) {
        throw new RuntimeException("should be empty");
    }
  }
  
  @Test
  public void testUnwrapWithNull() throws Exception
  {
    RoaringBitmapFactory factory = new RoaringBitmapFactory();

    ImmutableBitmap bitmap = factory.union(
        Iterables.transform(
            Lists.newArrayList(new WrappedRoaringBitmap()),
            new Function<WrappedRoaringBitmap, ImmutableBitmap>()
            {
              @Override
              public ImmutableBitmap apply(WrappedRoaringBitmap input)
              {
                return null;
              }
            }
        )
    );

    Assert.assertEquals(0, bitmap.size());
  }

  @Test
  public void testUnwrapMerge() throws Exception
  {
    RoaringBitmapFactory factory = new RoaringBitmapFactory();

    WrappedRoaringBitmap set = new WrappedRoaringBitmap();
    set.add(1);
    set.add(3);
    set.add(5);

    ImmutableBitmap bitmap = factory.union(
        Arrays.asList(
            factory.makeImmutableBitmap(set),
            null
        )
    );

    Assert.assertEquals(3, bitmap.size());
  }
}

/*
 * Copyright 2011 - 2015 Metamarkets Group Inc.
 *
 * 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.metamx.collections.bitmap;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.junit.Assert;
import org.junit.Test;
import org.roaringbitmap.IntIterator;

import java.util.Arrays;

public class RoaringBitmapFactoryTest
{
    
  // testing https://github.com/metamx/bytebuffer-collections/issues/26
  @Test
  public void testIssue26() throws Exception
  {
    checkEmptyComplement(new ConciseBitmapFactory());
    checkEmptyComplement(new RoaringBitmapFactory());
  }

  // used by issue 26
  private void checkEmptyComplement(BitmapFactory bitmapFactory) throws Exception
  {
    int numRow = 5104234;
    ImmutableBitmap bitmap = bitmapFactory.complement(bitmapFactory.makeEmptyImmutableBitmap(), numRow);
    ImmutableBitmap notBitmap = bitmapFactory.complement(bitmap,numRow);
    Assert.assertTrue(notBitmap.size() == 0);
    Assert.assertTrue(notBitmap.isEmpty());
    IntIterator intIter = notBitmap.iterator();
    Assert.assertFalse(intIter.hasNext());
  }
  
  @Test
  public void testUnwrapWithNull() throws Exception
  {
    RoaringBitmapFactory factory = new RoaringBitmapFactory();

    ImmutableBitmap bitmap = factory.union(
        Iterables.transform(
            Lists.newArrayList(new WrappedRoaringBitmap()),
            new Function<WrappedRoaringBitmap, ImmutableBitmap>()
            {
              @Override
              public ImmutableBitmap apply(WrappedRoaringBitmap input)
              {
                return null;
              }
            }
        )
    );

    Assert.assertEquals(0, bitmap.size());
  }

  @Test
  public void testUnwrapMerge() throws Exception
  {
    RoaringBitmapFactory factory = new RoaringBitmapFactory();

    WrappedRoaringBitmap set = new WrappedRoaringBitmap();
    set.add(1);
    set.add(3);
    set.add(5);

    ImmutableBitmap bitmap = factory.union(
        Arrays.asList(
            factory.makeImmutableBitmap(set),
            null
        )
    );

    Assert.assertEquals(3, bitmap.size());
  }
}

Make test for issue 26 more junit-y