opcode refactor: isSmallIntOp
This commit is contained in:
parent
4a6755d0d1
commit
350816ad40
|
@ -1,8 +1,9 @@
|
|||
'use strict';
|
||||
|
||||
function Opcode(num) {
|
||||
if (!(this instanceof Opcode))
|
||||
if (!(this instanceof Opcode)) {
|
||||
return new Opcode(num);
|
||||
}
|
||||
|
||||
if (typeof num === 'number') {
|
||||
this.num = num;
|
||||
|
@ -39,8 +40,9 @@ Opcode.prototype.fromString = function(str) {
|
|||
|
||||
Opcode.prototype.toString = function() {
|
||||
var str = Opcode.reverseMap[this.num];
|
||||
if (typeof str === 'undefined')
|
||||
if (typeof str === 'undefined') {
|
||||
throw new Error('Opcode does not have a string representation');
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
|
@ -191,4 +193,15 @@ for (var k in Opcode.map) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns true if opcode is one of OP_0, OP_1, ..., OP_16
|
||||
*/
|
||||
Opcode.isSmallIntOp = function(opcode) {
|
||||
if (opcode instanceof Opcode) {
|
||||
opcode = opcode.toNumber();
|
||||
}
|
||||
return ((opcode === Opcode.map.OP_0) ||
|
||||
((opcode >= Opcode.map.OP_1) && (opcode <= Opcode.map.OP_16)));
|
||||
};
|
||||
|
||||
module.exports = Opcode;
|
||||
|
|
|
@ -11,13 +11,13 @@ describe('Opcode', function() {
|
|||
var opcode = new Opcode(5);
|
||||
should.exist(opcode);
|
||||
});
|
||||
|
||||
|
||||
it('should convert to a string with this handy syntax', function() {
|
||||
Opcode(0).toString().should.equal('OP_0');
|
||||
Opcode(96).toString().should.equal('OP_16');
|
||||
Opcode(97).toString().should.equal('OP_NOP');
|
||||
});
|
||||
|
||||
|
||||
it('should convert to a number with this handy syntax', function() {
|
||||
Opcode('OP_0').toNumber().should.equal(0);
|
||||
Opcode('OP_16').toNumber().should.equal(96);
|
||||
|
@ -25,37 +25,37 @@ describe('Opcode', function() {
|
|||
});
|
||||
|
||||
describe('#fromNumber', function() {
|
||||
|
||||
|
||||
it('should work for 0', function() {
|
||||
Opcode().fromNumber(0).num.should.equal(0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('#toNumber', function() {
|
||||
|
||||
|
||||
it('should work for 0', function() {
|
||||
Opcode().fromNumber(0).toNumber().should.equal(0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('#fromString', function() {
|
||||
|
||||
|
||||
it('should work for OP_0', function() {
|
||||
Opcode().fromString('OP_0').num.should.equal(0);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('#toString', function() {
|
||||
|
||||
|
||||
it('should work for OP_0', function() {
|
||||
Opcode().fromString('OP_0').toString().should.equal('OP_0');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
describe('@map', function() {
|
||||
|
||||
it('should have a map containing 116 elements', function() {
|
||||
|
@ -65,12 +65,50 @@ describe('Opcode', function() {
|
|||
});
|
||||
|
||||
describe('@reverseMap', function() {
|
||||
|
||||
|
||||
it('should exist and have op 185', function() {
|
||||
should.exist(Opcode.reverseMap);
|
||||
Opcode.reverseMap[185].should.equal('OP_NOP10');
|
||||
});
|
||||
|
||||
});
|
||||
var smallints = [
|
||||
Opcode('OP_0'),
|
||||
Opcode('OP_1'),
|
||||
Opcode('OP_2'),
|
||||
Opcode('OP_3'),
|
||||
Opcode('OP_4'),
|
||||
Opcode('OP_5'),
|
||||
Opcode('OP_6'),
|
||||
Opcode('OP_7'),
|
||||
Opcode('OP_8'),
|
||||
Opcode('OP_9'),
|
||||
Opcode('OP_10'),
|
||||
Opcode('OP_11'),
|
||||
Opcode('OP_12'),
|
||||
Opcode('OP_13'),
|
||||
Opcode('OP_14'),
|
||||
Opcode('OP_15'),
|
||||
Opcode('OP_16')
|
||||
];
|
||||
|
||||
describe('@isSmallIntOp', function() {
|
||||
var testSmallInt = function() {
|
||||
Opcode.isSmallIntOp(this).should.equal(true);
|
||||
};
|
||||
for (var i = 0; i < smallints.length; i++) {
|
||||
var op = smallints[i];
|
||||
it('should work for small int ' + op, testSmallInt.bind(op));
|
||||
}
|
||||
|
||||
it('should work for non-small ints', function() {
|
||||
Opcode.isSmallIntOp(Opcode('OP_RETURN')).should.equal(false);
|
||||
Opcode.isSmallIntOp(Opcode('OP_CHECKSIG')).should.equal(false);
|
||||
Opcode.isSmallIntOp(Opcode('OP_IF')).should.equal(false);
|
||||
Opcode.isSmallIntOp(Opcode('OP_NOP')).should.equal(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue