| 1 |
;;; |
| 2 |
;;; Test example.mqueue-cpp |
| 3 |
;;; |
| 4 |
|
| 5 |
(use scheme.list) |
| 6 |
(use gauche.test) |
| 7 |
(use gauche.sequence) |
| 8 |
|
| 9 |
(test-start "example.mqueue-cpp") |
| 10 |
(use example.mqueue-cpp) |
| 11 |
(test-module 'example.mqueue-cpp) |
| 12 |
|
| 13 |
(define mq0 #f) |
| 14 |
(define mq1 #f) |
| 15 |
|
| 16 |
(test-section "basic APIs") |
| 17 |
|
| 18 |
(test* "constructor" #t |
| 19 |
(let ((q (make-mqueue "mqueue #0"))) |
| 20 |
(set! mq0 q) |
| 21 |
(is-a? q <mqueue>))) |
| 22 |
|
| 23 |
(test* "find" mq0 (mqueue-find "mqueue #0")) |
| 24 |
(test* "find" #f (mqueue-find "mqueue #1")) |
| 25 |
|
| 26 |
(test* "mqueue-name" "mqueue #0" (mqueue-name mq0)) |
| 27 |
|
| 28 |
(test* "mqueue-empty?" #t (mqueue-empty? mq0)) |
| 29 |
|
| 30 |
(test* "mqueue-push!" 1 (mqueue-push! mq0 "no news is good news")) |
| 31 |
(test* "mqueue-push!" 2 (mqueue-push! mq0 "something happening" 1)) |
| 32 |
(test* "mqueue-push!" 3 (mqueue-push! mq0 "it's nothing")) |
| 33 |
(test* "mqueue-push!" 4 (mqueue-push! mq0 "ALERT ALERT!" 4)) |
| 34 |
|
| 35 |
(test* "mqueue-empty?" #f (mqueue-empty? mq0)) |
| 36 |
|
| 37 |
(test* "mqueue-pop!" "ALERT ALERT!" (mqueue-pop! mq0)) |
| 38 |
(test* "mqueue-pop!" "something happening" (mqueue-pop! mq0)) |
| 39 |
(test* "mqueue-pop!" "no news is good news" (mqueue-pop! mq0)) |
| 40 |
(test* "mqueue-pop!" "it's nothing" (mqueue-pop! mq0)) |
| 41 |
|
| 42 |
(test* "mqueue-pop! (exception)" *test-error* (mqueue-pop! mq0)) |
| 43 |
|
| 44 |
(test* "mqueue-empty?" #t (mqueue-empty? mq0)) |
| 45 |
|
| 46 |
(test-section "multiple queue instance") |
| 47 |
|
| 48 |
(set! mq1 (make-mqueue "mqueue #1")) |
| 49 |
|
| 50 |
(test* "multiple instance" #t |
| 51 |
(and (eq? (mqueue-find "mqueue #0") mq0) |
| 52 |
(eq? (mqueue-find "mqueue #1") mq1) |
| 53 |
(not (eq? mq0 mq1)))) |
| 54 |
|
| 55 |
(test* "multiple instance" '("0" "1") |
| 56 |
(begin |
| 57 |
(mqueue-push! mq0 "0" 5) |
| 58 |
(mqueue-push! mq0 "a" 1) |
| 59 |
(mqueue-push! mq1 "1" -2) |
| 60 |
(mqueue-push! mq0 "b" -7) |
| 61 |
(list (mqueue-pop! mq0) (mqueue-pop! mq1)))) |
| 62 |
|
| 63 |
(test-section "stress test & GC") |
| 64 |
|
| 65 |
(define *size* 1000) |
| 66 |
(define *vec* (make-vector *size*)) |
| 67 |
(define *wvec* (make-weak-vector *size*)) |
| 68 |
|
| 69 |
(test* "stress test" #t |
| 70 |
(begin |
| 71 |
(dotimes (n *size*) |
| 72 |
(let1 q (make-mqueue (x->string n)) |
| 73 |
(set! (ref *vec* n) q) |
| 74 |
(set! (ref *wvec* n) q))) |
| 75 |
(dotimes (n *size*) |
| 76 |
(dotimes (k 10) |
| 77 |
(mqueue-push! (ref *vec* n) (x->string k))) |
| 78 |
(dotimes (k 10) |
| 79 |
(mqueue-pop! (ref *vec* n))) |
| 80 |
(gc)) |
| 81 |
(dotimes (n *size*) (set! (ref *vec* n) #f)) |
| 82 |
;; smash the stack |
| 83 |
(let loop ((j 0)) (unless (= j *size*) (loop (+ j 1))) #t) |
| 84 |
(dotimes (k 10) (gc)) |
| 85 |
(every (cut not <>) (coerce-to <list> *wvec*)))) |
| 86 |
|
| 87 |
;; epilogue |
| 88 |
(test-end) |