Home | History | Annotate | Download | only in tests
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 
     17 package com.badlogic.gdx.tests;
     18 
     19 import com.badlogic.gdx.Gdx;
     20 import com.badlogic.gdx.tests.utils.GdxTest;
     21 import com.badlogic.gdx.utils.JsonValue;
     22 import com.badlogic.gdx.utils.UBJsonReader;
     23 import com.badlogic.gdx.utils.UBJsonWriter;
     24 
     25 public class UBJsonTest extends GdxTest {
     26 	static final String fn = "test.ubjson";
     27 	static final String longString;
     28 	static {
     29 		StringBuilder sb = new StringBuilder();
     30 		for (int i = 0; i < 300; i++)
     31 			sb.append((char)((i % 26) + 'a'));
     32 		longString = sb.toString();
     33 	}
     34 
     35 	@Override
     36 	public void create () {
     37 		try {
     38 
     39 			UBJsonWriter uw = new UBJsonWriter(Gdx.files.external(fn).write(false));
     40 			uw.object();
     41 			uw.set(longString, longString);
     42 			uw.set("0floats", new float[] {});
     43 			uw.set("3floats", new float[] {1, 2, 3.456789f});
     44 			uw.set("xfloats", new float[] {Float.MIN_VALUE, Float.MAX_VALUE, Float.NaN, Float.NEGATIVE_INFINITY});
     45 			uw.set("double", 0.000000000000000000001);
     46 			uw.set("long", Long.MAX_VALUE);
     47 			uw.set("3bytes", new byte[] {(byte)1, (byte)2, (byte)3});
     48 			uw.set("3shorts", new short[] {(short)1, (short)2, (short)3});
     49 			uw.set("3ints", new int[] {1, 2, 3});
     50 			uw.set("3long", new long[] {1l, 2l, 3l});
     51 			uw.set("3double", new double[] {1, 2, 3.456789});
     52 			uw.set("3char", new char[] {'a', 'b', 'c'});
     53 			uw.set("3strings", new String[] {"", "a", "abc"});
     54 			uw.array("arr");
     55 			uw.object().pop();
     56 			uw.value(true).value(false).value(true);
     57 			uw.value((byte)254);
     58 			uw.value((byte)(-2));
     59 			uw.value((short)-32000);
     60 			uw.value((int)-123456);
     61 			uw.value((long)(-((1 << 63) - 1)));
     62 			uw.pop();
     63 			uw.pop();
     64 			uw.close();
     65 			UBJsonReader ur = new UBJsonReader();
     66 			ur.oldFormat = false;
     67 			JsonValue v = ur.parse(Gdx.files.external(fn));
     68 			Gdx.app.log("UBJsonTest", "result = \n" + v.toString());
     69 			performanceTest();
     70 			Gdx.app.log("UBJsonTest", "Test succeeded");
     71 		} catch (Throwable t) {
     72 			Gdx.app.error("UBJsonTest", "Test failed", t);
     73 		}
     74 	}
     75 
     76 	private void performanceTest () throws Exception {
     77 		Gdx.app.log("UBJsonTest", "--- performanceTest ---");
     78 		long start = System.currentTimeMillis();
     79 		UBJsonWriter uw = new UBJsonWriter(Gdx.files.external(fn).write(false, 8192));
     80 		uw.object();
     81 		uw.set("0floats", new float[] {});
     82 		uw.set("3floats", new float[] {1, 2, 3.456789f});
     83 		uw.set("xfloats", new float[] {Float.MIN_VALUE, Float.MAX_VALUE, Float.NaN, Float.NEGATIVE_INFINITY});
     84 		uw.set("double", 0.000000000000000000001);
     85 		uw.set("long", Long.MAX_VALUE);
     86 		uw.array("arr");
     87 		uw.object().pop();
     88 		for (int i = 0; i < 50000; i++) {
     89 			uw.value(true).value(false).value(true);
     90 			uw.value((byte)254);
     91 			uw.value((byte)(-2));
     92 			uw.value((short)-32000);
     93 			uw.value((int)-123456);
     94 			uw.value((long)(-((1 << 63) - 1)));
     95 			uw.value(longString);
     96 		}
     97 		uw.pop();
     98 		uw.pop();
     99 		uw.close();
    100 
    101 		Gdx.app.log("UBJsonTest", "Writing the test file took " + (System.currentTimeMillis() - start) + "ms");
    102 		Gdx.app.log("UBJsonTest", "File size is " + Gdx.files.external(fn).length());
    103 		UBJsonReader ur = new UBJsonReader();
    104 		ur.oldFormat = false;
    105 		start = System.currentTimeMillis();
    106 		ur.parse(Gdx.files.external(fn));
    107 		Gdx.app.log("UBJsonTest", "Parsing the test file took " + (System.currentTimeMillis() - start) + "ms");
    108 	}
    109 }
    110