From ae1acd2183ee8bf189b7eaeeeb6c6f9277e59052 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Thu, 5 Jul 2012 00:36:34 -0400 Subject: [PATCH] Implemented unit tests for buf.scm --- source/buf.scm | 12 ++-- tests/test_buf.scm | 134 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 114 insertions(+), 32 deletions(-) diff --git a/source/buf.scm b/source/buf.scm index 69394d2..bd003a4 100644 --- a/source/buf.scm +++ b/source/buf.scm @@ -34,6 +34,12 @@ (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))) @@ -44,12 +50,6 @@ (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 diff --git a/tests/test_buf.scm b/tests/test_buf.scm index 6dcd5f4..54be893 100644 --- a/tests/test_buf.scm +++ b/tests/test_buf.scm @@ -1,56 +1,138 @@ (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" '()) -- 2.49.0