aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/test/lux/host.jvm.lux
blob: bda13403b4d7c1a4b5f53abf74f209c98441d6b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
(.module:
  [lux #*
   [abstract/monad (#+ Monad do)]
   [control
    pipe]
   [data
    ["." text ("#\." equivalence)]
    [number
     ["n" nat]
     ["i" int]]]
   [math
    ["r" random]]
   ["_" test (#+ Test)]]
  {1
   ["." / (#+ import: class: interface: object)]})

(import: (java/util/concurrent/Callable a))

(import: java/lang/Long)

(import: java/lang/String)

(import: java/lang/Exception
  (new [java/lang/String]))

(import: java/lang/Object)

(import: (java/lang/Class a)
  (getName [] java/lang/String))

(import: java/lang/Runnable)

(import: java/lang/System
  (#static out java/io/PrintStream)
  (#static currentTimeMillis [] #io long)
  (#static getenv [java/lang/String] #io #? java/lang/String))

## TODO: Handle "class:" ASAP.
## (class: #final (TestClass A) [java/lang/Runnable]
##   ## Fields
##   (#private foo boolean)
##   (#private bar A)
##   (#private baz java/lang/Object)
##   ## Methods
##   (#public [] (new self {value A}) []
##            (exec (:= ::foo #1)
##              (:= ::bar value)
##              (:= ::baz "")
##              []))
##   (#public (virtual self) java/lang/Object
##            "")
##   (#public #static (static) java/lang/Object
##            "")
##   (java/lang/Runnable [] (run self) void
##                       []))

(def: test-runnable
  (object [] [java/lang/Runnable]
    []
    (java/lang/Runnable
     [] (run self) void
     [])))

(def: test-callable
  (object [a] [(java/util/concurrent/Callable a)]
    []
    ((java/util/concurrent/Callable a)
     [] (call self) a #throws [java/lang/Exception]
     (undefined))))

## (interface: TestInterface
##   ([] foo [boolean java/lang/String] void #throws [java/lang/Exception]))

(def: conversions
  Test
  (do r.monad
    [sample r.int]
    (`` ($_ _.and
            (~~ (template [<to> <from> <message>]
                  [(_.test <message>
                           (or (|> sample (:coerce java/lang/Long) <to> <from> (:coerce Int) (i.= sample))
                               (let [capped-sample (|> sample (:coerce java/lang/Long) <to> <from>)]
                                 (|> capped-sample <to> <from> (:coerce Int) (i.= (:coerce Int capped-sample))))))]

                  [/.long-to-byte /.byte-to-long "Can succesfully convert to/from byte."]
                  [/.long-to-short /.short-to-long "Can succesfully convert to/from short."]
                  [/.long-to-int /.int-to-long "Can succesfully convert to/from int."]
                  [/.long-to-float /.float-to-long "Can succesfully convert to/from float."]
                  [/.long-to-double /.double-to-long "Can succesfully convert to/from double."]
                  [(<| /.int-to-char /.long-to-int) (<| /.int-to-long /.char-to-int) "Can succesfully convert to/from char."]
                  ))
            ))))

(def: miscellaneous
  Test
  (do {! r.monad}
    [sample (:: ! map (|>> (:coerce java/lang/Object))
                (r.ascii 1))]
    ($_ _.and
        (_.test "Can check if an object is of a certain class."
                (and (case (/.check java/lang/String sample) (#.Some _) true #.None false)
                     (case (/.check java/lang/Long sample) (#.Some _) false #.None true)
                     (case (/.check java/lang/Object sample) (#.Some _) true #.None false)
                     (case (/.check java/lang/Object (/.null)) (#.Some _) false #.None true)))

        (_.test "Can run code in a 'synchronized' block."
                (/.synchronized sample #1))

        (_.test "Can access Class instances."
                (text\= "java.lang.Class" (java/lang/Class::getName (/.class-for java/lang/Class))))

        (_.test "Can check if a value is null."
                (and (/.null? (/.null))
                     (not (/.null? sample))))

        (_.test "Can safely convert nullable references into Maybe values."
                (and (|> (: (Maybe java/lang/Object) (/.??? (/.null)))
                         (case> #.None #1
                                _ #0))
                     (|> (: (Maybe java/lang/Object) (/.??? sample))
                         (case> (#.Some _) #1
                                _ #0))))
        )))

(def: arrays
  Test
  (do {! r.monad}
    [size (|> r.nat (:: ! map (|>> (n.% 100) (n.max 1))))
     idx (|> r.nat (:: ! map (n.% size)))
     value (:: ! map (|>> (:coerce java/lang/Long)) r.int)]
    ($_ _.and
        (_.test "Can create arrays of some length."
                (n.= size (/.array-length (/.array java/lang/Long size))))

        (_.test "Can set and get array values."
                (let [arr (/.array java/lang/Long size)]
                  (exec (/.array-write idx value arr)
                    (i.= (:coerce Int value)
                         (:coerce Int (/.array-read idx arr)))))))))

(def: #export test
  ($_ _.and
      (<| (_.context "Conversions.")
          ..conversions)
      (<| (_.context "Miscellaneous.")
          ..miscellaneous)
      (<| (_.context "Arrays.")
          ..arrays)))