Skip to content
Closed
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
7 changes: 6 additions & 1 deletion src/core/operations/BitShiftLeft.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

/**
* Bit shift left operation
Expand All @@ -27,7 +28,8 @@ class BitShiftLeft extends Operation {
{
"name": "Amount",
"type": "number",
"value": 1
"value": 1,
"min": 0
}
];
}
Expand All @@ -39,6 +41,9 @@ class BitShiftLeft extends Operation {
*/
run(input, args) {
const amount = args[0];
if (amount < 0) {
throw new OperationError("Amount must be non-negative.");
}
input = new Uint8Array(input);

return input.map(b => {
Expand Down
9 changes: 9 additions & 0 deletions tests/operations/tests/BitwiseOp.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ TestRegister.addTests([
"args": ["Space"] }
]
},
{
name: "Bit shift left rejects negative amount",
input: "a",
expectedOutput: "Amount must be non-negative.",
recipeConfig: [
{ "op": "Bit shift left",
"args": [-1] }
]
},
{
name: "Bit shift right: Logical shift",
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",
Expand Down