obj-test.scm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. (test-init "Objects" 146)
  2. ;; Force procedure to be applied without being inlined:
  3. (define-syntax force-eval
  4. (syntax-rules () ((force-eval proc arg ...)
  5. ((dynamic proc) arg ...))))
  6. ;; Force call to be compiled with (hopefully) inlining:
  7. (define-syntax force-compile
  8. (syntax-rules () ((force-compile proc arg ...)
  9. ((lambda (#!key keydummy) (proc arg ...))))))
  10. (define complex (make-record-type "complex" '(re im)))
  11. (define make-complex (record-constructor complex))
  12. (define z (make-complex 3 4))
  13. (define make-rcomplex (record-constructor complex '(im re)))
  14. (test z make-rcomplex 4 3)
  15. (test 4 'accessor1 ((record-accessor complex 'im) z))
  16. ((record-modifier complex 're) z 5)
  17. (test z make complex im: 4 re: 5)
  18. (test 5 'accessor2 ((record-accessor complex 're) z))
  19. (test #t record? z)
  20. (test #f record? 5)
  21. (test #t 'record-predicate ((record-predicate complex) z))
  22. (test complex record-type-descriptor z)
  23. (test "complex" record-type-name complex)
  24. (test '(re im) record-type-field-names complex)
  25. (test 20 'set! (begin (set! z:im 15) (+ z:re z:im)))
  26. ;; Check name mangling and demangling of records.
  27. (define funny-record (make-record-type 'lispy-name->foo!? '(a! b-c)))
  28. (test "lispy-name->foo!?" record-type-name funny-record)
  29. (test '(a! b-c) record-type-field-names funny-record)
  30. (define make-funny-record1 (record-constructor funny-record))
  31. (define make-funny-record2 (record-constructor funny-record '(b-c a!)))
  32. (define lt1 (make-funny-record1 10 12))
  33. (test 10 'accessor21 ((record-accessor funny-record 'a!) lt1))
  34. ((record-modifier funny-record 'b-c) lt1 (+ 2 lt1:b-c))
  35. (set! lt1:a! 9)
  36. (test (make-funny-record2 14 9) 'funny-record lt1)
  37. (test '(10 "10" 20 "20") 'object-with-field-1
  38. (let*
  39. ((obj (object (<object>)
  40. (fld 10)
  41. ((toString) ::String fld)))
  42. (val1 (field obj 'fld))
  43. (str1 (as <String> obj)))
  44. (begin
  45. (set! (field obj 'fld) 20)
  46. (let*
  47. ((val2 (field obj 'fld))
  48. (str2 (as <String> obj)))
  49. (list val1 str1 val2 str2)))))
  50. (test '(100 "100" 20 "20") 'object-with-field-2
  51. (let*
  52. ((val0 100)
  53. (obj (object (<object>)
  54. (fld val0)
  55. ((toString) ::String fld)))
  56. (val1 (field obj 'fld))
  57. (str1 (as <String> obj)))
  58. (begin
  59. (set! (field obj 'fld) 20)
  60. (let*
  61. ((val2 (field obj 'fld))
  62. (str2 (as <String> obj)))
  63. (list val1 str1 val2 str2)))))
  64. (test 1 'object-locals
  65. (let ((x (object (<java.lang.Object>) (z (lambda (x) (display x)))))) 1))
  66. (test 1 'object-locals
  67. (let* ((d display)
  68. (x (object (<java.lang.Object>) (z (lambda (x) (d x)))))) 1))
  69. (test 2 'object-with-closure-1
  70. (length (let*
  71. ((name 'x)
  72. (obj (object (<java.util.zip.Adler32>))))
  73. (letrec ((opt
  74. (lambda (args)
  75. (list obj
  76. (object (java.lang.Object
  77. java.awt.event.ItemListener)
  78. ((itemStateChanged
  79. (arg::java.awt.event.ItemEvent))
  80. ::void
  81. (display name)
  82. (newline)))))))
  83. (opt 3)))))
  84. (define (object-with-closure-2 c-name)
  85. (let* ((c-path (symbol->string c-name))
  86. (c-obj (object (<java.lang.Object>))))
  87. (letrec ((opt (lambda (args)
  88. (if (pair? args)
  89. (begin
  90. (let ((listener
  91. (object (<java.lang.Object>
  92. <java.awt.event.ItemListener>)
  93. ((itemStateChanged (arg::java.awt.event.ItemEvent))
  94. ::void
  95. (display "listener of checkbutton ")
  96. (display c-name)
  97. (display arg)
  98. (newline)))))
  99. (list c-obj listener))
  100. (opt (cddr args)))))))
  101. (opt (list )))
  102. c-path))
  103. (test ".x.c" object-with-closure-2 '.x.c)
  104. (define (document-filter arg1)
  105. (lambda (arg2)
  106. (object ()
  107. ((toString) ::String
  108. (format #f "{arg1: ~s arg2: ~s}" arg1 arg2)))))
  109. (test "{arg1: 23 arg2: 12}" 'object-with-closure-3
  110. (as <String> ((document-filter 23) 12)))
  111. (define i100 (force-eval make <integer> ival: 100))
  112. (define i200 (force-compile make <integer> ival: 200))
  113. (test 100 'test-make-1 i100)
  114. (test 200 'test-make-2 i200)
  115. (define cons1 (force-eval make <pair> 7 9))
  116. (test '(7 . 9) 'test-make-3 cons1)
  117. (test '(9 . 6) 'test-make-3 (force-compile make <pair> 9 6))
  118. (force-eval slot-set! cons1 'cdr 99)
  119. (test '(7 . 99) 'test-slot-set-1 cons1)
  120. (force-compile slot-set! cons1 'cdr (field '(88 99) 'car))
  121. (test '(7 . 88) 'test-slot-set-2 cons1)
  122. (set! (slot-ref cons1 'car) 8)
  123. (test '(8 . 88) 'test-slot-set-3 cons1)
  124. (set! (field cons1 'cdr) 55)
  125. (test '(8 . 55) 'test-slot-set-3 cons1)
  126. (test #t 'test-slot-ref-1
  127. (force-eval static-field <java.lang.Boolean> 'TRUE))
  128. (test '() 'test-slot-ref-2
  129. (force-compile static-field <list> 'Empty))
  130. (test #t 'test-slot-ref-3
  131. (force-eval field #f 'TRUE))
  132. (test #f 'test-slot-ref-4
  133. (force-compile field #t 'FALSE))
  134. ;; Make sure objects compiled in separate compilations don't cause
  135. ;; errors (naming clashes)
  136. (test '(1 2) (lambda ()
  137. ;; define each object in a separate compile
  138. (define obj1 (eval '(object () ((one) 1))))
  139. (define obj2 (eval '(object () ((two) 2))))
  140. (list (invoke obj1 'one)
  141. (invoke obj2 'two))))
  142. (define-variable internal-node-name list)
  143. (require <module2>)
  144. (test "fun2" fun1)
  145. (test "fun1" 'fun2 (fun2))
  146. (test '("fun1" "fun2") fun1fun2)
  147. (test 4 list-length-1 '(a b c d))
  148. (test 2 list-length-5 '(a b))
  149. (test 0 length (classify))
  150. (test 3 length-diff1 "abcdef" "abc")
  151. (test 3 length-diff2 'abcdef 'abc)
  152. (test 3 length-diff3 'abcdef 'abc)
  153. (test '(1 2 3 4) 'deldup-test list1234)
  154. ;; Test bug reported by Jocelyn Paine.
  155. (test '(boolean #t) make-literal #t)
  156. (test '(3 . 4) make-pair 3 4)
  157. (test 7 'my-array-length (field my-array-7 'length))
  158. (define-variable dvar1 2)
  159. (define-variable dvar2 3)
  160. (require <module3>)
  161. (test 13 'Savannah-bug-40822 (macro2))
  162. (let* ((all0 (all-zeros))
  163. (nlen (gnu.lists.LList:listLength all0 #t)))
  164. (test "#0=(0 . #0#) len:-1" 'Savannah-bug-43233
  165. (format #f "~w len:~d" all0 nlen)))
  166. (define-variable dvar3 4)
  167. (test '(2 3 13) 'dvar-test-1 dvar-test-1)
  168. (set! dvar1 1)
  169. (test '(1 3 13) list dvar1 dvar2 dvar3)
  170. (test 0 list-length-4 '())
  171. (test '(10 24 11 190) test1-import0)
  172. (test '(24 15 11 181 #(15 338 169)) test3-import1)
  173. (test '(11 111) 'test-mod0-v2
  174. (let ((v2 (get3-mod0-v2)))
  175. (set3-mod0-v2 (+ 100 v2))
  176. (list v2 (get3-mod0-v2))))
  177. (test 25 'test-mod2-v5 mod2-v5)
  178. ;; Test for Savannah bug #34004: Nullpointer exception in compiler
  179. (test 1 check-thunk)
  180. (test '(1 2) 'counter-test-result counter-test-result)
  181. (define ts1 (make <MyTimestamp> 10 1))
  182. (define ts2 (make <MyTimestamp> 10 2))
  183. (test #t < (my-compare ts1 ts2) 0)
  184. (test #(2147483648 21474836482147483648 #x7fffffff #x80000000 -1073741825 -1073741824 -1073741823)
  185. 'misc-ints misc-ints)
  186. ;; Based on Savannah bug#11822, contributed by Dean Ferreyra.
  187. ;; (Other parts of this testcase are in module1.scm and module3.scm.)
  188. (mB <NewClass> 'simple-sym)
  189. (test '(100 simple-sym) 'bug-11822
  190. (let ((s :: <simpleAux> (make <simpleAux>))
  191. (nc :: <NewClass> (make <NewClass>)))
  192. (invoke nc 'fn s)))
  193. (test 24 'factorial-4 factorial-4)
  194. (test 1062806400000 'namespace-syntax-test (namespace-syntax-call))
  195. (test 'Z check-fluid-let 'Z)
  196. (define IsClass2-value (make <IdClass2>))
  197. (require <classes1>)
  198. (require <classes2>)
  199. (test "thunk re-initialized" thunk "thunk re-initialized")
  200. (test 3 slot-ref IsClass2-value 'var1)
  201. (test 4 slot-ref IsClass2-value 'var2)
  202. (set! fail-expected "static counter is incorrectly initialized in non-static run method")
  203. (test 5 get-new-count)
  204. (define obj1 (make <SimpleA>))
  205. (test 4 slot-ref obj1 'a)
  206. (test 6 slot-ref obj1 'b)
  207. (test 35 'obj1-f (invoke obj1 'f 5))
  208. (test #(y) 'lambda-method1 ((invoke obj1 'lambda-method1) 'y))
  209. (test #(z z z) 'lambda-method2 ((invoke obj1 'lambda-method2 -1) 'z))
  210. (test 2 'lambda-method3 (invoke obj1 'lambda-method3))
  211. ((invoke obj1 'lambda-method5 13))
  212. (test 13 'lambda-method4 (invoke obj1 'lambda-method4))
  213. ((invoke obj1 'lambda-method6) 12)
  214. (test 12 'lambda-method4 (invoke obj1 'lambda-method4))
  215. ((invoke obj1 'lambda-method7) 2)
  216. (test 2 'lambda-method4 (invoke obj1 'lambda-method4))
  217. (test '(nn mm (r1 r2)) 'lambda-method-rest1
  218. ((invoke obj1 'lambda-method-rest1 'nn) 'mm 'r1 'r2))
  219. (test "TroubleIdentity" 'test-trouble ((invoke obj1 'trouble) "Trouble"))
  220. (slot-set! obj1 'a (+ 10 (static-field <SimpleA> 'b)))
  221. (test "yes" slot-ref obj1 'hyphenated-field?)
  222. (test 16 field obj1 'a)
  223. (slot-set! obj1 'happy #t)
  224. (test #t slot-ref obj1 'happy)
  225. (slot-set! obj1 'happy #f)
  226. (test #f slot-ref obj1 'happy)
  227. (force-compile slot-set! (as <SimpleA> obj1) 'happy #t)
  228. (test #t slot-ref (as <SimpleA> obj1) 'happy)
  229. (set! (field obj1 'happy) #f)
  230. (test #f field obj1 'happy)
  231. (test "a:5 b:(10 15)" 'with-var-arg (obj1:withVarArg 5 10 15))
  232. (define obj2 (make <SimpleB>))
  233. (test 4 field obj2 'a)
  234. (test 6 field obj2 'b)
  235. (test 6 static-field <SimpleB> 'b)
  236. (test 10 slot-ref obj2 'c)
  237. (test 1045 'obj2-f (invoke obj2 'f 15))
  238. (define obj3 (make <SimpleC> d: 25))
  239. (test 4 'obj3-a (slot-ref obj3 'a))
  240. (test 6 'obj3-b (slot-ref obj3 'b))
  241. (test 10 'obj3-c (slot-ref obj3 'c))
  242. (test 25 slot-ref obj3 'd)
  243. (test 24 slot-ref obj3 'e)
  244. (test 3 slot-ref (make-simpleC 3) 'i)
  245. (define obj4 (make <ClsC>))
  246. (test 14 'obj4-b (slot-ref obj4 'b))
  247. (test 22 'obj4-c (slot-ref obj4 'c))
  248. (test 44 'obj4-f (invoke obj4 'f 2))
  249. (define (make-ClsD) (make <ClsD>))
  250. (define obj5 (let ((x ::cls-d (make-ClsD))) x))
  251. (test 23 'obj5-d (slot-ref obj5 'd))
  252. (define obj6 (make <ClsE>))
  253. (set! (field obj6 'e) (- (field obj6 'e) 10))
  254. (test 29 'obj6-e (slot-ref obj6 'e))
  255. (test 156 'obj6-f (invoke obj6 'f 7))
  256. (define test-capture-1 (make-TestCapture1))
  257. (test '(56 11 21) 'test-capture-1 ((car (invoke test-capture-1 'ff 99)) 21))
  258. (define test-capture-2 (make <TestCapture2>))
  259. (test '(56 22) 'test-capture-2 ((car (invoke test-capture-2 'ff 99)) 22))
  260. (test 46 'classes2-capture-test-a
  261. ((cadr (classes2-capture-test-a 100)) 'a))
  262. (test 66 'classes2-capture-test-a
  263. ((classes2-capture-test-b 100) 'b))
  264. (require <MyFunc>)
  265. (test '(1 2 3) my-func-1 2 3)
  266. (require <MyModule>)
  267. (test '(#t 5 6) my-func-t 5 6)
  268. (define TestCapturedFieldRef-instance (<TestCapturedFieldRef> 7))
  269. (incr-field-function 2)
  270. (test 109 'test-captured-field-ref TestCapturedFieldRef-instance:var)
  271. (define-record-type pare
  272. (kons x y-part)
  273. pare?
  274. (x kar set-kar!)
  275. (y-part kdr))
  276. (test #t pare? (kons 1 2))
  277. (test #f pare? (cons 1 2))
  278. (test 1 kar (kons 1 2))
  279. (test 2 kdr (kons 1 2))
  280. (test 3 'set-kdr!
  281. (let ((k (kons 1 2)))
  282. (set-kar! k 3)
  283. (kar k)))
  284. (define obj-with-let
  285. (object ()
  286. ((meth v)
  287. (let ((n v) (r (this)))
  288. (list n)))) )
  289. (test '(3) 'ff (invoke obj-with-let 'meth 3))
  290. (define (create-main-frame)
  291. (object ()
  292. (myField init:
  293. (format #f "this: ~a\n" (this))
  294. )))
  295. (define main-frame (create-main-frame))
  296. (test (format #f "this: ~a\n" main-frame) field main-frame 'myField)
  297. (define simple-date (make <SimpleDateTest>))
  298. (define-namespace date "class:java.util.Date")
  299. (test (+ 1900 (date:new):year)
  300. invoke simple-date 'get-year)
  301. (define non-simple-date (make <DateTest>))
  302. (test (+ 1900 (*:get-year (date:new)))
  303. invoke non-simple-date 'get-year)
  304. (test #t 'date-1 (>= (invoke date-test-instance 'get-year) 2004))
  305. (test #f 'date-2 (invoke non-simple-date 'before date-test-instance))
  306. (test #t 'date-3 (>= (make-date-test) 2004))
  307. (test 13 'colon-test-1 (list 11 12 13 14):cdr:cdr:car)
  308. (test '(12 13 14) 'colon-test-2 (list 11 12 13 14):cdr)
  309. (define colon-test-list-1 (list 11 12 13 14))
  310. (test 13 'colon-test-3 colon-test-list-1:cdr:cdr:car)
  311. (test '(12 13 14) 'colon-test-4 colon-test-list-1:cdr)
  312. (test "(11 12 13 14)" 'colon-test-5 (colon-test-list-1:toString))
  313. (test "(12 13 14)" 'colon-test-6 (colon-test-list-1:cdr:toString))
  314. ;; Test for Savannah bug #4289
  315. (define pa-data (pa-new 10))
  316. (pa-setter pa-data 5 99)
  317. (test 99 pa-getter pa-data 5)
  318. (test #!null pa-getter pa-data 6)
  319. (test 10 pa-length pa-data)
  320. ;; Test for Savannah bug #5651
  321. (define arr-5 ((primitive-array-new <int>) 5))
  322. ((primitive-array-set <int>) arr-5 1 98)
  323. (iarr-set arr-5 0 99)
  324. (test '(99 98 0) 'prim-arr-test
  325. (let ((getter (primitive-array-get <int>)))
  326. (list (getter arr-5 0) (getter arr-5 1)
  327. ((primitive-array-get <int>) arr-5 4))))
  328. ;; Test for Savannah bug #15151
  329. (test (*:toString "gnu.math.IntNum") 'getClassTest
  330. (*:getName (getClassTest '123)))
  331. (require <cycle1>)
  332. (test #t is-even? 8)
  333. (test #f is-even? 3)
  334. (define c1-n1 (cycle1-name1))
  335. (define c1-n2 (cycle1-name2))
  336. (define c1-n3 (cycle1-name3))
  337. (test "cycle1" 'check-reference-module-name
  338. (and (eq? c1-n1 c1-n2) (eq? c1-n1 c1-n3)
  339. (java.lang.Class? c1-n1) (invoke c1-n1 'getName)))
  340. (require <cycle2>)
  341. (test #t is-even? 8)
  342. (test #f is-even? 3)
  343. (test #f is-odd? 8)
  344. (test #t is-odd? 3)
  345. (define m2-object (module2))
  346. (test '(#t #t #f #t) 'check-m2-types
  347. (list (module2? m2-object)
  348. (pair? m2-object)
  349. (java.lang.Appendable? m2-object)
  350. (java.io.Closeable? m2-object)))
  351. (set-cdr! m2-object '(5 6 7))
  352. (test 4 length m2-object)
  353. (test #!null 'before-m2-close m2-object:my-array-7)
  354. (let ((cl ::java.io.Closeable m2-object)) (cl:close))
  355. (test #f 'after-m2-close m2-object:my-array-7)
  356. (import (libx))
  357. (test '(ax in libx) libx-report)