]>
Ladyβs Gitweb - Habu/blob - mod.test.js
1 // ππ¦ γγγ β· mod.test.js
2 // ====================================================================
4 // Copyright Β© 2022 Lady [@ Ladyβs Computer].
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/>.
10 import { assertStrictEquals
} from "./dev-deps.js";
11 import γγγ from "./mod.js";
14 name
: "γγγ instances can store and retrieve data",
16 for (let bitWidth
= 1; bitWidth
<= 64; ++bitWidth
) {
17 const data
= Array
.from(function* () {
18 for (let i
= 0; i
< 69; i
++) {
20 Math
.random() * Number(1n
<< BigInt(bitWidth
)) >>> 0,
24 const instance
= new γγγ(bitWidth
, 69);
25 assertStrictEquals(instance
.length
, 69);
28 Math
.ceil(69 / instance
.wordScale
) * instance
.wordSize
,
30 for (const [index
, value
] of data
.entries()) {
31 instance
[index
] = value
;
33 for (const [index
, value
] of data
.entries()) {
34 const actual
= instance
[index
];
38 `Actual value for index ${index} of bitWidth ${bitWidth} was ${actual}, but ${value} was expected.`,
46 name
: "γγγ instances can share buffers",
49 const data
= Array
.from(function* () {
50 for (let i
= 0; i
< 69; i
++) {
52 Math
.random() * Number(1n
<< BigInt(bitWidth
)) >>> 0,
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
];
64 `Value mismatch for index ${index}.`,
71 name
: "γγγ instances can subset other instances using array buffers",
74 const data
= Array
.from(function* () {
75 for (let i
= 0; i
< 69; i
++) {
77 Math
.random() * Number(1n
<< BigInt(bitWidth
)) >>> 0,
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
90 `Value mismatch for index ${index}.`,
97 name
: "γγγ.from",
100 const data
= Array
.from(function* () {
101 for (let i
= 0; i
< 69; i
++) {
103 Math
.random() * Number(1n
<< BigInt(bitWidth
)) >>> 0,
107 const instance
= γγγ.from(bitWidth
, data
);
108 for (const [index
, value
] of data
.entries()) {
109 const actual
= instance
[index
];
113 `Value mismatch for index ${index}.`,
120 name
: "γγγ.of",
123 const data
= Array
.from(function* () {
124 for (let i
= 0; i
< 69; i
++) {
126 Math
.random() * Number(1n
<< BigInt(bitWidth
)) >>> 0,
130 const instance
= γγγ.of(bitWidth
, ...data
);
131 for (const [index
, value
] of data
.entries()) {
132 const actual
= instance
[index
];
136 `Value mismatch for index ${index}.`,
143 name
: "γγγ#concat",
146 const data
= Array
.from(function* () {
147 for (let i
= 0; i
< 69; i
++) {
149 Math
.random() * Number(1n
<< BigInt(bitWidth
)) >>> 0,
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
);
164 name
: "γγγ#constructor",
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);
175 name
: "γγγ#filter",
177 const data
= Array
.from(function* () {
178 for (let i
= 0; i
< 69; i
++) {
179 yield i
% 2 ? 0n
: 1n
;
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
);
This page took 0.071779 seconds and 5 git commands to generate.