Validate bit shift left amount

This commit is contained in:
Deepak kudi 2026-06-04 12:46:36 +05:30
parent 72ca1aeec7
commit ef174d2f41
2 changed files with 15 additions and 1 deletions

View File

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

View File

@ -21,6 +21,15 @@ TestRegister.addTests([
"args": ["Space"] } "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", name: "Bit shift right: Logical shift",
input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100", input: "01010101 10101010 11111111 00000000 11110000 00001111 00110011 11001100",