Debian Codegraph @

terps/tads/tads3/test/data/vector.t

raw
1 #include "tads.h"
2 #include "t3.h"
3 #include "vector.h"
4
5 main(args)
6 {
7 local v;
8 local x;
9 local v2;
10
11 v = new Vector(10);
12 x = v;
13
14 "initially empty:\n<<sayVector(v)>>";
15
16 v += 1;
17 v += 2;
18 v += 3;
19 "after adding new members:\n<<sayVector(v)>>";
20
21 v[1] += 30;
22 v[2] += 20;
23 v[3] += 10;
24 "after incrementing members:\n<<sayVector(v)>>";
25
26 "x should not be modified:\n<<sayVector(x)>>";
27
28 v -= 22;
29 "after subtracting 22:\n<<sayVector(v)>>";
30
31 v += [50, 60, 70];
32 "after adding list:\n<<sayVector(v)>>";
33
34 v -= [31, 50, 70];
35 "after subtracting list:\n<<sayVector(v)>>";
36
37 v.insertAt(1, 'a', 'b', 'c');
38 "after inserting at element 1:\n<<sayVector(v)>>";
39
40 v.insertAt(2, 'X', 'Y');
41 "after inserting at element 2:\n<<sayVector(v)>>";
42
43 v.removeElementAt(3);
44 "after removing element 3:\n<<sayVector(v)>>";
45
46 v.removeRange(2, 4);
47 "after removing elements 2-4:\n<<sayVector(v)>>";
48
49 v.prepend(999);
50 "after prepending 999:\n<<sayVector(v)>>";
51
52 v2 = new Vector(5);
53 v2 += 21;
54 v2 += 22;
55 v2 += 23;
56
57 v += v2;
58 "after adding vector:\n<<sayVector(v)>>";
59
60 v2 -= 22;
61 v2 -= 23;
62 v2 += 'a';
63 v -= v2;
64 "v2:\n<<sayVector(v2)>>";
65 "after subtracting vector v2:\n<<sayVector(v)>>";
66
67 v2.append('b');
68 "v2 after append:\n<<sayVector(v2)>>";
69
70 v2.appendAll(['c', 'd', 'e']);
71 "v2 after appendAll:\n<<sayVector(v2)>>";
72
73 "\b";
74 v = new Vector(5, [1, 2]);
75 v += 3;
76 x = [4, 5, 6];
77 x += v;
78 "list + vector:\n<<sayVector(x)>>";
79
80 v = new Vector(5, [8]);
81 v += [2, 4, 6];
82 x -= v;
83 "list - vector:\n<<sayVector(x)>>";
84
85 "\b";
86 v = Vector.generate({i: i*10}, 10);
87 "Vector.generate multiples of 10:\n<<sayVector(v)>>";
88
89 "\b";
90 v.splice(5, 2);
91 "splice(5,2): <<sayVectorI(v)>>\n";
92
93 v.splice(-2, 0, 'a', 'b', 'c');
94 "splice(-2,0,a,b,c): <<sayVectorI(v)>>\n";
95
96 v.splice(0, 0, 'd', 'e', 'f');
97 "splice(0,0,d,e,f): <<sayVectorI(v)>>\n";
98
99 v.splice(-2, 9, 'x');
100 "splice(-2,9,x): <<sayVectorI(v)>>\n";
101
102 v.splice(1, 0, 'xxx', 'yyy');
103 "splice(1,0,xxx,yyy): <<sayVectorI(v)>>\n";
104
105 "\b";
106 "toList(-3): <<sayVectorI(v.toList(-3))>>\n";
107
108 x = Vector.generate({i: i*11}, 10);
109 x.copyFrom(v, -3, -5, 3);
110 "copyFrom(-3, -5, 3): <<sayVectorI(x)>>\n";
111
112 x.fillValue('fill', -6, 3);
113 "fillValue(fill, -6, 3): <<sayVectorI(x)>>\n";
114
115 x.insertAt(-2, 'one', 'two');
116 "insertAt(-2, one, two): <<sayVectorI(x)>>\n";
117
118 x.removeElementAt(-3);
119 "removeElementAt(-3): <<sayVectorI(x)>>\n";
120
121 x.removeRange(-7, -5);
122 "removeRange(-7, -5): <<sayVectorI(x)>>\n";
123 }
124
125 sayVector(v)
126 {
127 for (local i = 1, local cnt = v.length() ; i <= cnt ; ++i)
128 "\t[<<i>>] <<v[i]>>\n";
129 }
130
131 sayVectorI(v)
132 {
133 "[";
134 for (local i in 1..v.length())
135 "<<if i > 1>>, <<end>><<v[i]>>";
136 "]";
137 }