/*
 * Copyright 2014-2018 Chronicle Software
 *
 * http://chronicle.software
 *
 * 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 net.openhft.chronicle.bytes;

import org.junit.Ignore;
import org.junit.Test;

import java.io.File;

import static org.junit.Assert.assertEquals;

public class CopyBytesTest {

    @Test
    public void testCanCopyBytesFromBytes() {
        Bytes toCopy = Bytes.allocateDirect(32);
        Bytes toValidate = Bytes.allocateDirect(32);

        toCopy.writeLong(0, (long) 'W' << 56L | 100L);
        toCopy.writeLong(8, (long) 'W' << 56L | 200L);

        Bytes buffer = Bytes.allocateElasticDirect();
        buffer.write(toCopy, 0, (long) 2 * 8);

        buffer.read(toValidate, 2 * 8);

        assertEquals((long) 'W' << 56L | 100L, toValidate.readLong(0));
        assertEquals((long) 'W' << 56L | 200L, toValidate.readLong(8));

    }

    @Ignore
    @Test
    public void testCanCopyBytesFromMappedBytes() throws Exception {
        Bytes toCopy = Bytes.allocateDirect(32);
        Bytes toValidate = Bytes.allocateDirect(32);

        toCopy.writeLong(0, (long) 'W' << 56L | 100L);
        toCopy.writeLong(8, (long) 'W' << 56L | 200L);

        Bytes buffer = MappedBytes.mappedBytes(File.createTempFile("mapped-test", "bytes"), 64 << 10);
        buffer.write(toCopy, 0, (long) 2 * 8);

        buffer.read(toValidate, 2 * 8);

        assertEquals((long) 'W' << 56L | 100L, toValidate.readLong(0));
        assertEquals((long) 'W' << 56L | 200L, toValidate.readLong(8));

    }
}

@@ -670,33 +670,8 @@
 s;%0A%0A
-import org.junit.Ignore;%0A
 impo
@@ -796,56 +796,49 @@
     
-@Test%0A    public void testCanCopyBytesFromBytes(
+static void doTest(Bytes toTest, int from
 ) %7B%0A
@@ -1063,51 +1063,74 @@
     
-Bytes buffer = Bytes.allocateElasticDirect(
+toTest.writePosition(from);%0A        toTest.write(toCopy, 0, 2 * 8L
 );%0A 
@@ -1128,38 +1128,38 @@
  * 8L);%0A        
-buffer
+toTest
 .write(toCopy, 0
@@ -1156,36 +1156,26 @@
 (toCopy, 0, 
-(long) 2 * 
 8
+L
 );%0A%0A        
@@ -1166,38 +1166,38 @@
 , 8L);%0A%0A        
-buffer
+toTest
 .read(toValidate
@@ -1190,33 +1190,33 @@
 ead(toValidate, 
-2
+3
  * 8);%0A%0A        
@@ -1355,27 +1355,170 @@
 ));%0A
-%0A    %7D%0A%0A    @Ignore
+        assertEquals((long) 'W' %3C%3C 56L %7C 100L, toValidate.readLong(16));%0A%0A        toTest.release();%0A        toCopy.release();%0A        toValidate.release();%0A    %7D%0A
 %0A   
@@ -1564,22 +1564,16 @@
 From
-Mapped
 Bytes() 
 thro
@@ -1572,50 +1572,25 @@
 s() 
-throws Exception %7B%0A        Bytes toCopy = 
+%7B%0A        doTest(
 Byte
@@ -1603,344 +1603,219 @@
 cate
+Elastic
 Direct(
-32
+), 0
 );%0A    
+%7D%0A%0A
     
-Bytes toValidate = Bytes.allocateDirect(32);%0A%0A        toCopy.writeLong(0, (long) 'W' %3C%3C 56L %7C 100L);%0A        toCopy.writeLong(8, (long) 'W' %3C%3C 56L %7C 200L);%0A%0A        Bytes buffer = MappedBytes.mappedBytes(File.createTempFile(%22mapped-test%22, %22bytes%22), 64 %3C%3C 10);%0A        buffer.write(toCopy, 0, (long) 2 * 8
+@Test%0A    public void testCanCopyBytesFromMappedBytes() throws Exception %7B%0A        File bytes = File.createTempFile(%22mapped-test%22, %22bytes%22);%0A        bytes.deleteOnExit(
 );%0A
-%0A
         
 buff
@@ -1814,186 +1814,141 @@
     
-buffer.read(toValidate, 2 * 8);%0A%0A        assertEquals((long) 'W' %3C%3C 56L %7C 100L, toValidate.readLong(0));%0A        assertEquals((long) 'W' %3C%3C 56L %7C 200L, toValidate.readLong(8)
+doTest(MappedBytes.mappedBytes(bytes, 64 %3C%3C 10, 0), 0);%0A        doTest(MappedBytes.mappedBytes(bytes, 64 %3C%3C 10, 0), (64 %3C%3C 10) - 8
 );%0A
-%0A
     

Fix a bug in MappedBytes.write(Bytes, int offset, int length) where the data is truncated silently is writeRemaining() is too low.