[m-rev.] diff: fix an abort in the Java implementation of array.copy/2
Julien Fischer
jfischer at opturion.com
Fri Nov 9 11:55:05 AEDT 2018
Fix an abort in the Java implementation of array.copy/2.
library/array.m:
Handle all Java primitive types in the implementation of
array.copy/2.
tests/hard_coded/Mmakefile:
tests/hard_coded/array_copy.{m,exp}:
Add a test case.
Julien.
diff --git a/library/array.m b/library/array.m
index a4dea56..f0a8c4d 100644
--- a/library/array.m
+++ b/library/array.m
@@ -2218,9 +2218,21 @@ copy(A1) = A2 :-
} else if (Array0 instanceof double[]) {
Size = ((double[]) Array0).length;
Array = new double[Size];
+ } else if (Array0 instanceof byte[]) {
+ Size = ((byte[]) Array0).length;
+ Array = new byte[Size];
+ } else if (Array0 instanceof short[]) {
+ Size = ((short[]) Array0).length;
+ Array = new short[Size];
+ } else if (Array0 instanceof long[]) {
+ Size = ((long[]) Array0).length;
+ Array = new long[Size];
} else if (Array0 instanceof char[]) {
Size = ((char[]) Array0).length;
Array = new char[Size];
+ } else if (Array0 instanceof float[]) {
+ Size = ((float[]) Array0).length;
+ Array = new float[Size];
} else if (Array0 instanceof boolean[]) {
Size = ((boolean[]) Array0).length;
Array = new boolean[Size];
diff --git a/tests/hard_coded/Mmakefile b/tests/hard_coded/Mmakefile
index 3df1bcb..9fc327d 100644
--- a/tests/hard_coded/Mmakefile
+++ b/tests/hard_coded/Mmakefile
@@ -14,6 +14,7 @@ ORDINARY_PROGS = \
address_of_builtins \
agg \
array_all_tf \
+ array_copy \
array_gen \
array_sort \
array_test \
diff --git a/tests/hard_coded/array_copy.exp b/tests/hard_coded/array_copy.exp
index e69de29..31edd1c 100644
--- a/tests/hard_coded/array_copy.exp
+++ b/tests/hard_coded/array_copy.exp
@@ -0,0 +1,42 @@
+=====================
+Array0 = array(['a', 'b', 'c'])
+Array = array(['a', 'b', 'c'])
+=====================
+Array0 = array([1, 2, 3])
+Array = array([1, 2, 3])
+=====================
+Array0 = array([1u, 2u, 3u])
+Array = array([1u, 2u, 3u])
+=====================
+Array0 = array([1i8, 2i8, 3i8])
+Array = array([1i8, 2i8, 3i8])
+=====================
+Array0 = array([1u8, 2u8, 3u8])
+Array = array([1u8, 2u8, 3u8])
+=====================
+Array0 = array([1i16, 2i16, 3i16])
+Array = array([1i16, 2i16, 3i16])
+=====================
+Array0 = array([1u16, 2u16, 3u16])
+Array = array([1u16, 2u16, 3u16])
+=====================
+Array0 = array([1i32, 2i32, 3i32])
+Array = array([1i32, 2i32, 3i32])
+=====================
+Array0 = array([1u32, 2u32, 3u32])
+Array = array([1u32, 2u32, 3u32])
+=====================
+Array0 = array([1i64, 2i64, 3i64])
+Array = array([1i64, 2i64, 3i64])
+=====================
+Array0 = array([1u64, 2u64, 3u64])
+Array = array([1u64, 2u64, 3u64])
+=====================
+Array0 = array(["foo", "bar", "baaz"])
+Array = array(["foo", "bar", "baaz"])
+=====================
+Array0 = array([apple, lemon, orange])
+Array = array([apple, lemon, orange])
+=====================
+Array0 = array([[1], [2, 2], [3, 3, 3]])
+Array = array([[1], [2, 2], [3, 3, 3]])
diff --git a/tests/hard_coded/array_copy.m b/tests/hard_coded/array_copy.m
index e69de29..26d0ec1 100644
--- a/tests/hard_coded/array_copy.m
+++ b/tests/hard_coded/array_copy.m
@@ -0,0 +1,52 @@
+%---------------------------------------------------------------------------%
+% vim: ft=mercury ts=4 sw=4 et
+%---------------------------------------------------------------------------%
+%
+% Test array.copy/2.
+%
+
+:- module array_copy.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+%---------------------------------------------------------------------------%
+%---------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module array.
+:- import_module exception.
+:- import_module list.
+
+:- type fruit ---> apple ; lemon ; orange.
+
+main(!IO) :-
+ test_copy(['a', 'b', 'c'], !IO),
+ test_copy([1, 2, 3], !IO),
+ test_copy([1u, 2u, 3u], !IO),
+ test_copy([1i8, 2i8, 3i8], !IO),
+ test_copy([1u8, 2u8, 3u8], !IO),
+ test_copy([1i16, 2i16, 3i16], !IO),
+ test_copy([1u16, 2u16, 3u16], !IO),
+ test_copy([1i32, 2i32, 3i32], !IO),
+ test_copy([1u32, 2u32, 3u32], !IO),
+ test_copy([1i64, 2i64, 3i64], !IO),
+ test_copy([1u64, 2u64, 3u64], !IO),
+ test_copy(["foo", "bar", "baaz"], !IO),
+ test_copy([apple, lemon, orange], !IO),
+ test_copy([[1], [2, 2], [3, 3,3]], !IO).
+
+:- pred test_copy(list(T)::in, io::di, io::uo) is det.
+
+test_copy(Elems, !IO) :-
+ io.write_string("=====================\n", !IO),
+ array.from_list(Elems, Array0),
+ io.write_string("Array0 = ", !IO),
+ io.write_line(Array0, !IO),
+ array.copy(Array0, Array),
+ io.write_string("Array = ", !IO),
+ io.write(Array, !IO),
+ io.nl(!IO).
More information about the reviews
mailing list