From d13b0243743e318299bfbb25b8426273e912ea08 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 10 Jun 2005 21:41:41 +0000 Subject: [PATCH] added binary word unit tests --- doc/handbook.tex | 26 +++++++++++++++++++ library/bootstrap/image.factor | 16 +++++++++--- library/collections/arrays.factor | 8 ++++++ library/collections/hashtables.factor | 2 +- library/collections/sequences-epilogue.factor | 3 --- library/generic/tuple.factor | 5 ---- library/test/binary.factor | 8 ++++++ library/test/test.factor | 2 +- 8 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 library/test/binary.factor diff --git a/doc/handbook.tex b/doc/handbook.tex index 3a1a3c0462..6f67f8561d 100644 --- a/doc/handbook.tex +++ b/doc/handbook.tex @@ -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, diff --git a/library/bootstrap/image.factor b/library/bootstrap/image.factor index ea96490c97..434833b890 100644 --- a/library/bootstrap/image.factor +++ b/library/bootstrap/image.factor @@ -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 -- ) - [ [ write-word ] each ] with-stream ; + [ (write-image) ] with-stream ; : with-minimal-image ( quot -- image ) [ diff --git a/library/collections/arrays.factor b/library/collections/arrays.factor index a1adf29183..425fa27074 100644 --- a/library/collections/arrays.factor +++ b/library/collections/arrays.factor @@ -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 ; diff --git a/library/collections/hashtables.factor b/library/collections/hashtables.factor index f72368aa5f..5a929154ca 100644 --- a/library/collections/hashtables.factor +++ b/library/collections/hashtables.factor @@ -140,7 +140,7 @@ IN: hashtables M: hashtable clone ( hash -- hash ) dup bucket-count 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? [ diff --git a/library/collections/sequences-epilogue.factor b/library/collections/sequences-epilogue.factor index d3762b44b8..f1888676bd 100644 --- a/library/collections/sequences-epilogue.factor +++ b/library/collections/sequences-epilogue.factor @@ -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= ; diff --git a/library/generic/tuple.factor b/library/generic/tuple.factor index 55ce819698..7d56015d06 100644 --- a/library/generic/tuple.factor +++ b/library/generic/tuple.factor @@ -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 diff --git a/library/test/binary.factor b/library/test/binary.factor new file mode 100644 index 0000000000..49b64b38d3 --- /dev/null +++ b/library/test/binary.factor @@ -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 diff --git a/library/test/test.factor b/library/test/test.factor index 88a5f2c3df..16c5df0deb 100644 --- a/library/test/test.factor +++ b/library/test/test.factor @@ -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