Add buffer shift rotate tests (moved from pxt)

This commit is contained in:
Michal Moskal 2016-08-18 10:52:19 +03:00
parent aeb4cb6a17
commit b863d5663d

View File

@ -22,6 +22,7 @@ console.log("Starting...")
basic.showNumber(0); basic.showNumber(0);
testBuffer() testBuffer()
testBufferShiftRotate()
basic.showNumber(2); basic.showNumber(2);
console.log("ALL TESTS OK") console.log("ALL TESTS OK")
@ -75,3 +76,63 @@ function testBuffer() {
b.shift(-1) b.shift(-1)
bufferIs(b, [0, 4, 13]); bufferIs(b, [0, 4, 13]);
} }
function testBufferShiftRotate() {
let b = pins.createBuffer(5);
function initb() {
for (let i = 0; i < b.length; ++i) {
b[i] = i;
}
}
function assertb(ex: number[]) {
bufferIs(b, ex)
}
initb()
assertb([0, 1, 2, 3, 4])
//shifting
initb()
b.shift(-1);
assertb([0, 0, 1, 2, 3])
initb()
b.shift(-1, 0, 3);
assertb([0, 0, 1, 3, 4])
initb()
b.shift(-1, 1, 3);
assertb([0, 0, 1, 2, 4])
initb()
b.shift(1)
assertb([1, 2, 3, 4, 0])
initb()
b.shift(1, 1, 3)
assertb([0, 2, 3, 0, 4])
//rotating
initb()
b.rotate(-1);
assertb([4, 0, 1, 2, 3])
initb()
b.rotate(-1, 0, 3);
assertb([2, 0, 1, 3, 4])
initb()
b.rotate(-1, 1, 3);
assertb([0, 3, 1, 2, 4])
initb()
b.rotate(2)
assertb([2, 3, 4, 0, 1])
initb()
b.rotate(1, 1, 3)
assertb([0, 2, 3, 1, 4])
}