ref: e761ce9ef2f89c9f87a4be72b88b837109630c2b
parent: 93db6a9fd69794d74e56cbdfc7796b46da407812
author: S. Gilles <[email protected]>
date: Mon Jan 8 09:05:55 EST 2018
let bychunk() read the last element; guard for negative sz I mentioned on IRC that bychunk() was skipping the last element of a slice, here's a fix for that, as well as some redundant tests and some probably unneeded input validation. I don't have any strong feelings about protecting someone from shooting themselves in the foot with bychunk([1][:], -100), but it's easier to remove in review than to add later.
--- a/lib/iter/chunk.myr
+++ b/lib/iter/chunk.myr
@@ -12,7 +12,7 @@
;;
generic bychunk = {a, sz
- -> [.sl = a, .idx = 0, .blksz = sz]
+ -> [.sl = a, .idx = 0, .blksz = std.max(sz, 1)]
}
impl iterable chunkiter(@a) -> @a[:] =
@@ -20,7 +20,7 @@
var len
len = std.min(itp.blksz, itp.sl.len - itp.idx)
- if itp.idx + len == itp.sl.len
+ if itp.idx + len > itp.sl.len || itp.idx >= itp.sl.len
-> false
;;
valp# = itp.sl[itp.idx: itp.idx + len]
--- /dev/null
+++ b/lib/iter/test/chunk.myr
@@ -1,0 +1,44 @@
+use std
+use testr
+
+use iter
+
+const main = {
+ testr.run([
+ [.name = "bychunk-01", .fn = bychunk01],
+ [.name = "bychunk-02", .fn = bychunk02],
+ [.name = "bychunk-03", .fn = bychunk03],
+ [.name = "bychunk-04", .fn = bychunk04],
+ [.name = "bychunk-05", .fn = bychunk05],
+ ][:])
+}
+
+const verify_bychunk = {c, v, n, e
+ var sb : std.strbuf# = std.mksb()
+ std.sbfmt(sb, " ")
+ for w : iter.bychunk(v, n)
+ std.sbfmt(sb, "{} ", w)
+ ;;
+ var a : byte[:] = std.sbfin(sb)
+ testr.check(c, std.eq(e, a), "expected “{}”, got “{}”", e, a)
+}
+
+const bychunk01 = {c
+ verify_bychunk(c, [1, 2, 3, 4][:], 1, " [1] [2] [3] [4] ")
+}
+
+const bychunk02 = {c
+ verify_bychunk(c, [1, 2, 3, 4, 5, 6, 7, 8][:], 3, " [1, 2, 3] [4, 5, 6] [7, 8] ")
+}
+
+const bychunk03 = {c
+ verify_bychunk(c, [1, 2, 3, 4][:], 99, " [1, 2, 3, 4] ")
+}
+
+const bychunk04 = {c
+ verify_bychunk(c, [1, 2, 3, 4][:], -2, " [1] [2] [3] [4] ")
+}
+
+const bychunk05 = {c
+ verify_bychunk(c, [][:], 3, " ")
+}