/*
 * Copyright 2017, Yahoo! Inc. Licensed under the terms of the
 * Apache License 2.0. See LICENSE file at the project root for terms.
 */

package com.yahoo.memory;

import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.channels.WritableByteChannel;
import java.util.concurrent.ThreadLocalRandom;

public class MemoryWriteToTest {

  @Test
  public void testOnHeap() throws IOException {
    testWriteTo(createRandomBytesMemory(0));
    testWriteTo(createRandomBytesMemory(7));
    testWriteTo(createRandomBytesMemory(1023));
    testWriteTo(createRandomBytesMemory(10_000));
    testWriteTo(createRandomBytesMemory(CompareAndCopy.UNSAFE_COPY_MEMORY_THRESHOLD * 5));
    testWriteTo(createRandomBytesMemory(CompareAndCopy.UNSAFE_COPY_MEMORY_THRESHOLD * 5 + 10));
  }

  @Test
  public void testOnHeapInts() throws IOException {
    testWriteTo(createRandomIntsMemory(0));
    testWriteTo(createRandomIntsMemory(7));
    testWriteTo(createRandomIntsMemory(1023));
    testWriteTo(createRandomIntsMemory(10_000));
    testWriteTo(createRandomIntsMemory(CompareAndCopy.UNSAFE_COPY_MEMORY_THRESHOLD * 5));
    testWriteTo(createRandomIntsMemory(CompareAndCopy.UNSAFE_COPY_MEMORY_THRESHOLD * 5 + 10));
  }

  @Test
  public void testOffHeap() throws IOException {
    try (WritableDirectHandle handle =
        WritableMemory.allocateDirect(CompareAndCopy.UNSAFE_COPY_MEMORY_THRESHOLD * 5 + 10)) {
      WritableMemory mem = handle.get();
      testWriteTo(mem.region(0, 0));
      testOffHeap(mem, 7);
      testOffHeap(mem, 1023);
      testOffHeap(mem, 10_000);
      testOffHeap(mem, CompareAndCopy.UNSAFE_COPY_MEMORY_THRESHOLD * 5);
      testOffHeap(mem, CompareAndCopy.UNSAFE_COPY_MEMORY_THRESHOLD * 5 + 10);
    }
  }

  private void testOffHeap(WritableMemory mem, int size) throws IOException {
    createRandomBytesMemory(size).copyTo(0, mem, 0, size);
    testWriteTo(mem.region(0, size));
  }

  private Memory createRandomBytesMemory(int size) {
    byte[] bytes = new byte[size];
    ThreadLocalRandom.current().nextBytes(bytes);
    return Memory.wrap(bytes);
  }

  private Memory createRandomIntsMemory(int size) {
    int[] ints = ThreadLocalRandom.current().ints(size).toArray();
    return Memory.wrap(ints);
  }

  private void testWriteTo(Memory mem) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    WritableByteChannel out = Channels.newChannel(baos);
    mem.writeTo(0, mem.getCapacity(), out);
    out.close();
    byte[] result = baos.toByteArray();
    Assert.assertTrue(mem.equalTo(Memory.wrap(result)));
  }
}

@@ -166,71 +166,8 @@
 y;%0A%0A
-import org.testng.Assert;%0Aimport org.testng.annotations.Test;%0A%0A
 impo
@@ -357,16 +357,79 @@
 andom;%0A%0A
+import org.testng.Assert;%0Aimport org.testng.annotations.Test;%0A%0A
 public c
@@ -821,32 +821,33 @@
 ndomBytesMemory(
+(
 CompareAndCopy.U
@@ -869,32 +869,33 @@
 RY_THRESHOLD * 5
+)
  + 10));%0A  %7D%0A%0A  
@@ -1257,32 +1257,33 @@
 andomIntsMemory(
+(
 CompareAndCopy.U
@@ -1309,24 +1309,25 @@
 HRESHOLD * 5
+)
  + 10));%0A  %7D
@@ -1462,16 +1462,17 @@
 eDirect(
+(
 CompareA
@@ -1506,24 +1506,25 @@
 HRESHOLD * 5
+)
  + 10)) %7B%0A  
@@ -1776,32 +1776,33 @@
 estOffHeap(mem, 
+(
 CompareAndCopy.U
@@ -1832,16 +1832,17 @@
 HOLD * 5
+)
  + 10);%0A
@@ -1854,32 +1854,39 @@
 %0A  %7D%0A%0A  private 
+static 
 void testOffHeap
@@ -2041,32 +2041,39 @@
 %0A  %7D%0A%0A  private 
+static 
 Memory createRan
@@ -2230,16 +2230,23 @@
 private 
+static 
 Memory c
@@ -2391,16 +2391,23 @@
 private 
+static 
 void tes
@@ -2510,24 +2510,29 @@
 ream();%0A    
+try (
 WritableByte
@@ -2570,18 +2570,22 @@
 el(baos)
-;%0A
+) %7B%0A  
     mem.
@@ -2624,16 +2624,18 @@
 t);%0A    
+  
 out.clos
@@ -2635,24 +2635,30 @@
 ut.close();%0A
+    %7D%0A
     byte%5B%5D r

Make some methods static, use try-with-resource.