处理队列的队列和测试用例

时间:2012-10-02 20:48:11

标签: racket

您好我试图了解mo程序到底出了什么问题。我为球拍中的队列编写了入队,出队,顶部和大小的代码。我还编写了测试用例来试用模块中的程序。当我尝试通过键入以下命令“racket run-tests.rkt | less”来运行测试用例时,我收到以下错误: “ default-load-handler:只需要一个module' declaration for队列',但在:

中找到了一个额外的表达式

=== context === 默认加载/使用编译 “

以下是我的三个文件及其中的代码

1- queue.rkt

(module queue racket
  (provide make-queue enqueue dequeue top size queue-tests)
  (struct queue (front back size) #:mutable #:transparent)

    (define (make-queue) (queue null null 0))

(define (enqueue q elt)
    (set-queue-back! q (cons elt (queue-back q)))
    (set-queue-size! q (+ (queue-size q) 1))
    )
(define (dequeue q)
    (cond  ((eq? (queue-size q) 0) null)
           ((null? (queue-front q))
            (begin
              (set-queue-front! q (cdr (reverse (queue-back q))))
              (set-queue-back! q null)
              (set-queue-size! q (- (queue-size q) 1))))
           (else
            (begin
              (set-queue-front! q (cdr (queue-front q)))
              (set-queue-size! q (- (queue-size q) 1))))
           )
  )

  (define (top q)
    (cond ((eq? (queue-size q) 0) null)
          ((null? (queue-front q)) (last (queue-back q)))
          (else (car (queue-front q)))))

   (define (size q)
    (queue-size q))

  ; Test cases Here
  (include "test-queue.rkt")
)

2- test-queue.rkt

 #lang racket/base

 (require rackunit)

 (define queue-tests
  (test-suite
    "Tests for the Queue"

    (test-case "Initial Queue Properties"
    (let  ((q (make-queue)))
      (check-equal? (size q) 0 "Queue initially should have size zero.")
      (check-equal? (dequeue q) null "Dequeue on an empty queue returns null")
      (check-equal? (size q) 0 "Dequeue on empty queue leaves size as zero")
      ))

    (test-case "One Element queue ---- Functional test"
       (let ((q (make-queue)))
         (check-equal? (size q) 0 "Stack initially should be of size zero")
         (enqueue q 1)
         (check-equal? (size q) 1 "Added one element to back of queue")
         (check-equal? (top q) 1 "Take a sneak peak at our first element")
         (dequeue q)
         (check-equal? (size 1) 0 "size should now be zero")
         (check-equal? (dequeue q) null "dequeue on empty queue returns null")
         ))

    (test-case "One Element queue --- Internal Test"
       (let  ((q (make-queue)))
         (check-equal? (size q) 0 "queue initially should have size zero.")
         (enqueue q 3)
         (check-equal? (size q) 1 "Added one element.")
         (check-equal? (queue-back q) '(3) "Is the element in the back list?")
         (check-equal? (top s) 3 "Look at our one element.")
         (check-equal? (queue-front q) '(3) "Is the element in the front list?")
         (dequeue s)
         (check-equal? (queue-front q) '() "Is the data empty now?")
         ))

    (test-case "Multi Element queue --- Functional Test"
    (let  ((q (make-queue))
           (elts (list 8 6 7 5 3 0 9))
           )
      (check-equal? (size q) 0 "queue initially should have size zero.")
      (for ((i elts)
            (num (in-range 1 8)))
           (enqueue q i)
           (check-equal? (size q) num "Testing size.")
           (check-equal? (top q) i "Testing top with multiple additions.")
           )
      (for ((i (reverse elts))
            (num (reverse (stream->list (in-range 1 8)))))
           (check-equal? (top q) i "Testing top with multiple deletions")
           (dequeue q)
           (check-equal? (size q) (- num 1) "Testing size.")
           )
      ))

    (test-case "Multi Element queue --- Ebb and Flow Test"
       (let  ((q (make-queue))
              (elts (list 8 6 7 5 3 0 9))
              )
         (check-equal? (size q) 0 "queue initially should have size zero.")
         (for ((i elts)
           (num (in-range 1 8)))
              (enqueue q i)
              (check-equal? (size q) num "Testing size, first flow.")
              (check-equal? (top q) i "Testing top with multiple additions.")
           )
         (for ((i (reverse elts))
               (num (reverse (stream->list (in-range 1 8)))))
              (check-equal? (top q) i "Testing top with multiple deletions")
              (dequeue q)
              (check-equal? (size q) (- num 1) "Testing size, first ebb.")
           )
         (for ((i elts)
               (num (in-range 1 8)))
               (enqueue q i)
               (check-equal? (size q) num "Testing size, second flow.")
               (check-equal? (top q) i "Testing top with multiple additions.")
           )
         (for ((i (reverse elts))
               (num (reverse (stream->list (in-range 1 8)))))
               (check-equal? (top q) i "Testing top with multiple deletions")
               (dequeue q)
               (check-equal? (size q) (- num 1) "Testing size, second ebb.")
           )
      ))

   (test-case "Multi Element queue --- Internal Test"
    (let  ((q (make-queue))
           (elts (list 5 8 8 2 3 0 0))
           (tmp null)
           )
      (for ((i elts)
            (num (in-range 1 8)))
           (enqueue q i)
           (check-equal? (size q) num "Testing size, second flow.")
           (set! tmp (cons i tmp)) ; "push" element to a list
           (check-equal? (queue-front q) tmp "Queue data is in correct form.")
           )
      ))

    ))

3- run-tests.rkt

#lang racket/base

(require "queue.rkt")

(require rackunit/text-ui)

(run-tests queue-tests)

任何有助于发现错误的帮助都将受到赞赏。提前谢谢。

1 个答案:

答案 0 :(得分:4)

我认为,您所指的错误可能是由于您的“queue.rkt”文件中的语言级别设置错误而导致的。特别是,所有这三个语言都应该设置为

“使用在源中声明的语言”。

老实说,我真的不确定你为什么把它建成三个单独的模块;我认为这会给你带来额外的困难,但应该可以这样做。

编辑:看起来你有问题。特别是,在定义“大小”后,您有三个近似值。 DrRacket在您运行时不会突出显示此问题吗?

编辑2:哦,你好:你正在使用“包含”。不要使用include。

编辑3:好的,这里:我将您的代码重新格式化为一个文件:

(module queue racket

  (require rackunit)

  (provide make-queue enqueue dequeue top size)
  (struct queue (front back size) #:mutable #:transparent)

  (define (make-queue) (queue null null 0))

  (define (enqueue q elt)
    (set-queue-back! q (cons elt (queue-back q)))
    (set-queue-size! q (+ (queue-size q) 1))
    )
  (define (dequeue q)
    (cond  ((eq? (queue-size q) 0) null)
           ((null? (queue-front q))
            (begin
              (set-queue-front! q (cdr (reverse (queue-back q))))
              (set-queue-back! q null)
              (set-queue-size! q (- (queue-size q) 1))))
           (else
            (begin
              (set-queue-front! q (cdr (queue-front q)))
              (set-queue-size! q (- (queue-size q) 1))))
           )
    )

  (define (top q)
    (cond ((eq? (queue-size q) 0) null)
          ((null? (queue-front q)) (last (queue-back q)))
          (else (car (queue-front q)))))

  (define (size q)
    (queue-size q))

  (test-case "Initial Queue Properties"
             (let  ((q (make-queue)))
               (check-equal? (size q) 0 "Queue initially should have size zero.")
               (check-equal? (dequeue q) null "Dequeue on an empty queue returns null")
               (check-equal? (size q) 0 "Dequeue on empty queue leaves size as zero")
               ))

  (test-case "One Element queue ---- Functional test"
             (let ((q (make-queue)))
               (check-equal? (size q) 0 "Stack initially should be of size zero")
               (enqueue q 1)
               (check-equal? (size q) 1 "Added one element to back of queue")
               (check-equal? (top q) 1 "Take a sneak peak at our first element")
               (dequeue q)
               (check-equal? (size 1) 0 "size should now be zero")
               (check-equal? (dequeue q) null "dequeue on empty queue returns null")
               ))

  #;(test-case "One Element queue --- Internal Test"
             (let  ((q (make-queue)))
               (check-equal? (size q) 0 "queue initially should have size zero.")
               (enqueue q 3)
               (check-equal? (size q) 1 "Added one element.")
               (check-equal? (queue-back q) '(3) "Is the element in the back list?")
               (check-equal? (top s) 3 "Look at our one element.")
               (check-equal? (queue-front q) '(3) "Is the element in the front list?")
               (dequeue s)
               (check-equal? (queue-front q) '() "Is the data empty now?")
               ))

  (test-case "Multi Element queue --- Functional Test"
             (let  ((q (make-queue))
                    (elts (list 8 6 7 5 3 0 9))
                    )
               (check-equal? (size q) 0 "queue initially should have size zero.")
               (for ((i elts)
                     (num (in-range 1 8)))
                 (enqueue q i)
                 (check-equal? (size q) num "Testing size.")
                 (check-equal? (top q) i "Testing top with multiple additions.")
                 )
               (for ((i (reverse elts))
                     (num (reverse (stream->list (in-range 1 8)))))
                 (check-equal? (top q) i "Testing top with multiple deletions")
                 (dequeue q)
                 (check-equal? (size q) (- num 1) "Testing size.")
                 )
               ))

  (test-case "Multi Element queue --- Ebb and Flow Test"
             (let  ((q (make-queue))
                    (elts (list 8 6 7 5 3 0 9))
                    )
               (check-equal? (size q) 0 "queue initially should have size zero.")
               (for ((i elts)
                     (num (in-range 1 8)))
                 (enqueue q i)
                 (check-equal? (size q) num "Testing size, first flow.")
                 (check-equal? (top q) i "Testing top with multiple additions.")
                 )
               (for ((i (reverse elts))
                     (num (reverse (stream->list (in-range 1 8)))))
                 (check-equal? (top q) i "Testing top with multiple deletions")
                 (dequeue q)
                 (check-equal? (size q) (- num 1) "Testing size, first ebb.")
                 )
               (for ((i elts)
                     (num (in-range 1 8)))
                 (enqueue q i)
                 (check-equal? (size q) num "Testing size, second flow.")
                 (check-equal? (top q) i "Testing top with multiple additions.")
                 )
               (for ((i (reverse elts))
                     (num (reverse (stream->list (in-range 1 8)))))
                 (check-equal? (top q) i "Testing top with multiple deletions")
                 (dequeue q)
                 (check-equal? (size q) (- num 1) "Testing size, second ebb.")
                 )
               ))

  (test-case "Multi Element queue --- Internal Test"
             (let  ((q (make-queue))
                    (elts (list 5 8 8 2 3 0 0))
                    (tmp null)
                    )
               (for ((i elts)
                     (num (in-range 1 8)))
                 (enqueue q i)
                 (check-equal? (size q) num "Testing size, second flow.")
                 (set! tmp (cons i tmp)) ; "push" element to a list
                 (check-equal? (queue-front q) tmp "Queue data is in correct form.")
                 )
               ))



  )

当我运行此代码时,我的一个测试用例中会出现一个相当简单的错误。

更一般地说,我很吃惊,你会写这么多测试代码而不运行它:)。我本人会写一个小测试用例,然后尝试一下,而不是在发现更基本的问题之前完成所有这些工作。我很佩服你的坚韧,但我认为你需要更多的偏执狂:)

如果您遇到更多麻烦,请告诉我。

编辑:根据您的要求分成两个文件。

文件1:queue.rkt

(module queue racket

  (provide make-queue enqueue dequeue top size)
  (struct queue (front back size) #:mutable #:transparent)

  (define (make-queue) (queue null null 0))

  (define (enqueue q elt)
    (set-queue-back! q (cons elt (queue-back q)))
    (set-queue-size! q (+ (queue-size q) 1)))

  (define (dequeue q)
    (cond  ((eq? (queue-size q) 0) null)
           ((null? (queue-front q))
            (begin
              (set-queue-front! q (cdr (reverse (queue-back q))))
              (set-queue-back! q null)
              (set-queue-size! q (- (queue-size q) 1))))
           (else
            (begin
              (set-queue-front! q (cdr (queue-front q)))
              (set-queue-size! q (- (queue-size q) 1))))))

  (define (top q)
    (cond ((eq? (queue-size q) 0) null)
          ((null? (queue-front q)) (last (queue-back q)))
          (else (car (queue-front q)))))

  (define (size q)
    (queue-size q))

  )

文件2:queue-tests.rkt,在同一个目录中:

(module test-queue racket

  (require "queue.rkt"
           rackunit)


  (test-case "Initial Queue Properties"
             (let  ((q (make-queue)))
               (check-equal? (size q) 0 "Queue initially should have size zero.")
               (check-equal? (dequeue q) null "Dequeue on an empty queue returns null")
               (check-equal? (size q) 0 "Dequeue on empty queue leaves size as zero")
               ))

  (test-case "One Element queue ---- Functional test"
             (let ((q (make-queue)))
               (check-equal? (size q) 0 "Stack initially should be of size zero")
               (enqueue q 1)
               (check-equal? (size q) 1 "Added one element to back of queue")
               (check-equal? (top q) 1 "Take a sneak peak at our first element")
               (dequeue q)
               (check-equal? (size 1) 0 "size should now be zero")
               (check-equal? (dequeue q) null "dequeue on empty queue returns null")
               ))

  (test-case "One Element queue --- Internal Test"
             (let  ((q (make-queue)))
               (check-equal? (size q) 0 "queue initially should have size zero.")
               (enqueue q 3)
               (check-equal? (size q) 1 "Added one element.")
               (check-equal? (queue-back q) '(3) "Is the element in the back list?")
               (check-equal? (top s) 3 "Look at our one element.")
               (check-equal? (queue-front q) '(3) "Is the element in the front list?")
               (dequeue s)
               (check-equal? (queue-front q) '() "Is the data empty now?")
               ))

  (test-case "Multi Element queue --- Functional Test"
             (let  ((q (make-queue))
                    (elts (list 8 6 7 5 3 0 9))
                    )
               (check-equal? (size q) 0 "queue initially should have size zero.")
               (for ((i elts)
                     (num (in-range 1 8)))
                 (enqueue q i)
                 (check-equal? (size q) num "Testing size.")
                 (check-equal? (top q) i "Testing top with multiple additions.")
                 )
               (for ((i (reverse elts))
                     (num (reverse (stream->list (in-range 1 8)))))
                 (check-equal? (top q) i "Testing top with multiple deletions")
                 (dequeue q)
                 (check-equal? (size q) (- num 1) "Testing size.")
                 )
               ))

  (test-case "Multi Element queue --- Ebb and Flow Test"
             (let  ((q (make-queue))
                    (elts (list 8 6 7 5 3 0 9))
                    )
               (check-equal? (size q) 0 "queue initially should have size zero.")
               (for ((i elts)
                     (num (in-range 1 8)))
                 (enqueue q i)
                 (check-equal? (size q) num "Testing size, first flow.")
                 (check-equal? (top q) i "Testing top with multiple additions.")
                 )
               (for ((i (reverse elts))
                     (num (reverse (stream->list (in-range 1 8)))))
                 (check-equal? (top q) i "Testing top with multiple deletions")
                 (dequeue q)
                 (check-equal? (size q) (- num 1) "Testing size, first ebb.")
                 )
               (for ((i elts)
                     (num (in-range 1 8)))
                 (enqueue q i)
                 (check-equal? (size q) num "Testing size, second flow.")
                 (check-equal? (top q) i "Testing top with multiple additions.")
                 )
               (for ((i (reverse elts))
                     (num (reverse (stream->list (in-range 1 8)))))
                 (check-equal? (top q) i "Testing top with multiple deletions")
                 (dequeue q)
                 (check-equal? (size q) (- num 1) "Testing size, second ebb.")
                 )
               ))

  (test-case "Multi Element queue --- Internal Test"
             (let  ((q (make-queue))
                    (elts (list 5 8 8 2 3 0 0))
                    (tmp null)
                    )
               (for ((i elts)
                     (num (in-range 1 8)))
                 (enqueue q i)
                 (check-equal? (size q) num "Testing size, second flow.")
                 (set! tmp (cons i tmp)) ; "push" element to a list
                 (check-equal? (queue-front q) tmp "Queue data is in correct form.")
                 )
               )))