]> git.mdlowis.com Git - archive/dlang-scm.git/commitdiff
Implemented unit tests for buf.scm
authorMichael D. Lowis <mike@mdlowis.com>
Thu, 5 Jul 2012 04:36:34 +0000 (00:36 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Thu, 5 Jul 2012 04:36:34 +0000 (00:36 -0400)
source/buf.scm
tests/test_buf.scm

index 69394d20b20b2aa66dc858dac960cb87e7b16c4f..bd003a479c147a95d01cbf7a2f9f2afc17b481c8 100644 (file)
 (define (buf-advance! b)
   (buf-pos-set! b (+ 1 (buf-pos b))))
 
+(define (buf-fill! b n)
+  (let loop ((i 0))
+    (buf-data-set!
+      (vector-append (buf-data b) ((buf-ldfn b) (buf-src b))))
+    (if (< i n) (loop (+ i 1)))))
+
 (define (buf-sync! b n)
   (let* ((pos     (buf-pos b))
          (size    (length (buf-data)))
       (if (>= nxt_idx max_idx)
         (buf-fill! b (- nxt_idx max_idx))))))
 
-(define (buf-fill! b n)
-  (let loop ((i 0))
-    (buf-data-set!
-      (vector-append (buf-data b) ((buf-ldfn b))))
-    (if (< i n) (loop (+ i 1)))))
-
 (define (buf-lookahead! b n)
   (buf-sync b n)
   (vector-ref
index 6dcd5f4ed00eb06ecde7460a2e002846329a53bc..54be893b273339207867c8412393c626e31eb9b8 100644 (file)
 (include "test.scm")
 (declare (unit test_buf)
+         (uses buf)
          (uses test))
 
 ; buf
 ;------------------------------------------------------------------------------
-(def-test "buf should create a new general purpose buffer" '())
-
-; buf-struct?
-;------------------------------------------------------------------------------
-(def-test "buf-struct? should return true if passed a buffer struct" '())
-(def-test "buf-struct? should return fals if not a buffer struct" '())
+(def-test "buf should create a new general purpose buffer"
+  (define buffer (buf (current-input-port) (lambda () '())))
+  (and (buf-struct? buffer)
+       (buf? buffer)))
 
 ; buf?
 ;------------------------------------------------------------------------------
-(def-test "buf? should return true if passed a buffer struct with the right elements" '())
-(def-test "buf? should return false if no load function" '())
-(def-test "buf? should return false if pos is not an integer" '())
-(def-test "buf? should return false if marks is not a list" '())
-(def-test "buf? should return false if data is not a vector" '())
+(def-test "buf? should return true if passed a buffer struct with the right elements"
+  (define buffer
+    (make-buf
+      (current-input-port)
+      (lambda () '())
+      0
+      '()
+      (vector)))
+  (buf? buffer))
+
+(def-test "buf? should return false if no load function"
+  (define buffer
+    (make-buf
+      (current-input-port)
+      #f ; <-- A procedure is required here
+      0
+      '()
+      (vector)))
+  (not (buf? buffer)))
+
+(def-test "buf? should return false if pos is not an integer"
+  (define buffer
+    (make-buf
+      (current-input-port)
+      (lambda () '())
+      "" ; <-- An integer required here
+      '()
+      (vector)))
+  (not (buf? buffer)))
+
+(def-test "buf? should return false if marks is not a list"
+  (define buffer
+    (make-buf
+      (current-input-port)
+      (lambda () '())
+      0
+      "" ; <-- a list required here
+      (vector)))
+  (not (buf? buffer)))
+
+(def-test "buf? should return false if data is not a vector"
+  (define buffer
+    (make-buf
+      (current-input-port)
+      (lambda () '())
+      0
+      '()
+      "" ; <-- A vector required here
+      ))
+  (not (buf? buffer)))
 
 ; buf-marked?
 ;------------------------------------------------------------------------------
-(def-test "buf-marked? should return true if the buffer is marked" '())
-(def-test "buf-marked? should return false if the buffer is NOT marked" '())
+(def-test "buf-marked? should return true if the buffer is marked"
+  (define buffer
+    (make-buf
+      (current-input-port)
+      (lambda () '())
+      0
+      '(0) ; Marks list is not empty
+      (vector)))
+  (buf-marked? buffer))
+
+
+(def-test "buf-marked? should return false if the buffer is NOT marked"
+  (define buffer
+    (make-buf
+      (current-input-port)
+      (lambda () '())
+      0
+      '() ; Marks lists is empty
+      (vector)))
+  (not (buf-marked? buffer)))
 
 ; buf-mark!
 ;------------------------------------------------------------------------------
-(def-test "buf-mark! should store the current pos in the marks list when the marks list is empty" '())
-(def-test "buf-mark! should prepend the current pos to the marks list" '())
+(def-test "buf-mark! should store the current pos in the marks list when the marks list is empty"
+  (define buffer (buf (current-input-port) (lambda () '())))
+  (buf-mark! buffer)
+  (equal? '(0) (buf-marks buffer)))
+
+(def-test "buf-mark! should prepend the current pos to the marks list"
+  (define buffer (buf (current-input-port) (lambda () '())))
+  (buf-marks-set! buffer '(1))
+  (buf-mark! buffer)
+  (equal? '(0 1) (buf-marks buffer)))
 
 ; buf-release!
 ;------------------------------------------------------------------------------
-(def-test "buf-release! should remove the current mark from the marks list when only one mark exists" '())
-(def-test "buf-release! should remove the current mark from the marks list when multiple marks exist" '())
+(def-test "buf-release! should remove the current mark from the marks list when only one mark exists"
+  (define buffer (buf (current-input-port) (lambda () '())))
+  (buf-marks-set! buffer '(0 1))
+  (buf-release! buffer)
+  (equal? '(1) (buf-marks buffer)))
+
+(def-test "buf-release! should remove the current mark from the marks list when multiple marks exist"
+  (define buffer (buf (current-input-port) (lambda () '())))
+  (buf-marks-set! buffer '(0 1 2))
+  (buf-release! buffer)
+  (equal? '(1 2) (buf-marks buffer)))
 
 ; buf-advance!
 ;------------------------------------------------------------------------------
-(def-test "buf-advance! should advance pos by 1" '())
+(def-test "buf-advance! should advance pos by 1"
+  (define buffer (buf (current-input-port) (lambda () '())))
+  (buf-advance! buffer)
+  (= (buf-pos buffer) 1))
+
+; buf-fill!
+;------------------------------------------------------------------------------
+(def-test "buf-fill! should load 0 items from the source" '())
+(def-test "buf-fill! should load 1 items from the source" '())
+(def-test "buf-fill! should load 2 items from the source" '())
+(def-test "buf-fill! should load 3 items from the source" '())
 
 ; buf-sync!
 ;------------------------------------------------------------------------------
-(def-test "buf-sync! should fill the buffer with the specified number of bytes when the buffer is empty" '())
+(def-test "buf-sync! should fill the buffer with the specified number of items when the buffer is empty" '())
 (def-test "buf-sync! should fill the buffer up to n when the buffer contains less data than required" '())
 (def-test "buf-sync! should do nothing if already synced" '())
 
-; buf-fill!
-;------------------------------------------------------------------------------
-(def-test "buf-fill! should load 0 bytes from the source" '())
-(def-test "buf-fill! should load 1 bytes from the source" '())
-(def-test "buf-fill! should load 2 bytes from the source" '())
-(def-test "buf-fill! should load 3 bytes from the source" '())
-
 ; buf-lookahead!
 ;------------------------------------------------------------------------------
 (def-test "buf-lookahead! should return the first item of lookahead" '())