]> git.mdlowis.com Git - archive/dlang-scm.git/commitdiff
Added unit testing capability and first unit test file
authorMichael D. Lowis <mike@mdlowis.com>
Thu, 5 Jul 2012 03:25:59 +0000 (23:25 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Thu, 5 Jul 2012 03:25:59 +0000 (23:25 -0400)
.gitignore
Makefile
inc/loop.scm [new file with mode: 0644]
inc/test.scm [new file with mode: 0644]
source/buf.scm
tests/main.scm
tests/test.scm [new file with mode: 0644]
tests/test_buf.scm [new file with mode: 0644]
tests/test_foo.scm [deleted file]

index 2065fc6c53ad810d454645db53679aa2a7273f65..12252007d872fa4516c9eff236302ebe7ff65034 100644 (file)
@@ -7,4 +7,4 @@ cscope.out
 *.lib
 *~
 *.d
-
+test_runner*
index c5ad55ad41940236e2a864a772119f7d06197dd4..bbc8363bd87d2ab34fa6be8a47738900ba6a1777 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -35,6 +35,10 @@ TEST_FILES = $(call flist, $(TEST_ROOT), $(TEST_EXT))
 SRC_OBJS = $(SRC_FILES:%.$(SRC_EXT)=%.o)
 TEST_OBJS = $(TEST_FILES:%.$(TEST_EXT)=%.o)
 
+# Include Directories
+SRC_INCS  =
+TEST_INCS = -I inc
+
 # Compiler and Linker Options
 #----------------------------
 CSC = csc
@@ -70,7 +74,7 @@ $(SRC_OBJS): %.o : %.$(SRC_EXT)
 
 $(TEST_OBJS): %.o : %.$(TEST_EXT)
        @echo $<
-       @$(CSC) $(CSCFLAGS) -o $@ $<
+       @$(CSC) $(CSCFLAGS) $(TEST_INCS) -o $@ $<
 
 # Cleanup
 clean:
diff --git a/inc/loop.scm b/inc/loop.scm
new file mode 100644 (file)
index 0000000..717c48a
--- /dev/null
@@ -0,0 +1,27 @@
+(declare (unit loops))
+
+; For loop macro
+(define-syntax for
+  (syntax-rules ()
+    ((_ var lst body ...)
+      (let loop ((var (car lst)))
+        body ...
+        (if (< var (cadr lst))
+          (loop (+ var (caddr lst))))))))
+
+; While loop macro
+(define-syntax while
+  (syntax-rules ()
+    ((_ cnd body ...)
+      (let loop ()
+        body ...
+        (if cnd (loop))))))
+
+; Until loop macro
+(define-syntax until
+  (syntax-rules ()
+    ((_ cnd body ...)
+      (let loop ()
+        body ...
+        (if (not cnd) (loop))))))
+
diff --git a/inc/test.scm b/inc/test.scm
new file mode 100644 (file)
index 0000000..48bb5be
--- /dev/null
@@ -0,0 +1,8 @@
+
+(define-syntax def-test
+  (syntax-rules ()
+    ((_ desc body ...)
+      (register-test!
+        (cons desc
+          (lambda () body ...))))))
+
index cb216341fae296b797b39ce586c0565948bc2f3b..69394d20b20b2aa66dc858dac960cb87e7b16c4f 100644 (file)
        (vector?   (buf-data obj))))
 
 (define (buf-marked? b)
-  (if (buf? b)
-    (> (length (buf-marks b)) 0)))
+  (> (length (buf-marks b)) 0))
 
-(define (buf-mark b)
-  (if (buf? b)
-    (buf-marks-set! b (cons (buf-pos b) (buf-marks b)))))
+(define (buf-mark! b)
+  (buf-marks-set! b (cons (buf-pos b) (buf-marks b))))
 
-(define (buf-release b)
-  (if (buf? b)
-    (buf-marks-set! b (cdr (buf-marks b)))))
+(define (buf-release! b)
+  (buf-marks-set! b (cdr (buf-marks b))))
 
-(define (buf-advance b)
-  (if (buf? b)
-    (buf-pos-set! b (+ 1 (buf-pos b)))))
+(define (buf-advance! b)
+  (buf-pos-set! b (+ 1 (buf-pos b))))
 
