exercise-21.scm (2327B)
1 (define-library (sicp tests chapter-3 exercise-21) 2 (import (scheme base)) 3 (import (scheme write)) 4 (import (srfi :64)) 5 (import (sicp solutions chapter-3 exercise-21)) 6 7 (begin 8 (test-begin "chapter-3-exercise-21") 9 10 (define (test-equal-print-queue string-ratsui queue-matsui) 11 (define string-port (open-output-string)) 12 13 (print-queue queue-matsui string-port) 14 15 (define string-matsui 16 (get-output-string string-port)) 17 18 (test-equal string-ratsui string-matsui)) 19 20 (define q1 (make-queue)) 21 (test-equal '(()) q1) 22 (test-equal-print-queue "()" q1) 23 24 (insert-queue! q1 'a) 25 (test-equal '((a) a) q1) 26 (test-equal-print-queue "(a)" q1) 27 28 (insert-queue! q1 'b) 29 (test-equal '((a b) b) q1) 30 (test-equal-print-queue "(a b)" q1) 31 32 (insert-queue! q1 'c) 33 (test-equal '((a b c) c) q1) 34 (test-equal-print-queue "(a b c)" q1) 35 36 (insert-queue! q1 'd) 37 (test-equal '((a b c d) d) q1) 38 (test-equal-print-queue "(a b c d)" q1) 39 40 (delete-queue! q1) 41 (test-equal '((b c d) d) q1) 42 (test-equal-print-queue "(b c d)" q1) 43 44 (delete-queue! q1) 45 (test-equal '((c d) d) q1) 46 (test-equal-print-queue "(c d)" q1) 47 48 (delete-queue! q1) 49 (test-equal '((d) d) q1) 50 (test-equal-print-queue "(d)" q1) 51 52 (delete-queue! q1) 53 (test-equal '(() d) q1) 54 (test-equal-print-queue "()" q1) 55 56 ;; When one display the queue, without a special procedure, one 57 ;; sees the a tree where the first element is the list of items 58 ;; sorted from first item insert to last item insert to the queue, 59 ;; and the second item is the last item inserted to the queue, 60 ;; whether if the queue is empty or not. 61 ;; ((first-item-inserted second-item-inserted ...) last-item-inserted) 62 ;; or 63 ;; (() last-item-inserted) 64 65 ;; One can see the last item inserted to the queue right after the 66 ;; only item in it is deleted because the rear pointer of the 67 ;; queue does not change which object it points at when 68 ;; delete-queue! is used when there is only one item in the 69 ;; queue. It does not need to change because when the queue is 70 ;; empty and then a new item is inserted, a new (cons item '()) is 71 ;; created, and both the front and rear pointers are pointed at 72 ;; it. 73 74 (test-end "chapter-3-exercise-21")))