forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSharedArrayBufferTests.cs
More file actions
157 lines (127 loc) · 5.15 KB
/
SharedArrayBufferTests.cs
File metadata and controls
157 lines (127 loc) · 5.15 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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using Xunit;
namespace System.Runtime.InteropServices.JavaScript.Tests
{
[ActiveIssue("https://github.com/dotnet/runtime/issues/61945", TestPlatforms.Browser)]
public static class SharedArrayBufferTests
{
private static Function _objectPrototype;
public static IEnumerable<object[]> Object_Prototype()
{
_objectPrototype ??= new Function("return Object.prototype.toString;");
yield return new object[] { _objectPrototype.Call() };
}
[Theory]
[MemberData(nameof(Object_Prototype))]
public static void SharedArrayBuffer_NonZeroLength(Function objectPrototype)
{
SharedArrayBuffer d = new SharedArrayBuffer(50);
Assert.Equal("[object SharedArrayBuffer]", objectPrototype.Call(d));
Assert.Equal(50, d.ByteLength);
}
[Fact]
public static void SharedArrayBufferSlice()
{
SharedArrayBuffer d = new SharedArrayBuffer(50);
Assert.Equal(50, d.Slice().ByteLength);
}
[Fact]
public static void SharedArrayBuffer_Slice_BeginEndForFullArray()
{
SharedArrayBuffer d = new SharedArrayBuffer(50);
Assert.Equal(50, d.Slice(0, 50).ByteLength);
}
[Fact]
public static void SharedArrayBuffer_Slice_BeginZero()
{
SharedArrayBuffer d = new SharedArrayBuffer(50);
Assert.Equal(50, d.Slice(0).ByteLength);
}
[Fact]
public static void SharedArrayBuffer_Slice_BeginNegative()
{
SharedArrayBuffer d = new SharedArrayBuffer(50);
Assert.Equal(3, d.Slice(-3).ByteLength);
}
[Fact]
public static void SharedArrayBuffer_Slice_BeginEndSubset()
{
SharedArrayBuffer d = new SharedArrayBuffer(50);
Assert.Equal(3, d.Slice(1, 4).ByteLength);
}
[Fact]
public static void SharedArrayBufferSliceAndDice()
{
// create a SharedArrayBuffer with a size in bytes
SharedArrayBuffer buffer = new SharedArrayBuffer(16);
Int32Array int32View = new Int32Array(buffer); // create view
// produces Int32Array [0, 0, 0, 0]
int32View[1] = 42;
Assert.Equal(4, int32View.Length);
Assert.Equal(42, int32View[1]);
Int32Array sliced = new Int32Array(buffer.Slice(4,12));
// expected output: Int32Array [42, 0]
Assert.Equal(2, sliced.Length);
Assert.Equal(42, sliced[0]);
Assert.Equal(0, sliced[1]);
}
[Fact]
public static void SharedArrayBufferSliceAndDiceAndUseThroughSpan()
{
// create a SharedArrayBuffer with a size in bytes
SharedArrayBuffer buffer = new SharedArrayBuffer(16);
Int32Array int32View = new Int32Array(buffer); // create view
// produces Int32Array [0, 0, 0, 0]
int32View[1] = 42;
Assert.Equal(4, int32View.Length);
Assert.Equal(42, int32View[1]);
Int32Array sliced = new Int32Array(buffer.Slice(4,12));
// expected output: Int32Array [42, 0]
Span<int> nativeArray = sliced;
int sum = 0;
for (int i = 0; i < nativeArray.Length; i++)
{
sum += nativeArray[i];
}
Assert.Equal(42, sum);
}
[Theory]
[MemberData(nameof(GetTestData), 16)]
public static void SharedArrayBufferSliceAndDice3_Subset(SharedArrayBuffer buffer)
{
Int32Array sliced = new Int32Array(buffer.Slice(4,12));
Assert.Equal(2, sliced.Length);
Assert.Equal(42, sliced[0]);
Assert.Equal(12, sliced[1]);
}
[Theory]
[MemberData(nameof(GetTestData), 16)]
public static void SharedArrayBufferSliceAndDice3_SubsetFromTheBack(SharedArrayBuffer buffer)
{
Int32Array sliced = new Int32Array(buffer.Slice(-4));
Assert.Equal(1, sliced.Length);
Assert.Equal(13, sliced[0]);
}
[Theory]
[MemberData(nameof(GetTestData), 16)]
public static void SharedArrayBufferSliceAndDice3_SubsetFromTheBackWithEnd(SharedArrayBuffer buffer)
{
Int32Array sliced = new Int32Array(buffer.Slice(-12, -4));
Assert.Equal(2, sliced.Length);
Assert.Equal(42, sliced[0]);
Assert.Equal(12, sliced[1]);
}
public static TheoryData<SharedArrayBuffer> GetTestData(int length)
{
// create a SharedArrayBuffer with a size in bytes
SharedArrayBuffer buffer = new SharedArrayBuffer(length);
Int32Array int32View = new Int32Array(buffer); // create view
for (int i = 0; i < int32View.Length; i ++)
int32View[i] = i + 10;
int32View[1] = 42;
return new TheoryData<SharedArrayBuffer> { buffer };
}
}
}