summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2017-03-08 12:51:49 +0100
committerJules Laplace <jules@okfoc.us>2017-03-08 12:51:49 +0100
commit4e9c0237d9b4a46dff0c8e80316ad2f4886fae7d (patch)
tree30381a22450f99bf95ef1dc137fd152662e924a5
parent0729c707f61254fbff440cf52daff23ae08e148f (diff)
write first test
-rw-r--r--index.js11
-rw-r--r--package.json8
-rw-r--r--test/01-basic.js20
3 files changed, 34 insertions, 5 deletions
diff --git a/index.js b/index.js
index f168d16..6218073 100644
--- a/index.js
+++ b/index.js
@@ -19,7 +19,7 @@ basic.validate_digits = function(s, b) {
// Validate that a base is valid.
basic.validate_base = function(b){
- return (b === 0 || b === 1 || b === -1 && (Math.abs(b) % 2) === 0)
+ return (b !== 0 && b !== 1 && b !== -1 && (Math.abs(b) % 2) === 0)
}
// Convert a string s representing a number in base b
@@ -45,7 +45,7 @@ basic.toString = function (n, b) {
var s = ""
var dividend, residual
while (n) {
- remainder = mod(n, b)
+ remainder = basic.mod(n, b)
n = Math.floor(n/b)
if (remainder) {
n += 1
@@ -55,5 +55,10 @@ basic.toString = function (n, b) {
return s
}
-function mod(n,m){ return n-(m * Math.floor(n/m)) }
+// Compute the residual of N mod M
+// Handle negative N and M correctly
+basic.mod = function (n,m){
+ m = Math.abs(m)
+ return n-(m * Math.floor(n/m))
+}
diff --git a/package.json b/package.json
index d322799..70f12ad 100644
--- a/package.json
+++ b/package.json
@@ -4,7 +4,7 @@
"description": "convert between bases",
"main": "index.js",
"scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
+ "test": "./node_modules/.bin/mocha -R spec"
},
"repository": {
"type": "git",
@@ -15,5 +15,9 @@
"bugs": {
"url": "https://github.com/julescarbon/basic/issues"
},
- "homepage": "https://github.com/julescarbon/basic#readme"
+ "homepage": "https://github.com/julescarbon/basic#readme",
+ "dependencies": {
+ "assert": "^1.4.1",
+ "mocha": "^3.2.0"
+ }
}
diff --git a/test/01-basic.js b/test/01-basic.js
new file mode 100644
index 0000000..5190008
--- /dev/null
+++ b/test/01-basic.js
@@ -0,0 +1,20 @@
+/*
+basic.validate = function (s, b) {
+basic.validate_digits = function(s, b) {
+basic.validate_base = function(b){
+basic.toNumber = function (s, b) {
+basic.toString = function (n, b) {
+basic.mod = function (m, n) {
+*/
+
+var assert = require('assert')
+var basic = require('../index')
+
+describe('basic', function(){
+ describe('#validate_base()', function(){
+ it('validates positive bases', function(done){
+ assert( basic.validate_base(2), true )
+ done()
+ })
+ })
+})