2
0

function_spec.js 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. var coreFunction = require('../../core/function.js');
  17. var corePsParser = require('../../core/ps_parser.js');
  18. var coreStream = require('../../core/stream.js');
  19. var sharedUtil = require('../../shared/util.js');
  20. var PostScriptEvaluator = coreFunction.PostScriptEvaluator;
  21. var PostScriptCompiler = coreFunction.PostScriptCompiler;
  22. var PostScriptParser = corePsParser.PostScriptParser;
  23. var PostScriptLexer = corePsParser.PostScriptLexer;
  24. var StringStream = coreStream.StringStream;
  25. var isArray = sharedUtil.isArray;
  26. describe('function', function () {
  27. beforeEach(function () {
  28. jasmine.addMatchers({
  29. toMatchArray: function (util, customEqualityTesters) {
  30. return {
  31. compare: function (actual, expected) {
  32. var result = {};
  33. if (actual.length !== expected.length) {
  34. result.pass = false;
  35. result.message = 'Array length: ' + actual.length + ', expected: ' + expected.length;
  36. return result;
  37. }
  38. result.pass = true;
  39. for (var i = 0; i < expected.length; i++) {
  40. var a = actual[i], b = expected[i];
  41. if (isArray(b)) {
  42. if (a.length !== b.length) {
  43. result.pass = false;
  44. break;
  45. }
  46. for (var j = 0; j < a.length; j++) {
  47. var suba = a[j], subb = b[j];
  48. if (suba !== subb) {
  49. result.pass = false;
  50. break;
  51. }
  52. }
  53. } else {
  54. if (a !== b) {
  55. result.pass = false;
  56. break;
  57. }
  58. }
  59. }
  60. return result;
  61. }
  62. };
  63. }
  64. });
  65. });
  66. describe('PostScriptParser', function () {
  67. function parse(program) {
  68. var stream = new StringStream(program);
  69. var parser = new PostScriptParser(new PostScriptLexer(stream));
  70. return parser.parse();
  71. }
  72. it('parses empty programs', function () {
  73. var output = parse('{}');
  74. expect(output.length).toEqual(0);
  75. });
  76. it('parses positive numbers', function () {
  77. var number = 999;
  78. var program = parse('{ ' + number + ' }');
  79. var expectedProgram = [number];
  80. expect(program).toMatchArray(expectedProgram);
  81. });
  82. it('parses negative numbers', function () {
  83. var number = -999;
  84. var program = parse('{ ' + number + ' }');
  85. var expectedProgram = [number];
  86. expect(program).toMatchArray(expectedProgram);
  87. });
  88. it('parses negative floats', function () {
  89. var number = 3.3;
  90. var program = parse('{ ' + number + ' }');
  91. var expectedProgram = [number];
  92. expect(program).toMatchArray(expectedProgram);
  93. });
  94. it('parses operators', function () {
  95. var program = parse('{ sub }');
  96. var expectedProgram = ['sub'];
  97. expect(program).toMatchArray(expectedProgram);
  98. });
  99. it('parses if statements', function () {
  100. var program = parse('{ { 99 } if }');
  101. var expectedProgram = [
  102. 3,
  103. 'jz',
  104. 99
  105. ];
  106. expect(program).toMatchArray(expectedProgram);
  107. });
  108. it('parses ifelse statements', function () {
  109. var program = parse('{ { 99 } { 44 } ifelse }');
  110. var expectedProgram = [
  111. 5,
  112. 'jz',
  113. 99,
  114. 6,
  115. 'j',
  116. 44
  117. ];
  118. expect(program).toMatchArray(expectedProgram);
  119. });
  120. it('handles missing brackets', function () {
  121. expect(function () {
  122. parse('{');
  123. }).toThrow(new Error('Unexpected symbol: found undefined expected 1.'));
  124. });
  125. it('handles junk after the end', function () {
  126. var number = 3.3;
  127. var program = parse('{ ' + number + ' }#');
  128. var expectedProgram = [number];
  129. expect(program).toMatchArray(expectedProgram);
  130. });
  131. });
  132. describe('PostScriptEvaluator', function () {
  133. function evaluate(program) {
  134. var stream = new StringStream(program);
  135. var parser = new PostScriptParser(new PostScriptLexer(stream));
  136. var code = parser.parse();
  137. var evaluator = new PostScriptEvaluator(code);
  138. var output = evaluator.execute();
  139. return output;
  140. }
  141. it('pushes stack', function () {
  142. var stack = evaluate('{ 99 }');
  143. var expectedStack = [99];
  144. expect(stack).toMatchArray(expectedStack);
  145. });
  146. it('handles if with true', function () {
  147. var stack = evaluate('{ 1 {99} if }');
  148. var expectedStack = [99];
  149. expect(stack).toMatchArray(expectedStack);
  150. });
  151. it('handles if with false', function () {
  152. var stack = evaluate('{ 0 {99} if }');
  153. var expectedStack = [];
  154. expect(stack).toMatchArray(expectedStack);
  155. });
  156. it('handles ifelse with true', function () {
  157. var stack = evaluate('{ 1 {99} {77} ifelse }');
  158. var expectedStack = [99];
  159. expect(stack).toMatchArray(expectedStack);
  160. });
  161. it('handles ifelse with false', function () {
  162. var stack = evaluate('{ 0 {99} {77} ifelse }');
  163. var expectedStack = [77];
  164. expect(stack).toMatchArray(expectedStack);
  165. });
  166. it('handles nested if', function () {
  167. var stack = evaluate('{ 1 {1 {77} if} if }');
  168. var expectedStack = [77];
  169. expect(stack).toMatchArray(expectedStack);
  170. });
  171. it('abs', function () {
  172. var stack = evaluate('{ -2 abs }');
  173. var expectedStack = [2];
  174. expect(stack).toMatchArray(expectedStack);
  175. });
  176. it('adds', function () {
  177. var stack = evaluate('{ 1 2 add }');
  178. var expectedStack = [3];
  179. expect(stack).toMatchArray(expectedStack);
  180. });
  181. it('boolean and', function () {
  182. var stack = evaluate('{ true false and }');
  183. var expectedStack = [false];
  184. expect(stack).toMatchArray(expectedStack);
  185. });
  186. it('bitwise and', function () {
  187. var stack = evaluate('{ 254 1 and }');
  188. var expectedStack = [254 & 1];
  189. expect(stack).toMatchArray(expectedStack);
  190. });
  191. it('calculates the inverse tangent of a number', function () {
  192. var stack = evaluate('{ 90 atan }');
  193. var expectedStack = [Math.atan(90)];
  194. expect(stack).toMatchArray(expectedStack);
  195. });
  196. it('handles bitshifting ', function () {
  197. var stack = evaluate('{ 50 2 bitshift }');
  198. var expectedStack = [200];
  199. expect(stack).toMatchArray(expectedStack);
  200. });
  201. it('calculates the ceiling value', function () {
  202. var stack = evaluate('{ 9.9 ceiling }');
  203. var expectedStack = [10];
  204. expect(stack).toMatchArray(expectedStack);
  205. });
  206. it('copies', function () {
  207. var stack = evaluate('{ 99 98 2 copy }');
  208. var expectedStack = [
  209. 99,
  210. 98,
  211. 99,
  212. 98
  213. ];
  214. expect(stack).toMatchArray(expectedStack);
  215. });
  216. it('calculates the cosine of a number', function () {
  217. var stack = evaluate('{ 90 cos }');
  218. var expectedStack = [Math.cos(90)];
  219. expect(stack).toMatchArray(expectedStack);
  220. });
  221. it('converts to int', function () {
  222. var stack = evaluate('{ 9.9 cvi }');
  223. var expectedStack = [9];
  224. expect(stack).toMatchArray(expectedStack);
  225. });
  226. it('converts negatives to int', function () {
  227. var stack = evaluate('{ -9.9 cvi }');
  228. var expectedStack = [-9];
  229. expect(stack).toMatchArray(expectedStack);
  230. });
  231. it('converts to real', function () {
  232. var stack = evaluate('{ 55.34 cvr }');
  233. var expectedStack = [55.34];
  234. expect(stack).toMatchArray(expectedStack);
  235. });
  236. it('divides', function () {
  237. var stack = evaluate('{ 6 5 div }');
  238. var expectedStack = [1.2];
  239. expect(stack).toMatchArray(expectedStack);
  240. });
  241. it('maps division by zero to infinity', function () {
  242. var stack = evaluate('{ 6 0 div }');
  243. var expectedStack = [Infinity];
  244. expect(stack).toMatchArray(expectedStack);
  245. });
  246. it('duplicates', function () {
  247. var stack = evaluate('{ 99 dup }');
  248. var expectedStack = [
  249. 99,
  250. 99
  251. ];
  252. expect(stack).toMatchArray(expectedStack);
  253. });
  254. it('accepts an equality', function () {
  255. var stack = evaluate('{ 9 9 eq }');
  256. var expectedStack = [true];
  257. expect(stack).toMatchArray(expectedStack);
  258. });
  259. it('rejects an inequality', function () {
  260. var stack = evaluate('{ 9 8 eq }');
  261. var expectedStack = [false];
  262. expect(stack).toMatchArray(expectedStack);
  263. });
  264. it('exchanges', function () {
  265. var stack = evaluate('{ 44 99 exch }');
  266. var expectedStack = [
  267. 99,
  268. 44
  269. ];
  270. expect(stack).toMatchArray(expectedStack);
  271. });
  272. it('handles exponentiation', function () {
  273. var stack = evaluate('{ 10 2 exp }');
  274. var expectedStack = [100];
  275. expect(stack).toMatchArray(expectedStack);
  276. });
  277. it('pushes false onto the stack', function () {
  278. var stack = evaluate('{ false }');
  279. var expectedStack = [false];
  280. expect(stack).toMatchArray(expectedStack);
  281. });
  282. it('calculates the floor value', function () {
  283. var stack = evaluate('{ 9.9 floor }');
  284. var expectedStack = [9];
  285. expect(stack).toMatchArray(expectedStack);
  286. });
  287. it('handles greater than or equal to', function () {
  288. var stack = evaluate('{ 10 9 ge }');
  289. var expectedStack = [true];
  290. expect(stack).toMatchArray(expectedStack);
  291. });
  292. it('rejects less than for greater than or equal to', function () {
  293. var stack = evaluate('{ 8 9 ge }');
  294. var expectedStack = [false];
  295. expect(stack).toMatchArray(expectedStack);
  296. });
  297. it('handles greater than', function () {
  298. var stack = evaluate('{ 10 9 gt }');
  299. var expectedStack = [true];
  300. expect(stack).toMatchArray(expectedStack);
  301. });
  302. it('rejects less than or equal for greater than', function () {
  303. var stack = evaluate('{ 9 9 gt }');
  304. var expectedStack = [false];
  305. expect(stack).toMatchArray(expectedStack);
  306. });
  307. it('divides to integer', function () {
  308. var stack = evaluate('{ 2 3 idiv }');
  309. var expectedStack = [0];
  310. expect(stack).toMatchArray(expectedStack);
  311. });
  312. it('divides to negative integer', function () {
  313. var stack = evaluate('{ -2 3 idiv }');
  314. var expectedStack = [0];
  315. expect(stack).toMatchArray(expectedStack);
  316. });
  317. it('duplicates index', function () {
  318. var stack = evaluate('{ 4 3 2 1 2 index }');
  319. var expectedStack = [
  320. 4,
  321. 3,
  322. 2,
  323. 1,
  324. 3
  325. ];
  326. expect(stack).toMatchArray(expectedStack);
  327. });
  328. it('handles less than or equal to', function () {
  329. var stack = evaluate('{ 9 10 le }');
  330. var expectedStack = [true];
  331. expect(stack).toMatchArray(expectedStack);
  332. });
  333. it('rejects greater than for less than or equal to', function () {
  334. var stack = evaluate('{ 10 9 le }');
  335. var expectedStack = [false];
  336. expect(stack).toMatchArray(expectedStack);
  337. });
  338. it('calculates the natural logarithm', function () {
  339. var stack = evaluate('{ 10 ln }');
  340. var expectedStack = [Math.log(10)];
  341. expect(stack).toMatchArray(expectedStack);
  342. });
  343. it('calculates the base 10 logarithm', function () {
  344. var stack = evaluate('{ 100 log }');
  345. var expectedStack = [2];
  346. expect(stack).toMatchArray(expectedStack);
  347. });
  348. it('handles less than', function () {
  349. var stack = evaluate('{ 9 10 lt }');
  350. var expectedStack = [true];
  351. expect(stack).toMatchArray(expectedStack);
  352. });
  353. it('rejects greater than or equal to for less than', function () {
  354. var stack = evaluate('{ 10 9 lt }');
  355. var expectedStack = [false];
  356. expect(stack).toMatchArray(expectedStack);
  357. });
  358. it('performs the modulo operation', function () {
  359. var stack = evaluate('{ 4 3 mod }');
  360. var expectedStack = [1];
  361. expect(stack).toMatchArray(expectedStack);
  362. });
  363. it('multiplies two numbers (positive result)', function () {
  364. var stack = evaluate('{ 9 8 mul }');
  365. var expectedStack = [72];
  366. expect(stack).toMatchArray(expectedStack);
  367. });
  368. it('multiplies two numbers (negative result)', function () {
  369. var stack = evaluate('{ 9 -8 mul }');
  370. var expectedStack = [-72];
  371. expect(stack).toMatchArray(expectedStack);
  372. });
  373. it('accepts an inequality', function () {
  374. var stack = evaluate('{ 9 8 ne }');
  375. var expectedStack = [true];
  376. expect(stack).toMatchArray(expectedStack);
  377. });
  378. it('rejects an equality', function () {
  379. var stack = evaluate('{ 9 9 ne }');
  380. var expectedStack = [false];
  381. expect(stack).toMatchArray(expectedStack);
  382. });
  383. it('negates', function () {
  384. var stack = evaluate('{ 4.5 neg }');
  385. var expectedStack = [-4.5];
  386. expect(stack).toMatchArray(expectedStack);
  387. });
  388. it('boolean not', function () {
  389. var stack = evaluate('{ true not }');
  390. var expectedStack = [false];
  391. expect(stack).toMatchArray(expectedStack);
  392. });
  393. it('bitwise not', function () {
  394. var stack = evaluate('{ 12 not }');
  395. var expectedStack = [-13];
  396. expect(stack).toMatchArray(expectedStack);
  397. });
  398. it('boolean or', function () {
  399. var stack = evaluate('{ true false or }');
  400. var expectedStack = [true];
  401. expect(stack).toMatchArray(expectedStack);
  402. });
  403. it('bitwise or', function () {
  404. var stack = evaluate('{ 254 1 or }');
  405. var expectedStack = [254 | 1];
  406. expect(stack).toMatchArray(expectedStack);
  407. });
  408. it('pops stack', function () {
  409. var stack = evaluate('{ 1 2 pop }');
  410. var expectedStack = [1];
  411. expect(stack).toMatchArray(expectedStack);
  412. });
  413. it('rolls stack right', function () {
  414. var stack = evaluate('{ 1 3 2 2 4 1 roll }');
  415. var expectedStack = [
  416. 2,
  417. 1,
  418. 3,
  419. 2
  420. ];
  421. expect(stack).toMatchArray(expectedStack);
  422. });
  423. it('rolls stack left', function () {
  424. var stack = evaluate('{ 1 3 2 2 4 -1 roll }');
  425. var expectedStack = [
  426. 3,
  427. 2,
  428. 2,
  429. 1
  430. ];
  431. expect(stack).toMatchArray(expectedStack);
  432. });
  433. it('rounds a number', function () {
  434. var stack = evaluate('{ 9.52 round }');
  435. var expectedStack = [10];
  436. expect(stack).toMatchArray(expectedStack);
  437. });
  438. it('calculates the sine of a number', function () {
  439. var stack = evaluate('{ 90 sin }');
  440. var expectedStack = [Math.sin(90)];
  441. expect(stack).toMatchArray(expectedStack);
  442. });
  443. it('calculates a square root (integer)', function () {
  444. var stack = evaluate('{ 100 sqrt }');
  445. var expectedStack = [10];
  446. expect(stack).toMatchArray(expectedStack);
  447. });
  448. it('calculates a square root (float)', function () {
  449. var stack = evaluate('{ 99 sqrt }');
  450. var expectedStack = [Math.sqrt(99)];
  451. expect(stack).toMatchArray(expectedStack);
  452. });
  453. it('subtracts (positive result)', function () {
  454. var stack = evaluate('{ 6 4 sub }');
  455. var expectedStack = [2];
  456. expect(stack).toMatchArray(expectedStack);
  457. });
  458. it('subtracts (negative result)', function () {
  459. var stack = evaluate('{ 4 6 sub }');
  460. var expectedStack = [-2];
  461. expect(stack).toMatchArray(expectedStack);
  462. });
  463. it('pushes true onto the stack', function () {
  464. var stack = evaluate('{ true }');
  465. var expectedStack = [true];
  466. expect(stack).toMatchArray(expectedStack);
  467. });
  468. it('truncates a number', function () {
  469. var stack = evaluate('{ 35.004 truncate }');
  470. var expectedStack = [35];
  471. expect(stack).toMatchArray(expectedStack);
  472. });
  473. it('calculates an exclusive or value', function () {
  474. var stack = evaluate('{ 3 9 xor }');
  475. var expectedStack = [10];
  476. expect(stack).toMatchArray(expectedStack);
  477. });
  478. });
  479. describe('PostScriptCompiler', function () {
  480. function check(code, domain, range, samples) {
  481. var compiler = new PostScriptCompiler();
  482. var compiledCode = compiler.compile(code, domain, range);
  483. if (samples === null) {
  484. expect(compiledCode).toBeNull();
  485. } else {
  486. expect(compiledCode).not.toBeNull();
  487. var fn = new Function('src', 'srcOffset', 'dest', 'destOffset', compiledCode);
  488. for (var i = 0; i < samples.length; i++) {
  489. var out = new Float32Array(samples[i].output.length);
  490. fn(samples[i].input, 0, out, 0);
  491. expect(Array.prototype.slice.call(out, 0)).toMatchArray(samples[i].output);
  492. }
  493. }
  494. }
  495. it('check compiled add', function () {
  496. check([
  497. 0.25,
  498. 0.5,
  499. 'add'
  500. ], [], [
  501. 0,
  502. 1
  503. ], [{
  504. input: [],
  505. output: [0.75]
  506. }]);
  507. check([
  508. 0,
  509. 'add'
  510. ], [
  511. 0,
  512. 1
  513. ], [
  514. 0,
  515. 1
  516. ], [{
  517. input: [0.25],
  518. output: [0.25]
  519. }]);
  520. check([
  521. 0.5,
  522. 'add'
  523. ], [
  524. 0,
  525. 1
  526. ], [
  527. 0,
  528. 1
  529. ], [{
  530. input: [0.25],
  531. output: [0.75]
  532. }]);
  533. check([
  534. 0,
  535. 'exch',
  536. 'add'
  537. ], [
  538. 0,
  539. 1
  540. ], [
  541. 0,
  542. 1
  543. ], [{
  544. input: [0.25],
  545. output: [0.25]
  546. }]);
  547. check([
  548. 0.5,
  549. 'exch',
  550. 'add'
  551. ], [
  552. 0,
  553. 1
  554. ], [
  555. 0,
  556. 1
  557. ], [{
  558. input: [0.25],
  559. output: [0.75]
  560. }]);
  561. check(['add'], [
  562. 0,
  563. 1,
  564. 0,
  565. 1
  566. ], [
  567. 0,
  568. 1
  569. ], [{
  570. input: [
  571. 0.25,
  572. 0.5
  573. ],
  574. output: [0.75]
  575. }]);
  576. check(['add'], [
  577. 0,
  578. 1
  579. ], [
  580. 0,
  581. 1
  582. ], null);
  583. });
  584. it('check compiled sub', function () {
  585. check([
  586. 0.5,
  587. 0.25,
  588. 'sub'
  589. ], [], [
  590. 0,
  591. 1
  592. ], [{
  593. input: [],
  594. output: [0.25]
  595. }]);
  596. check([
  597. 0,
  598. 'sub'
  599. ], [
  600. 0,
  601. 1
  602. ], [
  603. 0,
  604. 1
  605. ], [{
  606. input: [0.25],
  607. output: [0.25]
  608. }]);
  609. check([
  610. 0.5,
  611. 'sub'
  612. ], [
  613. 0,
  614. 1
  615. ], [
  616. 0,
  617. 1
  618. ], [{
  619. input: [0.75],
  620. output: [0.25]
  621. }]);
  622. check([
  623. 0,
  624. 'exch',
  625. 'sub'
  626. ], [
  627. 0,
  628. 1
  629. ], [
  630. -1,
  631. 1
  632. ], [{
  633. input: [0.25],
  634. output: [-0.25]
  635. }]);
  636. check([
  637. 0.75,
  638. 'exch',
  639. 'sub'
  640. ], [
  641. 0,
  642. 1
  643. ], [
  644. -1,
  645. 1
  646. ], [{
  647. input: [0.25],
  648. output: [0.5]
  649. }]);
  650. check(['sub'], [
  651. 0,
  652. 1,
  653. 0,
  654. 1
  655. ], [
  656. -1,
  657. 1
  658. ], [{
  659. input: [
  660. 0.25,
  661. 0.5
  662. ],
  663. output: [-0.25]
  664. }]);
  665. check(['sub'], [
  666. 0,
  667. 1
  668. ], [
  669. 0,
  670. 1
  671. ], null);
  672. check([
  673. 1,
  674. 'dup',
  675. 3,
  676. 2,
  677. 'roll',
  678. 'sub',
  679. 'sub'
  680. ], [
  681. 0,
  682. 1
  683. ], [
  684. 0,
  685. 1
  686. ], [{
  687. input: [0.75],
  688. output: [0.75]
  689. }]);
  690. });
  691. it('check compiled mul', function () {
  692. check([
  693. 0.25,
  694. 0.5,
  695. 'mul'
  696. ], [], [
  697. 0,
  698. 1
  699. ], [{
  700. input: [],
  701. output: [0.125]
  702. }]);
  703. check([
  704. 0,
  705. 'mul'
  706. ], [
  707. 0,
  708. 1
  709. ], [
  710. 0,
  711. 1
  712. ], [{
  713. input: [0.25],
  714. output: [0]
  715. }]);
  716. check([
  717. 0.5,
  718. 'mul'
  719. ], [
  720. 0,
  721. 1
  722. ], [
  723. 0,
  724. 1
  725. ], [{
  726. input: [0.25],
  727. output: [0.125]
  728. }]);
  729. check([
  730. 1,
  731. 'mul'
  732. ], [
  733. 0,
  734. 1
  735. ], [
  736. 0,
  737. 1
  738. ], [{
  739. input: [0.25],
  740. output: [0.25]
  741. }]);
  742. check([
  743. 0,
  744. 'exch',
  745. 'mul'
  746. ], [
  747. 0,
  748. 1
  749. ], [
  750. 0,
  751. 1
  752. ], [{
  753. input: [0.25],
  754. output: [0]
  755. }]);
  756. check([
  757. 0.5,
  758. 'exch',
  759. 'mul'
  760. ], [
  761. 0,
  762. 1
  763. ], [
  764. 0,
  765. 1
  766. ], [{
  767. input: [0.25],
  768. output: [0.125]
  769. }]);
  770. check([
  771. 1,
  772. 'exch',
  773. 'mul'
  774. ], [
  775. 0,
  776. 1
  777. ], [
  778. 0,
  779. 1
  780. ], [{
  781. input: [0.25],
  782. output: [0.25]
  783. }]);
  784. check(['mul'], [
  785. 0,
  786. 1,
  787. 0,
  788. 1
  789. ], [
  790. 0,
  791. 1
  792. ], [{
  793. input: [
  794. 0.25,
  795. 0.5
  796. ],
  797. output: [0.125]
  798. }]);
  799. check(['mul'], [
  800. 0,
  801. 1
  802. ], [
  803. 0,
  804. 1
  805. ], null);
  806. });
  807. it('check compiled max', function () {
  808. check([
  809. 'dup',
  810. 0.75,
  811. 'gt',
  812. 7,
  813. 'jz',
  814. 'pop',
  815. 0.75
  816. ], [
  817. 0,
  818. 1
  819. ], [
  820. 0,
  821. 1
  822. ], [{
  823. input: [0.5],
  824. output: [0.5]
  825. }]);
  826. check([
  827. 'dup',
  828. 0.75,
  829. 'gt',
  830. 7,
  831. 'jz',
  832. 'pop',
  833. 0.75
  834. ], [
  835. 0,
  836. 1
  837. ], [
  838. 0,
  839. 1
  840. ], [{
  841. input: [1],
  842. output: [0.75]
  843. }]);
  844. check([
  845. 'dup',
  846. 0.75,
  847. 'gt',
  848. 5,
  849. 'jz',
  850. 'pop',
  851. 0.75
  852. ], [
  853. 0,
  854. 1
  855. ], [
  856. 0,
  857. 1
  858. ], null);
  859. });
  860. it('check pop/roll/index', function () {
  861. check([
  862. 1,
  863. 'pop'
  864. ], [
  865. 0,
  866. 1
  867. ], [
  868. 0,
  869. 1
  870. ], [{
  871. input: [0.5],
  872. output: [0.5]
  873. }]);
  874. check([
  875. 1,
  876. 3,
  877. -1,
  878. 'roll'
  879. ], [
  880. 0,
  881. 1,
  882. 0,
  883. 1
  884. ], [
  885. 0,
  886. 1,
  887. 0,
  888. 1,
  889. 0,
  890. 1
  891. ], [{
  892. input: [
  893. 0.25,
  894. 0.5
  895. ],
  896. output: [
  897. 0.5,
  898. 1,
  899. 0.25
  900. ]
  901. }]);
  902. check([
  903. 1,
  904. 3,
  905. 1,
  906. 'roll'
  907. ], [
  908. 0,
  909. 1,
  910. 0,
  911. 1
  912. ], [
  913. 0,
  914. 1,
  915. 0,
  916. 1,
  917. 0,
  918. 1
  919. ], [{
  920. input: [
  921. 0.25,
  922. 0.5
  923. ],
  924. output: [
  925. 1,
  926. 0.25,
  927. 0.5
  928. ]
  929. }]);
  930. check([
  931. 1,
  932. 3,
  933. 1.5,
  934. 'roll'
  935. ], [
  936. 0,
  937. 1,
  938. 0,
  939. 1
  940. ], [
  941. 0,
  942. 1,
  943. 0,
  944. 1,
  945. 0,
  946. 1
  947. ], null);
  948. check([
  949. 1,
  950. 1,
  951. 'index'
  952. ], [
  953. 0,
  954. 1
  955. ], [
  956. 0,
  957. 1,
  958. 0,
  959. 1,
  960. 0,
  961. 1
  962. ], [{
  963. input: [0.5],
  964. output: [
  965. 0.5,
  966. 1,
  967. 0.5
  968. ]
  969. }]);
  970. check([
  971. 1,
  972. 3,
  973. 'index',
  974. 'pop'
  975. ], [
  976. 0,
  977. 1
  978. ], [
  979. 0,
  980. 1
  981. ], null);
  982. check([
  983. 1,
  984. 0.5,
  985. 'index',
  986. 'pop'
  987. ], [
  988. 0,
  989. 1
  990. ], [
  991. 0,
  992. 1
  993. ], null);
  994. });
  995. it('check input boundaries', function () {
  996. check([], [
  997. 0,
  998. 0.5
  999. ], [
  1000. 0,
  1001. 1
  1002. ], [{
  1003. input: [1],
  1004. output: [0.5]
  1005. }]);
  1006. check([], [
  1007. 0.5,
  1008. 1
  1009. ], [
  1010. 0,
  1011. 1
  1012. ], [{
  1013. input: [0],
  1014. output: [0.5]
  1015. }]);
  1016. check(['dup'], [
  1017. 0.5,
  1018. 0.75
  1019. ], [
  1020. 0,
  1021. 1,
  1022. 0,
  1023. 1
  1024. ], [{
  1025. input: [0],
  1026. output: [
  1027. 0.5,
  1028. 0.5
  1029. ]
  1030. }]);
  1031. check([], [
  1032. 100,
  1033. 1001
  1034. ], [
  1035. 0,
  1036. 10000
  1037. ], [{
  1038. input: [1000],
  1039. output: [1000]
  1040. }]);
  1041. });
  1042. it('check output boundaries', function () {
  1043. check([], [
  1044. 0,
  1045. 1
  1046. ], [
  1047. 0,
  1048. 0.5
  1049. ], [{
  1050. input: [1],
  1051. output: [0.5]
  1052. }]);
  1053. check([], [
  1054. 0,
  1055. 1
  1056. ], [
  1057. 0.5,
  1058. 1
  1059. ], [{
  1060. input: [0],
  1061. output: [0.5]
  1062. }]);
  1063. check(['dup'], [
  1064. 0,
  1065. 1
  1066. ], [
  1067. 0.5,
  1068. 1,
  1069. 0.75,
  1070. 1
  1071. ], [{
  1072. input: [0],
  1073. output: [
  1074. 0.5,
  1075. 0.75
  1076. ]
  1077. }]);
  1078. check([], [
  1079. 0,
  1080. 10000
  1081. ], [
  1082. 100,
  1083. 1001
  1084. ], [{
  1085. input: [1000],
  1086. output: [1000]
  1087. }]);
  1088. });
  1089. it('compile optimized', function () {
  1090. var compiler = new PostScriptCompiler();
  1091. var code = [
  1092. 0,
  1093. 'add',
  1094. 1,
  1095. 1,
  1096. 3,
  1097. -1,
  1098. 'roll',
  1099. 'sub',
  1100. 'sub',
  1101. 1,
  1102. 'mul'
  1103. ];
  1104. var compiledCode = compiler.compile(code, [
  1105. 0,
  1106. 1
  1107. ], [
  1108. 0,
  1109. 1
  1110. ]);
  1111. expect(compiledCode).toEqual('dest[destOffset + 0] = Math.max(0, Math.min(1, src[srcOffset + 0]));');
  1112. });
  1113. });
  1114. });