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

package xal.tools.text;

import org.junit.*;


/** test the ScientificNumberFormat class */
public class TestScientificNumberFormat {
	@Test
	public void testPositiveSimpleFormats() {
		assertFormat( "    4", 1, 5, 4.32 );
		assertFormat( "   -5", 1, 5, -5.13 );
		assertFormat( "    3.1416", 5, 10, 3.14159265 );
	}


	@Test
	public void testNegativeSimpleFormats() {
		assertFormat( "   -3.1416", 5, 10, -3.14159265 );
	}


	@Test
	public void testPositiveExponentialFormats() {
		assertFormat( "   1.000E1", 4, 10, 10.0 );
		assertFormat( "    3.00E2", 3, 10, 300.0 );
		assertFormat( "  5.24E250", 3, 10, 5.2395E+250 );
		assertFormat( "  1.000E-1", 4, 10, 0.1 );
		assertFormat( "  9.900E-1", 4, 10, 0.99 );
	}


	@Test
	public void testNegativeExponentialFormats() {
		assertFormat( "  -1.000E1", 4, 10, -10.0 );
		assertFormat( " -1.000E-1", 4, 10, -0.1 );
		assertFormat( " -9.900E-1", 4, 10, -0.99 );
		assertFormat( "   -3.00E2", 3, 10, -300.0 );
		assertFormat( "  -5.2395E250", 5, 13, -5.2395E+250 );
		assertFormat( " -5.2395E-250", 5, 13, -5.2395E-250 );
	}


	@Test
	public void testRoundingFormats() {
		assertFormat( "    1.49", 3, 8, 1.494999 );
		assertFormat( "    1.50", 3, 8, 1.495 );
	}


	/** Assert whether the formatted output matches the specified reference */
	static private void assertFormat( final String reference, final int significantDigits, final int width, final double value ) {
		final ScientificNumberFormat format = new ScientificNumberFormat( significantDigits, width );
		final String output = format.format( value );
		Assert.assertEquals( "Failed format equality for number: " + value + " with output: " + output + " of length: " + output.length(), reference, output );
	}
}
@@ -1353,16 +1353,250 @@
 );%0A%09%7D%0A%0A%0A
+%09@Test%0A%09public void testFormattedOutput() %7B%0A%09%09assertFormattedOutput( %22      1.49,      3.14,     -2.60,     -5.00,    5.50E3,   7.30E17, -1.24E-19%22, 3, 10, %22,%22,   1.494999, 3.14159, -2.6, -5, 5496.48, 7.298534E17, -1.2359E-19 );%0A%09%7D%0A%0A%0A
 %09/** Ass
@@ -1621,22 +1621,21 @@
 rmatted 
-output
+value
  matches
@@ -2091,9 +2091,666 @@
 t );%0A%09%7D%0A
+%0A%0A%09/** Assert whether the formatted output matches the specified reference */%0A%09static private void assertFormattedOutput( final String reference, final int significantDigits, final int width, final CharSequence separator, final double ... values ) %7B%0A%09%09final ScientificNumberFormat format = new ScientificNumberFormat( significantDigits, width );%0A%09%09final StringBuffer buffer = new StringBuffer();%0A%09%09format.appendTo( buffer, separator, values );%0A%09%09final String output = buffer.toString();%0A%09%09Assert.assertEquals( %22Failed formatted output equality for values with output %3E%3E%22 + output + %22%3C%3C and expected result of %3E%3E%22 + reference + %22%3C%3C%22, reference, output );%0A%09%7D%0A
 %7D

Add unit testing for formatted output of a series of values.