]> gitweb.factorcode.org Git - factor.git/commitdiff
added binary word unit tests
authorSlava Pestov <slava@factorcode.org>
Fri, 10 Jun 2005 21:41:41 +0000 (21:41 +0000)
committerSlava Pestov <slava@factorcode.org>
Fri, 10 Jun 2005 21:41:41 +0000 (21:41 +0000)
doc/handbook.tex
library/bootstrap/image.factor
library/collections/arrays.factor
library/collections/hashtables.factor
library/collections/sequences-epilogue.factor
library/generic/tuple.factor
library/test/binary.factor [new file with mode: 0644]
library/test/test.factor

index 3a1a3c0462959e54b0b5f1421918213dad221e03..6f67f8561dd27c859e60a4eb0afb3613bb1788f5 100644 (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,
index ea96490c972f4f21adaba82b337b424f608fc64d..434833b89054925e6d562ebfcf0af6a01fb081ff 100644 (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 )
     [
index a1adf291830b2ea9e7b4b24378559851b4f183a1..425fa27074dad80e65c8b365ade71c40d08b5926 100644 (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 ;
index f72368aa5fe38252b37833bb165c2ac8925498c8..5a929154ca4edd8c5094130ff979f6960009a34e 100644 (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? [
index d3762b44b89a163c7d93ea38d4adbe7cfbfd1f73..f1888676bd97eca279c34661c747908d7a8b79cc 100644 (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= ;
 
index 55ce819698a741523595b93a793c48b2718f35fd..7d56015d06d0145ef447a765a516710305b3f972 100644 (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
diff --git a/library/test/binary.factor b/library/test/binary.factor
new file mode 100644 (file)
index 0000000..49b64b3
--- /dev/null
@@ -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
index 88a5f2c3df3ef33babbbe939894ac7881d605561..16c5df0debfa9c4b43938ec8b866906658036aa5 100644 (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