Created
October 29, 2012 16:07
-
-
Save mokele/3974484 to your computer and use it in GitHub Desktop.
PropEr/Quickcheck example in Javascript
This file contains hidden or 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
var Proper = {}; | |
function even_number() { | |
return LET([integer()], | |
function(i) { | |
return i * 2; | |
} | |
); | |
} | |
Proper.props = { | |
string_fromCharCode: function() { | |
return FORALL([string()], | |
function(charlist) { | |
var s = String.fromCharCodes(charlist); | |
if(typeof s != 'string') { | |
return false; | |
} | |
var converted = s.toCharCodes(); | |
for(var i=0; i<charlist.length; i++) { | |
if(charlist[i] != converted[i]) { | |
return false; | |
} | |
} | |
return charlist.length == converted.length; | |
} | |
); | |
}, | |
string_reverse: function() { | |
return FORALL([string()], | |
function(charlist) { | |
var s = String.fromCharCodes(charlist); | |
var reversed = s.reverse(); | |
var half = s.length / 2; | |
var left = s.substring(0, Math.floor(half)); | |
var right = s.substring(Math.ceil(half)); | |
var symetric = left == right; | |
return (symetric && reversed == s) || (reversed != s && reversed.reverse() == s); | |
} | |
); | |
}, | |
even_number: function() { | |
return FORALL([even_number()], | |
function(i) { | |
return i % 2 == 0; | |
} | |
); | |
}, | |
pos_integer: function() { | |
return FORALL([pos_integer()], | |
function(i) { | |
return i > 0 | |
} | |
); | |
}, | |
neg_integer: function() { | |
return FORALL([neg_integer()], | |
function(i) { | |
return i < 0 | |
} | |
); | |
}, | |
non_neg_integer: function() { | |
return FORALL([non_neg_integer()], | |
function(i) { | |
return i >= 0 | |
} | |
); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment