upTreeTraversal finished, fixed a bug in fixture parser where every node had parent set to 0
unfinished test that checks agains that error
This commit is contained in:
@@ -32,6 +32,7 @@ export function upTraverseTree(fs: FileSystem, inode: number): number[] {
|
|||||||
let currentInode = inode;
|
let currentInode = inode;
|
||||||
while (currentInode !== 0) {
|
while (currentInode !== 0) {
|
||||||
const node = fs.getNode(currentInode) as TreeNode;
|
const node = fs.getNode(currentInode) as TreeNode;
|
||||||
|
if (!node) break;
|
||||||
|
|
||||||
result.push(node.parent);
|
result.push(node.parent);
|
||||||
currentInode = node.parent;
|
currentInode = node.parent;
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { FileSystem } from "@core/filesystem/filesystem";
|
import { FileSystem } from "@core/filesystem/filesystem";
|
||||||
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
|
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
|
||||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
|
import { dnTraverseTree } from "@core/util/tree_traversal";
|
||||||
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, test } from "vitest";
|
||||||
|
|
||||||
const testTree: FixtureNode = {
|
const testTree: FixtureNode = {
|
||||||
inode: 0,
|
inode: 0,
|
||||||
@@ -35,6 +36,28 @@ const testTree: FixtureNode = {
|
|||||||
|
|
||||||
const fs = buildTestFS(testTree);
|
const fs = buildTestFS(testTree);
|
||||||
|
|
||||||
|
test("walks down the tree to check if every node has the parent set correctly", () => {
|
||||||
|
const result = dnTraverseTree(fs, 0);
|
||||||
|
|
||||||
|
const out: boolean[] = [];
|
||||||
|
|
||||||
|
let parentInode = 0;
|
||||||
|
for (const inode of result) {
|
||||||
|
const node = fs.getNode(inode) as TreeNode;
|
||||||
|
|
||||||
|
if (node.parent !== parentInode) {
|
||||||
|
console.log(node.inode);
|
||||||
|
out.push(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
parentInode = node.parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(out, result);
|
||||||
|
|
||||||
|
expect(out.includes(false)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
describe("FileSystem.init", () => {
|
describe("FileSystem.init", () => {
|
||||||
const map: Map<number, TreeNode> = new Map();
|
const map: Map<number, TreeNode> = new Map();
|
||||||
const time = Date.now();
|
const time = Date.now();
|
||||||
|
|||||||
@@ -50,7 +50,9 @@ describe("upTraverseTree", () => {
|
|||||||
test("successfully traverses up the tree", () => {
|
test("successfully traverses up the tree", () => {
|
||||||
const result = upTraverseTree(fsTwo, 7);
|
const result = upTraverseTree(fsTwo, 7);
|
||||||
console.log(result);
|
console.log(result);
|
||||||
//expect(result, `Checking the result ${result} to be [0, 1, 2, 3]`).toEqual([0, 2, 3, 1]);
|
expect(result, `Checking the result ${result} to be [7, 6, 5, 4, 3, 2, 1, 0]`).toEqual([
|
||||||
|
7, 6, 5, 4, 3, 2, 1, 0,
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("returns empty array for a non existent inode", () => {
|
test("returns empty array for a non existent inode", () => {
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ export function buildTestFS(tree: FixtureNode): FileSystem {
|
|||||||
function parse(node: FixtureNode, parent: number): void {
|
function parse(node: FixtureNode, parent: number): void {
|
||||||
if (node.type === "Dir") {
|
if (node.type === "Dir") {
|
||||||
for (const child of node.children) {
|
for (const child of node.children) {
|
||||||
parse(child, parent);
|
parse(child, node.inode);
|
||||||
}
|
}
|
||||||
|
|
||||||
const children: Record<string, number> = {};
|
const children: Record<string, number> = {};
|
||||||
|
|||||||
Reference in New Issue
Block a user