Skip to content

Commit

Permalink
tests and benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
jostylr committed Apr 22, 2012
1 parent e4628cb commit aca7609
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 43 deletions.
30 changes: 30 additions & 0 deletions scheem/bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var fs = require('fs');

var pegjs = require('pegjs');

var text = fs.readFileSync("bm.scm", "utf8");

var request = require('request');

var Benchmark = require('benchmark');

var suite = new Benchmark.Suite;

suite
.on('cycle', function(event, bench) {
console.log(String(bench));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').pluck('name'));
});

var load = require("./load")

load(function (par, name) {
suite.add(name, function () {
par(text);
});
}, function () {
suite.run();
});

16 changes: 16 additions & 0 deletions scheem/bm.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(+ 1 2 f (
x y
;; a comment
z
'(1 2 3)
;; great
) g ( 1
2
atome
just
kidding
;;more comm
;;and more

))

54 changes: 54 additions & 0 deletions scheem/load.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var request = require('request');
var fs = require('fs');
var pegjs = require('pegjs');

//load
module.exports = function (single, all) {
var i, n;

var flag = 0;

var flagcb = function () {
if (flag < 1) {
all();
}
};

var makeParU = function (url, name) {
name = name || url.split("/")[3]
flag += 1;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var scheem = pegjs.buildParser(body);

var par = function (str, start) {
try {
return scheem.parse(str, start);
} catch (e) {
return undefined;
}
};
//console.log(name, "loaded");
single(par, name);
} else {
console.log("Error in fetching", url, error);
}
flag -= 1;
flagcb();
});
};

var urls = fs.readFileSync("repos.txt", "utf8").split("\n");
var u;
n = urls.length;
for (i = 0; i < n; i += 1){
u = urls[i].trim();
if (!u || u[0] === "!") {
continue;
}
console.log(u)
makeParU(u);
}


};
19 changes: 19 additions & 0 deletions scheem/repos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
https://raw.github.com/jostylr/nathansuniversity/master/scheem/scheem.pegjs
https://raw.github.com/McLeopold/PL101/master/scheem/scheem.peg
https://raw.github.com/arcanis/trivia.nathan-s-university/master/scheem/lib/grammar.peg
!https://raw.github.com/tdieb/PL101/master/scheem/scheem.peg
!https://raw.github.com/danielparks/nu-pl101-scheem/master/scheem.peg
!https://raw.github.com/p07r0457/NU_Compiler_Class/master/my.peg
!https://raw.github.com/schettino72/PL101/master/scheem.peg
https://raw.github.com/tsiivola/NathansUniversity/master/scheem/scheem.peg
https://raw.github.com/manvel7650/JavascriptLanguage/master/scheme/scheme.peg
!https://raw.github.com/fbs/nathan-pl101/master/peg/scheem/parser.pegjs
https://raw.github.com/joekarl/NathansUniversityRepo/master/scheem/Scheem.peg
!https://raw.github.com/Bogdanp/PL101/master/Scheem/Scheem.peg
!https://raw.github.com/DominionSpy/pl101/master/lesson3-part1/scheem.peg
https://raw.github.com/QWRp/PL101/master/scheem/scheem.peg
!https://raw.github.com/chrisvest/nathansuni-js-lang/master/scheem/scheem.peg
!https://raw.github.com/adamipc/pl101/master/scheem/scheem.peg
!https://raw.github.com/lmpessoa-pl101/pl101/master/scheem/scheem.peg
https://raw.github.com/SameDifference/pl101/master/peg/peg.js
https://raw.github.com/aheaume/PL101/master/scheem/scheem.pegjs
107 changes: 107 additions & 0 deletions scheem/repotests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*globals module, require, console, exports*/

var fs = require('fs');
var nu = require('nodeunit');
var load = require('./load');

var pegjs = require('pegjs');

var ref = fs.readFileSync("bm.scm", "utf8");

