|
|
|
@ -1,7 +1,8 @@
@@ -1,7 +1,8 @@
|
|
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ |
|
|
|
|
/* globals expect, it, describe, beforeEach, isArray, StringStream, |
|
|
|
|
PostScriptParser, PostScriptLexer, PostScriptEvaluator */ |
|
|
|
|
PostScriptParser, PostScriptLexer, PostScriptEvaluator, |
|
|
|
|
PostScriptCompiler*/ |
|
|
|
|
|
|
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
@ -413,5 +414,108 @@ describe('function', function() {
@@ -413,5 +414,108 @@ describe('function', function() {
|
|
|
|
|
expect(stack).toMatchArray(expectedStack); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('PostScriptCompiler', function() { |
|
|
|
|
function check(code, domain, range, samples) { |
|
|
|
|
var compiler = new PostScriptCompiler(); |
|
|
|
|
var compiledCode = compiler.compile(code, domain, range); |
|
|
|
|
if (samples === null) { |
|
|
|
|
expect(compiledCode).toBeNull(); |
|
|
|
|
} else { |
|
|
|
|
expect(compiledCode).not.toBeNull(); |
|
|
|
|
/*jshint -W054 */ |
|
|
|
|
var fn = new Function('args', compiledCode); |
|
|
|
|
for (var i = 0; i < samples.length; i++) { |
|
|
|
|
var out = fn(samples[i].input); |
|
|
|
|
expect(out).toMatchArray(samples[i].output); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
it('check compiled add', function() { |
|
|
|
|
check([0.25, 0.5, 'add'], [], [0, 1], [{input: [], output: [0.75]}]); |
|
|
|
|
check([0, 'add'], [0, 1], [0, 1], [{input: [0.25], output: [0.25]}]); |
|
|
|
|
check([0.5, 'add'], [0, 1], [0, 1], [{input: [0.25], output: [0.75]}]); |
|
|
|
|
check([0, 'exch', 'add'], [0, 1], [0, 1], |
|
|
|
|
[{input: [0.25], output: [0.25]}]); |
|
|
|
|
check([0.5, 'exch', 'add'], [0, 1], [0, 1], |
|
|
|
|
[{input: [0.25], output: [0.75]}]); |
|
|
|
|
check(['add'], [0, 1, 0, 1], [0, 1], |
|
|
|
|
[{input: [0.25, 0.5], output: [0.75]}]); |
|
|
|
|
check(['add'], [0, 1], [0, 1], null); |
|
|
|
|
}); |
|
|
|
|
it('check compiled sub', function() { |
|
|
|
|
check([0.5, 0.25, 'sub'], [], [0, 1], [{input: [], output: [0.25]}]); |
|
|
|
|
check([0, 'sub'], [0, 1], [0, 1], [{input: [0.25], output: [0.25]}]); |
|
|
|
|
check([0.5, 'sub'], [0, 1], [0, 1], [{input: [0.75], output: [0.25]}]); |
|
|
|
|
check([0, 'exch', 'sub'], [0, 1], [-1, 1], |
|
|
|
|
[{input: [0.25], output: [-0.25]}]); |
|
|
|
|
check([0.75, 'exch', 'sub'], [0, 1], [-1, 1], |
|
|
|
|
[{input: [0.25], output: [0.5]}]); |
|
|
|
|
check(['sub'], [0, 1, 0, 1], [-1, 1], |
|
|
|
|
[{input: [0.25, 0.5], output: [-0.25]}]); |
|
|
|
|
check(['sub'], [0, 1], [0, 1], null); |
|
|
|
|
|
|
|
|
|
check([1, 'dup', 3, 2, 'roll', 'sub', 'sub'], [0, 1], [0, 1], |
|
|
|
|
[{input: [0.75], output: [0.75]}]); |
|
|
|
|
}); |
|
|
|
|
it('check compiled mul', function() { |
|
|
|
|
check([0.25, 0.5, 'mul'], [], [0, 1], [{input: [], output: [0.125]}]); |
|
|
|
|
check([0, 'mul'], [0, 1], [0, 1], [{input: [0.25], output: [0]}]); |
|
|
|
|
check([0.5, 'mul'], [0, 1], [0, 1], [{input: [0.25], output: [0.125]}]); |
|
|
|
|
check([1, 'mul'], [0, 1], [0, 1], [{input: [0.25], output: [0.25]}]); |
|
|
|
|
check([0, 'exch', 'mul'], [0, 1], [0, 1], [{input: [0.25], output: [0]}]); |
|
|
|
|
check([0.5, 'exch', 'mul'], [0, 1], [0, 1], |
|
|
|
|
[{input: [0.25], output: [0.125]}]); |
|
|
|
|
check([1, 'exch', 'mul'], [0, 1], [0, 1], |
|
|
|
|
[{input: [0.25], output: [0.25]}]); |
|
|
|
|
check(['mul'], [0, 1, 0, 1], [0, 1], |
|
|
|
|
[{input: [0.25, 0.5], output: [0.125]}]); |
|
|
|
|
check(['mul'], [0, 1], [0, 1], null); |
|
|
|
|
}); |
|
|
|
|
it('check compiled max', function() { |
|
|
|
|
check(['dup', 0.6, 'gt', 7, 'jz', 'pop', 0.6], [0, 1], [0, 1], |
|
|
|
|
[{input: [0.5], output: [0.5]}]); |
|
|
|
|
check(['dup', 0.6, 'gt', 7, 'jz', 'pop', 0.6], [0, 1], [0, 1], |
|
|
|
|
[{input: [1], output: [0.6]}]); |
|
|
|
|
check(['dup', 0.6, 'gt', 5, 'jz', 'pop', 0.6], [0, 1], [0, 1], null); |
|
|
|
|
}); |
|
|
|
|
it('check pop/roll/index', function() { |
|
|
|
|
check([1, 'pop'], [0, 1], [0, 1], [{input: [0.5], output: [0.5]}]); |
|
|
|
|
check([1, 3, -1, 'roll'], [0, 1, 0, 1], [0, 1, 0, 1, 0, 1], |
|
|
|
|
[{input: [0.25, 0.5], output: [0.5, 1, 0.25]}]); |
|
|
|
|
check([1, 3, 1, 'roll'], [0, 1, 0, 1], [0, 1, 0, 1, 0, 1], |
|
|
|
|
[{input: [0.25, 0.5], output: [1, 0.25, 0.5]}]); |
|
|
|
|
check([1, 3, 1.5, 'roll'], [0, 1, 0, 1], [0, 1, 0, 1, 0, 1], null); |
|
|
|
|
check([1, 1, 'index'], [0, 1], [0, 1, 0, 1, 0, 1], |
|
|
|
|
[{input: [0.5], output: [0.5, 1, 0.5]}]); |
|
|
|
|
check([1, 3, 'index', 'pop'], [0, 1], [0, 1], null); |
|
|
|
|
check([1, 0.5, 'index', 'pop'], [0, 1], [0, 1], null); |
|
|
|
|
}); |
|
|
|
|
it('check input boundaries', function () { |
|
|
|
|
check([], [0, 0.5], [0, 1], [{input: [1], output: [0.5]}]); |
|
|
|
|
check([], [0.5, 1], [0, 1], [{input: [0], output: [0.5]}]); |
|
|
|
|
check(['dup'], [0.5, 0.6], [0, 1, 0, 1], |
|
|
|
|
[{input: [0], output: [0.5, 0.5]}]); |
|
|
|
|
check([], [100, 1001], [0, 10000], [{input: [1000], output: [1000]}]); |
|
|
|
|
}); |
|
|
|
|
it('check output boundaries', function () { |
|
|
|
|
check([], [0, 1], [0, 0.5], [{input: [1], output: [0.5]}]); |
|
|
|
|
check([], [0, 1], [0.5, 1], [{input: [0], output: [0.5]}]); |
|
|
|
|
check(['dup'], [0, 1], [0.5, 1, 0.6, 1], |
|
|
|
|
[{input: [0], output: [0.5, 0.6]}]); |
|
|
|
|
check([], [0, 10000], [100, 1001], [{input: [1000], output: [1000]}]); |
|
|
|
|
}); |
|
|
|
|
it('compile optimized', function () { |
|
|
|
|
var compiler = new PostScriptCompiler(); |
|
|
|
|
var code = [0, 'add', 1, 1, 3, -1, 'roll', 'sub', 'sub', 1, 'mul']; |
|
|
|
|
var compiledCode = compiler.compile(code, [0, 1], [0, 1]); |
|
|
|
|
expect(compiledCode).toEqual('return [\n' + |
|
|
|
|
' Math.max(0, Math.min(1, args[0]))\n' + |
|
|
|
|
'];'); |
|
|
|
|
|
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|