| 1 | (* A generic module for the test Fill: FILL of slices of T, and of |
| 2 | arrays of T, of 0 to 100 elements, each with elements around it that |
| 3 | must keep what they held. Elements are compared by their bytes, so T |
| 4 | may be a record, which = doesn't compare. *) |
| 5 | module Filled(T); |
| 6 | import Out; |
| 7 | import SYSTEM; |
| 8 | |
| 9 | (* The lengths of the slices filled *) |
| 10 | const Lens: [7]INT = [0, 1, 2, 7, 8, 9, 100]; |
| 11 | |
| 12 | (* An array of n elements of T, with one before it and one after it *) |
| 13 | type |
| 14 | G1 = record pre: T; a: [1]T; post: T; end; |
| 15 | G2 = record pre: T; a: [2]T; post: T; end; |
| 16 | G7 = record pre: T; a: [7]T; post: T; end; |
| 17 | G8 = record pre: T; a: [8]T; post: T; end; |
| 18 | G9 = record pre: T; a: [9]T; post: T; end; |
| 19 | G100 = record pre: T; a: [100]T; post: T; end; |
| 20 | |
| 21 | var |
| 22 | buf: [102]T; (* slices are cut from it, from its second element *) |
| 23 | g1: G1; g2: G2; g7: G7; g8: G8; g9: G9; g100: G100; |
| 24 | filled: INT; (* elements filled, and checked *) |
| 25 | wrong: INT; (* elements that hold what they shouldn't *) |
| 26 | |
| 27 | (* The bytes of the T at p: BYTES takes no BOOL, set or enumeration *) |
| 28 | proc bytesAt(p: ^T): []const U8; |
| 29 | var q: ^U8; |
| 30 | begin |
| 31 | q := ^U8(p); |
| 32 | return q[0..SIZE(T)]; |
| 33 | end; |
| 34 | |
| 35 | (* Counts the elements of a that aren't v *) |
| 36 | proc count(a: []T; v: T); |
| 37 | var i: INT; |
| 38 | begin |
| 39 | for i := 0 to LEN(a) - 1 do |
| 40 | if bytesAt(SYSTEM.ADR(a[i])) # bytesAt(SYSTEM.ADR(v)) then INC(wrong); end; |
| 41 | end; |
| 42 | end; |
| 43 | |
| 44 | (* Sets every element of a to s, by a loop *) |
| 45 | proc reset(a: []T; s: T); |
| 46 | var i: INT; |
| 47 | begin |
| 48 | for i := 0 to LEN(a) - 1 do a[i] := s; end; |
| 49 | end; |
| 50 | |
| 51 | (* Fills slices of buf with v, the rest s *) |
| 52 | proc slices(v, s: T); |
| 53 | var k, n: INT; |
| 54 | begin |
| 55 | for k := 0 to LEN(Lens) - 1 do |
| 56 | n := Lens[k]; |
| 57 | reset(buf, s); |
| 58 | FILL(buf[1..1 + n], v); |
| 59 | count(buf[0..1], s); |
| 60 | count(buf[1..1 + n], v); |
| 61 | count(buf[1 + n..102], s); |
| 62 | INC(filled, n); |
| 63 | end; |
| 64 | end; |
| 65 | |
| 66 | (* Fills each array with v, its guards and what it held before s *) |
| 67 | proc arrays(v, s: T); |
| 68 | begin |
| 69 | g1.pre := s; g1.post := s; reset(g1.a, s); FILL(g1.a, v); |
| 70 | count(g1.a, v); count([g1.pre, g1.post], s); INC(filled, LEN(g1.a)); |
| 71 | g2.pre := s; g2.post := s; reset(g2.a, s); FILL(g2.a, v); |
| 72 | count(g2.a, v); count([g2.pre, g2.post], s); INC(filled, LEN(g2.a)); |
| 73 | g7.pre := s; g7.post := s; reset(g7.a, s); FILL(g7.a, v); |
| 74 | count(g7.a, v); count([g7.pre, g7.post], s); INC(filled, LEN(g7.a)); |
| 75 | g8.pre := s; g8.post := s; reset(g8.a, s); FILL(g8.a, v); |
| 76 | count(g8.a, v); count([g8.pre, g8.post], s); INC(filled, LEN(g8.a)); |
| 77 | g9.pre := s; g9.post := s; reset(g9.a, s); FILL(g9.a, v); |
| 78 | count(g9.a, v); count([g9.pre, g9.post], s); INC(filled, LEN(g9.a)); |
| 79 | g100.pre := s; g100.post := s; reset(g100.a, s); FILL(g100.a, v); |
| 80 | count(g100.a, v); count([g100.pre, g100.post], s); INC(filled, LEN(g100.a)); |
| 81 | end; |
| 82 | |
| 83 | (* Fills slices and arrays with v, around s, and says how many elements |
| 84 | were filled and how many are wrong, after name *) |
| 85 | proc check*(name: []const U8; v, s: T); |
| 86 | begin |
| 87 | filled := 0; |
| 88 | wrong := 0; |
| 89 | slices(v, s); |
| 90 | arrays(v, s); |
| 91 | Out.str(name); Out.char(' '); Out.int(filled); Out.char(' '); Out.int(wrong); Out.ln; |
| 92 | end; |
| 93 | |
| 94 | end Filled. |