0

こんにちは、mo プログラムの正確な問題を理解しようとしています。ラケットのキューのエンキュー、デキュー、トップ、およびサイズのコードを作成しました。また、モジュール内で自分のプログラムを試すためのテスト ケースも作成しました。次のコマンド「racket run-tests.rkt | less」を入力してテスト ケースを実行しようとすると、次のエラーが表示されmodule' declaration forます。 : #

=== コンテキスト === デフォルトロード/使用コンパイル "

これが私の3つのファイルとその中のコードです

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)

バグの存在を見つけるためのあらゆる種類の助けをいただければ幸いです。前もって感謝します。

4

1 に答える 1

4

あなたが言及しているエラーは、「queue.rkt」ファイルで言語レベルが正しく設定されていないことが原因であると考えられます。特に、これら 3 つすべての言語を次のように設定する必要があります。

「ソースで宣言された言語を使用する」。

正直なところ、なぜこれを 3 つの個別のモジュールとして構築しているのか、よくわかりません。それはあなたにさらなる困難を引き起こしていると思いますが、確かにこの方法でそれを行うことができるはずです.

編集:親の問題があるようです。特に、「サイズ」の定義の後に 3 つの閉じ括弧があります。DrRacket は実行時にこの問題を強調表示しませんか?

編集 2: うーん、うんざり: 「インクルード」を使用しています。インクルードを使用しないでください。

編集 3: わかりました、ここに: コードを 1 つのファイルに再フォーマットしました:

(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 つで非常に単純なエラーが発生します。

より一般的に言えば、実行せずにこれほど多くのテストコードを書くことに驚いています:)。より根本的な問題を発見する前にすべての作業を行うよりも、個人的に小さなテスト ケースを 1 つ作成してから試してみたでしょう。私はあなたの粘り強さに感心しますが、もう少しパラノイアが必要だと思います :)

さらに問題が発生した場合はお知らせください。

編集: リクエストに応じて、2 つのファイルに分割します。

ファイル 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.")
                 )
               )))
于 2012-10-02T21:11:24.117 に答える