package net.openhft.chronicle.bytes.readme;

import net.openhft.chronicle.bytes.BytesTestCommon;
import net.openhft.chronicle.bytes.HexDumpBytes;
import net.openhft.chronicle.bytes.NativeBytes;
import net.openhft.chronicle.bytes.StopCharTesters;
import org.junit.Test;

import static org.junit.Assert.*;
import static org.junit.Assume.assumeFalse;

public class StringsTest extends BytesTestCommon {
    @Test
    public void testString() {
        assumeFalse(NativeBytes.areNewGuarded());
        HexDumpBytes bytes = new HexDumpBytes();
        bytes.comment("write8bit").write8bit("£ 1");
        bytes.comment("writeUtf8").writeUtf8("£ 1");
        bytes.comment("append8bit").append8bit("£ 1").append('\n');
        bytes.comment("appendUtf8").appendUtf8("£ 1").append('\n');

        System.out.println(bytes.toHexString());

        String a = bytes.read8bit();
        String b = bytes.readUtf8();
        String c = bytes.parse8bit(StopCharTesters.CONTROL_STOP);
        String d = bytes.parseUtf8(StopCharTesters.CONTROL_STOP);
        assertEquals("£ 1", a);
        assertEquals("£ 1", b);
        assertEquals("£ 1", c);
        assertEquals("£ 1", d);
        System.out.println(System.identityHashCode(a));
        System.out.println(System.identityHashCode(b));
        System.out.println(System.identityHashCode(c));
        System.out.println(System.identityHashCode(d));
        // uses the pool but a different hash.
        // assertSame(a, c); // uses a string pool
        assertSame(b, c); // uses a string pool
        assertSame(b, d); // uses a string pool
        bytes.releaseLast();
    }

    @Test
    public void testNull() {
        HexDumpBytes bytes = new HexDumpBytes();
        bytes.comment("write8bit").write8bit((String) null);
        bytes.comment("writeUtf8").writeUtf8(null);

        System.out.println(bytes.toHexString());

        String a = bytes.read8bit();
        String b = bytes.readUtf8();
        assertNull(a);
        assertNull(b);
        bytes.releaseLast();
    }
}

@@ -393,16 +393,17 @@
 ommon %7B%0A
+%0A
     @Tes
@@ -485,24 +485,25 @@
 ded());%0A
+%0A
         
 HexDumpB
@@ -486,32 +486,38 @@
 ed());%0A%0A        
+final 
 HexDumpBytes byt
@@ -533,32 +533,50 @@
 HexDumpBytes();%0A
+        try %7B%0A    
         bytes.co
@@ -612,32 +612,36 @@
 %22%C2%A3 1%22);%0A        
+  
+  
 bytes.comment(%22w
@@ -661,32 +661,36 @@
 iteUtf8(%22%C2%A3 1%22);%0A
+    
         bytes.co
@@ -741,32 +741,36 @@
 ('%5Cn');%0A        
+  
+  
 bytes.comment(%22a
@@ -813,32 +813,39 @@
 ('%5Cn');%0A%0A       
+     //
  System.out.prin
@@ -871,32 +871,42 @@
 ng());%0A%0A        
+    final 
 String a = bytes
@@ -918,32 +918,42 @@
 8bit();%0A        
+    final 
 String b = bytes
@@ -969,24 +969,34 @@
 ();%0A        
+    final 
 String c = b
@@ -1045,24 +1045,34 @@
 P);%0A        
+    final 
 String d = b
@@ -1109,32 +1109,36 @@
 .CONTROL_STOP);%0A
+    
         assertEq
@@ -1153,32 +1153,36 @@
 1%22, a);%0A        
+    
 assertEquals(%22%C2%A3 
@@ -1189,32 +1189,36 @@
 1%22, b);%0A        
+ 
+   
 assertEquals(%22%C2%A3 
@@ -1225,32 +1225,36 @@
 1%22, c);%0A        
+ 
+   
 assertEquals(%22%C2%A3 
@@ -1261,23 +1261,31 @@
 1%22, d);%0A
+%0A    
        
+ //
  System.
@@ -1324,32 +1324,39 @@
 ode(a));%0A       
+     //
  System.out.prin
@@ -1387,32 +1387,39 @@
 ode(b));%0A       
+     //
  System.out.prin
@@ -1450,32 +1450,39 @@
 ode(c));%0A       
+     //
  System.out.prin
@@ -1514,24 +1514,26 @@
 de(d));%0A
+%0A 
         
 // uses 
@@ -1524,16 +1524,19 @@
         
+   
 // uses 
@@ -1566,16 +1566,20 @@
 t hash.%0A
+    
         
@@ -1621,32 +1621,36 @@
 ng pool%0A        
+ 
+   
 assertSame(b, c)
@@ -1665,32 +1665,36 @@
 s a string pool%0A
+    
         assertSa
@@ -1717,32 +1717,56 @@
 s a string pool%0A
+        %7D finally %7B%0A    
         bytes.re
@@ -1782,15 +1782,26 @@
 ();%0A
+    
     %7D%0A%0A
+    %7D%0A%0A
     
@@ -1843,16 +1843,22 @@
         
+final 
 HexDumpB
@@ -1882,32 +1882,50 @@
 HexDumpBytes();%0A
+        try %7B%0A    
         bytes.co
@@ -1969,32 +1969,36 @@
  null);%0A        
+   
+ 
 bytes.comment(%22w
@@ -2034,16 +2034,22 @@
         
+    //
 System.o
@@ -2086,24 +2086,34 @@
 );%0A%0A        
+  
+  final 
 String a = b
@@ -2137,16 +2137,26 @@
         
+ 
+   final 
 String b
@@ -2176,32 +2176,36 @@
 Utf8();%0A        
+    
 assertNull(a);%0A 
@@ -2199,24 +2199,28 @@
 ertNull(a);%0A
+    
         asse
@@ -2226,24 +2226,48 @@
 ertNull(b);%0A
+        %7D finally %7B%0A    
         byte
@@ -2287,12 +2287,21 @@
 ();%0A
+        %7D%0A
     %7D%0A%7D
-%0A

Tidy and reduce clutter