Skip to content

Instantly share code, notes, and snippets.

@shsunmoonlee
Last active June 4, 2018 19:48
Show Gist options
  • Save shsunmoonlee/b961104c16825167dad6032babd477c8 to your computer and use it in GitHub Desktop.
Save shsunmoonlee/b961104c16825167dad6032babd477c8 to your computer and use it in GitHub Desktop.
module.exports = {
valueAtBit: function(num, bit) {
return (num).toString(2) & (bit).toString(2)
},
base10: function(str) {
return (str).toString(2)
},
convertToBinary: function(num) {
},
multiply: function(a, b) {
}
};
/**
* tests
*/
const expect = require('chai').expect;
const answers = require('../src/numbers');
describe('numbers', function() {
describe('binary operations', function() {
it('you should be able to find the value of a given bit', function() {
expect(answers.valueAtBit(128, 8)).to.eql(1);
expect(answers.valueAtBit(65, 1)).to.eql(1);
expect(answers.valueAtBit(65, 7)).to.eql(1);
expect(answers.valueAtBit(128, 1)).to.eql(0);
});
it('you should be able to return the base10 representation of a binary string', function() {
expect(answers.base10('11000000')).to.eql(192);
});
it('you should be able to convert an eight-bit number to a binary string', function() {
expect(answers.convertToBinary(128)).to.eql('10000000');
expect(answers.convertToBinary(65)).to.eql('01000001');
});
});
describe('decimals', function() {
it('you should be able to multiply with precision', function() {
expect(answers.multiply(3, 0.1)).to.eql(0.3);
expect(answers.multiply(3, 0.2)).to.eql(0.6);
expect(answers.multiply(3, 0.0001)).to.eql(0.0003);
});
});
});
2.
module.exports = {
count : function (start, end) {
return {
cancel: function() {
}
}
}
};
// tests
const expect = require('chai').expect;
const answers = require('../src/count');
/**
* This test describes a function, count, that takes two arguments: a starting number,
* and an ending number. The function should console.log each number from the start
* number to the end number, one number per 1/10th of a second. The function should
* return an object with a cancel method, which should cancel the counting.
*/
describe('counter', function () {
let nums;
beforeEach(function () {
nums = [];
// eslint-disable-next-line no-console
const _orig = console.log;
if (typeof console === 'undefined') {
console = {}; // eslint-disable-line no-native-reassign
}
// eslint-disable-next-line no-console
console.log = function (val) {
nums.push(val);
_orig.apply(console, arguments);
};
});
it('should count from start number to end number, one per 1/10th of a second', function (done) {
this.timeout(600);
answers.count(1, 5);
setTimeout(function () {
expect(nums.length > 1).to.be.ok;
expect(nums.length < 5).to.be.ok;
}, 200);
setTimeout(function () {
expect(nums.length).to.eql(5);
expect(nums[0]).to.eql(1);
expect(nums[4]).to.eql(5);
done();
}, 550);
});
it('should provide a method to cancel the counting', function (done) {
this.timeout(600);
const counter = answers.count(1, 5);
expect(counter).to.be.a('object');
expect(counter.cancel).to.be.a('function');
counter.cancel();
setTimeout(function () {
expect(nums.length < 5).to.be.ok;
done();
}, 550);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment