raz / test / run / VecEdit.raz

52 lines · 1.3 KiB · raw · history · last change 947d70f3

1module VecEdit;
2(* Vec's insert and remove: at the start, the middle and the end, n of 0,
3 growth past the first block, and removing everything. *)
4import Fmt, Heap, Rt, Ints := Vec(INT);
5
6(* Says the elements of v on one line. *)
7proc show(const v: Ints.Vec);
8var s: []INT; i: INT;
9begin
10 s := Ints.all(v);
11 for i := 0 to LEN(s) - 1 do Fmt.print("{} ", [Fmt.int(s[i])]); end;
12 Fmt.print("({})\n", [Fmt.int(v.len)]);
13end;
14
15(* Fills v with 1 to n. *)
16proc fill(var v: Ints.Vec; n: INT);
17var i: INT;
18begin
19 Ints.reset(v);
20 for i := 1 to n do Ints.push(v, i); end;
21end;
22
23proc main*;
24var v: Ints.Vec; i: INT; s: []INT;
25begin
26 Ints.init(v, Heap.heap);
27 fill(v, 5);
28 Ints.insert(v, 0, 2); show(v);
29 Ints.insert(v, 7, 1); show(v);
30 Ints.insert(v, 3, 2); show(v);
31 Ints.insert(v, 3, 0); show(v);
32 s := Ints.all(v);
33 s[3] := 8; s[4] := 9;
34 show(v);
35 Ints.remove(v, 0, 2); show(v);
36 Ints.remove(v, 1, 3); show(v);
37 Ints.remove(v, v.len - 1, 1); show(v);
38 Ints.remove(v, 1, 0); show(v);
39 Ints.remove(v, 0, v.len); show(v);
40 Ints.insert(v, 0, 3); show(v);
41 fill(v, 16);
42 Ints.insert(v, 8, 100);
43 s := Ints.all(v);
44 if (v.len # 116) or (s[7] # 8) or (s[8] # 0) or (s[107] # 0) or (s[108] # 9) or (s[115] # 16) then
45 Rt.stop("VecEdit: growth");
46 end;
47 for i := 1 to 100 do Ints.remove(v, 8, 1); end;
48 show(v);
49 Ints.done(v);
50end;
51
52end VecEdit.