/*
 * 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.mnemonic;

import org.apache.commons.lang3.RandomUtils;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.zip.CRC32;
import java.util.zip.Checksum;

/**
 * Test the functionality of ChunkBuffer
 */
public class ChunkBufferNGTest {

  private long m_keyid = 22L;
  private volatile long m_checksum;
  private int m_bufsize = 90 * 1024 * 1024;

  @Test
  public void testGenChunkBuffers() {
    Checksum cs = new CRC32();
    cs.reset();

    NonVolatileMemAllocator act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
            1024 * 1024 * 1024L, "./pmchunkbuffertest.dat", true);
    act.setChunkReclaimer(new Reclaim<Long>() {
      @Override
      public boolean reclaim(Long mres, Long sz) {
        System.out.println(String.format("Reclaim Memory Chunk: %X  Size: %s", System.identityHashCode(mres),
                null == sz ? "NULL" : sz.toString()));
        return false;
      }
    });
    DurableChunk<NonVolatileMemAllocator> mch;
    mch = act.createChunk(1000 * 1024 * 1024L);
    Assert.assertNotNull(mch);
    act.setHandler(m_keyid, mch.getHandler());
    long bufcnt = mch.getSize() / m_bufsize;
    ChunkBuffer ckbuf;
    byte[] rdbytes;
    for (int idx = 0; idx < bufcnt; ++idx) {
//      System.err.println(String.format("---- bufcnt: %d, bufsize: %d, idx: %d", bufcnt, m_bufsize, idx));
      ckbuf = mch.getChunkBuffer(idx * m_bufsize, m_bufsize);
      Assert.assertNotNull(ckbuf);
      rdbytes = RandomUtils.nextBytes(m_bufsize);
      Assert.assertNotNull(rdbytes);
      ckbuf.get().clear();
      ckbuf.get().put(rdbytes);
      cs.update(rdbytes, 0, rdbytes.length);
    }
    m_checksum = cs.getValue();
    act.close();
  }

  @Test(dependsOnMethods = {"testGenChunkBuffers"})
  public void testCheckChunkBuffers() {
    Checksum cs = new CRC32();
    cs.reset();
    NonVolatileMemAllocator act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"),
            1L, "./pmchunkbuffertest.dat", false);
    act.setChunkReclaimer(new Reclaim<Long>() {
      @Override
      public boolean reclaim(Long mres, Long sz) {
        System.out.println(String.format("Reclaim Memory Chunk: %X  Size: %s", System.identityHashCode(mres),
                null == sz ? "NULL" : sz.toString()));
        return false;
      }
    });
    DurableChunk<NonVolatileMemAllocator> mch;
    mch = act.retrieveChunk(act.getHandler(m_keyid));
    Assert.assertNotNull(mch);
    long bufcnt = mch.getSize() / m_bufsize;

    ChunkBuffer ckbuf;
    byte[] buf;
    for (int idx = 0; idx < bufcnt; ++idx) {
      ckbuf = mch.getChunkBuffer(idx * m_bufsize, m_bufsize);
      Assert.assertNotNull(ckbuf);
      buf = new byte[m_bufsize];
      ckbuf.get().clear();
      ckbuf.get().get(buf);
      cs.update(buf, 0, buf.length);
    }
    act.close();

    Assert.assertEquals(m_checksum, cs.getValue());
    System.out.println(String.format("The checksum of chunk buffers are %d", m_checksum));
  }

}

@@ -1142,16 +1142,49 @@
 ecksum;%0A
+  private volatile long m_count;%0A
   privat
@@ -1219,16 +1219,16 @@
 * 1024;%0A
-
 %0A  @Test
@@ -2078,35 +2078,36 @@
 bytes;%0A    for (
-int
+long
  idx = 0; idx %3C 
@@ -2553,24 +2553,46 @@
 getValue();%0A
+    m_count = bufcnt;%0A
     act.clos
@@ -3452,19 +3452,20 @@
    for (
-int
+long
  idx = 0
@@ -3783,24 +3783,66 @@
 etValue());%0A
+    Assert.assertEquals(m_count, bufcnt);%0A
     System.o
@@ -3901,16 +3901,35 @@
 ers are 
+%25d, Total count is 
 %25d%22, m_c
@@ -3935,16 +3935,25 @@
 checksum
+, m_count
 ));%0A  %7D%0A

Use long type for index in ChunkBuffer test case