-(define (buf-sync b n)
-  (if (buf? b)
-    (let* ((pos     (buf-pos b))
-           (size    (length (buf-data)))
-           (nxt_idx (- (+ pos n) 1))
-           (max_idx (- size 1)))
-      (if (= size 0)
-        (buf-fill b n)
-        (if (>= nxt_idx max_idx)
-          (buf-fill b (- nxt_idx max_idx)))))))
+(define (buf-sync! b n)
+  (let* ((pos     (buf-pos b))
+         (size    (length (buf-data)))
+         (nxt_idx (- (+ pos n) 1))
+         (max_idx (- size 1)))
+    (if (= size 0)
+      (buf-fill! b n)
+      (if (>= nxt_idx max_idx)
+        (buf-fill! b (- nxt_idx max_idx))))))
 
-(define (buf-fill b n)
-  (if (buf? b)
-    (let loop ((i 0))
-      (buf-data-set!
-        (vector-append (buf-data b) ((buf-ldfn b))))
-      (if (< i n) (loop (+ i 1))))))
+(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)
-  (if (buf? b)
-    (buf-sync b n)
-    (vector-ref
-      (buf-data b)
-      (- (+ (buf-pos b) n) 1))))
+(define (buf-lookahead! b n)
+  (buf-sync b n)
+  (vector-ref
+    (buf-data b)
+    (- (+ (buf-pos b) n) 1)))
 
-(define (buf-consume b n)
-  (if (buf? b)
+(define (buf-consume! b n)
+  (buf-advance! b)
+  (if
+    (and
+      (= (buf-pos b) (length (buf-data b)))
+      (not (buf-marked? b)))
     (begin
-      (buf-advance b)
-      (if
-        (and
-          (= location size)
-          (not (buf-marked? b)))
-        (begin
-          (buf-pos-set! 0)
-          (buf-data-set! (vector))))
-      (buf-sync b1))))
+      (buf-pos-set! 0)
+      (buf-data-set! (vector))))
+  (buf-sync! b1))
 
index 47a49106cf4d4b5d491fb8061209106ce1bad8fe..a3099970ca90a72ee9fe34ad7cb8c4c688b9a299 100644 (file)
@@ -1,6 +1,7 @@
 (declare
   (uses library)
-  (uses test_foo))
+  (uses test_buf)
+  (uses test))
 
-;(run-unit-tests)
+(run-all-unit-tests)
 
diff --git a/tests/test.scm b/tests/test.scm
new file mode 100644 (file)
index 0000000..ef7103d
--- /dev/null
@@ -0,0 +1,28 @@
+(declare (unit test)
+         (compile-syntax))
+
+(define unit-tests '())
+
+(define (register-test! test)
+  (set! unit-tests (append unit-tests (list test))))
+
+(define (print-summary pass fail)
+  (if (zero? fail)
+    (print "Success: " pass " tests passed.")
+    (print "Failure: " fail " / " (+ pass fail) " tests failed.")))
+
+(define (run-all-unit-tests)
+  (let loop ((pass  0)
+             (fail  0)
+             (tests unit-tests))
+    (if (not (null? tests))
+      (let* ((test (car tests))
+             (desc (car test))
+             (fn   (cdr test)))
+        (if (not (fn))
+          (begin
+            (print "FAIL: " desc)
+            (loop pass (+ fail 1) (cdr tests)))
+          (loop (+ pass 1) fail (cdr tests))))
+      (print-summary pass fail))))
+
diff --git a/tests/test_buf.scm b/tests/test_buf.scm
new file mode 100644 (file)
index 0000000..6dcd5f4
--- /dev/null
@@ -0,0 +1,65 @@
+(include "test.scm")
+(declare (unit test_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" '())
+
+; 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" '())
+
+; 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" '())
+
+; 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" '())
+
+; 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" '())
+
+; buf-advance!
+;------------------------------------------------------------------------------
+(def-test "buf-advance! should advance pos by 1" '())
+
+; 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 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" '())
+(def-test "buf-lookahead! should return the second item of lookahead" '())
+(def-test "buf-lookahead! should return the third item of lookahead" '())
+
+; buf-consume!
+;------------------------------------------------------------------------------
+(def-test "buf-consume! should clear the buffer if not marked and pos is equal to the buffer size" '())
+(def-test "buf-consume! should NOT clear the buffer if pos not equal to the buffer size" '())
+(def-test "buf-consume! should NOT clear the buffer if the buffer is marked" '())
+
diff --git a/tests/test_foo.scm b/tests/test_foo.scm
deleted file mode 100644 (file)
index 3f0f32e..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-(declare (unit test_foo))
-
-;(test-suite "Foo unit tests"
-;  (test "Foo does cool stuff"
-;    (check-equal 42 (foo 1 2)) ))
-
-(print "Hello, world!")
-