| 1 | module CopyOverlap; |
| 2 | (* COPY within one array, every pair of places in its first 24 |
| 3 | elements and every length that fits, for elements of 1, 4 and 3 |
| 4 | bytes: the destination inside the source, the source inside it, |
| 5 | the two the same, touching above and below, and apart. Each is |
| 6 | checked against a copy made through a second array. *) |
| 7 | import Out; |
| 8 | const N = 40; Most = 24; |
| 9 | type Rgb = record r, g, b: U8; end; |
| 10 | var bytes, bytesWant: [N]U8; words, wordsWant: [N]U32; rgbs, rgbsWant: [N]Rgb; |
| 11 | cases, wrong: INT; |
| 12 | |
| 13 | (* Says where a copy came out wrong. *) |
| 14 | proc fail(kind: []const U8; d, s, n: INT); |
| 15 | begin |
| 16 | Out.str(kind); Out.str(" wrong: d "); Out.int(I64(d)); Out.str(" s "); Out.int(I64(s)); |
| 17 | Out.str(" n "); Out.int(I64(n)); Out.ln; |
| 18 | INC(wrong); |
| 19 | end; |
| 20 | |
| 21 | (* n bytes from s to d. *) |
| 22 | proc tryBytes(d, s, n: INT); |
| 23 | var i: INT; tmp: [N]U8; |
| 24 | begin |
| 25 | for i := 0 to N - 1 do bytes[i] := U8(i + 1); end; |
| 26 | bytesWant := bytes; |
| 27 | for i := 0 to n - 1 do tmp[i] := bytesWant[s + i]; end; |
| 28 | for i := 0 to n - 1 do bytesWant[d + i] := tmp[i]; end; |
| 29 | COPY(bytes[d..d + n], bytes[s..s + n]); |
| 30 | if bytes # bytesWant then fail("U8", d, s, n); end; |
| 31 | end; |
| 32 | |
| 33 | (* n words from s to d. *) |
| 34 | proc tryWords(d, s, n: INT); |
| 35 | var i: INT; tmp: [N]U32; |
| 36 | begin |
| 37 | for i := 0 to N - 1 do words[i] := U32(i * 65537 + 1); end; |
| 38 | wordsWant := words; |
| 39 | for i := 0 to n - 1 do tmp[i] := wordsWant[s + i]; end; |
| 40 | for i := 0 to n - 1 do wordsWant[d + i] := tmp[i]; end; |
| 41 | COPY(words[d..d + n], words[s..s + n]); |
| 42 | if words # wordsWant then fail("U32", d, s, n); end; |
| 43 | end; |
| 44 | |
| 45 | (* n records of 3 bytes from s to d. *) |
| 46 | proc tryRgbs(d, s, n: INT); |
| 47 | var i: INT; tmp: [N]Rgb; |
| 48 | begin |
| 49 | for i := 0 to N - 1 do rgbs[i] := {r: U8(i), g: U8(i + 100), b: U8(i + 200)}; end; |
| 50 | rgbsWant := rgbs; |
| 51 | for i := 0 to n - 1 do tmp[i] := rgbsWant[s + i]; end; |
| 52 | for i := 0 to n - 1 do rgbsWant[d + i] := tmp[i]; end; |
| 53 | COPY(rgbs[d..d + n], rgbs[s..s + n]); |
| 54 | for i := 0 to N - 1 do |
| 55 | if (rgbs[i].r # rgbsWant[i].r) or (rgbs[i].g # rgbsWant[i].g) or (rgbs[i].b # rgbsWant[i].b) then |
| 56 | fail("Rgb", d, s, n); |
| 57 | return; |
| 58 | end; |
| 59 | end; |
| 60 | end; |
| 61 | |
| 62 | proc main*; |
| 63 | var d, s, n: INT; |
| 64 | begin |
| 65 | for d := 0 to Most do |
| 66 | for s := 0 to Most do |
| 67 | for n := 0 to N - Most do |
| 68 | tryBytes(d, s, n); |
| 69 | tryWords(d, s, n); |
| 70 | tryRgbs(d, s, n); |
| 71 | INC(cases); |
| 72 | end; |
| 73 | end; |
| 74 | end; |
| 75 | Out.int(I64(cases)); Out.str(" cases, "); Out.int(I64(wrong)); Out.str(" wrong"); Out.ln; |
| 76 | end; |
| 77 | end CopyOverlap. |