summaryrefslogtreecommitdiff
path: root/tests/basic-types/arrays.vala
blob: 0a94a6acbf8e197237d237fa575c201c4616d51e (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
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
[CCode (array_length = false, array_null_terminated = true)]
int[] foo;

[CCode (array_length = false)]
int[] bar;

void test_integer_array () {
	// declaration and initialization
	int[] a = { 42 };
	assert (a.length == 1);
	assert (a[0] == 42);

	// assignment
	a = { 42, 23 };
	assert (a.length == 2);
	assert (a[0] == 42);
	assert (a[1] == 23);

	// access
	int[] b = a;
	assert (b.length == 2);
	assert (b[0] == 42);
	assert (b[1] == 23);

	// +
	a += 11;
	assert (a.length == 3);
	assert (a[0] == 42);
	assert (a[1] == 23);
	assert (a[2] == 11);
	assert (b.length == 2);
	assert (b[0] == 42);
	assert (b[1] == 23);

	// slices
	int[] c = a[1:3];
	assert (c.length == 2);
	assert (c[0] == 23);
	assert (c[1] == 11);

	int[]? c0 = a[0:0];
	assert (c0 == null);
	assert (c0.length == 0);

	int[] c1 = a[1:];
	assert (c1.length == 2);
	assert (c1[0] == 23);
	assert (c1[1] == 11);

	int[] c2 = a[:2];
	assert (c2.length == 2);
	assert (c2[0] == 42);
	assert (c2[1] == 23);

	int[] c3 = a[:];
	assert (c3.length == 3);
	assert (c3[0] == 42);
	assert (c3[1] == 23);
	assert (c3[2] == 11);

	// in expressions
	assert (23 in a);
	assert (!(-1 in a));

	// nullable elements
	int?[] d = new int?[2];
	d[0] = 10;
	d[1] = null;
	assert (d[0] == 10);
	assert (d[1] == null);

	// element assignment
	int[] e = { 13, 47 };
	e[0] = 96;
	assert (e[0] == 96);
	e[0] /= 24;
	assert (e[0] == 4);
	e[0] += 2;
	assert (e[0] == 6);
	e[0] *= 4;
	assert (e[0] == 24);
	e[0] -= 23;
	assert (e[0] == 1);
}

void test_string_array () {
	// declaration and initialization
	string[] a = { "hello" };
	assert (a.length == 1);
	assert (a[0] == "hello");

	// assignment
	a = { "hello", "world" };
	assert (a.length == 2);
	assert (a[0] == "hello");
	assert (a[1] == "world");

	// access
	string[] b = a;
	assert (b.length == 2);
	assert (b[0] == "hello");
	assert (b[1] == "world");
}

int[] pass_helper (int[] a, out int[] b) {
	b = a;
	return { 42, 23 };
}

void test_array_pass () {
	int[] a, b;
	a = pass_helper ({ 42 }, out b);
	assert (a.length == 2);
	assert (a[0] == 42);
	assert (a[1] == 23);
	assert (b.length == 1);
	assert (b[0] == 42);
}

const int FOO = 2;
void test_static_array () {
	int a[2];
	assert (a.length == 2);
	a[1] = 23;
	assert (a[1] == 23);
	a = { 23, 34 };
	assert (a[0] == 23 && a[1] == 34);

	int b[FOO * 1 << 3];
	assert (b.length == FOO * 1 << 3);
}

void test_reference_transfer () {
	var baz = (owned) foo;
	baz = (owned) bar;

	var data = new string[]{"foo"};
	var data2 = (owned) data;
	assert (data.length == 0);
}

void test_length_assignment () {
	var a = new int[10];
	var b = new int[20,30];
	a.length = 8;
	b.length[0] = 5;
	assert (a.length == 8);
	assert (b.length[0] == 5);
}

void test_inline_array () {
	const int a[] = { 1, 2, 3 };
	assert (1 in a);
}

int[,,] nd_array_pass (int[,,] a, out int[,,] b) {
	assert (a.length[0] == 2);
	assert (a.length[1] == 2);
	assert (a.length[2] == 2);
	assert (a[1,1,0] == 7);

	b = a;
	return a;
}

void test_nd_array () {
	int[,,] a = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};
	assert (a[1,0,1] == 6);

	int[,,] b, c;
	c = nd_array_pass (a, out b);
	assert (b.length[0] == 2);
	assert (b.length[1] == 2);
	assert (b.length[2] == 2);
	assert (b[0,1,0] == 3);
	assert (c.length[0] == 2);
	assert (c.length[1] == 2);
	assert (c.length[2] == 2);
	assert (c[0,1,1] == 4);

	string[,,] s = {{{"a", "b", "c"}, {"d", "e", "f"}}};
	assert (s[0,0,2] == "c");
}

