//
//  TestJSONDecoding.java
//  xal
//
//  Created by Tom Pelaia on 2/17/2011.
//  Copyright 2011 Oak Ridge National Lab. All rights reserved.
//

package xal.tools.json;

import org.junit.*;
import java.util.*;
import java.io.*;


/** test the complex number class */
public class TestJSONCoding {
    @Test
    public void testNullCoding() {
        Assert.assertTrue( "null" == JSONCoder.encode( (Object)null ) );
    }
    
    
    @Test
    public void testDoubleCoding() {
        checkCodingEquality( -5.3E5 );
        checkCodingEquality( -17.8 );
        checkCodingEquality( 5.3 );
        checkCodingEquality( 2.5E17 );
        checkCodingEquality( 6.2E-23 );
    }
    
    
    @Test
    public void testBooleanCoding() {
        checkCodingEquality( true );
        checkCodingEquality( false );
    }
    
    
    @Test
    public void testStringCoding() {
        checkCodingEquality( "\"Hello, World\"", "Hello, World" );
        checkCodingEquality( "\"String with an \\\"internal\\\" string.\"", "String with an \"internal\" string." );
    }
    
    
    /** check whether the coder can encode values */
    static private <DataType> void checkCodingEquality( final DataType value ) {
        final String controlCoding = String.valueOf( value );
        checkCodingEquality( controlCoding, value );
    }
    
    
    /** check whether the coder can encode values */
    static private <DataType> void checkCodingEquality( final String controlCoding, final DataType value ) {
        final String testCoding = JSONCoder.encode( value );
        assertEquality( controlCoding, testCoding );
    }
    
    
    /** Assert whether the control value equals the test value */
    static private void assertEquality( final Object controlValue, final Object testValue ) {
        Assert.assertTrue( controlValue.equals( testValue ) );
    }
}
@@ -1060,32 +1060,1548 @@
     %7D%0A    %0A    %0A
+    @Test%0A    public void testArrayCoding() %7B%0A        assertEquality( %22%5B1.2, -17.6, 5.4E23, 1.2E-6%5D%22, JSONCoder.encode( new Double%5B%5D %7B 1.2, -17.6, 5.4E23, 1.2E-6 %7D ) );%0A        checkCodingEquality( %22%5B%5C%22Hello%5C%22, %5C%22World%5C%22%5D%22, new String%5B%5D %7B %22Hello%22, %22World%22 %7D );%0A        checkCodingEquality( %22%5B%5C%22Testing%5C%22, %5C%22%5C%5C%5C%22internal%5C%5C%5C%22 string.%5C%22%5D%22, new String%5B%5D %7B %22Testing%22, %22%5C%22internal%5C%22 string.%22 %7D );%0A    %7D%0A    %0A    %0A    @Test%0A    public void testListCoding() %7B%0A        final List testList = new ArrayList();%0A        testList.add( %22Hello%22 );%0A        testList.add( %22World%22 );%0A        testList.add( 1.5 );%0A        testList.add( 2.7 );%0A        testList.add( -324.2 );%0A        assertEquality( %22%5B%5C%22Hello%5C%22, %5C%22World%5C%22, 1.5, 2.7, -324.2%5D%22, JSONCoder.encode( testList ) );%0A    %7D%0A    %0A    %0A    @Test%0A    public void testMapCoding() %7B%0A        final Map testMap = new TreeMap();  // need to guarantee ordering so we can test against our control%0A        testMap.put( %22message%22, %22Hello, World%22 );%0A        testMap.put( %22value%22, 5.4 );%0A        testMap.put( %22cities%22, new String%5B%5D %7B %22Knoxville%22, %22Oak Ridge%22 %7D );%0A        // make sure the control coding lists keys in alphabetical ordering (corresponding to TreeMap)%0A        final String controlCoding = %22%7B%5C%22cities%5C%22: %5B%5C%22Knoxville%5C%22, %5C%22Oak Ridge%5C%22%5D, %5C%22message%5C%22: %5C%22Hello, World%5C%22, %5C%22value%5C%22: 5.4%7D%22;%0A//        System.out.println( controlCoding );%0A//        System.out.println( JSONCoder.encode( testMap ) );%0A        assertEquality( controlCoding, JSONCoder.encode( testMap ) );%0A    %7D%0A    %0A    %0A
     /** check wh

Add JSON coding tests for lists and maps