Unit tests and refactoring for ascii and latin1

db4
Daniel Ehrenberg 2008-03-06 15:07:00 -06:00
parent b32276d1da
commit c4d6201b77
4 changed files with 25 additions and 7 deletions

View File

@ -0,0 +1,9 @@
USING: io.encodings.string io.encodings.ascii tools.test strings arrays ;
IN: io.encodings.ascii.tests
[ B{ CHAR: f CHAR: o CHAR: o } ] [ "foo" ascii encode ] unit-test
[ { 128 } >string ascii encode ] must-fail
[ B{ 127 } ] [ { 127 } ascii encode ] unit-test
[ "bar" ] [ "bar" ascii decode ] unit-test
[ { CHAR: b HEX: fffd CHAR: r } ] [ { CHAR: b 233 CHAR: r } ascii decode >array ] unit-test

View File

@ -3,13 +3,16 @@
USING: io io.encodings strings kernel math sequences byte-arrays io.encodings ;
IN: io.encodings.ascii
: encode-check<= ( string stream max -- )
: encode-check< ( string stream max -- )
[ pick <= [ encode-error ] [ stream-write1 ] if ] 2curry each ;
: push-if< ( sbuf character max -- )
over <= [ drop HEX: fffd ] when swap push ;
TUPLE: ascii ;
M: ascii stream-write-encoded ( string stream encoding -- )
drop 127 encode-check<= ;
drop 128 encode-check< ;
M: ascii decode-step
drop dup 128 >= [ decode-error ] [ swap push ] if ;
drop 128 push-if< ;

View File

@ -0,0 +1,9 @@
USING: io.encodings.string io.encodings.latin1 tools.test strings arrays ;
IN: io.encodings.latin1.tests
[ B{ CHAR: f CHAR: o CHAR: o } ] [ "foo" latin1 encode ] unit-test
[ { 256 } >string latin1 encode ] must-fail
[ B{ 255 } ] [ { 255 } latin1 encode ] unit-test
[ "bar" ] [ "bar" latin1 decode ] unit-test
[ { CHAR: b 233 CHAR: r } ] [ { CHAR: b 233 CHAR: r } latin1 decode >array ] unit-test

View File

@ -6,7 +6,4 @@ IN: io.encodings.latin1
TUPLE: latin1 ;
M: latin1 stream-write-encoded
drop 255 encode-check<= ;
M: latin1 decode-step
drop dup 256 >= [ decode-error ] [ swap push ] if ;
drop 256 encode-check< ;