[CCode (has_target = false)]
delegate int SimpleFunc ();
SimpleFunc[] simple_delegates;

int simple_func () {
	return 0;
}

void test_delegate_array () {
	SimpleFunc[] a = {};
	a = (owned) simple_delegates;
	a += (SimpleFunc) simple_func;
	assert (a.length == 1);
	assert (simple_func in a);
}

void test_void_array () {
	void*[] a = {};
	a += (void*) null;
	a += (void*) null;
	assert (a.length == 2);
	assert ((void*) null in a);
}

void test_explicit_copying () {
	int[] a0 = { 1, 2, 3};
	var a1 = a0.copy ();
	assert (a1.length == 3);
	assert (a0[1] == a1[1]);
}

void test_array_with_primitives_move () {
	int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	assert (a[4] == 5);
	a.move (4, 0, 5);
	assert (a[4] == 9);
	assert (a[8] == 0);
}

struct TestMoveCall {
	int idx;

	public TestMoveCall (int idx) { this.idx = idx; }
}

void test_array_with_struct_move (int src, int dest, int count, int expected_destructor_calls)
{
	const int arr_size = 5;
	TestMoveCall[] arr = new TestMoveCall[arr_size];
        for(int i=0; i<arr_size; i++)
	{
            arr[i] = TestMoveCall (i);
	}

	TestMoveCall testObj = arr[src];
        arr.move (src, dest, count);
	assert(arr[dest].idx == testObj.idx);
}

void test_array_resize () {
	int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	assert (a[a.length - 1] == 9);
	a.resize (5);
	assert (a[a.length - 1] == 5);
}

struct Foo {
	unowned string array[2];
	int bar;
}

const Foo[] FOO_ARRAY_CONST = {
	{ { "foo", "bar" }, 42 },
};

struct Bar {
	public int bar;
}

struct Manam {
	Bar array[1024];
	Bar manam;
}

void test_struct_array () {
	assert (FOO_ARRAY_CONST[0].bar == 42);

	Bar b = { 4711 };
	var bar = new Bar[23];
	bar[7] = b;
	assert (b in bar);

	Manam? manam = {};
	Manam? manam_copy = manam;
}

void give_fixed_array (out int i[3]) {
	//FIXME	i = { 3, 4, 5 };
}

void take_fixed_array (int i[3]) {
	assert (i.length == 3);
	assert (i[1] == 2);
}

void change_fixed_array (ref int i[3]) {
	assert (i.length == 3);
	assert (i[1] == 7);
	i[1] = 9;
}

void test_fixed_array () {
	int[] i = { 1, 2, 3, 4 };
	assert (i.length == 4);
	take_fixed_array (i);

	int[] k = { 6, 7, 8 };
	change_fixed_array (ref k);
	assert (k.length == 3);
	assert (k[1] == 9);

	int[] j;
	//give_fixed_array (out j);
	//assert (j.length == 3);
	//assert (j[1] == 4);
}

void main () {
	test_integer_array ();
	test_string_array ();
	test_array_pass ();
	test_static_array ();
	test_reference_transfer ();
	test_length_assignment ();
	test_inline_array ();
	test_nd_array ();
	test_delegate_array ();
	test_void_array ();
	test_explicit_copying ();
	test_array_with_primitives_move ();
	//test_array_with_struct_move(0, 2, 3, 1);
	//test_array_with_struct_move(2, 0, 3, 2);
	//test_array_with_struct_move(0, 3, 1, 1);
	test_array_resize ();
	test_struct_array ();
	test_fixed_array ();
}