factor/extra/io/buffers/buffers-tests.factor

80 lines
1.8 KiB
Factor
Raw Normal View History

2008-03-01 17:00:45 -05:00
IN: io.buffers.tests
USING: alien alien.c-types io.buffers kernel kernel.private libc
2008-03-08 03:51:26 -05:00
sequences tools.test namespaces byte-arrays strings ;
2007-09-20 18:09:08 -04:00
: buffer-set ( string buffer -- )
2008-03-08 03:51:26 -05:00
over >byte-array over buffer-ptr byte-array>memory
2007-09-20 18:09:08 -04:00
>r length r> buffer-reset ;
: string>buffer ( string -- buffer )
dup length <buffer> tuck buffer-set ;
2008-03-08 03:51:26 -05:00
[ B{ } 65536 ] [
2007-09-20 18:09:08 -04:00
65536 <buffer>
dup (buffer>>)
over buffer-capacity
rot buffer-free
] unit-test
[ "hello world" "" ] [
"hello world" string>buffer
2008-03-08 03:51:26 -05:00
dup (buffer>>) >string
2007-09-20 18:09:08 -04:00
0 pick buffer-reset
2008-03-08 03:51:26 -05:00
over (buffer>>) >string
2007-09-20 18:09:08 -04:00
rot buffer-free
] unit-test
[ "hello" ] [
"hello world" string>buffer
2008-03-08 03:51:26 -05:00
5 over buffer> >string swap buffer-free
2007-09-20 18:09:08 -04:00
] unit-test
[ 11 ] [
"hello world" string>buffer
[ buffer-length ] keep buffer-free
] unit-test
[ "hello world" ] [
"hello" 1024 <buffer> [ buffer-set ] keep
2008-03-08 03:51:26 -05:00
" world" >byte-array over >buffer
dup (buffer>>) >string swap buffer-free
2007-09-20 18:09:08 -04:00
] unit-test
[ CHAR: e ] [
"hello" string>buffer
1 over buffer-consume [ buffer-pop ] keep buffer-free
] unit-test
[ "hello" CHAR: \r ] [
"hello\rworld" string>buffer
2008-03-08 03:51:26 -05:00
"\r" over buffer-until >r >string r>
2007-09-20 18:09:08 -04:00
rot buffer-free
] unit-test
[ "hello" CHAR: \r ] [
"hello\rworld" string>buffer
2008-03-08 03:51:26 -05:00
"\n\r" over buffer-until >r >string r>
2007-09-20 18:09:08 -04:00
rot buffer-free
] unit-test
[ "hello\rworld" f ] [
"hello\rworld" string>buffer
2008-03-08 03:51:26 -05:00
"X" over buffer-until >r >string r>
2007-09-20 18:09:08 -04:00
rot buffer-free
] unit-test
[ "hello" CHAR: \r "world" CHAR: \n ] [
"hello\rworld\n" string>buffer
2008-03-08 03:51:26 -05:00
[ "\r\n" swap buffer-until >r >string r> ] keep
[ "\r\n" swap buffer-until >r >string r> ] keep
2007-09-20 18:09:08 -04:00
buffer-free
] unit-test
"hello world" string>buffer "b" set
2008-03-08 03:51:26 -05:00
[ "hello world" ] [ 1000 "b" get buffer> >string ] unit-test
2007-09-20 18:09:08 -04:00
"b" get buffer-free
100 <buffer> "b" set
2008-03-08 03:51:26 -05:00
[ 1000 "b" get n>buffer >string ] must-fail
2007-09-20 18:09:08 -04:00
"b" get buffer-free