]> Lady’s Gitweb - Habu/blob - mod.test.js
Initial commit
[Habu] / mod.test.js
1 // πŸŽπŸ“¦ ハブ ∷ mod.test.js
2 // ====================================================================
3 //
4 // Copyright Β© 2022 Lady [@ Lady’s Computer].
5 //
6 // This Source Code Form is subject to the terms of the Mozilla Public
7 // License, v. 2.0. If a copy of the MPL was not distributed with this
8 // file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
9
10 import { assertStrictEquals } from "./dev-deps.js";
11 import ハブ from "./mod.js";
12
13 Deno.test({
14 name: "ハブ instances can store and retrieve data",
15 fn: () => {
16 for (let bitWidth = 1; bitWidth <= 64; ++bitWidth) {
17 const data = Array.from(function* () {
18 for (let i = 0; i < 69; i++) {
19 yield BigInt(
20 Math.random() * Number(1n << BigInt(bitWidth)) >>> 0,
21 );
22 }
23 }());
24 const instance = new ハブ(bitWidth, 69);
25 assertStrictEquals(instance.length, 69);
26 assertStrictEquals(
27 instance.byteLength,
28 Math.ceil(69 / instance.wordScale) * instance.wordSize,
29 );
30 for (const [index, value] of data.entries()) {
31 instance[index] = value;
32 }
33 for (const [index, value] of data.entries()) {
34 const actual = instance[index];
35 assertStrictEquals(
36 actual,
37 value,
38 `Actual value for index ${index} of bitWidth ${bitWidth} was ${actual}, but ${value} was expected.`,
39 );
40 }
41 }
42 },
43 });
44
45 Deno.test({
46 name: "ハブ instances can share buffers",
47 fn: () => {
48 const bitWidth = 7;
49 const data = Array.from(function* () {
50 for (let i = 0; i < 69; i++) {
51 yield BigInt(
52 Math.random() * Number(1n << BigInt(bitWidth)) >>> 0,
53 );
54 }
55 }());
56 const instance = new ハブ(bitWidth, data);
57 const otherInstance = new ハブ(bitWidth, instance.buffer, 0, 69);
58 assertStrictEquals(otherInstance.length, 69);
59 for (const [index, value] of otherInstance.entries()) {
60 const actual = instance[index];
61 assertStrictEquals(
62 actual,
63 value,
64 `Value mismatch for index ${index}.`,
65 );
66 }
67 },
68 });
69
70 Deno.test({
71 name: "ハブ instances can subset other instances using array buffers",
72 fn: () => {
73 const bitWidth = 7;
74 const data = Array.from(function* () {
75 for (let i = 0; i < 69; i++) {
76 yield BigInt(
77 Math.random() * Number(1n << BigInt(bitWidth)) >>> 0,
78 );
79 }
80 }());
81 const instance = new ハブ(bitWidth, data);
82 const otherInstance = new ハブ(bitWidth, instance.buffer, 8, 42);
83 assertStrictEquals(otherInstance.length, 42);
84 assertStrictEquals(otherInstance.byteLength, 40);
85 for (const [index, value] of otherInstance.entries()) {
86 const actual = instance[index + 9]; // 9 elements per 8 bytes
87 assertStrictEquals(
88 actual,
89 value,
90 `Value mismatch for index ${index}.`,
91 );
92 }
93 },
94 });
95
96 Deno.test({
97 name: "ハブ.from",
98 fn: () => {
99 const bitWidth = 7;
100 const data = Array.from(function* () {
101 for (let i = 0; i < 69; i++) {
102 yield BigInt(
103 Math.random() * Number(1n << BigInt(bitWidth)) >>> 0,
104 );
105 }
106 }());
107 const instance = ハブ.from(bitWidth, data);
108 for (const [index, value] of data.entries()) {
109 const actual = instance[index];
110 assertStrictEquals(
111 actual,
112 value,
113 `Value mismatch for index ${index}.`,
114 );
115 }
116 },
117 });
118
119 Deno.test({
120 name: "ハブ.of",
121 fn: () => {
122 const bitWidth = 7;
123 const data = Array.from(function* () {
124 for (let i = 0; i < 69; i++) {
125 yield BigInt(
126 Math.random() * Number(1n << BigInt(bitWidth)) >>> 0,
127 );
128 }
129 }());
130 const instance = ハブ.of(bitWidth, ...data);
131 for (const [index, value] of data.entries()) {
132 const actual = instance[index];
133 assertStrictEquals(
134 actual,
135 value,
136 `Value mismatch for index ${index}.`,
137 );
138 }
139 },
140 });
141
142 Deno.test({
143 name: "ハブ#concat",
144 fn: () => {
145 const bitWidth = 7;
146 const data = Array.from(function* () {
147 for (let i = 0; i < 69; i++) {
148 yield BigInt(
149 Math.random() * Number(1n << BigInt(bitWidth)) >>> 0,
150 );
151 }
152 }());
153 const instance = new ハブ(7);
154 const concatted = instance.concat(data);
155 assertStrictEquals(concatted.length, 69);
156 for (const [index, value] of data.entries()) {
157 const actual = concatted[index];
158 assertStrictEquals(actual, value);
159 }
160 },
161 });
162
163 Deno.test({
164 name: "ハブ#constructor",
165 fn: () => {
166 const instance = new ハブ(7);
167 const { constructor } = instance;
168 assertStrictEquals(new constructor(5).bitWidth, 7);
169 assertStrictEquals(new constructor(5).length, 5);
170 assertStrictEquals(constructor[Symbol.species], constructor);
171 },
172 });
173
174 Deno.test({
175 name: "ハブ#filter",
176 fn: () => {
177 const data = Array.from(function* () {
178 for (let i = 0; i < 69; i++) {
179 yield i % 2 ? 0n : 1n;
180 }
181 }());
182 const instance = new ハブ(7, data);
183 const filtered = instance.filter(($) => !!$);
184 assertStrictEquals(filtered.length, 35);
185 for (const value of filtered) {
186 assertStrictEquals(value, 1n);
187 }
188 },
189 });
This page took 0.170031 seconds and 5 git commands to generate.