var parserTests = {
basics: function (test) {
var par = this.par;
test.expect(6);
test.equal(par(""), undefined, "don't parse empty string");
test.deepEqual(par("@"), "@", "single char @");
test.deepEqual(par("atom"), "atom", "word");
test.deepEqual(par("(a b c)"), ["a", "b", "c"], "abc");
test.deepEqual(par("(+ x 3)"), ["+", "x", "3"], "parse (+ x 3)");
test.deepEqual(par("(+ 1 (f x 3 y))"), ["+", "1", ["f", "x", "3", "y"]], "parse (+ 1 (f x 3 y))");
test.done();
}
, space : function (test) {
var par = this.par;
test.expect(6);
test.equal(par(" "), undefined, "don't parse white space only string");
test.deepEqual(par(" @"), "@", " @");
test.deepEqual(par("atom "), "atom", "atom ");
test.deepEqual(par(" ( a b c ) "), ["a", "b", "c"], " ( a b c ) ");
test.deepEqual(par("(+ \t x \n 3)"), ["+", "x", "3"], "parse (+ \t x \n 3)");
test.deepEqual(par("(+ 1 \n \t (f x 3 y) \n)"), ["+", "1", ["f", "x", "3", "y"]], "(+ 1 \n \t (f x 3 y) \n)");
test.done();
}
, quote : function (test) {
var par = this.par;
test.expect(3);
test.deepEqual(par("'x"), ["quote", "x"], "'x");
test.deepEqual(par("'(1 2 3)"), ["quote", ["1", "2", "3"]]);
test.deepEqual(par("(+ '1 \n \t (f x 3 y) \n)"), ["+", ["quote", "1"], ["f", "x", "3", "y"]], "(+ '1 \n \t (f x 3 y) \n)");
test.done();
}
, comments : function (test) {
var par = this.par;
test.expect(3);
test.equal(par(";; this is a comment"), undefined, "don't parse comment only");
test.deepEqual(par(";; first comment\nx"), "x", ";; first comment\n x");
test.deepEqual(par("x\n;; later comment"), "x", "x\n;; later comment");
test.done();
}
, reference : function (test) {
var par = this.par;
test.expect(1);
test.deepEqual(par(ref), ["+", "1", "2", "f", ["x", "y", "z", ["quote", ["1", "2", "3"]]], "g", ["1", "2", "atome", "just", "kidding"]], "reference one");
test.done();
}

};

var clone = function (obj) {
var ret = {};
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
ret[key] = obj[key];
}
}
return ret;
};


var tests = {};

load(function (par, name) {
var t = tests[name] = clone(parserTests);

t.setUp = function (cb) {
this.par = par;
cb();
};

try{
nu.runSuite(name, t, {}, function (a, b) {
var i
, count = 0
, failed = 0
, n = b.length
;

for (i = 0; i < n; i += 1 ){
count += 1;
if (b[i].failed() ) {
failed += 1;
//console.log(name, b[i].error);
}
}
if (failed > 0) {
console.log(name, failed, "failed out of", count );
}
});
} catch (e) {
console.log(name, "error");
}
}, function () {
;
});


File renamed without changes.
119 changes: 76 additions & 43 deletions scheem/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,88 @@ var fs = require('fs');

var pegjs = require('pegjs');

var scheem = pegjs.buildParser(fs.readFileSync("scheem.pegjs", "utf8"));

