Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { describe, expect, it } from "@jest/globals";

import { traversePreOrderNAry } from "./index.ts";

type Node = { val: number; children: Node[] };

describe("traversePreOrderNAry", () => {
it("yields nothing for null/undefined root", () => {
expect([...traversePreOrderNAry(null)]).toStrictEqual([]);
expect([...traversePreOrderNAry(undefined)]).toStrictEqual([]);
});

it("yields root only for single node", () => {
const root: Node = { val: 1, children: [] };
expect([...traversePreOrderNAry(root)].map((n) => n.val)).toStrictEqual([
1,
]);
});

it("visits parent before children", () => {
const root: Node = {
val: 1,
children: [
{
val: 2,
children: [
{ val: 5, children: [] },
{ val: 6, children: [] },
],
},
{ val: 3, children: [] },
{
val: 4,
children: [{ val: 7, children: [] }],
},
],
};
expect([...traversePreOrderNAry(root)].map((n) => n.val)).toStrictEqual([
1, 2, 5, 6, 3, 4, 7,
]);
});

it("traverses deeper trees in pre-order", () => {
const root: Node = {
val: 1,
children: [
{
val: 2,
children: [
{ val: 4, children: [] },
{ val: 5, children: [] },
],
},
{ val: 3, children: [] },
],
};
expect([...traversePreOrderNAry(root)].map((n) => n.val)).toStrictEqual([
1, 2, 4, 5, 3,
]);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function* traversePreOrderNAry<T extends { children: T[] }>(
root: T | null | undefined,
): Generator<T, void, void> {
if (root == null) {
return;
}

const stack: T[] = [root];

do {
const node = stack.pop()!;
yield node;
// TODO: add an Array.prototype.valuesReversed() goody and use it here
for (let i = node.children.length - 1; i >= 0; --i) {
stack.push(node.children[i]);
}
} while (stack.length > 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2724,6 +2724,31 @@ function* traversePreOrder(root) {
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
`;

exports[`App can equip single goody: JavaScript traversePreOrderNAry 1`] = `
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
// Adventure Pack commit fake-commit-hash
// Running at: https://example.com/

function* traversePreOrderNAry(root) {
if (root == null) {
return;
}

const stack = [root];

do {
const node = stack.pop();
yield node;
// TODO: add an Array.prototype.valuesReversed() goody and use it here
for (let i = node.children.length - 1; i >= 0; --i) {
stack.push(node.children[i]);
}
} while (stack.length > 0);
}

/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
`;

exports[`App can equip single goody: Kotlin gcd(Int,Int) 1`] = `
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
// Adventure Pack commit fake-commit-hash
Expand Down Expand Up @@ -5509,3 +5534,30 @@ function* traversePreOrder<

/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
`;

exports[`App can equip single goody: TypeScript traversePreOrderNAry 1`] = `
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
// Adventure Pack commit fake-commit-hash
// Running at: https://example.com/

function* traversePreOrderNAry<T extends { children: T[] }>(
root: T | null | undefined,
): Generator<T, void, void> {
if (root == null) {
return;
}

const stack: T[] = [root];

do {
const node = stack.pop()!;
yield node;
// TODO: add an Array.prototype.valuesReversed() goody and use it here
for (let i = node.children.length - 1; i >= 0; --i) {
stack.push(node.children[i]);
}
} while (stack.length > 0);
}

/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,25 @@ exports[`App can render goody: JavaScript traversePreOrder 1`] = `
}"
`;

exports[`App can render goody: JavaScript traversePreOrderNAry 1`] = `
"export function* traversePreOrderNAry(root) {
if (root == null) {
return;
}

const stack = [root];

do {
const node = stack.pop();
yield node;
// TODO: add an Array.prototype.valuesReversed() goody and use it here
for (let i = node.children.length - 1; i >= 0; --i) {
stack.push(node.children[i]);
}
} while (stack.length > 0);
}"
`;

exports[`App can render goody: Kotlin gcd(Int,Int) 1`] = `
"package gcd_int_int

Expand Down Expand Up @@ -3159,3 +3178,24 @@ exports[`App can render goody: TypeScript traversePreOrder 1`] = `
} while (stack.length > 0);
}"
`;

exports[`App can render goody: TypeScript traversePreOrderNAry 1`] = `
"export function* traversePreOrderNAry<T extends { children: T[] }>(
root: T | null | undefined,
): Generator<T, void, void> {
if (root == null) {
return;
}

const stack: T[] = [root];

do {
const node = stack.pop()!;
yield node;
// TODO: add an Array.prototype.valuesReversed() goody and use it here
for (let i = node.children.length - 1; i >= 0; --i) {
stack.push(node.children[i]);
}
} while (stack.length > 0);
}"
`;
Loading