-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patharraytest.nt
98 lines (90 loc) · 2.22 KB
/
arraytest.nt
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
module arraytest;
macro import std.macro.assert;
void main() {
mut int[] arr;
assert(arr.length == 0);
// arr = [2, 3];
arr = new int[](0) ~ 2 ~ 3;
assert(arr.length == 2);
assert(arr[1] == 3);
arr = arr ~ 4; // [2, 3, 4]
assert(arr.length == 3);
assert(arr[2] == 4);
arr = arr ~ (new int[](0) ~ 5); // [2, 3, 4, 5]
assert(arr.length == 4);
assert(arr[3] == 5);
assert(arr == [2, 3, 4, 5]);
assert(arr[$ - 1] == 5);
assert(arr[0 .. $ - 1] == [2, 3, 4]);
assert(arr[1 .. 3] == [3, 4]);
assert(arr.ptr[1 .. 2] == arr[1 .. 2]);
assert("Hello World"["Hello ".length .. "Hello World".length] == "World");
int[] arr = [];
assert(arr == []);
assert(arr == null);
{
mut int count;
int[] test() { count += 1; return [1, 2]; }
assert(test()[$ - 1] == 2);
assert(count == 1);
}
doublingTestArrElem;
doublingTestArrArr;
appendLoopTest;
mutateDirectlyTest;
mutateAliasTest;
castTest;
// polysemous array literal
float[] arr = [2, 3];
}
void doublingTestArrElem() {
mut int[] a = [1, 2, 3, 4];
a ~= 5;
int[] a1 = a ~ 6;
int[] a2 = a ~ 7;
assert(a1 == [1, 2, 3, 4, 5, 6]);
assert(a2 == [1, 2, 3, 4, 5, 7]);
assert(a1.ptr is a.ptr);
assert(a2.ptr !is a.ptr);
}
void doublingTestArrArr() {
mut int[] a = [1, 2, 3, 4];
a ~= [5];
int[] a1 = a ~ [6];
int[] a2 = a ~ [7];
assert(a1 == [1, 2, 3, 4, 5, 6]);
assert(a2 == [1, 2, 3, 4, 5, 7]);
assert(a1.ptr is a.ptr);
assert(a2.ptr !is a.ptr);
}
void appendLoopTest() {
mut size_t[] ia;
for (i in 0 .. 100) ia ~= i;
for (i in 0 .. 100) assert(ia[i] == i);
}
void mutateDirectlyTest() {
mut int mut[] ia;
ia ~= 4;
ia[0] = 5;
}
void mutateAliasTest() {
auto ia = new int mut[](2);
alias first = ia[0];
first = 5;
int i = 1;
alias second = ia[i];
second = 6;
assert(ia == [5, 6]);
mut int[int] ih;
alias third = ih[3];
third = 3;
assert(ih[3] == 3);
}
void castTest() {
char[] foo = "A";
ubyte[] bar = cast(ubyte[]) foo;
assert(bar == [cast(ubyte) 65]);
char[] baz = "ABCD";
int[] ble = cast(int[]) baz;
assert(ble == [1145258561]);
}