package com.alibaba.json.test;

import com.alibaba.fastjson.util.IOUtils;
import junit.framework.TestCase;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

/**
 * Created by wenshao on 24/07/2017.
 */
public class UTF8Test extends TestCase {
    Charset charset = Charset.forName("UTF-8");
    String text = "央视的报道《陆军第82集团军：聚合重塑 强军路上当先锋！》披露，从南昌起义到平江起义，从井冈山斗争到两万五千里长征，从首战平型关到历经三大战役，从穿插三所里到全歼美军北极熊团，22勇士飞夺泸定桥、18勇士强渡乌江、鏖战冀中“野八旅”、屡战屡胜“老虎连”、万岁军，一支支善战的部队，一个个滚烫的名字，熔铸成第82集团军新的灵魂。";
    //String text = "Model and actress Emily Ratajkowski would you like you to know she has wonderful abs. We don’t know this because we’re psychic, but rather can surmise this desire from her many photos she posts on Instagram. Whether it’s due to genetics, diet, great Instagram techniques, or some combination of the above, the rising star takes ample opportunity to show you what she’s got, and her fans love it.";
    char[] chars = text.toCharArray();
    byte[] bytes = new byte[chars.length * 3];



    ByteBuffer byteBuffer;

    protected void setUp() throws Exception {
        byteBuffer = ByteBuffer.allocate(text.length() * 3);
    }

    public void test_encode() throws Exception {


        for (int i = 0; i < 10; ++i) {
            f0();
        }
        for (int i = 0; i < 10; ++i) {
            f1();
        }
        for (int i = 0; i < 10; ++i) {
            f2();
        }
    }

    private void f0() throws Exception {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000 * 1000; ++i) {
            text.getBytes(charset);
        }
        long millis = System.currentTimeMillis() - start;
        System.out.println("f0 millis : " + millis);
    }

    private void f1() throws Exception {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000 * 1000; ++i) {
            IOUtils.encodeUTF8(chars, 0, chars.length, bytes);
        }
        long millis = System.currentTimeMillis() - start;
        System.out.println("f1 millis : " + millis);
    }

    private void f2() throws Exception {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000 * 1000; ++i) {
            charset.newEncoder().encode(CharBuffer.wrap(chars));
        }
        long millis = System.currentTimeMillis() - start;
        System.out.println("f2 millis : " + millis);
    }
}

@@ -281,56 +281,8 @@
 e %7B%0A
-    Charset charset = Charset.forName(%22UTF-8%22);%0A
     
@@ -284,28 +284,26 @@
 %0A    String 
-text
+T0
  = %22%E5%A4%AE%E8%A7%86%E7%9A%84%E6%8A%A5%E9%81%93%E3%80%8A%E9%99%86%E5%86%9B
@@ -460,16 +460,576 @@
 %E6%96%B0%E7%9A%84%E7%81%B5%E9%AD%82%E3%80%82%22;%0A
+    String T1 = %22Model and actress Emily Ratajkowski would you like you to know she has wonderful abs. We don%E2%80%99t know this because we%E2%80%99re psychic, but rather can surmise this desire from her many photos she posts on Instagram. Whether it%E2%80%99s due to genetics, diet, great Instagram techniques, or some combination of the above, the rising star takes ample opportunity to show you what she%E2%80%99s got, and her fans love it.%22;%0A%0A    Charset charset = Charset.forName(%22UTF-8%22);%0A    String text = new StringBuilder().append(T0).append(System.currentTimeMillis()).toString();%0A
     //St
@@ -1594,32 +1594,104 @@
 ows Exception %7B%0A
+        System.out.println(System.getProperty(%22java.runtime.version%22));%0A
         byteBuff
@@ -1813,34 +1813,33 @@
 (int i = 0; i %3C 
-10
+5
 ; ++i) %7B%0A       
@@ -1879,34 +1879,33 @@
 (int i = 0; i %3C 
-10
+5
 ; ++i) %7B%0A       
@@ -1953,18 +1953,17 @@
  0; i %3C 
-10
+5
 ; ++i) %7B

add testcase.