Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

CBuffer

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

CBuffer - npm Package Compare versions

Comparing version 2.1.0 to 2.2.0

cbuffer.d.ts

640

cbuffer.js
(function (global) {
function CBuffer() {
// handle cases where "new" keyword wasn't used
if (!(this instanceof CBuffer)) {
// multiple conditions need to be checked to properly emulate Array
if (arguments.length > 1 || typeof arguments[0] !== 'number') {
return CBuffer.apply(new CBuffer(arguments.length), arguments);
} else {
return new CBuffer(arguments[0]);
}
}
// if no arguments, then nothing needs to be set
if (arguments.length === 0)
throw new Error('Missing Argument: You must pass a valid buffer size');
// this is the same in either scenario
this.length = this.start = 0;
// set to callback fn if data is about to be overwritten
this.overflow = null;
// emulate Array based on passed arguments
if (arguments.length > 1 || typeof arguments[0] !== 'number') {
this.data = new Array(arguments.length);
this.end = (this.size = arguments.length) - 1;
this.push.apply(this, arguments);
} else {
this.data = new Array(arguments[0]);
this.end = (this.size = arguments[0]) - 1;
}
// need to `return this` so `return CBuffer.apply` works
return this;
// handle cases where "new" keyword wasn't used
if (!(this instanceof CBuffer)) {
// multiple conditions need to be checked to properly emulate Array
if (arguments.length > 1 || typeof arguments[0] !== 'number') {
return CBuffer.apply(new CBuffer(arguments.length), arguments);
} else {
return new CBuffer(arguments[0]);
}
}
// if no arguments, then nothing needs to be set
if (arguments.length === 0)
throw new Error('Missing Argument: You must pass a valid buffer size');
// this is the same in either scenario
this.length = this.start = 0;
// set to callback fn if data is about to be overwritten
this.overflow = null;
// emulate Array based on passed arguments
if (arguments.length > 1 || typeof arguments[0] !== 'number') {
this.data = new Array(arguments.length);
this.end = (this.size = arguments.length) - 1;
this.push.apply(this, arguments);
} else {
this.data = new Array(arguments[0]);
this.end = (this.size = arguments[0]) - 1;
}
// need to `return this` so `return CBuffer.apply` works
return this;
}
function defaultComparitor(a, b) {
return a == b ? 0 : a > b ? 1 : -1;
return a == b ? 0 : a > b ? 1 : -1;
}

@@ -42,305 +42,305 @@

CBuffer.prototype = {
// properly set constructor
constructor : CBuffer,
// properly set constructor
constructor : CBuffer,
/* mutator methods */
// pop last item
pop : function () {
var item;
if (this.length === 0) return;
item = this.data[this.end];
// remove the reference to the object so it can be garbage collected
delete this.data[this.end];
this.end = (this.end - 1 + this.size) % this.size;
this.length--;
return item;
},
// push item to the end
push : function () {
var i = 0;
// check if overflow is set, and if data is about to be overwritten
if (this.overflow && this.length + arguments.length > this.size) {
// call overflow function and send data that's about to be overwritten
for (; i < this.length + arguments.length - this.size; i++) {
this.overflow(this.data[(this.end + i + 1) % this.size], this);
}
}
// push items to the end, wrapping and erasing existing items
// using arguments variable directly to reduce gc footprint
for (i = 0; i < arguments.length; i++) {
this.data[(this.end + i + 1) % this.size] = arguments[i];
}
// recalculate length
if (this.length < this.size) {
if (this.length + i > this.size) this.length = this.size;
else this.length += i;
}
// recalculate end
this.end = (this.end + i) % this.size;
// recalculate start
this.start = (this.size + this.end - this.length + 1) % this.size;
// return number current number of items in CBuffer
return this.length;
},
// reverse order of the buffer
reverse : function () {
var i = 0,
tmp;
for (; i < ~~(this.length / 2); i++) {
tmp = this.data[(this.start + i) % this.size];
this.data[(this.start + i) % this.size] = this.data[(this.start + (this.length - i - 1)) % this.size];
this.data[(this.start + (this.length - i - 1)) % this.size] = tmp;
}
return this;
},
// rotate buffer to the left by cntr, or by 1
rotateLeft : function (cntr) {
if (typeof cntr === 'undefined') cntr = 1;
if (typeof cntr !== 'number') throw new Error("Argument must be a number");
while (--cntr >= 0) {
this.push(this.shift());
}
return this;
},
// rotate buffer to the right by cntr, or by 1
rotateRight : function (cntr) {
if (typeof cntr === 'undefined') cntr = 1;
if (typeof cntr !== 'number') throw new Error("Argument must be a number");
while (--cntr >= 0) {
this.unshift(this.pop());
}
return this;
},
// remove and return first item
shift : function () {
var item;
// check if there are any items in CBuff
if (this.length === 0) return;
// store first item for return
item = this.data[this.start];
// recalculate start of CBuffer
this.start = (this.start + 1) % this.size;
// decrement length
this.length--;
return item;
},
// sort items
sort : function (fn) {
this.data.sort(fn || defaultComparitor);
this.start = 0;
this.end = this.length - 1;
return this;
},
// add item to beginning of buffer
unshift : function () {
var i = 0;
// check if overflow is set, and if data is about to be overwritten
if (this.overflow && this.length + arguments.length > this.size) {
// call overflow function and send data that's about to be overwritten
for (; i < this.length + arguments.length - this.size; i++) {
this.overflow(this.data[this.end - (i % this.size)], this);
}
}
for (i = 0; i < arguments.length; i++) {
this.data[(this.size + this.start - (i % this.size) - 1) % this.size] = arguments[i];
}
if (this.size - this.length - i < 0) {
this.end += this.size - this.length - i;
if (this.end < 0) this.end = this.size + (this.end % this.size);
}
if (this.length < this.size) {
if (this.length + i > this.size) this.length = this.size;
else this.length += i;
}
this.start -= arguments.length;
if (this.start < 0) this.start = this.size + (this.start % this.size);
return this.length;
},
/* mutator methods */
// pop last item
pop : function () {
var item;
if (this.length === 0) return;
item = this.data[this.end];
// remove the reference to the object so it can be garbage collected
delete this.data[this.end];
this.end = (this.end - 1 + this.size) % this.size;
this.length--;
return item;
},
// push item to the end
push : function () {
var i = 0;
// check if overflow is set, and if data is about to be overwritten
if (this.overflow && this.length + arguments.length > this.size) {
// call overflow function and send data that's about to be overwritten
for (; i < this.length + arguments.length - this.size; i++) {
this.overflow(this.data[(this.end + i + 1) % this.size], this);
}
}
// push items to the end, wrapping and erasing existing items
// using arguments variable directly to reduce gc footprint
for (i = 0; i < arguments.length; i++) {
this.data[(this.end + i + 1) % this.size] = arguments[i];
}
// recalculate length
if (this.length < this.size) {
if (this.length + i > this.size) this.length = this.size;
else this.length += i;
}
// recalculate end
this.end = (this.end + i) % this.size;
// recalculate start
this.start = (this.size + this.end - this.length + 1) % this.size;
// return number current number of items in CBuffer
return this.length;
},
// reverse order of the buffer
reverse : function () {
var i = 0,
tmp;
for (; i < ~~(this.length / 2); i++) {
tmp = this.data[(this.start + i) % this.size];
this.data[(this.start + i) % this.size] = this.data[(this.start + (this.length - i - 1)) % this.size];
this.data[(this.start + (this.length - i - 1)) % this.size] = tmp;
}
return this;
},
// rotate buffer to the left by cntr, or by 1
rotateLeft : function (cntr) {
if (typeof cntr === 'undefined') cntr = 1;
if (typeof cntr !== 'number') throw new Error("Argument must be a number");
while (--cntr >= 0) {
this.push(this.shift());
}
return this;
},
// rotate buffer to the right by cntr, or by 1
rotateRight : function (cntr) {
if (typeof cntr === 'undefined') cntr = 1;
if (typeof cntr !== 'number') throw new Error("Argument must be a number");
while (--cntr >= 0) {
this.unshift(this.pop());
}
return this;
},
// remove and return first item
shift : function () {
var item;
// check if there are any items in CBuff
if (this.length === 0) return;
// store first item for return
item = this.data[this.start];
// recalculate start of CBuffer
this.start = (this.start + 1) % this.size;
// decrement length
this.length--;
return item;
},
// sort items
sort : function (fn) {
this.data.sort(fn || defaultComparitor);
this.start = 0;
this.end = this.length - 1;
return this;
},
// add item to beginning of buffer
unshift : function () {
var i = 0;
// check if overflow is set, and if data is about to be overwritten
if (this.overflow && this.length + arguments.length > this.size) {
// call overflow function and send data that's about to be overwritten
for (; i < this.length + arguments.length - this.size; i++) {
this.overflow(this.data[this.end - (i % this.size)], this);
}
}
for (i = 0; i < arguments.length; i++) {
this.data[(this.size + this.start - (i % this.size) - 1) % this.size] = arguments[i];
}
if (this.size - this.length - i < 0) {
this.end += this.size - this.length - i;
if (this.end < 0) this.end = this.size + (this.end % this.size);
}
if (this.length < this.size) {
if (this.length + i > this.size) this.length = this.size;
else this.length += i;
}
this.start -= arguments.length;
if (this.start < 0) this.start = this.size + (this.start % this.size);
return this.length;
},
/* accessor methods */
// return index of first matched element
indexOf : function (arg, idx) {
if (!idx) idx = 0;
for (; idx < this.length; idx++) {
if (this.data[(this.start + idx) % this.size] === arg) return idx;
}
return -1;
},
// return last index of the first match
lastIndexOf : function (arg, idx) {
if (!idx) idx = this.length - 1;
for (; idx >= 0; idx--) {
if (this.data[(this.start + idx) % this.size] === arg) return idx;
}
return -1;
},
/* accessor methods */
// return index of first matched element
indexOf : function (arg, idx) {
if (!idx) idx = 0;
for (; idx < this.length; idx++) {
if (this.data[(this.start + idx) % this.size] === arg) return idx;
}
return -1;
},
// return last index of the first match
lastIndexOf : function (arg, idx) {
if (!idx) idx = this.length - 1;
for (; idx >= 0; idx--) {
if (this.data[(this.start + idx) % this.size] === arg) return idx;
}
return -1;
},
// return the index an item would be inserted to if this
// is a sorted circular buffer
sortedIndex : function(value, comparitor, context) {
comparitor = comparitor || defaultComparitor;
var isFull = this.length === this.size,
low = this.start,
high = isFull ? this.length - 1 : this.length;
// return the index an item would be inserted to if this
// is a sorted circular buffer
sortedIndex : function(value, comparitor, context) {
comparitor = comparitor || defaultComparitor;
var isFull = this.length === this.size,
low = this.start,
high = isFull ? this.length - 1 : this.length;
// Tricky part is finding if its before or after the pivot
// we can get this info by checking if the target is less than
// the last item. After that it's just a typical binary search.
if (low && comparitor.call(context, value, this.data[high]) > 0) {
low = 0, high = this.end;
}
// Tricky part is finding if its before or after the pivot
// we can get this info by checking if the target is less than
// the last item. After that it's just a typical binary search.
if (low && comparitor.call(context, value, this.data[high]) > 0) {
low = 0, high = this.end;
}
while (low < high) {
var mid = (low + high) >>> 1;
if (comparitor.call(context, value, this.data[mid]) > 0) low = mid + 1;
else high = mid;
}
return !isFull ? low :
// http://stackoverflow.com/a/18618273/1517919
(((low - this.start) % this.length) + this.length) % this.length;
},
while (low < high) {
var mid = (low + high) >>> 1;
if (comparitor.call(context, value, this.data[mid]) > 0) low = mid + 1;
else high = mid;
}
return !isFull ? low :
// http://stackoverflow.com/a/18618273/1517919
(((low - this.start) % this.length) + this.length) % this.length;
},
/* iteration methods */
// check every item in the array against a test
every : function (callback, context) {
var i = 0;
for (; i < this.length; i++) {
if (!callback.call(context, this.data[(this.start + i) % this.size], i, this))
return false;
}
return true;
},
// loop through each item in buffer
// TODO: figure out how to emulate Array use better
forEach : function (callback, context) {
var i = 0;
for (; i < this.length; i++) {
callback.call(context, this.data[(this.start + i) % this.size], i, this);
}
},
// construct new CBuffer of same length, apply map function, and return new CBuffer
map : function (callback, context) {
var outCBuffer = new CBuffer(this.size);
for (var i = 0; i < this.length; i++) {
var n = (this.start + i) % this.size;
outCBuffer.push(callback.call(context, this.data[n], i, this));
}
return outCBuffer;
},
// check items agains test until one returns true
// TODO: figure out how to emulate Array use better
some : function (callback, context) {
var i = 0;
for (; i < this.length; i++) {
if (callback.call(context, this.data[(this.start + i) % this.size], i, this))
return true;
}
return false;
},
// calculate the average value of a circular buffer
avg : function () {
return this.length == 0 ? 0 : (this.sum() / this.length);
},
// loop through each item in buffer and calculate sum
sum : function () {
var index = this.length;
var s = 0;
while (index--) s += this.data[index];
return s;
},
// loop through each item in buffer and calculate median
median : function () {
if (this.length === 0)
return 0;
var values = this.slice().sort(defaultComparitor);
var half = Math.floor(values.length / 2);
if(values.length % 2)
return values[half];
else
return (values[half-1] + values[half]) / 2.0;
},
/* utility methods */
// reset pointers to buffer with zero items
// note: this will not remove values in cbuffer, so if for security values
// need to be overwritten, run `.fill(null).empty()`
empty : function () {
var i = 0;
this.length = this.start = 0;
this.end = this.size - 1;
return this;
},
// fill all places with passed value or function
fill : function (arg) {
var i = 0;
if (typeof arg === 'function') {
while(this.data[i] = arg(), ++i < this.size);
} else {
while(this.data[i] = arg, ++i < this.size);
}
// reposition start/end
this.start = 0;
this.end = this.size - 1;
this.length = this.size;
return this;
},
// return first item in buffer
first : function () {
return this.data[this.start];
},
// return last item in buffer
last : function () {
return this.data[this.end];
},
// return specific index in buffer
get : function (arg) {
return this.data[mod(this.start + arg, this.size)];
},
isFull : function (arg) {
return this.size === this.length;
},
// set value at specified index
set : function (idx, arg) {
return this.data[(this.start + idx) % this.size] = arg;
},
// return clean array of values
toArray : function () {
return this.slice();
},
// return a string based on the array
join : function(separator) {
if (!separator) separator = ',';
var outString = new String(this.data[0]);
for (var i = 1; i < this.length; i++) {
var n = (this.start + i) % this.size;
outString = outString.concat(separator, this.data[i]);
}
return outString;
},
// slice the buffer to an arraay
slice : function (start, end) {
var size = this.length;
/* iteration methods */
// check every item in the array against a test
every : function (callback, context) {
var i = 0;
for (; i < this.length; i++) {
if (!callback.call(context, this.data[(this.start + i) % this.size], i, this))
return false;
}
return true;
},
// loop through each item in buffer
// TODO: figure out how to emulate Array use better
forEach : function (callback, context) {
var i = 0;
for (; i < this.length; i++) {
callback.call(context, this.data[(this.start + i) % this.size], i, this);
}
},
// construct new CBuffer of same length, apply map function, and return new CBuffer
map : function (callback, context) {
var outCBuffer = new CBuffer(this.size);
for (var i = 0; i < this.length; i++) {
var n = (this.start + i) % this.size;
outCBuffer.push(callback.call(context, this.data[n], i, this));
}
return outCBuffer;
},
// check items agains test until one returns true
// TODO: figure out how to emulate Array use better
some : function (callback, context) {
var i = 0;
for (; i < this.length; i++) {
if (callback.call(context, this.data[(this.start + i) % this.size], i, this))
return true;
}
return false;
},
// calculate the average value of a circular buffer
avg : function () {
return this.length == 0 ? 0 : (this.sum() / this.length);
},
// loop through each item in buffer and calculate sum
sum : function () {
var index = this.length;
var s = 0;
while (index--) s += this.data[index];
return s;
},
// loop through each item in buffer and calculate median
median : function () {
if (this.length === 0)
return 0;
var values = this.slice().sort(defaultComparitor);
var half = Math.floor(values.length / 2);
if(values.length % 2)
return values[half];
else
return (values[half-1] + values[half]) / 2.0;
},
/* utility methods */
// reset pointers to buffer with zero items
// note: this will not remove values in cbuffer, so if for security values
// need to be overwritten, run `.fill(null).empty()`
empty : function () {
var i = 0;
this.length = this.start = 0;
this.end = this.size - 1;
return this;
},
// fill all places with passed value or function
fill : function (arg) {
var i = 0;
if (typeof arg === 'function') {
while(this.data[i] = arg(), ++i < this.size);
} else {
while(this.data[i] = arg, ++i < this.size);
}
// reposition start/end
this.start = 0;
this.end = this.size - 1;
this.length = this.size;
return this;
},
// return first item in buffer
first : function () {
return this.data[this.start];
},
// return last item in buffer
last : function () {
return this.data[this.end];
},
// return specific index in buffer
get : function (arg) {
return this.data[mod(this.start + arg, this.size)];
},
isFull : function (arg) {
return this.size === this.length;
},
// set value at specified index
set : function (idx, arg) {
return this.data[(this.start + idx) % this.size] = arg;
},
// return clean array of values
toArray : function () {
return this.slice();
},
// return a string based on the array
join : function(separator) {
if (!separator) separator = ',';
var outString = new String(this.data[0]);
for (var i = 1; i < this.length; i++) {
var n = (this.start + i) % this.size;
outString = outString.concat(separator, this.data[i]);
}
return outString;
},
// slice the buffer to an arraay
slice : function (start, end) {
var size = this.length;
start = +start || 0;
start = +start || 0;
if (start < 0) {
if (start >= end)
return [];
start = (-start > size) ? 0 : size + start;
}
if (start < 0) {
if (start >= end)
return [];
start = (-start > size) ? 0 : size + start;
}
if (end == null || end > size)
end = size;
else if (end < 0)
end += size;
else
end = +end || 0;
if (end == null || end > size)
end = size;
else if (end < 0)
end += size;
else
end = +end || 0;
size = start < end ? end - start : 0;
size = start < end ? end - start : 0;
var result = Array(size);
for (var index = 0; index < size; index++) {
result[index] = this.data[(this.start + start + index) % this.size];
}
return result;
}
var result = Array(size);
for (var index = 0; index < size; index++) {
result[index] = this.data[(this.start + start + index) % this.size];
}
return result;
}
};

@@ -347,0 +347,0 @@

{
"name" : "CBuffer",
"version" : "2.1.0",
"description" : "Circular Buffer JavaScript implementation",
"homepage" : "https://github.com/trevnorris/cbuffer",
"main" : "./cbuffer.js",
"author" : {
"name" : "Trevor Norris",
"email" : "trev.norris@gmail.com",
"url" : "http://trevorjnorris.com"
},
"license": "MIT",
"licenses" : [{
"type" : "MIT",
"url" : "http://www.opensource.org/licenses/mit-license.php"
}],
"keywords": [
"circular buffer",
"ring",
"circular",
"buffer",
"data structure"
],
"devDependencies" : {
"vows" : "latest"
},
"scripts" : {
"test" : "vows"
},
"repository" : {
"type" : "git",
"url" : "https://github.com/trevnorris/cbuffer.git"
}
"name": "CBuffer",
"version": "2.2.0",
"description": "Circular Buffer JavaScript implementation",
"homepage": "https://github.com/trevnorris/cbuffer",
"main": "./cbuffer.js",
"types": "cbuffer.d.ts",
"author": {
"name": "Trevor Norris",
"email": "trev.norris@gmail.com",
"url": "http://trevorjnorris.com"
},
"license": "MIT",
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
],
"keywords": [
"circular buffer",
"ring",
"circular",
"buffer",
"data structure"
],
"devDependencies": {
"vows": "latest"
},
"scripts": {
"test": "vows"
},
"repository": {
"type": "git",
"url": "https://github.com/trevnorris/cbuffer.git"
}
}

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('indexOf');

@@ -8,15 +8,15 @@ require('../env');

suite.addBatch({
'indexOf' : {
'topic' : function () {
return CBuffer;
},
'find item' : function (CBuffer) {
assert.equal(CBuffer(1, 2, 3).indexOf(2), 1);
assert.equal(CBuffer('a', 'b', 'c').indexOf('c'), 2);
assert.equal(CBuffer(1, 2, 3).indexOf('1'), -1);
assert.equal(CBuffer(1, 2, 3).indexOf(4), -1);
}
}
'indexOf' : {
'topic' : function () {
return CBuffer;
},
'find item' : function (CBuffer) {
assert.equal(CBuffer(1, 2, 3).indexOf(2), 1);
assert.equal(CBuffer('a', 'b', 'c').indexOf('c'), 2);
assert.equal(CBuffer(1, 2, 3).indexOf('1'), -1);
assert.equal(CBuffer(1, 2, 3).indexOf(4), -1);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('lastIndexOf');

@@ -8,15 +8,15 @@ require('../env');

suite.addBatch({
'lastIndexOf' : {
'topic' : function () {
return CBuffer;
},
'find item' : function (CBuffer) {
assert.equal(CBuffer(1, 2, 3).lastIndexOf(2), 1);
assert.equal(CBuffer('a', 'b', 'c').lastIndexOf('c'), 2);
assert.equal(CBuffer(1, 2, 3).lastIndexOf('1'), -1);
assert.equal(CBuffer(1, 2, 3).lastIndexOf(4), -1);
}
}
'lastIndexOf' : {
'topic' : function () {
return CBuffer;
},
'find item' : function (CBuffer) {
assert.equal(CBuffer(1, 2, 3).lastIndexOf(2), 1);
assert.equal(CBuffer('a', 'b', 'c').lastIndexOf('c'), 2);
assert.equal(CBuffer(1, 2, 3).lastIndexOf('1'), -1);
assert.equal(CBuffer(1, 2, 3).lastIndexOf(4), -1);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert'),
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('sortedIndex');

@@ -8,96 +8,96 @@ require('../env');

suite.addBatch({
'sortedIndex': {
'works with simple sorted case': {
topic: function() {
return CBuffer(1, 3, 5, 7, 11);
},
'no iterator provided': function(buffer) {
assert.equal(buffer.sortedIndex(-1), 0);
assert.equal(buffer.sortedIndex(1.5), 1);
},
'returns index of existing items': function(buffer) {
assert.equal(buffer.sortedIndex(1), 0);
assert.equal(buffer.sortedIndex(5), 2);
assert.equal(buffer.sortedIndex(11), 4);
},
'around the corner': function(buffer) {
assert.equal(buffer.sortedIndex(-1), 0);
assert.equal(buffer.sortedIndex(12), 4);
},
'takes an iterator': function() {
var buffer = CBuffer(7, 5, 11, 3, 1);
function iter(a, b) {
return Math.abs(a - 8) - Math.abs(b - 8);
}
assert.equal(buffer.sortedIndex(6, iter), 1);
},
'sortedIndex': {
'works with simple sorted case': {
topic: function() {
return CBuffer(1, 3, 5, 7, 11);
},
'no iterator provided': function(buffer) {
assert.equal(buffer.sortedIndex(-1), 0);
assert.equal(buffer.sortedIndex(1.5), 1);
},
'returns index of existing items': function(buffer) {
assert.equal(buffer.sortedIndex(1), 0);
assert.equal(buffer.sortedIndex(5), 2);
assert.equal(buffer.sortedIndex(11), 4);
},
'around the corner': function(buffer) {
assert.equal(buffer.sortedIndex(-1), 0);
assert.equal(buffer.sortedIndex(12), 4);
},
'takes an iterator': function() {
var buffer = CBuffer(7, 5, 11, 3, 1);
function iter(a, b) {
return Math.abs(a - 8) - Math.abs(b - 8);
}
assert.equal(buffer.sortedIndex(6, iter), 1);
},
'non-full circular buffer': {
topic: function() {
var buffer = CBuffer(20);
buffer.push(1, 2, 3, 4, 5, 6, 7, 8);
return buffer;
},
'works with partially complete buffers': function(buffer) {
assert.equal(buffer.sortedIndex(3), 2);
assert.equal(buffer.sortedIndex(8), 7);
},
'can determine postion in a fixed length buffer': function(buffer) {
assert.equal(buffer.sortedIndex(0), 0);
assert.equal(buffer.sortedIndex(1), 0);
assert.equal(buffer.sortedIndex(3), 2);
assert.equal(buffer.sortedIndex(10), 8);
}
},
'non-full circular buffer': {
topic: function() {
var buffer = CBuffer(20);
buffer.push(1, 2, 3, 4, 5, 6, 7, 8);
return buffer;
},
'works with partially complete buffers': function(buffer) {
assert.equal(buffer.sortedIndex(3), 2);
assert.equal(buffer.sortedIndex(8), 7);
},
'can determine postion in a fixed length buffer': function(buffer) {
assert.equal(buffer.sortedIndex(0), 0);
assert.equal(buffer.sortedIndex(1), 0);
assert.equal(buffer.sortedIndex(3), 2);
assert.equal(buffer.sortedIndex(10), 8);
}
},
'supports classic repeative item case': {
topic: function() {
return CBuffer(0, 0, 0, 1, 1, 1, 1, 1, 1, 3);
},
'supports repative item': function(buffer) {
assert.equal(buffer.sortedIndex(2), 9);
}
}
},
'supports classic repeative item case': {
topic: function() {
return CBuffer(0, 0, 0, 1, 1, 1, 1, 1, 1, 3);
},
'supports repative item': function(buffer) {
assert.equal(buffer.sortedIndex(2), 9);
}
}
},
'handles circular cases': {
'mid circular case': {
topic: function() {
var buffer = CBuffer(-1, 0, 1, 3, 5);
buffer.push(7, 9);
return buffer;
},
'simple circular buffer': function(buffer) {
assert.equal(buffer.sortedIndex(0), 0);
assert.equal(buffer.sortedIndex(2), 1);
assert.equal(buffer.sortedIndex(4), 2);
assert.equal(buffer.sortedIndex(7), 3);
assert.equal(buffer.sortedIndex(8), 4);
assert.equal(buffer.sortedIndex(10), 4);
},
'circular buffer on the pivot': function(buffer) {
assert.equal(buffer.sortedIndex(4.999), 2);
assert.equal(buffer.sortedIndex(5), 2);
assert.equal(buffer.sortedIndex(6), 3);
},
'returns index of existing item': function(buffer) {
assert.equal(buffer.sortedIndex(7), 3);
}
},
'handles circular cases': {
'mid circular case': {
topic: function() {
var buffer = CBuffer(-1, 0, 1, 3, 5);
buffer.push(7, 9);
return buffer;
},
'simple circular buffer': function(buffer) {
assert.equal(buffer.sortedIndex(0), 0);
assert.equal(buffer.sortedIndex(2), 1);
assert.equal(buffer.sortedIndex(4), 2);
assert.equal(buffer.sortedIndex(7), 3);
assert.equal(buffer.sortedIndex(8), 4);
assert.equal(buffer.sortedIndex(10), 4);
},
'circular buffer on the pivot': function(buffer) {
assert.equal(buffer.sortedIndex(4.999), 2);
assert.equal(buffer.sortedIndex(5), 2);
assert.equal(buffer.sortedIndex(6), 3);
},
'returns index of existing item': function(buffer) {
assert.equal(buffer.sortedIndex(7), 3);
}
},
'almost sorted data cases (1 item out of place)': {
topic: function() {
var buffer = CBuffer(-3, -1, 0, 1, 3, 5, 7);
buffer.push(7, 9, 11, 13, 15, 17);
return buffer;
},
'single item out of place': function(buffer) {
assert.equal(buffer.sortedIndex(0), 0);
assert.equal(buffer.sortedIndex(17), 6);
}
}
}
}
'almost sorted data cases (1 item out of place)': {
topic: function() {
var buffer = CBuffer(-3, -1, 0, 1, 3, 5, 7);
buffer.push(7, 9, 11, 13, 15, 17);
return buffer;
},
'single item out of place': function(buffer) {
assert.equal(buffer.sortedIndex(0), 0);
assert.equal(buffer.sortedIndex(17), 6);
}
}
}
}
});
suite.export(module);

@@ -1,5 +0,5 @@

var vows = require('vows'),
assert = require('assert'),
suite = vows.describe('CBuffer'),
und = undefined;
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('constructor');
var und = undefined;

@@ -9,40 +9,40 @@ require('../env');

suite.addBatch({
'CBuffer' : {
'topic' : function () {
return CBuffer;
},
'construction' : function (CBuffer) {
assert.isTrue((new CBuffer(1)) instanceof CBuffer);
assert.isTrue(CBuffer(1) instanceof CBuffer);
assert.isTrue((new CBuffer(1, 2, 3)) instanceof CBuffer);
assert.isTrue(CBuffer(1, 2, 3) instanceof CBuffer);
assert.isTrue(CBuffer(1).constructor === CBuffer);
},
'Missing argument exception': function () {
assert.throws(function () { CBuffer(); }, Error);
assert.throws(function () { new CBuffer(); }, Error);
},
'data' : function (CBuffer) {
assert.deepEqual(CBuffer(3).data, [,,]);
assert.deepEqual(CBuffer(1, 2, 3).data, [1, 2, 3]);
},
'end' : function (CBuffer) {
assert.equal(CBuffer(3).end, 2);
assert.equal(CBuffer(1, 2, 3).end, 2);
},
'length' : function (CBuffer) {
assert.equal(CBuffer(3).length, 0);
assert.equal(CBuffer(1, 2, 3).length, 3);
},
'size' : function (CBuffer) {
assert.equal(CBuffer(3).size, 3);
assert.equal(CBuffer(1, 2, 3).size, 3);
},
'start' : function (CBuffer) {
assert.equal(CBuffer(3).start, 0);
assert.equal(CBuffer(1, 2, 3).start, 0);
}
}
'CBuffer' : {
'topic' : function () {
return CBuffer;
},
'construction' : function (CBuffer) {
assert.isTrue((new CBuffer(1)) instanceof CBuffer);
assert.isTrue(CBuffer(1) instanceof CBuffer);
assert.isTrue((new CBuffer(1, 2, 3)) instanceof CBuffer);
assert.isTrue(CBuffer(1, 2, 3) instanceof CBuffer);
assert.isTrue(CBuffer(1).constructor === CBuffer);
},
'Missing argument exception': function () {
assert.throws(function () { CBuffer(); }, Error);
assert.throws(function () { new CBuffer(); }, Error);
},
'data' : function (CBuffer) {
assert.deepEqual(CBuffer(3).data.length, 3);
assert.deepEqual(CBuffer(1, 2, 3).data, [1, 2, 3]);
},
'end' : function (CBuffer) {
assert.equal(CBuffer(3).end, 2);
assert.equal(CBuffer(1, 2, 3).end, 2);
},
'length' : function (CBuffer) {
assert.equal(CBuffer(3).length, 0);
assert.equal(CBuffer(1, 2, 3).length, 3);
},
'size' : function (CBuffer) {
assert.equal(CBuffer(3).size, 3);
assert.equal(CBuffer(1, 2, 3).size, 3);
},
'start' : function (CBuffer) {
assert.equal(CBuffer(3).start, 0);
assert.equal(CBuffer(1, 2, 3).start, 0);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('every');

@@ -8,21 +8,25 @@ require('../env.js');

suite.addBatch({
'every' : {
'topic' : function () {
return CBuffer;
},
'every items' : function (CBuffer) {
var tmp;
'every' : {
'topic' : function () {
return CBuffer;
},
'every items ok' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3,4);
assert.ok(tmp.every(function (a) {
return ~~a === a;
}));
tmp = new CBuffer(1,2,3,4);
assert.ok(tmp.every(function (a) {
return ~~a === a;
}));
},
'every items error' : function (CBuffer) {
var tmp;
assert.ifError(tmp.every(function (a) {
return a < 4;
}));
}
}
tmp = new CBuffer(1,2,3,4);
assert.ok(tmp.every(function (a) {
return a <= 4;
}));
},
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('forEach');

@@ -8,14 +8,14 @@ require('../env.js');

suite.addBatch({
'forEach' : {
'topic' : function () {
return CBuffer;
},
'forEach' : function (CBuffer) {
var tmp;
'forEach' : {
'topic' : function () {
return CBuffer;
},
'forEach' : function (CBuffer) {
var tmp;
// TODO: first need to finish forEach implementation
}
}
// TODO: first need to finish forEach implementation
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('map');

@@ -8,31 +8,31 @@ require('../env.js');

suite.addBatch({
'map' : {
'topic' : function () {
return CBuffer;
},
'map' : function (CBuffer) {
var tmp, tmp2, tmp3, tmp4;
tmp = new CBuffer(1, 2, 3, 4);
tmp2 = tmp.map(function timesTwo(val) { return val * 2; });
assert.ok (tmp2 instanceof CBuffer);
assert.equal (tmp2.get(0), 2);
assert.equal (tmp2.get(1), 4);
assert.equal (tmp2.get(2), 6);
assert.equal (tmp2.get(3), 8);
'map' : {
'topic' : function () {
return CBuffer;
},
'map' : function (CBuffer) {
var tmp, tmp2, tmp3, tmp4;
tmp = new CBuffer(1, 2, 3, 4);
tmp2 = tmp.map(function timesTwo(val) { return val * 2; });
assert.ok (tmp2 instanceof CBuffer);
assert.equal (tmp2.get(0), 2);
assert.equal (tmp2.get(1), 4);
assert.equal (tmp2.get(2), 6);
assert.equal (tmp2.get(3), 8);
tmp3 = tmp.map(function timesI(val, i) { return val * i; });
assert.equal (tmp3.get(0), 0);
assert.equal (tmp3.get(1), 2);
assert.equal (tmp3.get(2), 6);
assert.equal (tmp3.get(3), 12);
tmp3 = tmp.map(function timesI(val, i) { return val * i; });
assert.equal (tmp3.get(0), 0);
assert.equal (tmp3.get(1), 2);
assert.equal (tmp3.get(2), 6);
assert.equal (tmp3.get(3), 12);
tmp4 = tmp.map(function timesPrevious(val, i, cbuf) { return val * cbuf.get(i - 1); });
assert.equal (tmp4.get(0), 4);
assert.equal (tmp4.get(1), 2);
assert.equal (tmp4.get(2), 6);
assert.equal (tmp4.get(3), 12);
}
}
tmp4 = tmp.map(function timesPrevious(val, i, cbuf) { return val * cbuf.get(i - 1); });
assert.equal (tmp4.get(0), 4);
assert.equal (tmp4.get(1), 2);
assert.equal (tmp4.get(2), 6);
assert.equal (tmp4.get(3), 12);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('math');

@@ -8,54 +8,54 @@ require('../env.js');

suite.addBatch({
'sum' : {
'topic' : function () {
return CBuffer;
},
'Calculate sum' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3,4);
assert.equal (tmp.sum(),10);
},
'Calculate sum on zero items' : function (CBuffer) {
var tmp;
tmp = new CBuffer(10);
assert.equal (tmp.sum(),0);
}
},
'avg' : {
'topic' : function () {
return CBuffer;
},
'Calculate average' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3,4);
assert.equal (tmp.avg(),2.5);
},
'Calculate average on zero items(devision by zero)' : function (CBuffer) {
var tmp;
tmp = new CBuffer(10);
assert.equal (tmp.avg(),0);
}
},
'median' : {
'topic' : function () {
return CBuffer;
},
'Calculate median even buffer length' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3,4);
assert.equal (tmp.median(),2.5);
},
'Calculate median uneven buffer length' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
assert.equal (tmp.median(),2);
},
'Calculate median on zero items(devision by zero)' : function (CBuffer) {
var tmp;
tmp = new CBuffer(10);
assert.equal (tmp.median(),0);
}
}
'sum' : {
'topic' : function () {
return CBuffer;
},
'Calculate sum' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3,4);
assert.equal (tmp.sum(),10);
},
'Calculate sum on zero items' : function (CBuffer) {
var tmp;
tmp = new CBuffer(10);
assert.equal (tmp.sum(),0);
}
},
'avg' : {
'topic' : function () {
return CBuffer;
},
'Calculate average' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3,4);
assert.equal (tmp.avg(),2.5);
},
'Calculate average on zero items(devision by zero)' : function (CBuffer) {
var tmp;
tmp = new CBuffer(10);
assert.equal (tmp.avg(),0);
}
},
'median' : {
'topic' : function () {
return CBuffer;
},
'Calculate median even buffer length' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3,4);
assert.equal (tmp.median(),2.5);
},
'Calculate median uneven buffer length' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
assert.equal (tmp.median(),2);
},
'Calculate median on zero items(devision by zero)' : function (CBuffer) {
var tmp;
tmp = new CBuffer(10);
assert.equal (tmp.median(),0);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('some');

@@ -8,14 +8,14 @@ require('../env.js');

suite.addBatch({
'some' : {
'topic' : function () {
return CBuffer;
},
'some' : function (CBuffer) {
var tmp;
'some' : {
'topic' : function () {
return CBuffer;
},
'some' : function (CBuffer) {
var tmp;
// TODO: first need to finish some implementation
}
}
// TODO: first need to finish some implementation
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('pop');

@@ -8,30 +8,38 @@ require('../env');

suite.addBatch({
'pop' : {
'topic' : function () {
return CBuffer;
},
'pop items' : function (CBuffer) {
var tmp;
'pop' : {
'topic' : function () {
return CBuffer;
},
'pop items' : function (CBuffer) {
var tmp;
tmp = CBuffer(1, 2, 3);
assert.equal(tmp.pop(), 3);
tmp = CBuffer(1, 2, 3);
assert.equal(tmp.pop(), 3);
tmp = CBuffer(1, 2, 3);
tmp.pop();
assert.deepEqual(tmp.toArray(), [1, 2]);
tmp = CBuffer(1, 2, 3);
tmp.pop();
assert.deepEqual(tmp.toArray(), [1, 2]);
tmp = CBuffer(3);
assert.isUndefined(tmp.pop());
},
'pop properties' : function (CBuffer) {
var tmp;
tmp = CBuffer(3);
assert.isUndefined(tmp.pop());
},
'pop properties' : function (CBuffer) {
var tmp;
tmp = CBuffer(1, 2, 3);
tmp.pop();
assert.equal(tmp.end, 1);
assert.equal(tmp.length, 2);
}
}
tmp = CBuffer(1, 2, 3);
tmp.pop();
assert.equal(tmp.end, 1);
assert.equal(tmp.length, 2);
},
'over pop': function(CBuffer) {
var tmp = CBuffer(5);
tmp.push(4, 5, 6, 7, 8, 9, 10);
assert.equal(tmp.pop(), 10);
assert.equal(tmp.end, 0);
assert.equal(tmp.start, 2);
assert.equal(tmp.length, 4);
},
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('push');

@@ -8,27 +8,27 @@ require('../env');

suite.addBatch({
'push' : {
'topic' : function () {
return CBuffer;
},
'push items' : function (CBuffer) {
var tmp;
'push' : {
'topic' : function () {
return CBuffer;
},
'push items' : function (CBuffer) {
var tmp;
tmp = CBuffer(3);
tmp.push(1, 2, 3);
assert.deepEqual(tmp.data, [1, 2, 3]);
tmp.push(4);
assert.deepEqual(tmp.data, [4, 2, 3]);
},
'push properties' : function (CBuffer) {
var tmp;
tmp = CBuffer(3);
tmp.push(1, 2, 3);
assert.deepEqual(tmp.data, [1, 2, 3]);
tmp.push(4);
assert.deepEqual(tmp.data, [4, 2, 3]);
},
'push properties' : function (CBuffer) {
var tmp;
tmp = CBuffer(3);
tmp.push(1, 2);
assert.equal(tmp.length, 2);
assert.equal(tmp.start, 0);
assert.equal(tmp.end, 1);
}
}
tmp = CBuffer(3);
tmp.push(1, 2);
assert.equal(tmp.length, 2);
assert.equal(tmp.start, 0);
assert.equal(tmp.end, 1);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('reverse');

@@ -8,13 +8,13 @@ require('../env');

suite.addBatch({
'reverse' : {
'topic' : function () {
return CBuffer;
},
'reverse buffer' : function (CBuffer) {
assert.deepEqual(CBuffer(1, 2, 3).reverse().data, [3, 2, 1]);
assert.deepEqual(CBuffer(1, 2, 3, 4).reverse().data, [4, 3, 2, 1]);
}
}
'reverse' : {
'topic' : function () {
return CBuffer;
},
'reverse buffer' : function (CBuffer) {
assert.deepEqual(CBuffer(1, 2, 3).reverse().data, [3, 2, 1]);
assert.deepEqual(CBuffer(1, 2, 3, 4).reverse().data, [4, 3, 2, 1]);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('rotateLeft');

@@ -8,13 +8,13 @@ require('../env.js');

suite.addBatch({
'rotateLeft' : {
'topic' : function () {
return CBuffer;
},
'simple rotateLeft' : function (CBuffer) {
assert.deepEqual(CBuffer(1, 2, 3).rotateLeft(2).toArray(), [3, 1, 2]);
assert.deepEqual(CBuffer(1, 2, 3).rotateLeft().toArray(), [2, 3, 1]);
}
}
'rotateLeft' : {
'topic' : function () {
return CBuffer;
},
'simple rotateLeft' : function (CBuffer) {
assert.deepEqual(CBuffer(1, 2, 3).rotateLeft(2).toArray(), [3, 1, 2]);
assert.deepEqual(CBuffer(1, 2, 3).rotateLeft().toArray(), [2, 3, 1]);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('rotateRight');

@@ -8,13 +8,13 @@ require('../env.js');

suite.addBatch({
'rotateRight' : {
'topic' : function () {
return CBuffer;
},
'simple rotateRight' : function (CBuffer) {
assert.deepEqual(CBuffer(1, 2, 3).rotateRight(2).toArray(), [2, 3, 1]);
assert.deepEqual(CBuffer(1, 2, 3).rotateRight().toArray(), [3, 1, 2]);
}
}
'rotateRight' : {
'topic' : function () {
return CBuffer;
},
'simple rotateRight' : function (CBuffer) {
assert.deepEqual(CBuffer(1, 2, 3).rotateRight(2).toArray(), [2, 3, 1]);
assert.deepEqual(CBuffer(1, 2, 3).rotateRight().toArray(), [3, 1, 2]);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('shift');

@@ -8,37 +8,37 @@ require('../env');

suite.addBatch({
'shift' : {
'topic' : function () {
return CBuffer;
},
'shift items' : function (CBuffer) {
var tmp;
'shift' : {
'topic' : function () {
return CBuffer;
},
'shift items' : function (CBuffer) {
var tmp;
tmp = CBuffer(1, 2, 3);
assert.equal(tmp.shift(), 1);
assert.deepEqual(tmp.toArray(), [2, 3]);
tmp = CBuffer(1, 2, 3);
assert.equal(tmp.shift(), 1);
assert.deepEqual(tmp.toArray(), [2, 3]);
tmp = CBuffer(1, 2, 3);
tmp.push(4);
assert.equal(tmp.shift(), 2);
assert.deepEqual(tmp.toArray(), [3, 4]);
},
'shift properties' : function (CBuffer) {
var tmp;
tmp = CBuffer(1, 2, 3);
tmp.push(4);
assert.equal(tmp.shift(), 2);
assert.deepEqual(tmp.toArray(), [3, 4]);
},
'shift properties' : function (CBuffer) {
var tmp;
tmp = CBuffer(1, 2, 3);
tmp.shift();
assert.equal(tmp.length, 2);
assert.equal(tmp.start, 1);
assert.equal(tmp.end, 2);
tmp = CBuffer(1, 2, 3);
tmp.shift();
assert.equal(tmp.length, 2);
assert.equal(tmp.start, 1);
assert.equal(tmp.end, 2);
tmp = CBuffer(1, 2, 3);
tmp.push(4);
tmp.shift();
assert.equal(tmp.length, 2);
assert.equal(tmp.start, 2);
assert.equal(tmp.end, 0);
}
}
tmp = CBuffer(1, 2, 3);
tmp.push(4);
tmp.shift();
assert.equal(tmp.length, 2);
assert.equal(tmp.start, 2);
assert.equal(tmp.end, 0);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('sort');

@@ -8,20 +8,20 @@ require('../env');

suite.addBatch({
'sort' : {
'topic' : function () {
return CBuffer;
},
'sort items' : function (CBuffer) {
var tmp;
'sort' : {
'topic' : function () {
return CBuffer;
},
'sort items' : function (CBuffer) {
var tmp;
tmp = new CBuffer(7,4,5,2,1);
tmp.sort(function(a, b) { return a - b; });
assert.deepEqual(tmp.toArray(), [1,2,4,5,7]);
tmp = new CBuffer(7,4,5,2,1);
tmp.sort(function(a, b) { return a - b; });
assert.deepEqual(tmp.toArray(), [1,2,4,5,7]);
tmp = new CBuffer('a','c','b');
tmp.sort();
assert.deepEqual(tmp.toArray(), ['a','b','c']);
},
}
tmp = new CBuffer('a','c','b');
tmp.sort();
assert.deepEqual(tmp.toArray(), ['a','b','c']);
},
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('unshift');

@@ -8,27 +8,27 @@ require('../env');

suite.addBatch({
'unshift' : {
'topic' : function () {
return CBuffer;
},
'unshift items' : function (CBuffer) {
var tmp;
'unshift' : {
'topic' : function () {
return CBuffer;
},
'unshift items' : function (CBuffer) {
var tmp;
tmp = CBuffer(3);
tmp.unshift(1, 2, 3);
assert.deepEqual(tmp.data, [3, 2, 1]);
tmp.unshift(4);
assert.deepEqual(tmp.data, [3, 2, 4]);
},
'unshift properties' : function (CBuffer) {
var tmp;
tmp = CBuffer(3);
tmp.unshift(1, 2, 3);
assert.deepEqual(tmp.data, [3, 2, 1]);
tmp.unshift(4);
assert.deepEqual(tmp.data, [3, 2, 4]);
},
'unshift properties' : function (CBuffer) {
var tmp;
tmp = CBuffer(3);
tmp.unshift(1, 2);
assert.equal(tmp.length, 2);
assert.equal(tmp.start, 1);
assert.equal(tmp.end, 2);
}
}
tmp = CBuffer(3);
tmp.unshift(1, 2);
assert.equal(tmp.length, 2);
assert.equal(tmp.start, 1);
assert.equal(tmp.end, 2);
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('CBuffer');

@@ -8,11 +8,11 @@ require('../env.js');

suite.addBatch({
'' : {
'topic' : function () {
return CBuffer;
},
'' : function (CBuffer) {
}
}
'' : {
'topic' : function () {
return CBuffer;
},
'' : function (CBuffer) {
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert'),
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('join');

@@ -8,19 +8,19 @@ require('../env');

suite.addBatch({
'join': {
topic: function() {
return CBuffer(1, 3, 5, 7, 11);
},
'no arguments returns comma-separated string': function(buffer) {
assert.equal(buffer.join(), '1,3,5,7,11');
assert.equal(buffer.join(), [1, 3, 5, 7, 11].join());
},
'handles custom join string': function(buffer) {
assert.equal(buffer.join(', '), '1, 3, 5, 7, 11');
},
'handles join argument that is not a string': function(buffer) {
assert.equal(buffer.join(true), '1true3true5true7true11');
}
}
'join': {
topic: function() {
return CBuffer(1, 3, 5, 7, 11);
},
'no arguments returns comma-separated string': function(buffer) {
assert.equal(buffer.join(), '1,3,5,7,11');
assert.equal(buffer.join(), [1, 3, 5, 7, 11].join());
},
'handles custom join string': function(buffer) {
assert.equal(buffer.join(', '), '1, 3, 5, 7, 11');
},
'handles join argument that is not a string': function(buffer) {
assert.equal(buffer.join(true), '1true3true5true7true11');
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert'),
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('slice');

@@ -8,53 +8,53 @@ require('../env');

suite.addBatch({
'slice': {
topic: function() {
return CBuffer(1, 3, 5, 7, 11);
},
'no arguments returns array of data': function(buffer) {
assert.deepEqual(buffer.slice(), [1, 3, 5, 7, 11]);
},
'handles postive start and end indexs': function(buffer) {
assert.deepEqual(buffer.slice(0), [1, 3, 5, 7, 11]);
assert.deepEqual(buffer.slice(0, 3), [1, 3, 5]);
assert.deepEqual(buffer.slice(1, 3), [3, 5]);
},
'handles negative start and end indexs': function(buffer) {
assert.deepEqual(buffer.slice(-2), [7, 11]);
assert.deepEqual(buffer.slice(0, -2), [1, 3, 5]);
},
'handles indexes outside of the buffer size': function(buffer) {
assert.deepEqual(buffer.slice(0, 10), [1, 3, 5, 7, 11]);
assert.deepEqual(buffer.slice(0, -7), []);
assert.deepEqual(buffer.slice(-10), [1, 3, 5, 7, 11]);
},
'works with partial CBuffers': {
topic: function() {
var buffer = CBuffer(10);
buffer.push(1, 2, 3);
return buffer;
},
'handles partial CBuffers': function(buffer) {
assert.deepEqual(buffer.slice(1, 3), [2, 3]);
}
},
'handles circular cases': {
topic: function() {
var buffer = CBuffer(-1, 0, 1, 3, 5);
buffer.push(7, 9);
return buffer;
},
'handles circular buffers': function(buffer) {
assert.deepEqual(buffer.slice(), [1, 3, 5, 7, 9]);
assert.deepEqual(buffer.slice(0), [1, 3, 5, 7, 9]);
assert.deepEqual(buffer.slice(1, 3), [3, 5]);
assert.deepEqual(buffer.slice(-3), [5, 7, 9]);
assert.deepEqual(buffer.slice(-4, -1), [3, 5, 7]);
assert.deepEqual(buffer.slice(-1, 1), []);
assert.deepEqual(buffer.slice(-4, -4), []);
assert.deepEqual(buffer.slice(-4, -5), []);
}
}
}
'slice': {
topic: function() {
return CBuffer(1, 3, 5, 7, 11);
},
'no arguments returns array of data': function(buffer) {
assert.deepEqual(buffer.slice(), [1, 3, 5, 7, 11]);
},
'handles postive start and end indexs': function(buffer) {
assert.deepEqual(buffer.slice(0), [1, 3, 5, 7, 11]);
assert.deepEqual(buffer.slice(0, 3), [1, 3, 5]);
assert.deepEqual(buffer.slice(1, 3), [3, 5]);
},
'handles negative start and end indexs': function(buffer) {
assert.deepEqual(buffer.slice(-2), [7, 11]);
assert.deepEqual(buffer.slice(0, -2), [1, 3, 5]);
},
'handles indexes outside of the buffer size': function(buffer) {
assert.deepEqual(buffer.slice(0, 10), [1, 3, 5, 7, 11]);
assert.deepEqual(buffer.slice(0, -7), []);
assert.deepEqual(buffer.slice(-10), [1, 3, 5, 7, 11]);
},
'works with partial CBuffers': {
topic: function() {
var buffer = CBuffer(10);
buffer.push(1, 2, 3);
return buffer;
},
'handles partial CBuffers': function(buffer) {
assert.deepEqual(buffer.slice(1, 3), [2, 3]);
}
},
'handles circular cases': {
topic: function() {
var buffer = CBuffer(-1, 0, 1, 3, 5);
buffer.push(7, 9);
return buffer;
},
'handles circular buffers': function(buffer) {
assert.deepEqual(buffer.slice(), [1, 3, 5, 7, 9]);
assert.deepEqual(buffer.slice(0), [1, 3, 5, 7, 9]);
assert.deepEqual(buffer.slice(1, 3), [3, 5]);
assert.deepEqual(buffer.slice(-3), [5, 7, 9]);
assert.deepEqual(buffer.slice(-4, -1), [3, 5, 7]);
assert.deepEqual(buffer.slice(-1, 1), []);
assert.deepEqual(buffer.slice(-4, -4), []);
assert.deepEqual(buffer.slice(-4, -5), []);
}
}
}
});
suite.export(module);

@@ -1,4 +0,4 @@

var vows = require('vows'),
assert = require('assert')
suite = vows.describe('CBuffer');
var vows = require('vows');
var assert = require('assert');
var suite = vows.describe('utilities');

@@ -8,54 +8,54 @@ require('../env.js');

suite.addBatch({
'utility' : {
'topic' : function () {
return CBuffer;
},
'empty' : function (CBuffer) {
var tmp;
'utility' : {
'topic' : function () {
return CBuffer;
},
'empty' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
tmp.empty();
assert.equal(tmp.length, 0);
},
'fill' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
tmp.empty();
assert.equal(tmp.length, 0);
},
'fill' : function (CBuffer) {
var tmp;
tmp = new CBuffer(3);
tmp.fill(1);
assert.deepEqual(tmp.toArray(), [1,1,1]);
},
'first' : function (CBuffer) {
var tmp;
tmp = new CBuffer(3);
tmp.fill(1);
assert.deepEqual(tmp.toArray(), [1,1,1]);
},
'first' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
assert.equal(tmp.first(), 1);
},
'last' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
assert.equal(tmp.first(), 1);
},
'last' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
assert.equal(tmp.last(), 3);
},
'get' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
assert.equal(tmp.last(), 3);
},
'get' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
assert.equal(tmp.get(2), 3);
},
'set' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
assert.equal(tmp.get(2), 3);
},
'set' : function (CBuffer) {
var tmp;
tmp = new CBuffer(1,2,3);
tmp.set(1, 2);
assert.equal(tmp.get(1), 2);
},
'toArray' : function (CBuffer) {
assert.ok(CBuffer(1).toArray() instanceof Array);
},
'overflow' : function (CBuffer) {
// TODO: this needs to be an async test
}
}
tmp = new CBuffer(1,2,3);
tmp.set(1, 2);
assert.equal(tmp.get(1), 2);
},
'toArray' : function (CBuffer) {
assert.ok(CBuffer(1).toArray() instanceof Array);
},
'overflow' : function (CBuffer) {
// TODO: this needs to be an async test
}
}
});
suite.export(module);
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc