Last active
August 29, 2015 14:13
-
-
Save n2westman/04b9ef2f1e1dc9f48402 to your computer and use it in GitHub Desktop.
hw1-tests.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tests( | |
F, | |
{ | |
name: 'primitive number', | |
code: '6', | |
expected: 6 | |
}, | |
{ | |
name: 'primitive boolean', | |
code: 'true', | |
expected: true | |
}, | |
{ | |
name: 'plus', | |
code: '6 + 7', | |
expected: 13 | |
}, | |
{ | |
name: 'minus', | |
code: '6 - 7', | |
expected: -1 | |
}, | |
{ | |
name: 'mult', | |
code: '6 * 7', | |
expected: 42 | |
}, | |
{ | |
name: 'div', | |
code: '14 / 2', | |
expected: 7 | |
}, | |
{ | |
name: 'div by zero', | |
code: '14 / 0', | |
shouldThrow: true | |
}, | |
{ | |
name: 'mod by zero', | |
code: '14 % 0', | |
shouldThrow: true | |
}, | |
{ | |
name: 'compound', | |
code: '6*7 - 14/7', | |
expected: 40 | |
}, | |
{ | |
name: 'compound 2', | |
code: '(6 + 7) * 2', | |
expected: 26 | |
}, | |
{ | |
name: 'eq', | |
code: '14 = 0', | |
expected: false | |
}, | |
{ | |
name: 'eq2', | |
code: '14 = 14', | |
expected: true | |
}, | |
{ | |
name: 'neq', | |
code: '14 != 0', | |
expected: true | |
}, | |
{ | |
name: 'neq 2', | |
code: 'false != 0', | |
expected: true | |
}, | |
{ | |
name: 'neq 3', | |
code: '(fun x -> x) != 0', | |
expected: true | |
}, | |
{ | |
name: 'less than', | |
code: '6 < 7', | |
expected: true | |
}, | |
{ | |
name: 'greater than', | |
code: '6 > 7', | |
expected: false | |
}, | |
{ | |
name: 'arithmetic and relational operators should require args to be numbers', | |
code: '(fun x -> x) + 1', | |
shouldThrow: true | |
}, | |
{ | |
name: 'or', | |
code: 'true || false', | |
expected: true | |
}, | |
{ | |
name: 'and', | |
code: '6 < 7 && true', | |
expected: true | |
}, | |
{ | |
name: 'broken and', | |
code: '6 < (7 && true)', | |
shouldThrow: true | |
}, | |
{ | |
name: 'boolean op throw', | |
code: '(fun x -> x) || true', | |
shouldThrow: true | |
}, | |
{ | |
name: 'conditional', | |
code: 'if 2 > 3 then 4321 else 1234', | |
expected: 1234 | |
}, | |
{ | |
name: 'conditional 2', | |
code: 'if 2 < 3 then 4321 else 1234', | |
expected: 4321 | |
}, | |
{ | |
name: 'let', | |
code: 'let x = 3 + 4 in\n' + | |
' x - 2', | |
expected: 5 | |
}, | |
{ | |
name: 'unbound identifier', | |
code: 'x + 1', | |
shouldThrow: true | |
}, | |
{ | |
name: 'unbound identifier 2', | |
code: '(let x = 1 in x) + x', | |
shouldThrow: true | |
}, | |
{ | |
name: 'bound identifier', | |
code: '(let x = 1 in x) + 10', | |
expected: 11 | |
}, | |
{ | |
name: 'fun and call', | |
code: '(fun x -> x + 1) 3', | |
expected: 4 | |
}, | |
{ | |
name: 'passing too many args is not OK', | |
code: '(fun x -> x + 1) 3 4', | |
shouldThrow: true | |
}, | |
{ | |
name: 'nested funs', | |
code: 'let add = fun x -> fun y -> x + y in\n' + | |
' let inc = add 1 in\n' + | |
' inc 10', | |
expected: 11 | |
}, | |
{ //Closures are the tough one | |
name: 'closures', | |
code: 'let x = (let y = 10 in fun x -> x + y) in x 20', | |
expected: 30 | |
}, | |
{ | |
name: 'shadowing and closures', | |
code: 'let f = (let x = 10 in let x = 5 in fun y -> y + x) in f 10', | |
expected: 15 | |
}, | |
{ | |
name: 'double lookup', | |
code: 'let y = 5 in let x = 5 in y', | |
expected: 5 | |
}, | |
{ | |
name: 'unit function', | |
code: 'let f = fun -> 5 in f ()', | |
expected: 5 | |
}, | |
{ | |
name: 'unit closure', | |
code: 'let f = (let x = 10 in fun -> x) in f ()', | |
expected: 10 | |
}, | |
{ | |
name: 'equality of functions (shallow)', | |
code: 'let f = fun x -> 0 in ' + | |
'let f2 = f in ' + | |
'f = f2', | |
expected: true | |
}, | |
{ | |
name: 'inequality of functions (shallow)', | |
code: 'let f = fun x -> 0 in ' + | |
'let f2 = fun x -> 0 in ' + | |
'f != f2', | |
expected: true | |
}, | |
{ | |
name: 'function with multiple parameters', | |
code: '(fun x y -> x + y) 1 2', | |
expected: 3 | |
}, | |
{ | |
name: 'equality of different types', | |
code: '(fun x -> x) = 1 || 1 = true || null = 2 || null = (fun y -> 1)', | |
expected: false | |
}, | |
{ | |
name: 'local variables', | |
code: 'let y = 1 in' + | |
'(fun x -> x + y) 2 + x', | |
shouldThrow: true | |
}, | |
/* start Runhang's test */ | |
{ | |
name: 'rh-t1', | |
code: 'let f = fun x -> ((x + 2) * x / 1) % 2 in\n' + | |
'(let x = 5 in let x = 3 in x) * (let x = 3 in f (x + 2))', | |
expected: 3 | |
}, | |
{ | |
name: 'rh-t2', | |
code: 'let f = fun x -> let x = 1 in x = 1 in f 2', | |
expected: true | |
}, | |
/* start Zhixuan's test */ | |
{ | |
name: 'primValueNumber1', | |
code: '1', | |
expected: 1 | |
}, | |
{ | |
name: 'primValueNumber2', | |
code: '-10', | |
expected: -10 | |
}, | |
{ | |
name: 'primValueNumber3', | |
code: '0', | |
expected: 0 | |
}, | |
{ | |
name: 'primValueBoolean1', | |
code: 'true', | |
expected: true | |
}, | |
{ | |
name: 'primValueBoolean2', | |
code: 'false', | |
expected: false | |
}, | |
{ | |
name: 'primValueNull1', | |
code: 'null', | |
expected: null | |
}, | |
{ | |
name: 'primValueNull2', | |
code: 'undefined', | |
shouldThrow: true | |
}, | |
// ops | |
{ | |
name: 'arithmetic and relational operators type checking', | |
code: 'true > false', | |
shouldThrow: true | |
}, | |
{ | |
name: 'arithmetic and relational operators type checking', | |
code: 'true < 3', | |
shouldThrow: true | |
}, | |
{ | |
name: 'arithmetic and relational operators type checking', | |
code: 'true / 3', | |
shouldThrow: true | |
}, | |
{ | |
name: 'arithmetic and relational operators type checking', | |
code: '3 - false', | |
shouldThrow: true | |
}, | |
{ | |
name: 'arithmetic and relational operators type checking', | |
code: 'true % false', | |
shouldThrow: true | |
}, | |
// TODO: confirm this | |
{ | |
name: 'logical operators type checking', | |
code: '1 || 1', | |
shouldThrow: true | |
}, | |
{ | |
name: 'logical operators type checking', | |
code: '4 && 1', | |
shouldThrow: true | |
}, | |
{ | |
name: 'logical operators type checking', | |
code: 'true || 1', | |
shouldThrow: true | |
}, | |
{ | |
name: 'logical operators type checking', | |
code: 'false && 1', | |
shouldThrow: true | |
}, | |
// equality | |
{ | |
name: 'equalty', | |
code: '1 = 1', | |
expected: true | |
}, | |
{ | |
name: 'equalty', | |
code: 'true = 1', | |
expected: false | |
}, | |
{ | |
name: 'equalty', | |
code: '1 = null', | |
expected: false | |
}, | |
{ | |
name: 'equalty', | |
code: 'null = null', | |
expected: true | |
}, | |
// id | |
{ | |
name: 'variable shadowing', | |
code: 'let x = 3 in let x = 4 in x', | |
expected: 4 | |
}, | |
{ | |
name: 'unbound value', | |
code: 'let f = fun x y -> x*y in f y', | |
shouldThrow: true | |
}, | |
// if | |
// TODO: confirm this | |
{ | |
name: 'if requires e1 to be boolean', | |
code: 'if 4 then 2 else false', | |
shouldThrow: true | |
}, | |
{ | |
name: 'conditional', | |
code: 'if 2 < 3 then 4321 else 1234', | |
expected: 4321 | |
}, | |
// call | |
{ | |
name: 'call', | |
code: 'let x = fun x -> x in x 1', | |
expected: 1 | |
}, | |
{ | |
name: 'calling with multiple args', | |
code: 'let f = fun x y -> x*y in f 10 3', | |
expected: 30 | |
}, | |
{ | |
name: 'calling with too few args', | |
code: 'let f = fun x y -> x in f 10 ', | |
shouldThrow: true | |
}, | |
{ | |
name: 'calling with too many args', | |
code: 'let f = fun x y -> x in f 10 3 true', | |
shouldThrow: true | |
}, | |
{ | |
name: 'if condtion must be boolean', | |
code: 'if 0 then 1 else 2', | |
shouldThrow: true | |
} | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment