1. rivo
  2. bitstring

Commits

rivo  committed d8a0316

fix tests and examples

  • Participants
  • Parent commits fe23e3a
  • Branches default

Comments (0)

Files changed (3)

File bitstring.scm

View file
     ((_ "write" stream handler ((VALUE ...) bitstring) rest ...)
       (and-let* ((tmp (VALUE ...))
                  (bits (bitstring-length tmp)))
-        (bitstring-pattern2 "write" stream handler (tmp bits bitstring) rest ...)))
+        (bitstring-pattern "write" stream handler (tmp bits bitstring) rest ...)))
     ; bitpacket
     ((_ mode stream handler (NAME bitpacket) rest ...)
       (bitstring-packet-expand mode stream handler NAME rest ...))

File tests/examples.scm

View file
 
 (use bitstring)
 
+; The following will print "integer:3721182122",
+; which is the decimal value of #xDDCCBBAA
 (bitmatch "\x01\xAA\xBB\xCC\xDD"
   (((#x01) (IValue 32 little))
       (print "integer:" IValue))
 
 (use bitstring)
 
+; This will print "StringData:(65 66 67 68 69)"
+; First it reads the length byte of 5, bind it to Length and
+; then it will read a bit string with a length of that many octets.
 (bitmatch "\x05\x00ABCDE"
   (((Length 16 little)
     (StringData (* 8 Length) bitstring))
-      (print "StringData:" (bitstring->list StringData)))
+      (print "StringData:" (bitstring->list StringData 8)))
   (else
       (print "invalid string")))
 
 (use bitstring srfi-4)
 
 (define IPRaw `#u8( #x45 #x00 #x00 #x6c
-		    #x92 #xcc #x00 #x00
-		    #x38 #x06 #x00 #x00
-		    #x92 #x95 #xba #x14
-		    #xa9 #x7c #x15 #x95 ))
+        #x92 #xcc #x00 #x00
+        #x38 #x06 #x00 #x00
+        #x92 #x95 #xba #x14
+        #xa9 #x7c #x15 #x95 ))
 
 (bitmatch IPRaw
   (((Version 4)
     (Reserved 1) (DF 1) (MF 1)
     (FramgentOffset 13)
     (TTL 8)
-    (Protocol 8) (check (or (= Protocol 1)
-    	                    (= Protocol 2)
-    	                    (= Protocol 6)
-    	                    (= Protocol 17))) 
+    (Protocol 8) (? (or (= Protocol 1)
+                        (= Protocol 2)
+                        (= Protocol 6)
+                        (= Protocol 17))) 
     (CheckSum 16)
     (SourceAddr 32 bitstring)
     (DestinationAddr 32 bitstring)
     (Optional bitstring))
-    	; print packet filds
-    	(print "\n Version: " Version
-    	       "\n IHL: " IHL
-    	       "\n TOS: " TOS
-    	       "\n TL:  " TL
-    	       "\n Identification: " Identification
-    	       "\n DF: " DF
-    	       "\n MF: " MF
-    	       "\n FramgentOffset: " FramgentOffset
-    	       "\n Protocol: " Protocol
-    	       "\n CheckSum: " CheckSum
-    	       "\n SourceAddr: " 
-    	           (bitmatch SourceAddr (((A)(B)(C)(D)) (list A B C D)))
+      ; print packet filds
+      (print "\n Version: " Version
+             "\n IHL: " IHL
+             "\n TOS: " TOS
+             "\n TL:  " TL
+             "\n Identification: " Identification
+             "\n DF: " DF
+             "\n MF: " MF
+             "\n FramgentOffset: " FramgentOffset
+             "\n Protocol: " Protocol
+             "\n CheckSum: " CheckSum
+             "\n SourceAddr: " 
+                 (bitmatch SourceAddr (((A)(B)(C)(D)) (list A B C D)))
                "\n DestinationAddr: " 
                    (bitmatch DestinationAddr (((A)(B)(C)(D)) (list A B C D)))))
   (else
 
 (define (construct-fixed-string str)
   (bitconstruct
-    (( (string-length str) 16) (str bitstring) )))
+    ((string-length str) 16) (str bitstring) ))
 
+; The following will print "#t".  First, it reads a 16-bit number length
+; and compares it to the immediate value of 7.  Then it will read a
+; string and compare it to the immediate value of "qwerty.".  If there
+; was any remaining data in the string, it would fail.
 (bitmatch (construct-fixed-string "qwerty.")
   (((7 16) ("qwerty."))
     (print #t))
 
 (define (construct-complex-object)
   (bitconstruct
-    ( ((construct-fixed-string "A") bitstring)
-      (#xAABB 16)
-      ((construct-fixed-string "RRR") bitstring))
-      (#\X)))
+    ((construct-fixed-string "A") bitstring)
+    (#xAABB 16)
+    ((construct-fixed-string "RRR") bitstring)
+    (#\X)))
 
 (print (construct-complex-object))
-

File tests/run.scm

View file
 (test-end)
 
 (test-begin "single")
-(test +inf.0 (bitstring->single (bitstring-of-vector `#( #x7F #x80 #x00 #x00))))
-(test -inf.0 (bitstring->single (bitstring-of-vector `#( #xFF #x80 #x00 #x00))))
+(test +inf.0 (bitstring->single (bitstring-of-vector `#( #x00 #x00 #x80 #x7F))))
+(test -inf.0 (bitstring->single (bitstring-of-vector `#( #x00 #x00 #x80 #xFF))))
 ;(test +nan.0 (bitstring->single (bitstring-of-vector `#( #x7F #xC0 #x00 #x00))))
 (test 0. (bitstring->single (bitstring-of-vector `#( #x00 #x00 #x00 #x00))))
-(test -0. (bitstring->single (bitstring-of-vector `#( #x80 #x00 #x00 #x00))))
-(test #t (equal? 1. (bitstring->single (bitstring-of-vector `#( #x3f #x80 #x00 #x00)))))
-(test 0.5 (bitstring->single (bitstring-of-vector `#( #x3f #x00 #x00 #x00))))
-(test 25. (bitstring->single (bitstring-of-vector `#( #x41 #xc8 #x00 #x00))))
-(test 0.1 (bitstring->single (bitstring-of-vector `#( #x3d #xcc #xcc #xcd))))
-(test -0.123 (bitstring->single (bitstring-of-vector `#( #xBD #xFB #xE7 #x6D))))
-(test `(0 32 #u8( #x3f #x00 #x00 #x00)) (get-fields (single->bitstring 0.5)))
-(test `(0 32 #u8( #xBD #xFB #xE7 #x6D)) (get-fields (single->bitstring -0.123)))
+(test -0. (bitstring->single (bitstring-of-vector `#( #x00 #x00 #x00 #x80))))
+(test #t (equal? 1. (bitstring->single (bitstring-of-vector `#( #x00 #x00 #x80 #x3f)))))
+(test 0.5 (bitstring->single (bitstring-of-vector `#( #x00 #x00 #x00 #x3f))))
+(test 25. (bitstring->single (bitstring-of-vector `#( #x00 #x00 #xc8 #x41))))
+(test 0.1 (bitstring->single (bitstring-of-vector `#( #xcd #xcc #xcc #x3d))))
+(test -0.123 (bitstring->single (bitstring-of-vector `#( #xE7 #x6D #xFB #xBD))))
+(test `(0 32 #u8( #x00 #x00 #x00 #x3f)) (get-fields (single->bitstring 0.5)))
+(test `(0 32 #u8( #x6D #xE7 #xFB #xBD)) (get-fields (single->bitstring -0.123)))
 (test-end)
 
 (test-exit)