forked from dotnet/java-interop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaPrimitiveArrayContract.cs
More file actions
92 lines (81 loc) · 2.7 KB
/
JavaPrimitiveArrayContract.cs
File metadata and controls
92 lines (81 loc) · 2.7 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using Java.Interop;
using NUnit.Framework;
namespace Java.InteropTests
{
public abstract class JavaPrimitiveArrayContract<TArray, TElement> : JavaArrayContract<TElement>
where TArray : JavaPrimitiveArray<TElement>
{
protected override TElement CreateValueA ()
{
return FromInt32 ((int) 'A');
}
protected override TElement CreateValueB ()
{
return FromInt32 ((int) 'B');
}
protected abstract ICollection<TElement> CreateCollection (IList<TElement> values);
protected abstract ICollection<TElement> CreateCollection (int length);
protected TElement FromInt32 (int value)
{
return (TElement) Convert.ChangeType (value, typeof(TElement));
}
[Test]
public void Constructor_Exceptions ()
{
Assert.Throws<ArgumentNullException>(() => CreateCollection ((IEnumerable<TElement>) null));
Assert.Throws<ArgumentNullException>(() => CreateCollection ((IList<TElement>) null));
Assert.Throws<ArgumentException>(() => CreateCollection (-1));
}
[Test]
public void GetElements ()
{
var a = (TArray) CreateCollection (new[]{FromInt32 ('A')});
JniArrayElements e;
using (e = a.GetElements ()) {
if (e == null) // OOM?
return;
Assert.IsTrue (e.Elements != IntPtr.Zero);
// Multi-dispose is supported.
e.Dispose ();
}
Assert.Throws<ObjectDisposedException> (() => e.Release (JniReleaseArrayElementsMode.Abort));
Assert.Throws<ObjectDisposedException> (() => {
#pragma warning disable 0219
var _ = e.Elements;
#pragma warning restore 0219
});
a.Dispose ();
}
// TODO: http://developer.android.com/training/articles/perf-jni.html#arrays
// "Also, if the Get call fails, you must ensure that your code doesn't
// try to Release a NULL pointer later."
// This implies that JNIEnv::Get<Type>ArrayElements() can return NULL; how/when?
// Theory: this happens if the array is empty (kinda like what C# `fixed` does?).
// Try to test this.
// (Alas, on OpenJDK JNIEnv::Get<Type>ArrayElements() returns a non-NULL pointer
// when the array is empty, so we'll need to run this on Android.)
[Test]
public void GetElements_EmptyArray ()
{
var a = (TArray) CreateCollection (new TElement[0]);
JniArrayElements e;
using (e = a.GetElements ()) {
if (e == null)
return;
Assert.IsTrue (e.Elements != IntPtr.Zero);
// Multi-dispose is supported.
e.Dispose ();
}
Assert.Throws<ObjectDisposedException> (() => e.Release (JniReleaseArrayElementsMode.Abort));
Assert.Throws<ObjectDisposedException> (() => {
#pragma warning disable 0219
var _ = e.Elements;
#pragma warning restore 0219
});
a.Dispose ();
}
}
}