2011-12-24 12:41:12 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2013-02-05 03:01:19 +09:00
|
|
|
/* globals expect, it, describe, beforeEach, isArray, StringStream,
|
|
|
|
PostScriptParser, PostScriptLexer, PostScriptEvaluator */
|
2011-12-24 12:41:12 +09:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('function', function() {
|
2011-12-24 14:19:15 +09:00
|
|
|
beforeEach(function() {
|
|
|
|
this.addMatchers({
|
|
|
|
toMatchArray: function(expected) {
|
|
|
|
var actual = this.actual;
|
|
|
|
if (actual.length != expected.length)
|
|
|
|
return false;
|
|
|
|
for (var i = 0; i < expected.length; i++) {
|
|
|
|
var a = actual[i], b = expected[i];
|
|
|
|
if (isArray(b)) {
|
|
|
|
if (a.length != b.length)
|
|
|
|
return false;
|
|
|
|
for (var j = 0; j < a.length; j++) {
|
|
|
|
var suba = a[j], subb = b[j];
|
|
|
|
if (suba !== subb)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (a !== b)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2011-12-24 12:41:12 +09:00
|
|
|
|
|
|
|
describe('PostScriptParser', function() {
|
|
|
|
function parse(program) {
|
|
|
|
var stream = new StringStream(program);
|
2011-12-24 14:19:15 +09:00
|
|
|
var parser = new PostScriptParser(new PostScriptLexer(stream));
|
|
|
|
return parser.parse();
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
it('parses empty programs', function() {
|
|
|
|
var output = parse('{}');
|
2011-12-30 06:41:54 +09:00
|
|
|
expect(output.length).toEqual(0);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('parses positive numbers', function() {
|
|
|
|
var number = 999;
|
|
|
|
var program = parse('{ ' + number + ' }');
|
2011-12-30 06:41:54 +09:00
|
|
|
var expectedProgram = [number];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(program).toMatchArray(expectedProgram);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('parses negative numbers', function() {
|
|
|
|
var number = -999;
|
|
|
|
var program = parse('{ ' + number + ' }');
|
2011-12-30 06:41:54 +09:00
|
|
|
var expectedProgram = [number];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(program).toMatchArray(expectedProgram);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('parses negative floats', function() {
|
|
|
|
var number = 3.3;
|
|
|
|
var program = parse('{ ' + number + ' }');
|
2011-12-30 06:41:54 +09:00
|
|
|
var expectedProgram = [number];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(program).toMatchArray(expectedProgram);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('parses operators', function() {
|
|
|
|
var program = parse('{ sub }');
|
2011-12-30 06:41:54 +09:00
|
|
|
var expectedProgram = ['sub'];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(program).toMatchArray(expectedProgram);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('parses if statements', function() {
|
|
|
|
var program = parse('{ { 99 } if }');
|
2011-12-30 06:41:54 +09:00
|
|
|
var expectedProgram = [3, 'jz', 99];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(program).toMatchArray(expectedProgram);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('parses ifelse statements', function() {
|
|
|
|
var program = parse('{ { 99 } { 44 } ifelse }');
|
2011-12-30 06:41:54 +09:00
|
|
|
var expectedProgram = [5, 'jz', 99, 6, 'j', 44];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(program).toMatchArray(expectedProgram);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('handles missing brackets', function() {
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(function() { parse('{'); }).toThrow(
|
|
|
|
new Error('Unexpected symbol: found undefined expected 1.'));
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
2012-04-19 01:48:28 +09:00
|
|
|
it('handles junk after the end', function() {
|
|
|
|
var number = 3.3;
|
|
|
|
var program = parse('{ ' + number + ' }#');
|
|
|
|
var expectedProgram = [number];
|
|
|
|
expect(program).toMatchArray(expectedProgram);
|
|
|
|
});
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('PostScriptEvaluator', function() {
|
|
|
|
function evaluate(program) {
|
|
|
|
var stream = new StringStream(program);
|
2011-12-24 14:19:15 +09:00
|
|
|
var parser = new PostScriptParser(new PostScriptLexer(stream));
|
|
|
|
var code = parser.parse();
|
2011-12-30 06:41:54 +09:00
|
|
|
var evaluator = new PostScriptEvaluator(code);
|
2011-12-24 14:19:15 +09:00
|
|
|
var output = evaluator.execute();
|
|
|
|
return output;
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
2011-12-30 06:41:54 +09:00
|
|
|
|
2011-12-24 12:41:12 +09:00
|
|
|
it('pushes stack', function() {
|
|
|
|
var stack = evaluate('{ 99 }');
|
|
|
|
var expectedStack = [99];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('handles if with true', function() {
|
|
|
|
var stack = evaluate('{ 1 {99} if }');
|
|
|
|
var expectedStack = [99];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('handles if with false', function() {
|
|
|
|
var stack = evaluate('{ 0 {99} if }');
|
|
|
|
var expectedStack = [];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('handles ifelse with true', function() {
|
|
|
|
var stack = evaluate('{ 1 {99} {77} ifelse }');
|
|
|
|
var expectedStack = [99];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('handles ifelse with false', function() {
|
|
|
|
var stack = evaluate('{ 0 {99} {77} ifelse }');
|
|
|
|
var expectedStack = [77];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('handles nested if', function() {
|
|
|
|
var stack = evaluate('{ 1 {1 {77} if} if }');
|
|
|
|
var expectedStack = [77];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
2011-12-24 14:19:15 +09:00
|
|
|
|
2011-12-24 12:41:12 +09:00
|
|
|
it('abs', function() {
|
|
|
|
var stack = evaluate('{ -2 abs }');
|
|
|
|
var expectedStack = [2];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('adds', function() {
|
|
|
|
var stack = evaluate('{ 1 2 add }');
|
|
|
|
var expectedStack = [3];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('boolean ands', function() {
|
|
|
|
var stack = evaluate('{ true false and }');
|
|
|
|
var expectedStack = [false];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('bitwise ands', function() {
|
|
|
|
var stack = evaluate('{ 254 1 and }');
|
|
|
|
var expectedStack = [254 & 1];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
|
|
|
});
|
|
|
|
// TODO atan
|
|
|
|
// TODO bitshift
|
|
|
|
// TODO ceiling
|
|
|
|
// TODO copy
|
|
|
|
// TODO cos
|
2011-12-31 02:24:13 +09:00
|
|
|
it('converts to int', function() {
|
|
|
|
var stack = evaluate('{ 9.9 cvi }');
|
|
|
|
var expectedStack = [9];
|
|
|
|
expect(stack).toMatchArray(expectedStack);
|
|
|
|
});
|
|
|
|
it('converts negatives to int', function() {
|
|
|
|
var stack = evaluate('{ -9.9 cvi }');
|
|
|
|
var expectedStack = [-9];
|
|
|
|
expect(stack).toMatchArray(expectedStack);
|
|
|
|
});
|
2011-12-24 14:19:15 +09:00
|
|
|
// TODO cvr
|
|
|
|
// TODO div
|
2011-12-24 12:41:12 +09:00
|
|
|
it('duplicates', function() {
|
|
|
|
var stack = evaluate('{ 99 dup }');
|
|
|
|
var expectedStack = [99, 99];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
2011-12-24 14:19:15 +09:00
|
|
|
// TODO eq
|
|
|
|
it('exchanges', function() {
|
2011-12-24 12:41:12 +09:00
|
|
|
var stack = evaluate('{ 44 99 exch }');
|
|
|
|
var expectedStack = [99, 44];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
|
|
|
});
|
|
|
|
// TODO exp
|
|
|
|
// TODO false
|
|
|
|
// TODO floor
|
|
|
|
// TODO ge
|
|
|
|
// TODO gt
|
2011-12-31 06:25:34 +09:00
|
|
|
it('divides to integer', function() {
|
|
|
|
var stack = evaluate('{ 2 3 idiv }');
|
|
|
|
var expectedStack = [0];
|
|
|
|
expect(stack).toMatchArray(expectedStack);
|
|
|
|
});
|
|
|
|
it('divides to negative integer', function() {
|
|
|
|
var stack = evaluate('{ -2 3 idiv }');
|
|
|
|
var expectedStack = [0];
|
|
|
|
expect(stack).toMatchArray(expectedStack);
|
|
|
|
});
|
2011-12-24 14:19:15 +09:00
|
|
|
it('duplicates index', function() {
|
2011-12-24 12:41:12 +09:00
|
|
|
var stack = evaluate('{ 4 3 2 1 2 index }');
|
|
|
|
var expectedStack = [4, 3, 2, 1, 3];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
|
|
|
});
|
|
|
|
// TODO le
|
|
|
|
// TODO ln
|
|
|
|
// TODO log
|
|
|
|
// TODO lt
|
|
|
|
// TODO mod
|
|
|
|
// TODO mul
|
|
|
|
// TODO ne
|
|
|
|
// TODO neg
|
|
|
|
// TODO not
|
|
|
|
// TODO or
|
|
|
|
it('pops stack', function() {
|
2011-12-24 12:41:12 +09:00
|
|
|
var stack = evaluate('{ 1 2 pop }');
|
|
|
|
var expectedStack = [1];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
2011-12-24 14:19:15 +09:00
|
|
|
it('rolls stack right', function() {
|
2011-12-24 12:41:12 +09:00
|
|
|
var stack = evaluate('{ 1 3 2 2 4 1 roll }');
|
|
|
|
var expectedStack = [2, 1, 3, 2];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
it('rolls stack left', function() {
|
|
|
|
var stack = evaluate('{ 1 3 2 2 4 -1 roll }');
|
|
|
|
var expectedStack = [3, 2, 2, 1];
|
2011-12-24 14:19:15 +09:00
|
|
|
expect(stack).toMatchArray(expectedStack);
|
|
|
|
});
|
|
|
|
// TODO round
|
|
|
|
// TODO sin
|
|
|
|
// TODO sqrt
|
|
|
|
// TODO sub
|
|
|
|
// TODO true
|
|
|
|
// TODO truncate
|
|
|
|
// TODO xor
|
2011-12-24 12:41:12 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|