aboutsummaryrefslogtreecommitdiff
path: root/js/tests
diff options
context:
space:
mode:
Diffstat (limited to 'js/tests')
-rw-r--r--js/tests/common.js15
l---------js/tests/node_modules1
-rw-r--r--js/tests/reader.js68
-rw-r--r--js/tests/step5_tco.js22
-rw-r--r--js/tests/types.js94
5 files changed, 200 insertions, 0 deletions
diff --git a/js/tests/common.js b/js/tests/common.js
new file mode 100644
index 0000000..a95d79b
--- /dev/null
+++ b/js/tests/common.js
@@ -0,0 +1,15 @@
+fs = require('fs');
+assert = require('assert');
+
+function assert_eq(a, b) {
+ GLOBAL.assert.deepEqual(a, b, a + " !== " + b);
+}
+
+function load(file) {
+ console.log(process.cwd());
+ //process.chdir('../');
+ eval(fs.readFileSync(file,'utf8'));
+}
+
+exports.assert_eq = assert_eq;
+exports.load = load;
diff --git a/js/tests/node_modules b/js/tests/node_modules
new file mode 120000
index 0000000..b870225
--- /dev/null
+++ b/js/tests/node_modules
@@ -0,0 +1 @@
+../ \ No newline at end of file
diff --git a/js/tests/reader.js b/js/tests/reader.js
new file mode 100644
index 0000000..2aa81c6
--- /dev/null
+++ b/js/tests/reader.js
@@ -0,0 +1,68 @@
+common = require('./common.js');
+types = require('../types');
+reader = require('../reader');
+var assert_eq = common.assert_eq,
+ read_str = reader.read_str,
+ nth = types.ns.nth;
+
+console.log("Testing read of constants/strings");
+assert_eq(2,read_str('2'));
+assert_eq(12345,read_str('12345'));
+assert_eq(12345,read_str('12345 "abc"'));
+assert_eq('abc',read_str('"abc"'));
+assert_eq('a string (with parens)',read_str('"a string (with parens)"'));
+
+console.log("Testing read of symbols");
+assert(types.symbol_Q(read_str('abc')));
+assert_eq('abc',read_str('abc').value);
+assert_eq('.',read_str('.').value);
+
+console.log("Testing READ_STR of strings");
+assert_eq('a string',read_str('"a string"'));
+assert_eq('a string (with parens)',read_str('"a string (with parens)"'));
+assert_eq('a string',read_str('"a string"()'));
+assert_eq('a string',read_str('"a string"123'));
+assert_eq('a string',read_str('"a string"abc'));
+assert_eq('',read_str('""'));
+assert_eq('abc ',read_str('"abc "'));
+assert_eq(' abc',read_str('" abc"'));
+assert_eq('$abc',read_str('"$abc"'));
+assert_eq('abc$()',read_str('"abc$()"'));
+assert_eq('"xyz"',read_str('"\\"xyz\\""'));
+
+
+console.log("Testing READ_STR of lists");
+assert_eq(2,types.ns.count(read_str('(2 3)')));
+assert_eq(2,types.ns.first(read_str('(2 3)')));
+assert_eq(3,types.ns.first(types.ns.rest(read_str('(2 3)'))));
+L = read_str('(+ 1 2 "str1" "string (with parens) and \'single quotes\'")');
+assert_eq(5,types.ns.count(L));
+assert_eq('str1',nth(L,3));
+assert_eq('string (with parens) and \'single quotes\'',nth(L,4));
+assert_eq([2,3],read_str('(2 3)'));
+assert_eq([2,3, 'string (with parens)'],read_str('(2 3 "string (with parens)")'));
+
+
+console.log("Testing READ_STR of quote/quasiquote");
+assert_eq('quote',nth(read_str('\'1'),0).value);
+assert_eq(1,nth(read_str('\'1'),1));
+assert_eq('quote',nth(read_str('\'(1 2 3)'),0).value);
+assert_eq(3,nth(nth(read_str('\'(1 2 3)'),1),2));
+
+assert_eq('quasiquote',nth(read_str('`1'),0).value);
+assert_eq(1,nth(read_str('`1'),1));
+assert_eq('quasiquote',nth(read_str('`(1 2 3)'),0).value);
+assert_eq(3,nth(nth(read_str('`(1 2 3)'),1),2));
+
+assert_eq('unquote',nth(read_str('~1'),0).value);
+assert_eq(1,nth(read_str('~1'),1));
+assert_eq('unquote',nth(read_str('~(1 2 3)'),0).value);
+assert_eq(3,nth(nth(read_str('~(1 2 3)'),1),2));
+
+assert_eq('splice-unquote',nth(read_str('~@1'),0).value);
+assert_eq(1,nth(read_str('~@1'),1));
+assert_eq('splice-unquote',nth(read_str('~@(1 2 3)'),0).value);
+assert_eq(3,nth(nth(read_str('~@(1 2 3)'),1),2));
+
+
+console.log("All tests completed");
diff --git a/js/tests/step5_tco.js b/js/tests/step5_tco.js
new file mode 100644
index 0000000..60c0576
--- /dev/null
+++ b/js/tests/step5_tco.js
@@ -0,0 +1,22 @@
+common = require('./common.js');
+var assert_eq = common.assert_eq;
+var rep = require('../step5_tco.js').rep;
+
+console.log("Testing Stack Exhaustion Function");
+rep('(def! sum-to (fn* (n) (if (= n 0) 0 (+ n (sum-to (- n 1))))))');
+try {
+ rep('(sum-to 10000)');
+ throw new Error("Did not get expected stack exhaustion");
+} catch (e) {
+ if (e.toString().match(/RangeError/)) {
+ console.log("Got expected stack exhaustion");
+ } else {
+ throw new Error("Unexpected error: " + e);
+ }
+}
+
+console.log("Testing Tail Call Optimization/Elimination");
+rep('(def! sum2 (fn* (n acc) (if (= n 0) acc (sum2 (- n 1) (+ n acc)))))');
+rep('(sum2 10000 0)');
+
+console.log("All tests completed");
diff --git a/js/tests/types.js b/js/tests/types.js
new file mode 100644
index 0000000..71b276f
--- /dev/null
+++ b/js/tests/types.js
@@ -0,0 +1,94 @@
+common = require('./common.js');
+var assert_eq = common.assert_eq;
+var types = require('../types.js');
+var symbol = types.symbol,
+ hash_map = types.ns['hash-map'],
+ hash_map_Q = types.ns['map?'],
+ assoc = types.ns['assoc'],
+ dissoc = types.ns['dissoc'],
+ get = types.ns['get'],
+ contains_Q = types.ns['contains?'],
+ count = types.ns['count'],
+ equal_Q = types.ns['='];
+
+
+console.log("Testing hash_maps");
+X = hash_map();
+assert_eq(true, hash_map_Q(X));
+
+assert_eq(null, get(X,'a'));
+assert_eq(false, contains_Q(X, 'a'));
+X1 = assoc(X, 'a', "value of X a");
+assert_eq(null, get(X,'a'));
+assert_eq(false, contains_Q(X, 'a'));
+assert_eq("value of X a", get(X1, 'a'));
+assert_eq(true, contains_Q(X1, 'a'));
+
+Y = hash_map();
+assert_eq(0, count(Y));
+Y1 = assoc(Y, 'a', "value of Y a");
+assert_eq(1, count(Y1));
+Y2 = assoc(Y1, 'b', "value of Y b");
+assert_eq(2, count(Y2));
+assert_eq("value of Y a", get(Y2, 'a'));
+assert_eq("value of Y b", get(Y2, 'b'));
+
+X2 = assoc(X1, 'b', Y2);
+assert_eq(2, count(Y2));
+
+assert_eq(true, hash_map_Q(get(X2,'b')));
+
+assert_eq('value of Y a', get(get(X2,'b'),'a'));
+assert_eq('value of Y b', get(get(X2,'b'),'b'));
+
+Y3 = dissoc(Y2, 'a');
+assert_eq(2, count(Y2));
+assert_eq(1, count(Y3));
+assert_eq(null, get(Y3, 'a'));
+Y4 = dissoc(Y3, 'b');
+assert_eq(0, count(Y4));
+assert_eq(null, get(Y4, 'b'));
+
+
+console.log("Testing equal? function");
+assert_eq(true, equal_Q(2,2));
+assert_eq(false, equal_Q(2,3));
+assert_eq(false, equal_Q(2,3));
+assert_eq(true, equal_Q("abc","abc"));
+assert_eq(false, equal_Q("abc","abz"));
+assert_eq(false, equal_Q("zbc","abc"));
+assert_eq(true, equal_Q(symbol("abc"),symbol("abc")));
+assert_eq(false, equal_Q(symbol("abc"),symbol("abz")));
+assert_eq(false, equal_Q(symbol("zbc"),symbol("abc")));
+L6 = [1, 2, 3];
+L7 = [1, 2, 3];
+L8 = [1, 2, "Z"];
+L9 = ["Z", 2, 3];
+L10 = [1, 2];
+assert_eq(true, equal_Q(L6, L7));
+assert_eq(false, equal_Q(L6, L8));
+assert_eq(false, equal_Q(L6, L9));
+assert_eq(false, equal_Q(L6, L10));
+assert_eq(false, equal_Q(L10, L6));
+
+
+console.log("Testing ENV (1 level)")
+env1 = new types.Env();
+assert_eq('val_a',env1.set('a','val_a'));
+assert_eq('val_b',env1.set('b','val_b'));
+assert_eq('val_eq',env1.set('=','val_eq'));
+assert_eq('val_a',env1.get('a'));
+assert_eq('val_b',env1.get('b'));
+assert_eq('val_eq',env1.get('='));
+
+console.log("Testing ENV (2 levels)");
+env2 = new types.Env(env1);
+assert_eq('val_b2',env2.set('b','val_b2'));
+assert_eq('val_c',env2.set('c','val_c'));
+assert_eq(env1,env2.find('a'));
+assert_eq(env2,env2.find('b'));
+assert_eq(env2,env2.find('c'));
+assert_eq('val_a', env2.get('a'));
+assert_eq('val_b2',env2.get('b'));
+assert_eq('val_c', env2.get('c'));
+