-1

I've just started learning about trees and heaps and I'm unsure about how to go about writing the test case. These codes are from my lesson slides. Although they give codes, they sadly don't provide test cases of said codes, so I am confused on how I would call it.

I've tried test cases such as any regular integers like 5, and I've also tried going about it with lists, but I run into errors and it just doesn't seem right as I know from diagrams that heaps are like trees with its roots being the smallest value and with its subheaps.

(define (value H)
  (car H))

(define (weight H)
  (cdr H))

(define (create-heap vw-pair left-child right-child)
  (list vw-pair left-child right-child))

(define (h-min heap)
  (car heap))

(define (left heap)
  (cadr heap))

(define (right heap)
  (caddr heap))

(define (insert vw-pair heap)
  (cond ((null? heap) (create-heap vw-pair '() '()))
        ((< (weight vw-pair) (weight (h-min heap)))
         (create-heap vw-pair (right heap) (insert (h-min heap) (left heap))))
        (else
         (create-heap (h-min heap) (right heap) (insert vw-pair (left heap))))))


(define (insert-list-of-pairs vw-pair-list heap)
  (if (null? vw-pair-list)
      heap
      (insert-list-of-pairs (cdr vw-pair-list) (insert (car vw-pair-list) heap))))


(define (remove-min heap)
  (define (combine-heaps h1 h2)
    (cond ((null? h1) h2)
          ((null? h2) h1)
          ((< (cdr (h-min h1)) (cdr (h-min h2)))
           (create-heap (h-min h1) h2 (combine-heaps (left h1) (right h1))))
          (else
           (create-heap (h-min h2)
                        h1
                        (combine-heaps (left h2) (right h2))))))
  (combine-heaps (left heap) (right heap)))
seaweed
  • 21
  • 2

1 Answers1

1

Your test cases should explain exactly what you want to do.

They are the way to explain, using code, the intended use for the functions you write.

For your specific case, I obviously can't help you because that's exactly what's missing from your code: the meaning it should have.

But I can still explain how to write unit tests in Racket:

;; This is a function you would write.
;; It does something, but it's not completely obvious
;; how to use it.
(define (find type basket)
  (let ([obj (assq type basket)])
    (and obj
         (cadr obj))))

;; By creating a test module, you add code that describes
;; how to use the functions in this file.
(module+ test
  (require rackunit)

  ;; This is some sample data.
  ;; It's useful to understand what kind of data
  ;; your functions are expected to process.
  (define basket '((bread baguette)
                   (fruit ananas)
                   (fruit banana)
                   (vegetable carrot)
                   (misc fork&knife)))

  ;; Here we call the function and show the expected result.
  ;; It's now clear how to use it.
  (check-equal? (find 'fruit basket) 'ananas)
  (check-equal? (find 'vegetable basket) 'carrot)
  (check-false (find 'fruit '()))
)

You can run those tests by using raco:

> raco test myfile.rkt
raco test: (submod "myfile.rkt" test)
3 tests passed
Zoé Martin
  • 1,887
  • 1
  • 16
  • 28