-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from FlorianWendelborn/0xflotus-circular-shift
feature(#44): Add Circular Shift
- Loading branch information
Showing
6 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Bits } from '../types' | ||
import circularShiftLeft from './circular-shift-left' | ||
|
||
test('circularShiftLeft with amount 1', () => { | ||
const testCases: Array<[Bits, Bits]> = [ | ||
[ | ||
[1, 0, 0, 0, 1, 1, 0, 1], | ||
[0, 0, 0, 1, 1, 0, 1, 1], | ||
], | ||
[ | ||
[0, 0, 0, 1, 1, 1, 1, 1], | ||
[0, 0, 1, 1, 1, 1, 1, 0], | ||
], | ||
] | ||
|
||
for (const [input, expectedResult] of testCases) | ||
expect(circularShiftLeft(input, 1)).toEqual(expectedResult) | ||
}) | ||
|
||
test('circularShiftLeft with amount 2', () => { | ||
const testCases: Array<[Bits, Bits]> = [ | ||
[ | ||
[1, 0, 0, 0, 1, 1, 0, 1], | ||
[0, 0, 1, 1, 0, 1, 1, 0], | ||
], | ||
[ | ||
[0, 0, 0, 1, 1, 1, 1, 1], | ||
[0, 1, 1, 1, 1, 1, 0, 0], | ||
], | ||
] | ||
|
||
for (const [input, expectedResult] of testCases) | ||
expect(circularShiftLeft(input, 2)).toEqual(expectedResult) | ||
}) | ||
|
||
test('circularShiftLeft with amount 3', () => { | ||
const testCases: Array<[Bits, Bits]> = [ | ||
[ | ||
[1, 0, 0, 0, 1, 1, 0, 1], | ||
[0, 1, 1, 0, 1, 1, 0, 0], | ||
], | ||
[ | ||
[0, 0, 0, 1, 1, 1, 1, 1], | ||
[1, 1, 1, 1, 1, 0, 0, 0], | ||
], | ||
] | ||
|
||
for (const [input, expectedResult] of testCases) | ||
expect(circularShiftLeft(input, 3)).toEqual(expectedResult) | ||
}) | ||
|
||
test('circularShiftLeft throws when amount too large', () => { | ||
expect(() => circularShiftLeft([0, 1], 3)).toThrow() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Bits } from '../types' | ||
|
||
/** | ||
* Circular Shift Left | ||
* | ||
* @example | ||
* circularShiftLeft([1,0,1,1,0,1]) => [0,1,1,0,1,1] | ||
* | ||
* @see {@link https://en.wikipedia.org/wiki/Circular_shift} | ||
* | ||
* @param {Array} bits input data | ||
* @param {number} amount how far should it be shifted | ||
* @return {Array} [ROL bits] | ||
*/ | ||
export default (bits: Bits, amount: number): Bits => { | ||
const result: Bits = [] | ||
|
||
if (amount > bits.length) | ||
throw new Error('shift amount can’t be larger than bits array length') | ||
|
||
for (let i = 0; i < bits.length; i++) | ||
result[(bits.length + i - amount) % bits.length] = bits[i] | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Bits } from '../types' | ||
import circularShiftRight from './circular-shift-right' | ||
|
||
test('circularShiftRight with amount 1', () => { | ||
const testCases: Array<[Bits, Bits]> = [ | ||
[ | ||
[1, 0, 0, 0, 1, 1, 0, 1], | ||
[1, 1, 0, 0, 0, 1, 1, 0], | ||
], | ||
[ | ||
[0, 0, 0, 1, 1, 1, 1, 1], | ||
[1, 0, 0, 0, 1, 1, 1, 1], | ||
], | ||
] | ||
|
||
for (const [input, expectedResult] of testCases) | ||
expect(circularShiftRight(input, 1)).toEqual(expectedResult) | ||
}) | ||
|
||
test('circularShiftRight with amount 2', () => { | ||
const testCases: Array<[Bits, Bits]> = [ | ||
[ | ||
[1, 0, 0, 0, 1, 1, 0, 1], | ||
[0, 1, 1, 0, 0, 0, 1, 1], | ||
], | ||
[ | ||
[0, 0, 0, 1, 1, 1, 1, 1], | ||
[1, 1, 0, 0, 0, 1, 1, 1], | ||
], | ||
] | ||
|
||
for (const [input, expectedResult] of testCases) | ||
expect(circularShiftRight(input, 2)).toEqual(expectedResult) | ||
}) | ||
|
||
test('circularShiftRight with amount 3', () => { | ||
const testCases: Array<[Bits, Bits]> = [ | ||
[ | ||
[1, 0, 0, 0, 1, 1, 0, 1], | ||
[1, 0, 1, 1, 0, 0, 0, 1], | ||
], | ||
[ | ||
[0, 0, 0, 1, 1, 1, 1, 1], | ||
[1, 1, 1, 0, 0, 0, 1, 1], | ||
], | ||
] | ||
|
||
for (const [input, expectedResult] of testCases) | ||
expect(circularShiftRight(input, 3)).toEqual(expectedResult) | ||
}) | ||
|
||
test('circularShiftRight throws when amount too large', () => { | ||
expect(() => circularShiftRight([0, 1], 3)).toThrow() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Bits } from '../types' | ||
|
||
/** | ||
* Circular Shift Right | ||
* | ||
* @example | ||
* circularShiftRight([1,0,1,1,0,1]) => [1,1,0,1,1,0] | ||
* | ||
* @see {@link https://en.wikipedia.org/wiki/Circular_shift} | ||
* | ||
* @param {Array} bits input data | ||
* @param {number} amount how far should it be shifted | ||
* @return {Array} [ROR bits] | ||
*/ | ||
export default (bits: Bits, amount: number): Bits => { | ||
const result: Bits = [] | ||
|
||
if (amount > bits.length) | ||
throw new Error('shift amount can’t be larger than bits array length') | ||
|
||
for (let i = 0; i < bits.length; i++) | ||
result[(i + amount) % bits.length] = bits[i] | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters