2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { assertStrictEquals } from "@std/assert";
|
2023-11-15 10:17:44 +00:00
|
|
|
import { RedBlackNode } from "./_red_black_node.ts";
|
2022-03-24 15:21:12 +00:00
|
|
|
|
2023-12-28 04:17:21 +00:00
|
|
|
Deno.test("RedBlackNode", () => {
|
2022-06-30 05:58:42 +00:00
|
|
|
const parent: RedBlackNode<number> = new RedBlackNode(null, 5);
|
|
|
|
const child: RedBlackNode<number> = new RedBlackNode(parent, 7);
|
2022-03-24 15:21:12 +00:00
|
|
|
parent.left = child;
|
|
|
|
assertStrictEquals(parent.red, true);
|
|
|
|
parent.red = false;
|
|
|
|
|
|
|
|
assertStrictEquals(parent.parent, null);
|
|
|
|
assertStrictEquals(parent.left, child);
|
|
|
|
assertStrictEquals(parent.right, null);
|
|
|
|
assertStrictEquals(parent.value, 5);
|
|
|
|
assertStrictEquals(parent.red, false);
|
|
|
|
|
|
|
|
assertStrictEquals(child.parent, parent);
|
|
|
|
assertStrictEquals(child.left, null);
|
|
|
|
assertStrictEquals(child.right, null);
|
|
|
|
assertStrictEquals(child.value, 7);
|
|
|
|
assertStrictEquals(child.red, true);
|
2023-12-28 04:17:21 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test("RedBlackNode.from()", () => {
|
|
|
|
const parent: RedBlackNode<number> = new RedBlackNode(null, 5);
|
|
|
|
const child: RedBlackNode<number> = new RedBlackNode(parent, 7);
|
|
|
|
parent.left = child;
|
|
|
|
parent.red = false;
|
|
|
|
|
|
|
|
const parentClone: RedBlackNode<number> = RedBlackNode.from(parent);
|
|
|
|
const childClone: RedBlackNode<number> = RedBlackNode.from(child);
|
2022-03-24 15:21:12 +00:00
|
|
|
|
|
|
|
assertStrictEquals(parentClone.parent, null);
|
|
|
|
assertStrictEquals(parentClone.left, child);
|
|
|
|
assertStrictEquals(parentClone.right, null);
|
|
|
|
assertStrictEquals(parentClone.value, 5);
|
|
|
|
assertStrictEquals(parentClone.red, false);
|
|
|
|
|
|
|
|
assertStrictEquals(childClone.parent, parent);
|
|
|
|
assertStrictEquals(childClone.left, null);
|
|
|
|
assertStrictEquals(childClone.right, null);
|
|
|
|
assertStrictEquals(childClone.value, 7);
|
|
|
|
assertStrictEquals(childClone.red, true);
|
|
|
|
});
|