|
| 1 | +// Copyright 2026 International Digital Economy Academy |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +///| |
| 16 | +/// The block offsets `removes_array` and `removes_list` remove, ascending: |
| 17 | +/// every multiple of `k` that still leaves a whole block inside `n`. |
| 18 | +fn block_offsets(k : Int, n : Int) -> Array[Int] { |
| 19 | + let offsets = [] |
| 20 | + for start = 0; start + k <= n; start = start + k { |
| 21 | + offsets.push(start) |
| 22 | + } |
| 23 | + offsets |
| 24 | +} |
| 25 | + |
| 26 | +///| |
| 27 | +/// Pins the candidate sequence of `removes_array` so that a change to how the |
| 28 | +/// candidates are produced cannot quietly change which candidates come out, or |
| 29 | +/// in what order. Every `(n, k)` pair up to 24 elements is covered. |
| 30 | +test "removes_array removes one block per offset, ascending" { |
| 31 | + for n = 0; n <= 24; n = n + 1 { |
| 32 | + let xs = Array::makei(n, i => i) |
| 33 | + for k = 1; k <= n; k = k + 1 { |
| 34 | + let expected = block_offsets(k, n).map(start => { |
| 35 | + [..xs[:start], ..xs[start + k:]] |
| 36 | + }) |
| 37 | + let candidates = removes_array(k, n, xs) |
| 38 | + guard candidates.size_hint() is Some(hint) && hint == expected.length() else { |
| 39 | + abort( |
| 40 | + "n=\{n} k=\{k}: wrong size_hint for \{expected.length()} candidates", |
| 41 | + ) |
| 42 | + } |
| 43 | + guard candidates.collect() == expected else { |
| 44 | + abort("n=\{n} k=\{k}: candidates differ") |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +///| |
| 51 | +/// The same for `removes_list`, whose offsets run the other way. |
| 52 | +test "removes_list removes one block per offset, descending" { |
| 53 | + for n = 0; n <= 24; n = n + 1 { |
| 54 | + let xs : @list.List[Int] = List(Array::makei(n, i => i)) |
| 55 | + for k = 1; k <= n; k = k + 1 { |
| 56 | + let expected = block_offsets(k, n) |
| 57 | + .rev() |
| 58 | + .map(start => xs.take(start).concat(xs.drop(start + k))) |
| 59 | + let candidates = removes_list(k, n, xs) |
| 60 | + guard candidates.size_hint() is Some(hint) && hint == expected.length() else { |
| 61 | + abort( |
| 62 | + "n=\{n} k=\{k}: wrong size_hint for \{expected.length()} candidates", |
| 63 | + ) |
| 64 | + } |
| 65 | + guard candidates.collect() == expected else { |
| 66 | + abort("n=\{n} k=\{k}: candidates differ") |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +///| |
| 73 | +/// `k` larger than the collection leaves nothing to remove. |
| 74 | +test "removing more than the collection holds yields nothing" { |
| 75 | + let xs = Array::makei(4, i => i) |
| 76 | + let ls : @list.List[Int] = List(xs) |
| 77 | + guard removes_array(5, 4, xs).collect() is [] else { abort("array") } |
| 78 | + guard removes_list(5, 4, ls).collect() is [] else { abort("list") } |
| 79 | +} |
0 commit comments