PageRenderTime 24ms CodeModel.GetById 8ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/test/selfchecks.js

https://bitbucket.org/darrint/qc.js/
JavaScript | 110 lines | 89 code | 16 blank | 5 comment | 7 complexity | 7dc3c5f34b8998852bd87c56e31d6192 MD5 | raw file
  1// Copyright (c) 2009, Darrin Thompson
  2
  3// Some self checks for our generators.
  4
  5declare("randWhole", [justSize],
  6        function(c, a) {
  7            var result = randWhole(a);
  8            c.noteArg(result);
  9            c.assert(result < a || result == 0);
 10        });
 11
 12declare("randWhole nonzero", [justSize],
 13        function(c, a) {
 14            c.guard(a > 10);
 15            var result = randWhole(a);
 16            c.noteArg(result);
 17            c.guard(result > 0);
 18        });
 19
 20declare("randWhole zero result", [justSize],
 21        function(c, a) {
 22            var result = randWhole(a);
 23            c.noteArg(result);
 24            c.guard(result == 0);
 25        });
 26
 27declare("randInt show positive", [arbWholeNum],
 28        function(c, a) {
 29            //console.log('input: ' + a);
 30            var result = randInt(a);
 31            c.noteArg(result);
 32            c.guard(result > 0);
 33        });
 34
 35declare("randInt show negative", [arbWholeNum],
 36        function(c, a) {
 37            var result = randInt(a);
 38            c.noteArg(result);
 39            c.guard(result < 0);
 40        });
 41
 42declare("randInt zero result", [arbWholeNum],
 43        function(c, a) {
 44            c.guard(a > 0);
 45            var result = randInt(a);
 46            c.noteArg(result);
 47            c.guard(result == 0);
 48        });
 49
 50declare("randRange between", [arbInt, arbInt],
 51        function(c, a, b) {
 52            c.guard(a < b);
 53            var result = randRange(a, b);
 54            c.noteArg(result);
 55            c.assert(a <= result);
 56            c.assert(b > result);
 57        });
 58
 59declare("randRange between backwards", [arbInt, arbInt],
 60        function(c, a, b) {
 61            c.guard(b < a);
 62            var result = randRange(a, b);
 63            c.noteArg(result);
 64            c.assert(b <= result);
 65            c.assert(a > result);
 66        });
 67
 68declare("randRange equal", [arbInt],
 69        function(c, a, b) {
 70            var result = randRange(a, a);
 71            c.assert(a == result);
 72        });
 73
 74declare("randFloatUnit", [],
 75	function(c) {
 76	    var result = randFloatUnit();
 77	    c.noteArg(result);
 78	    c.assert(result >= 0);
 79	    c.assert(result < 1);
 80	});
 81
 82declare("collectTest", [arbArray(arbInt)],
 83    function(c,a) {
 84        c.classify(a.length == 0, "empty array");
 85        c.collect(a.length);
 86    });
 87
 88var testArr = [1,2,0,1,0];
 89var testArb = {
 90    arb: function(){
 91        return testArr;
 92    },
 93    shrink: function(s,a) {
 94        //print('input: ' + a);
 95        var r = a.slice(1);
 96        //print('ret: ' + r);
 97        return r.length == 0 ? [] : [r];
 98    }
 99}
100
101declare("shrinkTest... must FAIL", [testArb],
102        function(c){
103            c.assert(false);
104        });
105
106declare("expect Exception", [arbInt],
107        expectException(function(c,i) {
108            throw("test");
109        }));
110