var par = function (str, start) {
try {
return scheem.parse(str, start);
} catch (e) {
console.log(e)
return undefined;
var ref = fs.readFileSync("bm.scm", "utf8");

var parserTests = {
basics: function (test) {
var par = this.par;
test.expect(6);
test.equal(par(""), undefined, "don't parse empty string");
test.deepEqual(par("@"), "@", "single char @");
test.deepEqual(par("atom"), "atom", "word");
test.deepEqual(par("(a b c)"), ["a", "b", "c"], "abc");
test.deepEqual(par("(+ x 3)"), ["+", "x", "3"], "parse (+ x 3)");
test.deepEqual(par("(+ 1 (f x 3 y))"), ["+", "1", ["f", "x", "3", "y"]], "parse (+ 1 (f x 3 y))");
test.done();
}
, space : function (test) {
var par = this.par;
test.expect(6);
test.equal(par(" "), undefined, "don't parse white space only string");
test.deepEqual(par(" @"), "@", " @");
test.deepEqual(par("atom "), "atom", "atom ");
test.deepEqual(par(" ( a b c ) "), ["a", "b", "c"], " ( a b c ) ");
test.deepEqual(par("(+ \t x \n 3)"), ["+", "x", "3"], "parse (+ \t x \n 3)");
test.deepEqual(par("(+ 1 \n \t (f x 3 y) \n)"), ["+", "1", ["f", "x", "3", "y"]], "(+ 1 \n \t (f x 3 y) \n)");
test.done();
}
, quote : function (test) {
var par = this.par;
test.expect(3);
test.deepEqual(par("'x"), ["quote", "x"], "'x");
test.deepEqual(par("'(1 2 3)"), ["quote", ["1", "2", "3"]]);
test.deepEqual(par("(+ '1 \n \t (f x 3 y) \n)"), ["+", ["quote", "1"], ["f", "x", "3", "y"]], "(+ '1 \n \t (f x 3 y) \n)");
test.done();
}
, comments : function (test) {
var par = this.par;
test.expect(3);
test.equal(par(";; this is a comment"), undefined, "don't parse comment only");
test.deepEqual(par(";; first comment\nx"), "x", ";; first comment\n x");
test.deepEqual(par("x\n;; later comment"), "x", "x\n;; later comment");
test.done();
}
, reference : function (test) {
var par = this.par;
test.expect(1);
test.deepEqual(par(ref), ["+", "1", "2", "f", ["x", "y", "z", ["quote", ["1", "2", "3"]]], "g", ["1", "2", "atome", "just", "kidding"]], "reference one");
test.done();
}
};

var clone = function (obj) {
var ret = {};
var key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
ret[key] = obj[key];
}
}
return ret;
};


//console.log(scheem.parse(""));

exports.basics = function (test) {
test.expect(6);
test.equal(par(""), undefined, "don't parse empty string");
test.deepEqual(par("@"), "@", "single char @");
test.deepEqual(par("atom"), "atom", "word");
test.deepEqual(par("(a b c)"), ["a", "b", "c"], "abc");
test.deepEqual(par("(+ x 3)"), ["+", "x", "3"], "parse (+ x 3)");
test.deepEqual(par("(+ 1 (f x 3 y))"), ["+", "1", ["f", "x", "3", "y"]], "parse (+ 1 (f x 3 y))");
test.done();
};
makePar = function (fname, tname) {
var par = clone(parserTests);

par.setUp = function (cb) {
var scheem = pegjs.buildParser(fs.readFileSync(fname, "utf8"));

exports.space = function (test) {
test.expect(6);
test.equal(par(" "), undefined, "don't parse white space only string");
test.deepEqual(par(" @"), "@", " @");
test.deepEqual(par("atom "), "atom", "atom ");
test.deepEqual(par(" ( a b c ) "), ["a", "b", "c"], " ( a b c ) ");
test.deepEqual(par("(+ \t x \n 3)"), ["+", "x", "3"], "parse (+ \t x \n 3)");
test.deepEqual(par("(+ 1 \n \t (f x 3 y) \n)"), ["+", "1", ["f", "x", "3", "y"]], "(+ 1 \n \t (f x 3 y) \n)");
test.done();
this.par = function (str, start) {
try {
return scheem.parse(str, start);
} catch (e) {
//console.log(e);
return undefined;
}
};
cb();
};

exports[tname] = par;
};

exports.quote = function (test) {
test.expect(3);
test.deepEqual(par("'x"), ["quote", "x"], "'x");
test.deepEqual(par("'(1 2 3)"), ["quote", ["1", "2", "3"]]);
test.deepEqual(par("(+ '1 \n \t (f x 3 y) \n)"), ["+", ["quote", "1"], ["f", "x", "3", "y"]], "(+ '1 \n \t (f x 3 y) \n)");
test.done();
}

exports.comments = function (test) {
test.expect(3)
test.equal(par(";; this is a comment"), undefined, "don't parse comment only");
test.deepEqual(par(";; first comment\nx"), "x", ";; first comment\n x");
test.deepEqual(par("x\n;; later comment"), "x", "x\n;; later comment");
test.done();
}
makePar("scheem.peg", "mine");


0 comments on commit aca7609

Please sign in to comment.