Skip to content
Browse files

first commit

  • Loading branch information...
0 parents commit 499937be84bccb7a463b890ceb6fd5ba599451bb @mcandre committed Mar 14, 2011
Showing with 171 additions and 0 deletions.
  1. +11 −0 README.md
  2. +40 −0 example.js
  3. +20 −0 package.json
  4. +100 −0 quickcheck.js
11 README.md
@@ -0,0 +1,11 @@
+quickcheck - Node.js port of the QuickCheck unit test framework
+
+Example
+
+ var qc = require("quickcheck");
+
+ function propertyEven(x) { return x % 2 == 0; }
+
+ qc.forAll(propertyEven, qc.arbByte);
+
+ *** Failed! 27
40 example.js
@@ -0,0 +1,40 @@
+#!/usr/bin/env node
+
+var qc = require("./quickcheck");
+
+function propertyEven(x) { return x % 2 == 0; }
+
+function arbEven() {
+ var b = qc.arbByte();
+
+ if (b % 2 == 0) { return b; }
+ else { return (b + 1) % 256; }
+}
+
+function validInteger(s) {
+ var i = parseInt(s, 10);
+ return typeof(i) == "number" && !isNaN(i);
+}
+
+function arbDigits() {
+ var d = "";
+
+ while (d.length < 1) { d = qc.arbArray(function () {
+ return String.fromCharCode(48 + Math.floor(Math.random() * 10));
+ });
+ }
+
+ return d;
+}
+
+function main() {
+ qc.forAll(propertyEven, qc.arbByte); // *** Failed!
+
+ qc.forAll(propertyEven, arbEven); // +++ OK, passed 100 tests.
+
+ qc.forAll(validInteger, qc.arbString); // *** Failed!
+
+ qc.forAll(validInteger, arbDigits); // +++ OK, passed 100 tests.
+}
+
+if (!module.parent) { main(); }
20 package.json
@@ -0,0 +1,20 @@
+{
+ "name": "quickcheck",
+ "description": "Node.js port of the QuickCheck unit test framework",
+ "version": "0.0.0",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/mcandre/node-quickcheck.git"
+ },
+ "author": "Andrew Pennebaker <andrew.pennebaker@gmail.com> (http://www.yellosoft.us/)",
+ "main": "quickcheck",
+ "directories": {
+ "lib": "."
+ },
+ "scripts": {
+ "test": "test()"
+ },
+ "engines": {
+ "node": "*"
+ }
+}
100 quickcheck.js
@@ -0,0 +1,100 @@
+function arbBool() {
+ return Math.random() > 0.5 ? true : false;
+}
+
+exports.arbBool = arbBool;
+
+function arbDouble() {
+ var sign = Math.random() > 0.5 ? 1 : -1;
+ return sign * Math.random() * Number.MAX_VALUE;
+}
+
+exports.arbDouble = arbDouble;
+
+function arbInt() {
+ var sign = Math.random() > 0.5 ? 1 : -1;
+ return sign * Math.floor(Math.random() * Number.MAX_VALUE);
+}
+
+exports.arbInt = arbInt;
+
+function arbByte() {
+ return Math.floor(Math.random() * 256);
+}
+
+exports.arbByte = arbByte;
+
+function arbChar() {
+ return String.fromCharCode(arbByte());
+}
+
+exports.arbChar = arbChar;
+
+function arbArray(generator) {
+ var len = Math.floor(Math.random() * 100);
+ var array = [];
+
+ for (var i = 0; i < len; i++) {
+ array.push(generator());
+ }
+
+ return array;
+}
+
+exports.arbArray = arbArray;
+
+function arbString() {
+ return arbArray(arbChar).join("");
+}
+
+exports.arbString = arbString;
+
+function forAll(property) {
+ var generators = Array.prototype.slice.call(arguments, 1);
+
+ for (var i = 0; i < 100; i ++) {
+ var values = generators.map(function (f) { return f(); });
+
+ var propertyHolds = property.apply(null, values);
+
+ if (!propertyHolds) {
+ console.log("*** Failed! " + values);
+ return false;
+ }
+ }
+
+ console.log("+++ OK, passed 100 tests.");
+
+ return true;
+}
+
+exports.forAll = forAll;
+
+function forAllSilent() {
+ console.oldLog = console.log;
+ console.log = function () {}
+
+ var result = forAll.apply(null, arguments);
+
+ console.log = console.oldLog;
+
+ return result;
+}
+
+exports.forAllSilent = forAllSilent;
+
+// Test quickcheck itself
+function test() {
+ var propertyEven = function (x) { return x % 2 == 0; };
+ console.assert(!forAllSilent(propertyEven, arbByte));
+
+ var propertyNumber = function (x) { return typeof(x) == "number"; }
+ console.assert(forAllSilent(propertyNumber, arbInt));
+
+ var propertyTrue = function (x) { return x; }
+ console.assert(!forAllSilent(propertyTrue, arbBool));
+
+ return true;
+}
+
+exports.test = test;

0 comments on commit 499937b

Please sign in to comment.
Something went wrong with that request. Please try again.