extend("test")
extend("array")
describe("array:find", () => {
it("finds the first item matching the predicate", () => {
result = array:find(x => x > 15, [4,8,15,16,23,42])
expect_equal(result, 16)
})
it("returns _ if no item matches", () => {
result = array:find(x => x > 50, [1,2,3])
expect_equal(result, _)
})
it("supports partial usage (predicate first)", () => {
pred = array:find(x => x == 42)
expect_equal(pred([40,41,42,43]), 42)
})
it("supports placeholder usage (array known)", () => {
arrPartial = array:find(_, [5,6,9,12])
expect_equal(arrPartial(x => x % 3 == 0), 6)
})
})
describe("array:findIndex", () => {
it("returns the index of first match", () => {
result = array:findIndex(x => x > 20, [4,8,15,16,23,42])
expect_equal(result, 4)
})
it("returns -1 if not found", () => {
result = array:findIndex(x => x < 0, [1,2,3])
expect_equal(result, -1)
})
it("partial usage (predicate first)", () => {
pred = array:findIndex(x => x == 42)
expect_equal(pred([1,2,42]), 2)
})
it("placeholder usage (array known)", () => {
arrPartial = array:findIndex(_, [10,12,14])
expect_equal(arrPartial(x => x % 7 == 0), 2)
})
})
describe("array:includes", () => {
it("returns true if value is present", () => {
expect_equal(array:includes(42, [1,2,3,42]), true)
expect_equal(array:includes("a", ["b","a","c"]), true)
})
it("returns false if not present", () => {
expect_equal(array:includes(0, [1,2,3]), false)
expect_equal(array:includes("x", ["y","z"]), false)
})
it("partial usage (value first)", () => {
p = array:includes(7)
expect_equal(p([6,7,8]), true)
})
it("placeholder usage (array known)", () => {
arrPartial = array:includes(_, [5,8,12])
expect_equal(arrPartial(99), false)
expect_equal(arrPartial(12), true)
})
})
describe("array:every (all)", () => {
it("returns true if all elements match", () => {
expect_equal(array:every(x => x > 0, [1,2,3]), true)
})
it("returns false if any element fails predicate", () => {
expect_equal(array:every(x => x < 10, [4,12,6]), false)
})
it("partial usage", () => {
pred = array:every(x => x % 2 == 0)
expect_equal(pred([2,4,6,8]), true)
expect_equal(pred([2,3,4]), false)
})
it("placeholder usage", () => {
arrPartial = array:every(_, [3,3,3])
expect_equal(arrPartial(x => x == 3), true)
})
})
describe("array:some (any)", () => {
it("returns true if any element matches", () => {
expect_equal(array:some(x => x > 10, [1,10,11]), true)
})
it("returns false if none match", () => {
expect_equal(array:some(x => x < 0, [1,2,3]), false)
})
it("partial usage", () => {
pred = array:some(x => x == 7)
expect_equal(pred([5,6,7,8]), true)
expect_equal(pred([1,2,3]), false)
})
it("placeholder usage", () => {
arrPartial = array:some(_, [1,1,1])
expect_equal(arrPartial(x => x == 2), false)
})
})
describe("array:filter", () => {
it("returns elements matching predicate", () => {
expect_equal(array:filter(x => x % 2 == 0, [1,2,3,4]), [2,4])
})
it("returns empty array if none match", () => {
expect_equal(array:filter(x => x < 0, [1,2,3]), [])
})
it("partial usage", () => {
evens = array:filter(x => x % 2 == 0)
expect_equal(evens([10,11,12,13]), [10,12])
})
it("placeholder usage", () => {
arrPartial = array:filter(_, [10,20,30])
expect_equal(arrPartial(x => x > 15), [20,30])
})
})