factor/basis/io/buffers/buffers-tests.factor

83 lines
2.1 KiB
Factor
Raw Normal View History

2008-03-01 17:00:45 -05:00
IN: io.buffers.tests
USING: accessors alien alien.data arrays byte-arrays destructors
io.buffers kernel libc namespaces sequences strings tools.test ;
2007-09-20 18:09:08 -04:00
: buffer-set ( string buffer -- )
[ ptr>> swap >byte-array binary-object memcpy ]
[ [ length ] dip buffer-reset ]
2bi ;
2007-09-20 18:09:08 -04:00
: string>buffer ( string -- buffer )
dup length <buffer> [ buffer-set ] keep ;
2007-09-20 18:09:08 -04:00
: buffer-read-all ( buffer -- byte-array )
2014-11-20 18:46:07 -05:00
[ buffer@ ] [ buffer-length ] bi memory>byte-array ;
{ B{ } 65536 } [
2007-09-20 18:09:08 -04:00
65536 <buffer>
dup buffer-read-all
2007-09-20 18:09:08 -04:00
over buffer-capacity
rot dispose
2007-09-20 18:09:08 -04:00
] unit-test
{ "hello world" "" } [
2007-09-20 18:09:08 -04:00
"hello world" string>buffer
dup buffer-read-all >string
2007-09-20 18:09:08 -04:00
0 pick buffer-reset
over buffer-read-all >string
rot dispose
2007-09-20 18:09:08 -04:00
] unit-test
{ "hello" } [
2007-09-20 18:09:08 -04:00
"hello world" string>buffer
5 over buffer-read >string swap dispose
2007-09-20 18:09:08 -04:00
] unit-test
{ 11 } [
2007-09-20 18:09:08 -04:00
"hello world" string>buffer
[ buffer-length ] keep dispose
2007-09-20 18:09:08 -04:00
] unit-test
{ "hello world" } [
2007-09-20 18:09:08 -04:00
"hello" 1024 <buffer> [ buffer-set ] keep
2014-11-16 21:54:24 -05:00
" world" >byte-array binary-object pick buffer-write
dup buffer-read-all >string swap dispose
2007-09-20 18:09:08 -04:00
] unit-test
{ CHAR: e } [
2007-09-20 18:09:08 -04:00
"hello" string>buffer
1 over buffer-consume [ buffer-pop ] keep dispose
2007-09-20 18:09:08 -04:00
] unit-test
"hello world" string>buffer "b" set
{ "hello world" } [ 1000 "b" get buffer-read >string ] unit-test
"b" get dispose
2007-09-20 18:09:08 -04:00
100 <buffer> "b" set
2014-11-16 21:54:24 -05:00
[ 1000 "b" get buffer+ >string ] must-fail
"b" get dispose
"hello world" string>buffer "b" set
{ "hello" CHAR: \s } [ " " "b" get buffer-read-until [ >string ] dip ] unit-test
"b" get dispose
"hello world" string>buffer "b" set
{ "hello worl" CHAR: d } [ "d" "b" get buffer-read-until [ >string ] dip ] unit-test
"b" get dispose
"hello world" string>buffer "b" set
{ "hello world" f } [ "\n" "b" get buffer-read-until [ >string ] dip ] unit-test
"b" get dispose
{ 4 B{ 1 2 3 4 0 0 0 0 0 0 } } [
10 <buffer>
[ B{ 1 2 3 4 } binary-object rot buffer-write ]
[ 10 <byte-array> [ 10 rot buffer-read-into ] keep ] bi
] unit-test
{ 4 { 1 2 3 4 f f f f f f } } [
10 <buffer>
[ B{ 1 2 3 4 } binary-object rot buffer-write ]
[ 10 f <array> [ 10 rot buffer-read-into ] keep ] bi
] unit-test