@rniv/base-n
Version:
## Installation
94 lines (87 loc) • 2.94 kB
JavaScript
const chai = require('chai')
const should = chai.should()
const { BaseN } = require('../')
const bs16 = new BaseN('0123456789abcdef')
describe('BaseN', function () {
describe('constructor(symbols)', function(){
it('should throw error for invalid symbols', function () {
should.Throw(function () {
new BaseN('0123456789*')
})
})
it('Should throw error for duplicate symbols', function () {
should.Throw(function () {
new BaseN('01234567899')
})
})
})
describe('encode(n)', function () {
it('should return encoded string', function () {
const result = bs16.encode(9007199254740991)
result.should.equal('1fffffffffffff')
})
it('should return 0', function() {
const result = bs16.encode('0')
result.should.equal('0')
})
})
describe('decode(s)', function () {
it('should return decoded number', function () {
const result = bs16.decode('1fffffffffffff')
result.should.equal(9007199254740991)
})
it('should throw error for symbols other than base symbols', function(){
should.Throw(function(){
bs16.decode('1A')
})
})
})
describe('compare(s1, s2)', function() {
it('should throw error for invalid symbols', function(){
should.Throw(function(){
bs16.compare('1A', '1B')
})
})
it('should return 1 if s1 > s2', function(){
bs16.compare('100', 'ff').should.equal(1)
})
it('should return -1 if s1 < s2', function(){
bs16.compare('ff', '1ff').should.equal(-1)
})
it('should return 0 if s1 = s2', function(){
bs16.compare('1ff', '1ff').should.equal(0)
})
})
describe('add(s1, s2)', function(){
it('should throw error for invalid symbols', function(){
should.Throw(function(){
bs16.add('1A', '1B')
})
})
it('should return addition of two encoded strings', function(){
bs16.add('1fffffffffffff', '10').should.equal('2000000000000f')
})
it('should return 10 if we add 00000, 10', function() {
bs16.add('0000', '10').should.equal('10')
})
})
describe('subtract(s1, s2, abs=true)', function(){
it('should throw error for invalid symbols', function(){
should.Throw(function(){
bs16.subtract('1A', '1C')
})
})
it('should return absolute subtraction of two encoded strings', function(){
bs16.subtract('1fffffffffffff', '10').should.equal('1fffffffffffef')
})
it('should return absolute subtraction of two encoded strings', function(){
bs16.subtract('10', '1fffffffffffff').should.equal('1fffffffffffef')
})
it('should return subtraction of two encoded strings', function(){
bs16.subtract('10', '1fffffffffffff', false).should.equal('-1fffffffffffef')
})
it('should equal 10 if subtrack 0000, 10', function() {
bs16.subtract('0000', '10').should.equal('10')
})
})
})