added binary word unit tests

cvs
Slava Pestov 2005-06-10 21:41:41 +00:00
parent cd9e2f6c9f
commit d13b024374
8 changed files with 56 additions and 14 deletions

View File

@ -3840,6 +3840,32 @@ Like \verb|with-stream| extend the stream is only closed in the case of an error
Calls the quotation in a new dynamic scope, with the \texttt{stdio} variable set to a new string buffer. Executing \texttt{write}, \texttt{write-attr} or \texttt{print} will append text to the string buffer. When the quotation returns, the string buffer is coverted to
a string and returned.
\subsection{Reading and writing binary data}
\glossary{name=big endian,
description={a representation of an integer as a sequence of bytes, ordered from most significant to least significant. This is the native byte ordering for PowerPC, SPARC, Alpha and ARM processors}}
\glossary{name=little endian,
description={a representation of an integer as a sequence of bytes, ordered from least significant to most significant. This is the native byte ordering for x86 and x86-64 processors}}
The core stream words read and write strings. Integers and floats can be read and written by converting to and from sequences of bytes.
There are two ways to order the bytes making up an integer; \emph{little endian} byte order outputs the least significant byte first, and the most significant byte last, whereas \emph{big endian} is the other way around.
Consider the hexadecimal integer \texttt{HEX: cafebabe}. Big endian byte order yields the following sequence of bytes:
\begin{tabular}{l|c|c|c|c}
Byte:&1&2&3&4\\
\hline
Value:&\verb|be|&\verb|ba|&\verb|fe|&\verb|ca|\\
\end{tabular}
Compare this with little endian byte order:
\begin{tabular}{l|c|c|c|c}
Byte:&1&2&3&4\\
\hline
Value:&\verb|ca|&\verb|fe|&\verb|ba|&\verb|be|\\
\end{tabular}
\subsection{Reading and writing files}
\glossary{name=file reader,

View File

@ -296,15 +296,23 @@ M: hashtable ' ( hashtable -- pointer )
( Image output )
: write-word ( word -- )
: (write-image) ( image -- )
"64-bits" get [
"big-endian" get [ write-be8 ] [ write-le8 ] ifte
"big-endian" get [
[ write-be8 ] each
] [
[ write-le8 ] each
] ifte
] [
"big-endian" get [ write-be4 ] [ write-le4 ] ifte
"big-endian" get [
[ write-be4 ] each
] [
[ write-le4 ] each
] ifte
] ifte ;
: write-image ( image file -- )
<file-writer> [ [ write-word ] each ] with-stream ;
<file-writer> [ (write-image) ] with-stream ;
: with-minimal-image ( quot -- image )
[

View File

@ -11,6 +11,9 @@
! low-level... but be aware that vectors are usually a better
! choice.
IN: math
DEFER: repeat
IN: kernel-internals
USING: kernel math-internals sequences ;
@ -26,3 +29,8 @@ M: array length array-capacity ;
M: array nth array-nth ;
M: array set-nth set-array-nth ;
M: array resize resize-array ;
: copy-array ( to from -- )
dup array-capacity [
3dup swap array-nth pick rot set-array-nth
] repeat 2drop ;

View File

@ -140,7 +140,7 @@ IN: hashtables
M: hashtable clone ( hash -- hash )
dup bucket-count <hashtable>
over hash-size over set-hash-size
[ hash-array swap hash-array copy-into ] keep ;
[ hash-array swap hash-array copy-array ] keep ;
M: hashtable = ( obj hash -- ? )
2dup eq? [

View File

@ -137,9 +137,6 @@ M: object peek ( sequence -- element )
M: object reverse ( seq -- seq ) [ nreverse ] immutable ;
: copy-into ( to from -- )
dup length [ 3dup swap nth pick rot set-nth ] repeat 3drop ;
! Equality testing
: length= ( seq seq -- ? ) length swap length number= ;

View File

@ -12,11 +12,6 @@ hashtables errors sequences vectors ;
! slot 2 - the class, a word
! slot 3 - the delegate tuple, or f
: copy-array ( to from -- )
dup array-capacity [
3dup swap array-nth pick rot set-array-nth
] repeat 2drop ;
: make-tuple ( class size -- tuple )
#! Internal allocation function. Do not call it directly,
#! since you can fool the runtime and corrupt memory by

View File

@ -0,0 +1,8 @@
IN: temporary
USING: stdio test ;
[ "\0\0\u0004\u00d2" ] [ 1234 4 >be ] unit-test
[ "\u00d2\u0004\0\0" ] [ 1234 4 >le ] unit-test
[ 1234 ] [ 1234 4 >be be> ] unit-test
[ 1234 ] [ 1234 4 >le le> ] unit-test

View File

@ -89,7 +89,7 @@ SYMBOL: failures
"crashes" "sbuf" "threads" "parsing-word"
"inference" "interpreter" "alien"
"line-editor" "gadgets" "memory" "redefine"
"annotate" "sequences"
"annotate" "sequences" "binary"
] run-tests ;
: benchmarks