-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtest.R
1539 lines (1245 loc) · 49.5 KB
/
test.R
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Sys.unsetenv("R_TESTS")
test_that("Some smoke tests", {
expect_equal(juliaCall("prod", c(1,2,3)), 6)
juliaEval("")
juliaCall("string", list())
juliaCall("string", list(as.integer(1), "bla" = 23L))
juliaEval("String[]")
expect(!is.null(juliaEval("using Random; Random.seed!(5)")),
"Must be able to set random seed")
expect(is.null(juliaEval("Random.seed!(5);")),
"Eval with semicolon at end returns NULL")
})
test_that("Output is transferred", {
output <- capture_output({
juliaCall("println", as.integer(22))
})
expect_match(output, "^[\n]*22[\n]*$")
output <- capture_output({
juliaCall("println", "hello world")
})
expect_match(output, "^[\n]*hello world[\n]*$")
})
test_that("Warnings are transferred", {
output <- capture.output(type = "message", {
juliaEval('@warn "This might be serious"')
})
expect_match(output[1], "This might be serious")
})
test_that("Warnings and normal output are both transferred and stable", {
stdoutput <- capture_output({
stderroutput <- capture.output(type = "message", {
juliaLet('for i = 1:100
println(join(rand(["1", "ä", "ö"], rand(1:100))));
println(stderr,join(rand(["2", "u", "a"], rand(1:100))));
end')
ret <- juliaLet('println(22); 17')
})
})
expect_match(stdoutput, "[1äö\n]+")
expect_match(stderroutput, "[2ua\n]+") # TODO problems with non-ascii-characters
stdoutput <- capture_output({
stderroutput <- capture.output(type = "message", {
ret <- juliaLet('println(22); @warn "This might be serious"; 17')
})
})
expect_equal(ret, 17)
expect_match(stdoutput[1], "^[\n]*22[\n]*$")
expect_match(stderroutput[1], "This might be serious")
})
test_that("A return value of nothing is not printed", {
output <- capture_output({
juliaEval("nothing")
juliaCall("identity", NULL)
juliaLet("x = y", y = NULL)
})
expect_match(output, "^[\n]*$")
})
test_that("Test loading and importing a complex package", {
skip_on_cran()
juliaEval('begin
using Pkg;
try
@eval import StatsBase
catch ex
Pkg.add("StatsBase")
end
end
') # tests also trailing whitespace
StatsBase <- juliaImport("StatsBase")
expect_equivalent(juliaGet(StatsBase$mean_and_var(c(1,2,3))), list(2,1))
StatsBase$renyientropy(rnorm(100), 1)
})
test_that("Error when trying to import a non-existent package or module", {
expect_error(juliaImport("NonExistingPkg"))
expect_error(capture.output({juliaImport(".NonExistingModule")}, type = "message"))
})
test_that("Error when passing multiple strings to import or using", {
expect_error(juliaImport(c("Pkg", "UUID")), regexp = "exactly one")
})
test_that("Example for juliaEval runs", {
v1 <- juliaExpr('v"1.0.5"')
v2 <- juliaExpr('v"1.3.0"')
expect(juliaCall("<", v1, v2), "Comparison must work")
})
test_that("Echo: empty R vector", {testEcho(c())})
test_that("Echo: double", {
expect(is.integer(juliaEcho(1L)), "Must be integer")
testEcho(1L)
})
test_that("Echo: Single Int", {
testEcho(1L)
expect(is.integer(juliaEval("1")), "1 is not integer")
testEcho(juliaEval("1"))
expect(is.double(juliaEval("2^52")), "2^52 is not double") # TODO inexactness?
testEcho(juliaEval("2^52"))
})
test_that("Echo: 1-element vector of Int in Julia", {testEcho(juliaEval("[1]"))})
test_that("Echo: Matrix of Int", {testEcho(matrix(1:6, nrow = 2))})
test_that("Echo: Single Float64", {
testEcho(juliaEval("1.0"))
testEcho(1)
})
test_that("UInt32, Float16, and Float32 translated to doubles in R", {
toTest <- c("Float16(1.5)", "Float16[]", "Float16[1.5]",
"Float32(1.5)", "Float32[]", "Float32[1.5]",
"UInt32(32)", "UInt32[]", "UInt32[32]")
for (expr in toTest) {
expect(is.double(juliaEval(expr)), "Must be double")
}
})
test_that("Echo: 0-element vector of Float64 in Julia", {testEcho(juliaEval("Float64[]"))})
test_that("Echo: 1-element vector of Float64 in Julia", {testEcho(juliaEval("[1.0]"))})
test_that("Echo: 2-element vector of Float64/double", {
testEcho(c(1, 2))
testEcho("[1.0; 2.0]")
})
test_that("Echo: matrix of Float64 in Julia", {
testEcho(juliaEval("[1.0 2.0; 3.0 4.0]"))
testEcho(matrix(c(1,2,3,4,4,5), nrow = 2))
})
test_that("Echo: 1-element vector of Float32 in Julia", {testEcho(juliaEval("[1.0f0]"))})
test_that("Echo: matrix of Float32 in Julia", {testEcho(juliaEval("[1.0f0 2.0f0; 3.0f0 4.0f0]"))})
test_that("Echo: 1-element vector of String in Julia", {testEcho(juliaEval('String["bla"]'))})
test_that("Echo: 0-element vector of String in Julia", {testEcho("String[]")})
test_that("Echo: 1-element vector of String in R", {testEcho("bla")})
test_that("Echo: 2-element vector of String in R", {testEcho(c("bla", "blup"))})
test_that("Echo: 2-element vector of String in Julia", {testEcho(juliaEval('String["bla", "blup"]'))})
test_that("Echo: String with missing values", {
x <- c("Yes", "No", NA)
y <- juliaEcho(x)
expect_true(is.na(y[3]))
x <- matrix(c("bla", NA, NA, "NA"), nrow = 2)
y <- juliaEcho(x)
expect_false(is.na(y[1,1]))
expect_true(is.na(y[1,2]))
expect_true(is.na(y[2,1]))
expect_false(is.na(y[2,2]))
})
test_that("Echo: Int vector with missing values", {
expect_true(juliaLet("x[1] == 1 && ismissing(x[2]) && x[3] == 3", x = c(1L,NA, 3L)))
expect_true(juliaLet("ismissing(x)", x = NA_integer_))
x <- juliaEval("[1 missing 3; 3 missing 4 ]")
expect_type(x, "integer")
expect_true(all(is.na(x[,2])))
expect_null(attr(x, "JLTYPE"))
x <- juliaEval("[2^52 missing 3; 2^53 missing 4 ]")
expect_type(x, "double")
expect_true(all(is.na(x[,2])))
expect_equal(x[1,1], 2^52)
expect_equal(x[2,1], 2^53)
testEcho(x)
x <- juliaEval("Union{Int32,Missing}[1 missing 3; 3 missing 4 ]")
expect_type(x, "integer")
expect_true(all(is.na(x[,2])))
expect_equal(x[1,1], 1)
expect_equal(x[2,1], 3)
testEcho(x)
x <- juliaEval("Union{Int16,Missing}[-1 missing 3; 3 missing 4 ]")
expect_type(x, "integer")
expect_true(all(is.na(x[,2])))
expect_equal(x[1,1], -1)
expect_equal(x[2,1], 3)
testEcho(x)
testEcho(c(1L, 2L, NA, NA))
testEcho(matrix(c(1L, 2L, NA, NA), nrow = 2))
})
test_that("Echo: Double vector with missing values", {
expect_true(juliaLet("x[1] == 1.0 && ismissing(x[2]) && x[3] == 3.0", x = c(1,NA, 3)))
expect_true(juliaLet("ismissing(x)", x = NA_integer_))
x <- juliaEval("[1.0 missing 3.0; 3.0 missing 4.0 ]")
expect_type(x, "double")
expect_true(all(is.na(x[,2])))
expect_null(attr(x, "JLTYPE"))
x <- juliaEval("Union{Float32,Missing}[1.0f0 missing 3.0f0; 3.0f0 missing 4.0f0 ]")
expect_true(all(is.na(x[,2])))
expect_equal(x[1,1], 1)
expect_equal(x[2,1], 3)
testEcho(x)
testEcho(c(1, 2, NA, NA))
testEcho(matrix(c(1, 2, NA, NA), nrow = 2))
})
test_that("Dirty missing values are recognized", {
dirtyNa <- c(as.raw(0xa2), as.raw(0x07), as.raw(0x00), as.raw(0x00),
as.raw(0x00), as.raw(0x00), as.raw(0xf8), as.raw(0x7f))
dirtyNA <- readBin(dirtyNa, what = "double", endian = "little")
expect_true(is.na(dirtyNA) && !is.nan(dirtyNA))
expect_true(juliaCall("ismissing", dirtyNA))
expect_true(juliaLet("ismissing(x[2])", x = c(1, dirtyNA)))
echoDirtyNA <- juliaEcho(dirtyNA)
expect_true(is.na(echoDirtyNA) && ! is.nan(echoDirtyNA))
dirtyComplexNA <- complex(real = dirtyNA, imaginary = dirtyNA)
expect_true(is.na(dirtyComplexNA) && !is.nan(dirtyComplexNA))
expect_true(juliaCall("ismissing", dirtyComplexNA))
x <- c(1+1i, dirtyComplexNA, NA)
expect_true(juliaLet("ismissing(x[2]) && ismissing(x[3])", x = x))
testEcho(x)
# The following doesn't work: dirtNA coerced to NA+0i, not NA+NA*i:
# expect_true(juliaLet("ismissing(x[2])", x = c(1+1i, dirtyNA)))
echoDirtyComplexNA <- juliaEcho(dirtyComplexNA)
expect_true(is.na(echoDirtyComplexNA) && !is.nan(echoDirtyComplexNA))
})
test_that("NaN and NA are handled differently", {
# NaN in R is translated to a NaN Float64 value in Julia
# NA in R is translated to a missing value in Julia
x <- juliaEval("[1; missing; NaN]")
expect_equal(x[1], 1)
expect_true(is.na(x[2]) && !is.nan(x[2]))
expect_true(is.nan(x[3]))
y <- juliaEcho(x)
expect_equal(y[1], 1)
expect_true(is.na(y[2]) && !is.nan(y[2]))
expect_true(is.nan(y[3]))
expect_equal(juliaCall("typeof", c(1, NA)),
juliaEval("Array{Union{Float64,Missing},1}"))
expect_equal(juliaCall("typeof", c(1, NaN)),
juliaEval("Array{Float64,1}"))
expect_equal(juliaCall("typeof", c(1+1i, NA)),
juliaEval("Array{Union{Complex{Float64},Missing},1}"))
expect_equal(juliaCall("typeof", c(1+1i, NaN)),
juliaEval("Array{Complex{Float64},1}"))
expect_true(juliaLet("ismissing(x[2])", x = c(1+1i, NA)))
})
test_that("Missing values transferred as NA", {
expect_true(is.na(juliaEval("missing")))
expect_true(all(is.na(juliaEval("[missing; missing]"))))
expect_true(all(is.na(juliaEval("[missing missing; missing missing]"))))
})
test_that("Logical with missing values", {
juliaLet("x[1] && !x[2] && ismissing(x[3])", x = c(TRUE, FALSE, NA))
x <- c(TRUE, FALSE, NA)
testEcho(x)
})
test_that("Complex with missing values", {
juliaLet("x[1] == 1+1im && x[2] == 2+2im && ismissing(x[3])", x = c(1 +1i, 2+2i, NA))
x <- c(1 +1i, 2+2i, NA)
testEcho(x)
x <- juliaEval("[1+1im 1+2im; 0 missing]")
expect_equivalent(x, matrix(c(1+1i, 0, 1+2i, NA), nrow = 2))
testEcho(x)
x <- juliaEval("[1.0+im 2.0+im; 0.0 missing]")
expect_equal(x, matrix(c(1+1i, 0, 2+1i, NA), nrow = 2))
expect_true(is.null(attr(x, "JLTYPE")))
testEcho(x)
})
test_that("Echo: logical vectors", {
testEcho(juliaEval('Bool[]'))
testEcho(logical())
testEcho(TRUE)
testEcho(FALSE)
testEcho(c(TRUE, FALSE))
testEcho("Bool[true]")
})
test_that("Echo: Pointers", {
arrPtr <- juliaFun("Base.pointer", list(1,2,3))
ptr <- arrPtr(2L)
expect_type(ptr, "raw")
testEcho(ptr)
ptrs <- juliaCall("map", arrPtr, as.integer(1:3))
testEcho(ptrs)
})
test_that("Echo: Ref", {
testEcho(juliaCall("Ref", list(1,2,3), 2L))
r <- juliaEval("global reftestvar = 1; Ref(reftestvar)")
testEcho(r)
})
test_that("Complex values are handled first class", {
testEcho(1i)
testEcho(juliaEval("1+im"))
testEcho(juliaEval("[1+im]"))
testEcho(juliaEval("[1+im; 2+im]"))
testEcho(c(1+1i,2 + 2i))
suppressWarnings({jla <- juliaImport("LinearAlgebra")})
testEcho(matrix(c(1, 0, 0, -1), ncol = 2))
expect(all(jla$eigvals(matrix(c(1, 0, 0, -1), ncol = 2),
matrix(c(0, 1, 1, 0), ncol = 2)) %in% c(1i, -1i)),
"")
expect(all(jla$eigmax(matrix(c(0, 1i, -1i, 0), ncol = 2)) == 1.0), "")
complexTypeParameters = c("Int8", "Int16", "Int32", "Int64",
"Float16", "Float32", "Float64")
for (complexPar in complexTypeParameters) {
juliaComplexType <- paste0("Complex{", complexPar, "}")
c <- juliaEval(paste0(juliaComplexType, "(5-3im)"))
expect_equivalent(c, 5 - 3i)
carr <- juliaEval(paste0(juliaComplexType, "[4+4im, 2-2im]"))
expect_equivalent(carr, c(4+4i, 2-2i))
if (complexPar == "Float64") {
expect_null(attr(c, "JLTYPE"))
} else {
expect_true(juliaCall("==", juliaEval(attr(c, "JLTYPE")),
juliaEval(juliaComplexType)))
}
}
})
test_that("Echo: raw vector", {
testEcho(as.raw(c(1,2,3)))
testEcho(juliaEval("[0x01]"))
testEcho(juliaEval("UInt8[]"))
expect_equivalent(juliaEval("[0x01 0x02; 0x03 0x04]"),
matrix(c(1,3,2,4), nrow = 2))
expect_equal(juliaCall("string", c(as.raw(0xca), as.raw(0xfe))),
"UInt8[0xca, 0xfe]")
})
test_that("Echo: raw vector", {
testEcho(juliaEval("Int32[1, 2]"))
testEcho(juliaEval("Int32[]"))
testEcho(juliaEval("Int32[1]"))
})
test_that("Echo: Single Int16", {testEcho(juliaEval('Int16(300)'))})
test_that("Echo: Int16 Vector", {testEcho(juliaEval('Int16[1,2,3]'))})
test_that("Echo: 1-element Int16 Vector", {
expect_equal(juliaCall("string", juliaEval('Int16[300]')),
"Int16[300]")
})
test_that("Echo: Single UInt128", {testEcho(juliaEval('UInt128(2)^100 +1'))})
test_that("Echo: UInt128 Vector", {testEcho(juliaEval('UInt128[1,2,3]'))})
test_that("Echo: 1-element UInt128 Vector", {
expect_equal(juliaCall("string", juliaEval('UInt128[1]')),
"UInt128[0x00000000000000000000000000000001]")
})
test_that("Echo: List with NULL elements", {
x <- juliaGet(juliaEcho(list(1, NULL, 3)))
expect_equal(x[[1]], 1)
expect_null(x[[2]])
expect_equal(x[[3]], 3)
expect_equal(length(x), 3)
testEcho(juliaEval('[1, nothing, 3]'))
x <- list("bla", NULL)
expect_equal(x[[1]], "bla")
expect_null(x[[2]])
x <- list(NULL, NULL)
expect_null(x[[1]])
expect_null(x[[2]])
})
test_that("Mutable struct usable by reference", {
juliaEval('mutable struct TestMutableStruct
x::Int
end')
jlRef <- juliaEval("TestMutableStruct(1)")
refEcho <- juliaEcho(jlRef)
expect_true(juliaCall("==", jlRef, refEcho))
expect_true(all(get("ref", jlRef) == get("ref", refEcho)))
expect_equal(refEcho$x, 1)
refEcho$x <- 2L
expect_equal(jlRef$x, 2)
ref <- get("ref", jlRef)
jlRef <- NULL
refEcho <- NULL
invisible(gc())
juliaEval("1")
expect_false(juliaLet("haskey(communicator.sharedheap, ref)", ref = ref,
communicator = JuliaConnectoR:::pkgLocal$communicator))
# Test behaviuor with juliaGet:
# The reference must be attached
jlRefEnv <- attr(juliaGet(juliaEval("TestMutableStruct(1)")), "JLREF")
expect_true(is.environment(jlRefEnv))
expect_true(is.raw(jlRefEnv$ref))
})
test_that("Immutable struct usable by reference and translated", {
juliaEval('struct TestImmutableStruct
x::Int
end')
jlRef <- juliaEval("TestImmutableStruct(1)")
expect_s3_class(jlRef, "JuliaStructProxy")
refEcho <- juliaEcho(jlRef)
expect_true(juliaCall("==", jlRef, refEcho))
expect_equal(refEcho$x, 1)
ref <- get("ref", jlRef)
expect_equal(juliaGet(jlRef)$x, 1)
expect_false(juliaLet("haskey(communicator.sharedheap, ref)", ref = ref,
communicator = JuliaConnectoR:::pkgLocal$communicator))
gc()
juliaEval("1")
})
test_that("Currying in juliaFun works", {
plus1 <- juliaFun("+", 1)
plus1(2)
expect_equal(juliaCall("map", plus1, c(1,2,3)), c(2,3,4))
})
test_that("Multidimensional object arrays work", {
juliaEval("struct MultiTestStruct
f::Float64
end")
content <- juliaEval("rand(4,5,6)")
testEcho(content)
x <- juliaLet("map(MultiTestStruct, c)", c = content)
testEcho(x)
expect_equivalent(dim(x), c(4,5,6))
expect_equivalent(juliaGet(x[1,1,1]), list(juliaGet(x[[1,1,1]])))
x[[1,2,3]] <- juliaEval("MultiTestStruct(17.0)")
expect_equal(x[[1,2,3]]$f, 17)
x[1,2,2] <- juliaEval("MultiTestStruct(19.0)")
expect_equal(x[[1,2,2]]$f, 19)
x[1,2,2] <- list(juliaEval("MultiTestStruct(19.0)"))
expect_equal(x[[1,2,2]]$f, 19)
expect_equal(dim(x[3:4, 4:5, 1]), c(2,2,1))
expect_equal(dim(x[3:4, as.integer(4:5), 1L]), c(2,2,1))
x[3:4, as.integer(4:5), 1L] <- juliaEval("MultiTestStruct(20.0)")
expect_equal(juliaGet(x[[3, 4, 1]])$f, 20)
expect_equal(juliaGet(x[[4, 5, 1]])$f, 20)
y <- juliaGet(x)
attr(y, "JLDIM") <- c(1L, 2L, 3L)
expect_error(juliaEcho(y), regexp = "Incorrect dimensions")
# Must also work with dimensions of zero
testEcho(juliaEval("Matrix{MultiTestStruct}(undef, 0, 0)"))
x <- juliaEval("Array{MultiTestStruct}(undef, 0, 0, 0)")
testEcho(x)
expect_equivalent(dim(x), c(0,0,0))
# Two dimensions
content <- juliaEval("rand(2,3)")
x <- juliaLet("map(MultiTestStruct, c)", c = content)
x[[1,2]] <- juliaEval("MultiTestStruct(3.0)")
expect_equal(x[[1,2]]$f, 3)
})
test_that("Arrays with undef entries are translated", {
juliaEval("mutable struct MutableTestStruct
f::Float64
end")
# all undefs
testEcho(juliaEval("Vector{MutableTestStruct}(undef, 3)"),
comparableInJulia = FALSE)
# undefs and values
juliaLet("x = Vector{MutableTestStruct}(undef, 3);
x[1] = MutableTestStruct(x1);
x[2] = MutableTestStruct(x2);
x", x1 = 1.0, x2 = 2.0)
testEcho(juliaEval("Vector{MutableTestStruct}(undef, 3)"),
comparableInJulia = FALSE)
# multidimensional arrays with undefs and values
x <- juliaLet("x = Array{MutableTestStruct}(undef, 2, 3, 4);
x[1,1,1] = MutableTestStruct(x1);
x[2,2,2] = MutableTestStruct(x2);
x", x1 = 1.0, x2 = 2.0)
testEcho(x, comparableInJulia = FALSE)
expect_equal(x[[1]]$f, 1)
})
test_that("Undefined strings are transferred as empty strings", {
expect_equal(juliaEval("Vector{String}(undef, 3)"), c("", "", ""))
})
test_that("String with null can be translated back to Julia", {
# einfacher string
jlStrWithNul <- "String([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x5a, 0x65, 0x72, 0x6f, 0x57])"
str <- juliaEval(jlStrWithNul)
expect_true(is.raw(str))
expect_equal(as.character(juliaCall("typeof", str)), "String")
expect_equal(as.character(juliaLet("String(x[1:5])", x = str)), "Hello")
# string array
strArr <- juliaLet("String[a,b]", a = str, b = str)
expect_true(is.list(strArr))
strArr <- juliaLet("String[a,b,c]", a = str, b = str, c = str)
expect_true(is.list(strArr))
expect_equal(juliaCall("typeof", strArr), juliaEval("Array{String,1}"))
})
# Test Let
test_that("Let: used like eval", {
output <- capture_output({expect(is.null(juliaLet("print(1)")), "Failed")})
expect_equal(output, "1")
})
test_that("Let: must error with no named argument", {expect_error(juliaLet("print(x)", 1), "")})
test_that("Let: basic echo", {expect(all(juliaLet("identity(x)", x=c(2, 3)) == c(2,3)), "Failed")})
test_that("Let: Simple example from documentation works", {
expect_equal(capture_output({juliaLet('println(x)', x = 1)}),
capture_output({juliaEval('let x = 1.0; println(x) end')}))
})
test_that("Let: arguments without names not accepted", {
expect_error(juliaLet("println(1)", 1), regexp = "names")
expect_error(juliaLet("println(1)", x=1, 1, y=2), regexp = "names")
})
#Test Pairs
test_that("Echo: Pairs", {
testEcho(juliaEval("(1 => 2.0)"))
testEcho(juliaEval("1 => 2.0 => 3.0"))
testEcho(juliaEval("[1 => 2.0, 2 => 3.0]"))
})
# Test Tuples
test_that("Echo: Tuples", {
x <- juliaEval("(1, 17, 18)")
expect_equal(x[[2]], 17)
expect_equal(juliaGet(x[1]), juliaGet(juliaEval("(1,)")))
expect_length(x, 3)
expect_length(x[1:2], 2)
testEcho(juliaEval("(1, 2.0)"))
testEcho(juliaEval("((1, 2.0), 3.0)"))
testEcho(juliaLet("collect(zip(x,y))", x = c(1L,2L, 3L), y = c(1,0,1)))
testEcho(juliaEval("(Ref(false), Ref(true))"))
testEcho(juliaEval("Tuple{}()"))
})
# Test Named Tuples
test_that("Echo: Named Tuples", {
namedTuple <- juliaLet("y=2*x; z = 3*u + 1; (x=y, y=z)", x=2, u=4)
expect_s3_class(namedTuple, "JuliaStructProxy")
expect_equal(namedTuple$y, 13)
expect_type(juliaGet(namedTuple), "list")
expect_equal(juliaGet(namedTuple)$y, 13)
testEcho(namedTuple)
})
# Test Module
test_that("Echo: Module", {
juliaEval("module TestModule end")
testEcho(juliaEval("TestModule"))
})
test_that("Echo: Symbol", {
x <- juliaEval(":asymbol")
expect_true(is.symbol(x))
expect_true(juliaCall("isa", as.symbol("s"), juliaExpr("Symbol")))
testEcho(x)
})
# Test Dictionary
test_that("Echo: Dictionary", {
d <- juliaEval("Dict(:bla => 1.0, :blup => 3.0)")
expect_equivalent(d[juliaExpr(":bla")][[1]], 1)
expect_equal(d[juliaExpr(":bla"), juliaExpr(":bla")], as.list(c(1, 1)))
expect_equal(d[[as.symbol("bla")]], 1)
expect_equal(d[as.symbol("blup")],list(3))
d[juliaExpr(":blup")] <- 4
expect_equal(d[[juliaExpr(":blup")]], 4)
d[[juliaExpr(":blup")]] <- 5
expect_equal(d[[juliaExpr(":blup")]], 5)
d2 <- juliaGet(d)
expect_setequal(d2[["keys"]], juliaGet(juliaEval("[:bla, :blup]")))
expect_setequal(d2[["values"]], list(1, 5))
d[juliaExpr(":hi"), juliaExpr(":hello")] <- c(15, 14)
expect_equal(d[juliaExpr(":bla"), juliaExpr(":hi")], as.list(c(1, 15)))
d[juliaExpr(":hi"), juliaExpr(":hello")] <- as.list(c(16, 17))
expect_equal(d[juliaExpr(":bla"), juliaExpr(":hello"), juliaExpr(":hi")], as.list(c(1, 17, 16)))
expect_length(d[juliaExpr(":bla"), juliaExpr(":hi"), juliaExpr(":hello"), juliaExpr(":hello")], 4)
d <- juliaLet("Dict(zip(x, y))", x = c("bla", "blup"), y = c(1,2))
expect_equal(d[["bla"]], 1)
expect_equivalent(d["blup"], list(2))
d2 <- juliaGet(d)
expect_setequal(d2[["keys"]], list("bla", "blup"))
expect_setequal(d2[["values"]], list(1, 2))
d2 <- juliaGet(d)
expect_setequal(d2[["keys"]], list("bla", "blup"))
expect_setequal(d2[["values"]], list(1,2))
d$bla <- 17
expect_equal(d$bla, 17)
d <- juliaLet("Dict(zip(x, y))", x = list("bla"), y = list(1))
expect_equal(length(juliaCall("keys", d)), 1)
testEcho(d)
d <- juliaLet("Dict(zip(x, y))", x = list(), y = list())
d2 <- juliaGet(d)
expect_length(d2[["keys"]], 0)
expect_length(d2[["values"]], 0)
testEcho(d)
})
# Test Set
test_that("Echo: Set", {
s1 <- juliaEval("Set([1; 2; 3; 4])")
s2 <- juliaEval("Set([1; 2])")
expect_length(setdiff(juliaGet(juliaEval("Set([1; 2; 3; 4])")),
c(1,2,3,4)), 0)
expect_length(setdiff(juliaGet(juliaLet("setdiff(s1, s2)", s1 = s1, s2 = s2)),
c(3,4)), 0)
testEcho(s1)
})
# Test types with bitstypes
test_that("Object with bitstype", {
uuidregex <- '^[a-f0-9]{8}-?[a-f0-9]{4}-?4[a-f0-9]{3}-?[89ab][a-f0-9]{3}-?[a-f0-9]{12}$'
UUIDs <- juliaImport("UUIDs")
expect_match(juliaCall("string", UUIDs$uuid4()), uuidregex)
})
test_that("Exotic objects handled gracefully", {
x <- quote(1 + 1)
expect_type(x, "language")
expect_warning({ y <- juliaEcho(list(x = x))},
regexp = "not translatable", all = FALSE)
expect_null(y$x)
})
# Test complex constructor with all kinds of types
test_that("Complex Julia object with different member type", {
juliaEval('struct TestTypeWithAllKindsOfStuff
n::Nothing
f16::Float16
f32::Float32
f64::Float64
b::Bool
i8::Int8
ui8::UInt8
i16::Int16
ui16::UInt16
i32::Int32
ui32::UInt32
ch::Char
i64::Int64
ui64::UInt64
i128::Int128
ui128::UInt128
f16vec::Vector{Float16}
f32vec::Vector{Float32}
f64vec::Vector{Float64}
bvec::Vector{Bool}
i8vec::Vector{Int8}
ui8vec::Vector{UInt8}
i16vec::Vector{Int16}
ui16vec::Vector{UInt16}
i32vec::Vector{Int32}
ui32vec::Vector{UInt32}
chvec::Vector{Char}
i64vec::Vector{Int64}
ui64vec::Vector{UInt64}
i128vec::Vector{Int128}
ui128vec::Vector{UInt128}
end')
juliaEval('function TestTypeWithAllKindsOfStuff()
TestTypeWithAllKindsOfStuff(
nothing,
rand(Float16), rand(Float32), rand(Float64),
rand(Bool),
rand(Int8), rand(UInt8),
rand(Int16), rand(UInt16),
rand(Int32), rand(UInt32),
rand(Char),
rand(Int64), rand(UInt64),
rand(Int128), rand(UInt128),
rand(Float16, 2), rand(Float32, 2), rand(Float64, 2),
rand(Bool, 2),
rand(Int8, 2), rand(UInt8, 2),
rand(Int16, 2), rand(UInt16, 2),
rand(Int32, 2), rand(UInt32, 2),
rand(Char, 2),
rand(Int64, 2), rand(UInt64, 2),
rand(Int128, 2), rand(UInt128, 2))
end')
testEcho(juliaEval("TestTypeWithAllKindsOfStuff()"))
})
test_that("Private inner constructor is forged", {
juliaEval('struct MyPrivateX
x::Int
function MyPrivateX()
new(5)
end
end')
p <- juliaEval("MyPrivateX()")
testEcho(p)
})
test_that("Imported modules are printed", {
# a stdlib module
Pkg <- juliaImport("Pkg")
expect_match(regexp = 'Julia module "Pkg": [0-9]+ function.*',
capture.output(print(Pkg))[1])
# a user defined module
module <- juliaEval('module ImportTestModule1
export f
f(x)=1
end')
itm1 <- juliaImport(module, all = FALSE)
expectedOutput <-
'Julia module "Main.ImportTestModule1": 1 function.*'
expect_match(capture.output(print(itm1))[1], regexp = expectedOutput)
itm1_2 <- juliaImport(".ImportTestModule1", all = FALSE)
expect_match(capture.output(print(itm1_2))[1], regexp = expectedOutput)
})
test_that("Empty module does not cause problems", {
juliaEval("module EmptyTestModule end")
expect_length(ls(juliaImport(".EmptyTestModule")), 2) # (eval and include)
juliaEval("module EmptyTestModule2 end")
expect_length(ls(juliaImport(".EmptyTestModule2", all = FALSE)), 0)
})
test_that("Errors are handled gracefully", {
expect_error(juliaCall("sum", c(1,2,3, "bla")))
expect_error(juliaCall("thisisnotarealfunction", 100, 4))
expect_error(juliaCall("RConnector.thisisnotarealfunction", "haha"))
expect_error(juliaCall("NotARealModule.thisisnotarealfunction", list(1,2,3)))
})
test_that("Vectors of objects can be accessed by index via proxy", {
x <- juliaEval("[ [1;2;3], [4;5;6], [7;8;9] ]")
expect_s3_class(x, "JuliaArrayProxy")
expect_equal(x[[1]][2], 2)
expect_equal(x[[2]][2], 5)
x[[1]][1] <- 17L
expect_equal(x[[1]][1], 17)
expect_equal(x[2:3][[1]][[1]], 4)
x[2:3][[1]][[1]] <- 18L
expect_equal(x[2:3][[1]][[1]], 18)
expect_equal(x[[1]][x[[2]] == 5], 2) # logical indexing
# juliaGet version must behave in the same way as the proxy version
y <- juliaGet(x)
expect_equal(x[[2]], y[[2]])
expect_equal(x[[1]][2], y[[1]][2])
expect_equal(x[[2]][2], y[[2]][2])
x[[1]][1] <- 17L
y[[1]][1] <- 17L
expect_equal(x[[1]][1], 17)
expect_equal(y[[1]][1], 17)
x[2:3][[1]][[1]] <- 18L
y <- juliaGet(x)
expect_equal(y[2:3][[1]][[1]], 18)
expect_equal(x[2:3][[1]][1], y[2:3][[1]][1])
expect_equal(x[[2]], y[[2]])
# Multiple dimensions
y <- juliaEval("map( x-> [1;x;3], rand(2,2,2))")
expect_equivalent(y[c(1,2)][[1]], y[[1]])
expect_equivalent(y[c(1,3)][[2]], y[[3]])
y[c(1,3)] <- juliaEval("[[17.0], [18.0]]")
expect_equivalent(juliaGet(y[1]), list(17))
expect_equivalent(juliaGet(y[3]), list(18))
expect_equivalent(juliaGet(y)[c(4,5)], juliaGet(y[c(4,5)]))
})
test_that("Callback functions", {
x <- juliaEcho(function() {})
expect_equal(typeof(x), "closure")
expect_equal(x, function() {})
outputenv <- new.env(parent = emptyenv())
outputenv$output <- c()
doOutput <- function(x) {
outputenv$output <- c(outputenv$output, x)
}
# Nested callback functions
juliaEval('struct TestStruct f::Function end')
juliaEval('function testNestedFun(ts::Vector{TestStruct}) map(t -> t.f(), ts) end')
t <- juliaCall("testNestedFun",
list(juliaCall("TestStruct", function() {doOutput("a")}),
juliaCall("TestStruct", function() {doOutput("b")}),
juliaCall("TestStruct", function() {doOutput("c")})))
expect_equal(outputenv$output, c("a", "b", "c"))
# as named arguments
outputenv$output <- c()
juliaEval('function testNestedFunNamed(;ts::Vector{TestStruct}) map(t -> t.f(), ts) end')
t <- juliaCall("testNestedFunNamed", ts =
list(juliaCall("TestStruct", function() {doOutput("x")}),
juliaCall("TestStruct", function() {doOutput("y")})))
expect_equal(outputenv$output, c("x", "y"))
# test repeated call of nested functions
outputenv$output <- c()
juliaEval('function testNestedFun2(ts::Vector{TestStruct}) for i = 1:2 map(t -> t.f(), ts) end end')
t <- juliaCall("testNestedFun2",
list(juliaCall("TestStruct", function() {doOutput(1)}),
juliaCall("TestStruct", function() {doOutput(2)})))
expect_equal(outputenv$output, c(1,2,1,2))
# Test multiple arguments
outputenv$output <- c()
juliaEval('function testNestedFunArgs(ts::Vector{TestStruct}, args...) map(t -> t.f(args...), ts) end')
t <- juliaCall("testNestedFunArgs",
list(juliaCall("TestStruct", function(x, y, z) {doOutput(x)}),
juliaCall("TestStruct", function(x, y, z) {doOutput(z); return(c(5,6,7))})),
1, 2, 3)
expect_equal(outputenv$output, c(1,3))
outputenv$output <- c()
juliaEval('function testNestedAndUnnested(f::Function, ts::Vector{TestStruct}, args...)
testNestedFunArgs(ts, args...)
f(args...)
end')
t <- juliaCall("testNestedAndUnnested",
function(x, y) {doOutput(x)},
list(juliaCall("TestStruct", function(x,y) {doOutput(x)}),
juliaCall("TestStruct", function(x, y) {doOutput(y); return(list(5,6,7))})),
17,18)
expect_equal(outputenv$output, c(17, 18, 17))
})
test_that("Callback function calling Julia erroring in Julia", {
f <- juliaEval('(x) -> error("Error")')
g <- function(x) {cat("OK"); f(x); print("NotOK"); x}
output <- capture.output(expect_error(juliaCall("map", g, c(1,2,3))))
expect_equal(output, "OK")
})
test_that("Builtin functions can be used as callback functions", {
expect_equal(juliaCall("map", sqrt, c(1,4,9)), c(1,2,3))
})
test_that("Callback function might have error in R", {
fOK <- function(x) { return(x+1) }
fnotOK <- function(x) {
if (x == 2) {
stop("No even numbers please")
} else {
return(1)
}
}
expect_equal(juliaCall("map", fOK, c(1,2,3)), c(2,3,4))
expect_error(juliaCall("map", fnotOK, c(1,2,3)), class = "error")
})
test_that("Julia functions as members are transferred and usable in R", {
op1 <- juliaFun("+")
juliaEval('struct FunTestStruct f::Function end')
funTestStruct <- juliaCall("FunTestStruct", op1)
expect_equal(funTestStruct$f(1,2), 3)
})
test_that("juliaCall checks given name before running", {
expect_error(juliaCall())
expect_error(juliaCall(1))
expect_error(juliaCall(c("bla", "bla")))
})
test_that("juliaCall can handle keyword arguments with key \"name\"", {
Pkg <- juliaImport("Pkg")
# indirect via created function object
expect_equal(Pkg$PackageSpec(name = "RData", version = "v0.10")$name,
"RData")
# direct call of juliaCall
pkgspec <- juliaCall("Pkg.PackageSpec", name = "RData", version = "0.10")
expect_equal(pkgspec$name, "RData")
})
test_that("Documentation example of juliaFun", {
juliaSqrt <- juliaFun("sqrt")
expect_equal(juliaSqrt(2), sqrt(2))
expect_equal(juliaCall("map", juliaSqrt, c(1,4,9)), c(1,2,3))
})
test_that("Constructors stand for their types", {
juliaEval('module TestTypeModule
export TestType
struct TestType end
end')
TestTypeModule <- juliaImport(".TestTypeModule")
expect_equal(as.character(juliaCall("typeof", TestTypeModule$TestType)),
"DataType")
})
test_that("Parametric types are imported", {
juliaEval("module ParametricTypeTestModule
export MyParametricType
struct MyParametricType{T}
i::T