Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: chunkIntoN to chunk correctly #6035

Merged
merged 7 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/prover/src/utils/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ export function cleanObject<T extends Record<string, unknown> | unknown[]>(obj:
}

/**
* Convert an array to array of chunks
* Convert an array to array of chunks of length N
* @example
* chunkIntoN([1,2,3,4], 2)
* => [[1,2], [3,4]]
* chunkIntoN([1,2,3,4,5,6], 2)
* => [[1,2], [3,4], [5,6]]
*/
export function chunkIntoN<T extends unknown[]>(arr: T, n: number): T[] {
const size = Math.ceil(arr.length / n);
return Array.from({length: n}, (v, i) => arr.slice(i * size, i * size + size)) as T[];
return Array.from({length: Math.ceil(arr.length / n)}, (_, i) => arr.slice(i * n, i * n + n)) as T[];
}
90 changes: 90 additions & 0 deletions packages/prover/test/unit/utils/conversion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import {expect} from "chai";
import {chunkIntoN} from "../../../src/utils/conversion.js";

describe("utils/conversion", () => {
describe("chunkIntoN", () => {
const testCases = [
{
title: "even number of chunks",
input: {
data: [1, 2, 3, 4, 5, 6],
n: 2,
},
output: [
[1, 2],
[3, 4],
[5, 6],
],
},
{
title: "even number of chunks with additional element",
input: {
data: [1, 2, 3, 4, 5, 6, 7],
n: 2,
},
output: [[1, 2], [3, 4], [5, 6], [7]],
},
{
title: "odd number of chunks",
input: {
data: [1, 2, 3, 4, 5, 6],
n: 3,
},
output: [
[1, 2, 3],
[4, 5, 6],
],
},
{
title: "odd number of chunks with additional element",
input: {
data: [1, 2, 3, 4, 5, 6, 7],
n: 3,
},
output: [[1, 2, 3], [4, 5, 6], [7]],
},
{
title: "data less than chunk size",
input: {
data: [1],
n: 3,
},
output: [[1]],
},
{
title: "data 1 less than chunk size",
input: {
data: [1, 2],
n: 3,
},
output: [[1, 2]],
},
{
title: "data 1 extra than chunk size",
input: {
data: [1, 2, 3, 4],
n: 3,
},
output: [[1, 2, 3], [4]],
},
{
title: "when data have different order",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this test case make sense? it makes it seem like the type of elements are relevant, rather would add 1-2 tests cases that show that it works with any type of elements, e.g. objects or nested arrays

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes just want to make sure doing chunk does not change the order.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the other existing test cases already assert that.

My point is that why would the values of elements impact that?

data have different order

Makes it sounds like this chunkIntoN inspects the value of elements, which it does not. IMO better way to prove this is to use different types of elements.

input: {
data: [6, 5, 4, 3, 2, 1],
n: 2,
},
output: [
[6, 5],
[4, 3],
[2, 1],
],
},
];

for (const {title, input, output} of testCases) {
it(`should split the chunks correctly for "${title}"`, async () => {
nflaig marked this conversation as resolved.
Show resolved Hide resolved
expect(chunkIntoN(input.data, input.n)).to.be.deep.eq(output);
});
}
});
});