litesoft
@ 947
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.codec; import junit.framework.*; import org.litesoft.commonfoundation.charstreams.*; public class PrimitiveBooleanCodecTest extends TestCase { public static TestSuite suite() { return new TestSuite( PrimitiveBooleanCodecTest.class ); } public PrimitiveBooleanCodecTest( String name ) { super( name ); } public static void main( String[] args ) { junit.textui.TestRunner.run( suite() ); } public void test_It() throws Exception { PrimitiveBooleanCodec zCodec = PrimitiveBooleanCodec.INSTANCE; checkRT( zCodec, true ); checkRT( zCodec, false ); try { decode( zCodec, "~" ); fail( "Bad Char OK?" ); } catch ( IllegalArgumentException expected ) { // OK } String zEncoded = encode( zCodec, true ); try { decode( zCodec, zEncoded + AbstractTypedTerminatingCodec.NULL_AS_CHAR ); fail( "Extra Char OK?" ); } catch ( IllegalArgumentException expected ) { // OK } } private void checkRT( PrimitiveBooleanCodec pCodec, boolean pValue ) { String encoded = encode( pCodec, pValue ); boolean rtValue = decode( pCodec, encoded ); assertEquals( "RT " + pCodec.getClass().getSimpleName() + " (" + encoded + ") ", pValue, rtValue ); System.out.println( "RT " + pCodec.getClass().getSimpleName() + " " + pValue + ": " + encoded ); } private boolean decode( PrimitiveBooleanCodec pCodec, String pEncoded ) { CharSourceFromSequence zCharSource = new CharSourceFromSequence( pEncoded ); boolean rv = pCodec.decode( zCharSource ); if ( zCharSource.anyRemaining() ) { throw new IllegalArgumentException( "Not All Consumed!" ); } return rv; } private String encode( PrimitiveBooleanCodec pCodec, boolean pValue ) { CharSinkToString zCharSink = new CharSinkToString(); pCodec.encode( zCharSink, pValue ); return zCharSink.toString(); } } |