source: trunk/abcl/contrib/jss/jss-tests.lisp @ 15064

Last change on this file since 15064 was 15064, checked in by Mark Evenson, 6 years ago

Add jss hook and test
(Alan Ruttenberg)

From <https://github.com/armedbear/abcl/pull/52/commits/155db3e3fbb19a2969221edce465bf45f1560abe>.

Part of merge <https://github.com/armedbear/abcl/pull/52>.

File size: 2.9 KB
Line 
1(in-package :cl-user)
2
3(defpackage jss-test
4  (:use :cl :cl-user :jss :prove))
5
6(in-package :jss-test)
7
8(plan 6)
9
10(is (read-from-string "#\"{bar}.{foo}\"") '(get-java-field bar foo t))
11(is (read-from-string "#\"q.bar.{foo}\"") '(get-java-field (load-time-value (find-java-class "q.bar")) foo t))
12(is (read-from-string "#\"{bar}.foo\"") '(get-java-field bar "foo" t))
13(is-error (read-from-string "#\".bar.foo\"") 'simple-error)
14;;; http://abcl.org/trac/ticket/205
15(is (with-constant-signature ((substring "substring")) (substring "01234" 2)) "234")
16;;; http://abcl.org/trac/ticket/229 - note: version of test for this ticket was broken in tests.lisp
17(is (#"toString" (find "size" 
18      (#"getMethods" (find-java-class "java.util.Collections$UnmodifiableMap"))
19    :test 'string-equal :key #"getName"))
20    (#"toString" (java::jmethod "java.util.Collections$UnmodifiableMap" "size" )))
21
22;; test that optimized jss is much faster than unoptimized
23(defun optimized-jss (count)
24  (loop repeat count do (#"compile" 'regex.Pattern ".*")))
25
26(let ((jss::*inhibit-jss-optimization* t))
27  (defun unoptimized-jss (count)
28  (loop repeat count do (#"compile" 'regex.Pattern ".*"))))
29
30(defun just-loop (count)
31  (loop repeat count))
32
33(let ((jss::*inhibit-jss-optimization* nil))
34  (compile 'just-loop)
35  (compile 'optimized-jss))
36(let ((jss::*inhibit-jss-optimization* t))
37  (compile 'unoptimized-jss))
38
39(defmacro timeit (&body body)
40  `(let ((start (#"currentTimeMillis" 'system)))
41    ,@body
42    (- (#"currentTimeMillis" 'system) start)))
43
44
45(plan 1)
46(is-type (let ((just-loop (timeit (just-loop 10000))))
47     (+ 0.0 
48        (/ (-  (timeit (optimized-jss 10000)) just-loop)
49      (-  (timeit (unoptimized-jss 10000)) just-loop))))
50   '(float 0 0.1))
51
52(plan 2)
53(let* ((jss::*inhibit-jss-optimization* nil)
54       (optimized-jss (macroexpand (precompiler::precompile-form '(#"compile" 'regex.Pattern ".*") t))))
55  (let* ((jss::*inhibit-jss-optimization* t)
56         (unoptimized-jss (macroexpand (precompiler::precompile-form '(#"compile" 'regex.Pattern ".*") t))))
57    (is (car optimized-jss) 'java:jstatic)
58    (is (caar unoptimized-jss) 'lambda)))
59
60(finalize)
61
62(plan 1)
63
64(in-package :jss)
65(defparameter expanded '(let ((jss::this jss::*object-for-this*))
66      (jcall "getLoaded"
67       (jcall "load"
68        (jcall "make"
69         (jcall "intercept"
70          (jcall "method"
71           (jcall "subclass"
72            (new '|ByteBuddy|)
73            (find-java-class '|Object|)
74            t)
75           (jstatic "named"
76              '|ElementMatchers|
77              "toString"))
78          (jstatic "value"
79             '|FixedValue|
80             "Hello World!")))
81        (jcall "getClassLoader"
82         (jcall "getClass" jss::this))))))
83
84(defparameter source '#1"new ByteBuddy().subclass(Object.class,t)
85   .method(ElementMatchers.named("toString"))
86   .intercept(FixedValue.value("Hello World!"))
87   .make()
88   .load(getClass().getClassLoader())
89   .getLoaded()" )
90
91(in-package :jss-test)
92
93(is jss::source
94    jss::expanded)
95
96(finalize)
Note: See TracBrowser for help on using the repository browser.