package org.luaj.vm;

import java.io.IOException;
import java.io.InputStream;

import org.luaj.vm.LString;

import junit.framework.TestCase;

public class LStringTest extends TestCase {
	public void testToInputStream() throws IOException {
		LString str = new LString("Hello");
		
		InputStream is = str.toInputStream();
		
		assertEquals( 'H', is.read() );
		assertEquals( 'e', is.read() );
		assertEquals( 2, is.skip( 2 ) );
		assertEquals( 'o', is.read() );
		assertEquals( -1, is.read() );
		
		assertTrue( is.markSupported() );
		
		is.reset();
		
		assertEquals( 'H', is.read() );
		is.mark( 4 );
		
		assertEquals( 'e', is.read() );
		is.reset();
		assertEquals( 'e', is.read() );
		
		LString substr = str.substring( 1, 4 );
		assertEquals( 3, substr.length() );
		
		is.close();
		is = substr.toInputStream();
		
		assertEquals( 'e', is.read() );
		assertEquals( 'l', is.read() );
		assertEquals( 'l', is.read() );
		assertEquals( -1, is.read() );
		
		is = substr.toInputStream();
		is.reset();
		
		assertEquals( 'e', is.read() );
	}
	
	
	private static final String userFriendly( String s ) {
		StringBuffer sb = new StringBuffer();
		for ( int i=0, n=s.length(); i<n; i++ ) {
			int c = s.charAt(i);
			if ( c < ' ' || c >= 0x80 ) { 
				sb.append( "\\u"+Integer.toHexString(0x10000+c).substring(1) );
			} else {
				sb.append( (char) c );
			}
		}
		return sb.toString();
	}
	
	public void testUtf8() {		
		for ( int i=4; i<0xffff; i+=4 ) {
			char[] c = { (char) (i+0), (char) (i+1), (char) (i+2), (char) (i+3) };
			String before = new String(c)+" "+i+"-"+(i+4);
			LString ls = new LString(before);
			String after = ls.toJavaString();
			assertEquals( userFriendly( before ), userFriendly( after ) );
		}
		char[] c = { (char) (1), (char) (2), (char) (3) };
		String before = new String(c)+" 1-3";
		LString ls = new LString(before);
		String after = ls.toJavaString();
		assertEquals( userFriendly( before ), userFriendly( after ) );
		
	}

	public void testNullTerminated() {		
		char[] c = { 'a', 'b', 'c', '\0', 'd', 'e', 'f' };
		String before = new String(c);
		LString ls = new LString(before);
		String after = ls.toJavaString();
		assertEquals( userFriendly( "abc" ), userFriendly( after ) );
		
	}
}

@@ -70,16 +70,61 @@
 tStream;
+%0Aimport java.io.UnsupportedEncodingException;
 %0A%0Aimport
@@ -1999,16 +1999,485 @@
 %0A%09%09%0A%09%7D%0A%0A
+%09public void testSpotCheckUtf8() throws UnsupportedEncodingException %7B%0A%09%09byte%5B%5D bytes = %7B(byte)194,(byte)160,(byte)194,(byte)161,(byte)194,(byte)162,(byte)194,(byte)163,(byte)194,(byte)164%7D;%0A%09%09String expected = new String(bytes, %22UTF8%22);%0A%09%09String actual = new LString(bytes).toJavaString();%0A%09%09char%5B%5D d = actual.toCharArray();%0A%09%09assertEquals(160, d%5B0%5D);%0A%09%09assertEquals(161, d%5B1%5D);%0A%09%09assertEquals(162, d%5B2%5D);%0A%09%09assertEquals(163, d%5B3%5D);%0A%09%09assertEquals(164, d%5B4%5D);%0A%09%09%0A%09%7D%0A%09%0A
 %09public 

Add spot-check test on utf8 conversions.