/collects/tests/framework/mem.rkt

http://github.com/gmarceau/PLT · Racket · 172 lines · 148 code · 21 blank · 3 comment · 15 complexity · a891258a8481c686b5f8d9419b4aabde MD5 · raw file

  1. #lang racket/base
  2. (require "test-suite-utils.rkt")
  3. ; mem-boxes : (list-of (list string (list-of (weak-box TST))))
  4. (send-sexp-to-mred '(define mem-boxes null))
  5. (define mem-count 10)
  6. (define (test-allocate tag open close)
  7. (queue-sexp-to-mred
  8. `(let ([new-boxes
  9. (let loop ([n ,mem-count])
  10. (cond
  11. [(zero? n) null]
  12. [else
  13. (let* ([o (,open)]
  14. [b (make-weak-box o)])
  15. (,close o)
  16. ;; break at least that link.
  17. (set! o #f)
  18. ;; flush pending events
  19. (let ([s (make-semaphore 0)])
  20. (queue-callback (lambda () (semaphore-post s)) #f)
  21. (yield s))
  22. (cons b (loop (- n 1))))]))])
  23. (sleep/yield 1/10) (collect-garbage)
  24. (sleep/yield 1/10) (collect-garbage)
  25. (sleep/yield 1/10) (collect-garbage)
  26. (set! mem-boxes (cons (list ,tag new-boxes) mem-boxes)))))
  27. (define (done)
  28. (queue-sexp-to-mred
  29. `(begin
  30. (yield) (collect-garbage)
  31. (yield) (collect-garbage)
  32. (yield) (collect-garbage)
  33. (yield) (collect-garbage)
  34. (yield) (collect-garbage)
  35. (yield) (collect-garbage)
  36. (let* ([f (make-object dialog% "Results" #f 300 500)]
  37. [text (make-object text%)]
  38. [ec (make-object editor-canvas% f text)]
  39. [hp (instantiate horizontal-panel% ()
  40. (parent f)
  41. (stretchable-width #f)
  42. (stretchable-height #f))]
  43. [vp (instantiate vertical-panel% ()
  44. (parent hp)
  45. (stretchable-width #f)
  46. (stretchable-height #f))]
  47. [gc-canvas (make-object canvas% hp '(border))]
  48. [anything? #f])
  49. (define (update-gui)
  50. (send text erase)
  51. (let ([anything? #f])
  52. (send text begin-edit-sequence)
  53. (for-each
  54. (lambda (boxl)
  55. (let* ([tag (car boxl)]
  56. [boxes (cadr boxl)]
  57. [calc-results
  58. (lambda ()
  59. (let loop ([boxes boxes]
  60. [n 0])
  61. (cond
  62. [(null? boxes) n]
  63. [else (if (weak-box-value (car boxes))
  64. (loop (cdr boxes) (+ n 1))
  65. (loop (cdr boxes) n))])))])
  66. (let ([res (calc-results)])
  67. (when (> res 0)
  68. (set! anything? #t)
  69. (send text insert (format "~a: ~a of ~a\n" tag res ,mem-count))))))
  70. (reverse mem-boxes))
  71. (unless anything?
  72. (send text insert "Nothing!\n"))
  73. (send text end-edit-sequence)))
  74. (update-gui)
  75. (let ([onb (icon:get-gc-on-bitmap)]
  76. [offb (icon:get-gc-off-bitmap)])
  77. (when (and (send onb ok?)
  78. (send offb ok?))
  79. (send* gc-canvas
  80. (min-client-width (max (send gc-canvas min-width) (send onb get-width)))
  81. (min-client-height (max (send gc-canvas min-height) (send onb get-height)))
  82. (stretchable-width #f)
  83. (stretchable-height #f))
  84. (register-collecting-blit gc-canvas
  85. 0 0
  86. (send onb get-width)
  87. (send onb get-height)
  88. onb offb)))
  89. (make-object button% "Collect" vp
  90. (lambda (x y)
  91. (send text erase)
  92. (send text insert "Collecting Garbage\n")
  93. (collect-garbage)(collect-garbage)(collect-garbage)
  94. (collect-garbage)(collect-garbage)(collect-garbage)
  95. (collect-garbage)(collect-garbage)(collect-garbage)
  96. (update-gui)))
  97. (make-object button% "Close" vp (lambda (x y) (send f show #f)))
  98. (send f show #t)))))
  99. (define (test-frame-allocate %)
  100. (let ([name (format "~s" %)])
  101. (queue-sexp-to-mred '(preferences:set 'framework:exit-when-no-frames #f))
  102. (test-allocate name
  103. `(lambda ()
  104. (let ([f (make-object ,% ,name)])
  105. (send f show #t)
  106. (yield) (yield)
  107. f))
  108. `(lambda (f)
  109. (yield) (yield)
  110. (send f close)
  111. (when (send f is-shown?)
  112. (error 'test-frame-allocate "~a instance didn't close" ',%))
  113. (yield) (yield)))
  114. (queue-sexp-to-mred '(preferences:set 'framework:exit-when-no-frames #t))))
  115. (test-allocate "frame%"
  116. '(lambda ()
  117. (let ([f (make-object frame% "test frame")])
  118. (send f show #t)
  119. f))
  120. '(lambda (f) (send f show #f)))
  121. (define (test-editor-allocate object-name)
  122. (test-allocate (symbol->string object-name)
  123. `(lambda () (make-object ,object-name))
  124. '(lambda (e) (send e on-close))))
  125. (test-editor-allocate 'text:basic%)
  126. (test-editor-allocate 'text:keymap%)
  127. (test-editor-allocate 'text:autowrap%)
  128. (test-editor-allocate 'text:file%)
  129. (test-editor-allocate 'text:clever-file-format%)
  130. (test-editor-allocate 'text:backup-autosave%)
  131. (test-editor-allocate 'text:searching%)
  132. (test-editor-allocate 'text:info%)
  133. (test-editor-allocate 'pasteboard:basic%)
  134. (test-editor-allocate 'pasteboard:keymap%)
  135. (test-editor-allocate 'pasteboard:file%)
  136. (test-editor-allocate 'pasteboard:backup-autosave%)
  137. (test-editor-allocate 'pasteboard:info%)
  138. (test-editor-allocate 'scheme:text%)
  139. (test-allocate "text:return%"
  140. '(lambda () (make-object text:return% void))
  141. '(lambda (t) (void)))
  142. (test-frame-allocate '(class frame% (inherit show) (define/public (close) (show #f)) (super-new)))
  143. (test-frame-allocate 'frame:basic%)
  144. (test-frame-allocate 'frame:info%)
  145. (test-frame-allocate 'frame:text-info%)
  146. (test-frame-allocate 'frame:pasteboard-info%)
  147. (test-frame-allocate 'frame:standard-menus%)
  148. (test-frame-allocate 'frame:text%)
  149. (test-frame-allocate 'frame:searchable%)
  150. (test-frame-allocate 'frame:pasteboard%)
  151. (done)