mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-21 04:35:49 +00:00
Add unit tests for convertBase
This commit is contained in:
@@ -38,6 +38,9 @@ function encodeString(alphabet, input) {
|
|||||||
return d === 0;
|
return d === 0;
|
||||||
});
|
});
|
||||||
var out = convertBase(input, 256, 58).map(function(digit) {
|
var out = convertBase(input, 256, 58).map(function(digit) {
|
||||||
|
if (digit < 0 || digit >= alphabet.length) {
|
||||||
|
throw new Error('Value ' + digit + ' is out of bounds for encoding');
|
||||||
|
}
|
||||||
return alphabet[digit];
|
return alphabet[digit];
|
||||||
});
|
});
|
||||||
var prefix = leadingZeros.map(function() {
|
var prefix = leadingZeros.map(function() {
|
||||||
@@ -52,7 +55,11 @@ function decodeString(indexes, input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var input58 = input.split('').map(function(c) {
|
var input58 = input.split('').map(function(c) {
|
||||||
return indexes[c.charCodeAt(0)];
|
var charCode = c.charCodeAt(0);
|
||||||
|
if (charCode >= indexes.length) {
|
||||||
|
throw new Error('Character ' + c + ' is not valid for encoding');
|
||||||
|
}
|
||||||
|
return indexes[charCode];
|
||||||
});
|
});
|
||||||
var leadingZeros = _.takeWhile(input58, function(d) {
|
var leadingZeros = _.takeWhile(input58, function(d) {
|
||||||
return d === 0;
|
return d === 0;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
'use strict';
|
||||||
exports.Remote = require('./remote').Remote;
|
exports.Remote = require('./remote').Remote;
|
||||||
exports.Request = require('./request').Request;
|
exports.Request = require('./request').Request;
|
||||||
exports.Amount = require('./amount').Amount;
|
exports.Amount = require('./amount').Amount;
|
||||||
@@ -20,6 +21,7 @@ exports.Wallet = require('./wallet');
|
|||||||
exports.Ledger = require('./ledger').Ledger;
|
exports.Ledger = require('./ledger').Ledger;
|
||||||
exports.TransactionQueue = require('./transactionqueue').TransactionQueue;
|
exports.TransactionQueue = require('./transactionqueue').TransactionQueue;
|
||||||
exports.RangeSet = require('./rangeset').RangeSet;
|
exports.RangeSet = require('./rangeset').RangeSet;
|
||||||
|
exports.convertBase = require('./baseconverter');
|
||||||
|
|
||||||
// Important: We do not guarantee any specific version of SJCL or for any
|
// Important: We do not guarantee any specific version of SJCL or for any
|
||||||
// specific features to be included. The version and configuration may change at
|
// specific features to be included. The version and configuration may change at
|
||||||
@@ -34,8 +36,8 @@ exports.types = require('./serializedtypes');
|
|||||||
exports.config = require('./config');
|
exports.config = require('./config');
|
||||||
|
|
||||||
// camelCase to under_scored API conversion
|
// camelCase to under_scored API conversion
|
||||||
function attachUnderscored(c) {
|
function attachUnderscored(name) {
|
||||||
var o = exports[c];
|
var o = exports[name];
|
||||||
|
|
||||||
Object.keys(o.prototype).forEach(function(key) {
|
Object.keys(o.prototype).forEach(function(key) {
|
||||||
var UPPERCASE = /([A-Z]{1})[a-z]+/g;
|
var UPPERCASE = /([A-Z]{1})[a-z]+/g;
|
||||||
@@ -50,7 +52,7 @@ function attachUnderscored(c) {
|
|||||||
|
|
||||||
o.prototype[underscored] = o.prototype[key];
|
o.prototype[underscored] = o.prototype[key];
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
['Remote',
|
['Remote',
|
||||||
'Request',
|
'Request',
|
||||||
|
|||||||
53
test/baseconverter-test.js
Normal file
53
test/baseconverter-test.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
'use strict';
|
||||||
|
var assert = require('assert');
|
||||||
|
var convertBase = require('ripple-lib').convertBase;
|
||||||
|
|
||||||
|
// Test cases from RFC-1924 (a joke RFC)
|
||||||
|
var BASE85 = ('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
|
+ 'abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~');
|
||||||
|
var BASE10 = BASE85.slice(0, 10);
|
||||||
|
var BASE16 = BASE85.slice(0, 16);
|
||||||
|
|
||||||
|
var DATA16 = '108000000000000000080800200C417A';
|
||||||
|
var DATA10 = '21932261930451111902915077091070067066';
|
||||||
|
var DATA85 = '4)+k&C#VzJ4br>0wv%Yp';
|
||||||
|
|
||||||
|
function encode(digitArray, encoding) {
|
||||||
|
return digitArray.map(function(i) {
|
||||||
|
return encoding.charAt(i);
|
||||||
|
}).join('');
|
||||||
|
}
|
||||||
|
|
||||||
|
function decode(encoded, encoding) {
|
||||||
|
return encoded.split('').map(function(c) {
|
||||||
|
return encoding.indexOf(c);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function convertBaseEncoded(value, fromEncoding, toEncoding) {
|
||||||
|
var digitArray = decode(value, fromEncoding);
|
||||||
|
var converted = convertBase(digitArray, fromEncoding.length,
|
||||||
|
toEncoding.length);
|
||||||
|
return encode(converted, toEncoding);
|
||||||
|
}
|
||||||
|
|
||||||
|
describe('convertBase', function() {
|
||||||
|
it('DEC -> HEX', function () {
|
||||||
|
assert.strictEqual(convertBaseEncoded(DATA10, BASE10, BASE16), DATA16);
|
||||||
|
});
|
||||||
|
it('HEX -> DEC', function () {
|
||||||
|
assert.strictEqual(convertBaseEncoded(DATA16, BASE16, BASE10), DATA10);
|
||||||
|
});
|
||||||
|
it('DEC -> B85', function () {
|
||||||
|
assert.strictEqual(convertBaseEncoded(DATA10, BASE10, BASE85), DATA85);
|
||||||
|
});
|
||||||
|
it('HEX -> B85', function () {
|
||||||
|
assert.strictEqual(convertBaseEncoded(DATA16, BASE16, BASE85), DATA85);
|
||||||
|
});
|
||||||
|
it('B85 -> DEC', function () {
|
||||||
|
assert.strictEqual(convertBaseEncoded(DATA85, BASE85, BASE10), DATA10);
|
||||||
|
});
|
||||||
|
it('B85 -> HEX', function () {
|
||||||
|
assert.strictEqual(convertBaseEncoded(DATA85, BASE85, BASE16), DATA16);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user