<commit_before>var assert = require('assert');
var vows = require('vows');
var LRU = require('../');

function keys(obj) {
  var result = [];
  for(k in obj) {
    if(obj.hasOwnProperty(k)) {
      result.push(k);
    }
  }
  return result;
}

var suite = vows.describe('LRU');

suite.addBatch({
  "setting keys doesn't grow past max size": function() {
    var lru = new LRU(3);
    assert.equal(0, lru.length);
    lru.set('foo1', 'bar1');
    assert.equal(1, lru.length);
    lru.set('foo2', 'bar2');
    assert.equal(2, lru.length);
    lru.set('foo3', 'bar3');
    assert.equal(3, lru.length);

    lru.set('foo4', 'bar4');
    assert.equal(3, lru.length);
  }
});

suite.addBatch({
  "setting keys returns the value": function() {
    var lru = new LRU(2);
    assert.equal('bar1', lru.set('foo1', 'bar1'));
    assert.equal('bar2', lru.set('foo2', 'bar2'));
    assert.equal('bar3', lru.set('foo3', 'bar3'));
    assert.equal('bar2', lru.get('foo2'));
    assert.equal(undefined, lru.get('foo1'));
    assert.equal('bar1', lru.set('foo1','bar1'));
  }
});

suite.addBatch({
  "lru invariant is maintained for set()": function() {
    var lru = new LRU(2);

    lru.set('foo1', 'bar1');
    lru.set('foo2', 'bar2');
    lru.set('foo3', 'bar3');
    lru.set('foo4', 'bar4');

    assert.deepEqual(['foo3','foo4'], keys(lru.cache));
  }
})

suite.addBatch({
  "ovrewriting a key updates the value": function() {
    var lru = new LRU(2);
    lru.set('foo1', 'bar1');
    assert.equal('bar1', lru.get('foo1'))
    lru.set('foo1', 'bar2');
    assert.equal('bar2', lru.get('foo1'))
  }
})

suite.addBatch({
  "lru invariant is maintained for get()": function() {
    var lru = new LRU(2);

    lru.set('foo1', 'bar1');
    lru.set('foo2', 'bar2');

    lru.get('foo1'); //now foo2 should be deleted instead of foo1

    lru.set('foo3', 'bar3');

    assert.deepEqual(['foo1','foo3'], keys(lru.cache));
  }
});

suite.addBatch({
  "lru invariant is maintained for get()": function() {
    var lru = new LRU(2);

    lru.set('foo1', 'bar1');
    lru.set('foo2', 'bar2');

    lru.get('foo2'); //now foo2 should be deleted instead of foo1<commit_msg>add set, get and remove test<commit_after>var assert = require('assert');
var vows = require('vows');
var LRU = require('../');

function keys(obj) {
  var result = [];
  for(k in obj) {
    if(obj.hasOwnProperty(k)) {
      result.push(k);
    }
  }
  return result;
}

var suite = vows.describe('LRU');

suite.addBatch({
  "setting keys doesn't grow past max size": function() {
    var lru = new LRU(3);
    assert.equal(0, lru.length);
    lru.set('foo1', 'bar1');
    assert.equal(1, lru.length);
    lru.set('foo2', 'bar2');
    assert.equal(2, lru.length);
    lru.set('foo3', 'bar3');
    assert.equal(3, lru.length);

    lru.set('foo4', 'bar4');
    assert.equal(3, lru.length);
  }
});

suite.addBatch({
  "setting keys returns the value": function() {
    var lru = new LRU(2);
    assert.equal('bar1', lru.set('foo1', 'bar1'));
    assert.equal('bar2', lru.set('foo2', 'bar2'));
    assert.equal('bar3', lru.set('foo3', 'bar3'));
    assert.equal('bar2', lru.get('foo2'));
    assert.equal(undefined, lru.get('foo1'));
    assert.equal('bar1', lru.set('foo1','bar1'));
  }
});

suite.addBatch({
  "lru invariant is maintained for set()": function() {
    var lru = new LRU(2);

    lru.set('foo1', 'bar1');
    lru.set('foo2', 'bar2');
    lru.set('foo3', 'bar3');
    lru.set('foo4', 'bar4');

    assert.deepEqual(['foo3','foo4'], keys(lru.cache));
  }
})

suite.addBatch({
  "ovrewriting a key updates the value": function() {
    var lru = new LRU(2);
    lru.set('foo1', 'bar1');
    assert.equal('bar1', lru.get('foo1'))
    lru.set('foo1', 'bar2');
    assert.equal('bar2', lru.get('foo1'))
  }
})

suite.addBatch({
  "lru invariant is maintained for get()": function() {
    var lru = new LRU(2);

    lru.set('foo1', 'bar1');
    lru.set('foo2', 'bar2');

    lru.get('foo1'); //now foo2 should be deleted instead of foo1

    lru.set('foo3', 'bar3');

    assert.deepEqual(['foo1','foo3'], keys(lru.cache));
  },
  "lru invariant is maintained after set(), get() and remove()": function() {
    var lru = new LRU(2);
    lru.set('a', 1);
    lru.set('b', 2);
    assert.deepEqual(lru.get('a'), 1);
    lru.remove('a');
    lru.set('c', 1);
    lru.set('d', 1);
    assert.deepEqual(['c','d'], keys(lru.cache));
  }
});

suite.addBatch({
  "lru invariant is maintained for get()": function() {
    var lru = new LRU(2);

    lru.set('foo1', 'bar1');
    lru.set('foo2', 'bar2');

    lru.get('foo2'); //now foo2 should be deleted instead of foo1
{'fraction_non_alphanumeric': 0.22205372616984403, 'fraction_numerical': 0.03162911611785095, 'mean_word_length': 3.3929590865842054, 'pattern_counts': {'":': 13, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '868589', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1804, 'n_words': 383}