Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: test/tests/typedObjects.js

Issue 29323484: Issue 507 - Implement lightweight array.slice() method (Closed)
Patch Set: Small improvements Created Aug. 19, 2015, 12:14 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/typedObjects/objectTypes.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 (function() 18 (function()
19 { 19 {
20 "use strict";
20 module("Typed objects"); 21 module("Typed objects");
21 22
22 test("Math utilities", function() 23 test("Math utilities", function()
23 { 24 {
24 let {nextPow2, ilog2} = require("typedObjects/utils"); 25 let {nextPow2, ilog2} = require("typedObjects/utils");
25 26
26 equal(nextPow2(0), 0, "nextPow2(0)"); 27 equal(nextPow2(0), 0, "nextPow2(0)");
27 equal(nextPow2(1), 1, "nextPow2(1)"); 28 equal(nextPow2(1), 1, "nextPow2(1)");
28 equal(nextPow2(2), 2, "nextPow2(2)"); 29 equal(nextPow2(2), 2, "nextPow2(2)");
29 equal(nextPow2(3), 4, "nextPow2(3)"); 30 equal(nextPow2(3), 4, "nextPow2(3)");
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 deepEqual(array.toJS(), [5, 2, 8], "Pushing three elements succeeded"); 705 deepEqual(array.toJS(), [5, 2, 8], "Pushing three elements succeeded");
705 706
706 equal(array.pop(), 8, "Popping returns element"); 707 equal(array.pop(), 8, "Popping returns element");
707 equal(array.pop(), 2, "Popping returns element"); 708 equal(array.pop(), 2, "Popping returns element");
708 deepEqual(array.toJS(), [5], "Popping two elements succeeded"); 709 deepEqual(array.toJS(), [5], "Popping two elements succeeded");
709 710
710 equal(array.unshift(4), 2, "Unshifting returns new length"); 711 equal(array.unshift(4), 2, "Unshifting returns new length");
711 equal(array.unshift(0, 9), 4, "Unshifting returns new length"); 712 equal(array.unshift(0, 9), 4, "Unshifting returns new length");
712 deepEqual(array.toJS(), [0, 9, 4, 5], "Unshifting two elements succeeded"); 713 deepEqual(array.toJS(), [0, 9, 4, 5], "Unshifting two elements succeeded");
713 714
715 let sliced = array.slice(1, 3);
716 deepEqual(sliced.toJS(), [9, 4], "Slicing an array works");
717 sliced.release();
718 deepEqual(sliced.toJS(), [], "Releasing a sliced array works");
719 deepEqual(array.toJS(), [0, 9, 4, 5], "Releasing a sliced array does not rel ease the parent");
720 array.release();
721 deepEqual(array.toJS(), [], "Releasing a previously sliced array works");
722
723 array = uint32Array();
724 array.push(0, 9, 4, 5);
725 sliced = array.slice();
726 array.release();
727 deepEqual(sliced.toJS(), [0, 9, 4, 5], "Releasing a parent does not release a sliced array");
728 sliced.release();
729 deepEqual(sliced.toJS(), [], "Releasing a parent and sliced array does relea se a sliced array");
730
731 array = uint32Array();
732 array.push(0, 9, 4, 5);
733 sliced = array.slice(3);
734 deepEqual(sliced.toJS(), [5], "Array slicing adjusts byteOffset correctly");
735 deepEqual(array.toJS(), [0, 9, 4, 5], "Slicing an array does not alter the o riginal array");
736 sliced.release();
737
738 sliced = array.slice(0);
739 deepEqual(sliced.toJS(), [0, 9, 4, 5], "Slicing can be used as 'copy'");
740 sliced.release();
741
742 sliced = array.slice(-2);
743 deepEqual(sliced.toJS(), [4, 5], "Slicing supports negative start index");
744 sliced.release();
745
746 sliced = array.slice(-10);
747 deepEqual(sliced.toJS(), [0, 9, 4, 5], "Slicing supports excessive negative start index");
748 sliced.release();
749
750 sliced = array.slice(-3, -1);
751 deepEqual(sliced.toJS(), [9, 4], "Slicing supports negative start and end in dex");
752 sliced.release();
753
754 sliced = array.slice(-10, -100);
755 deepEqual(sliced.toJS(), [], "Slicing supports excessive negative start and end index");
756 sliced.release();
757
758 sliced = array.slice(1, -1);
759 deepEqual(sliced.toJS(), [9, 4], "Slicing supports negative end index");
760 sliced.release();
761
762 sliced = array.slice(1, 3);
763 deepEqual(sliced.toJS(), [9, 4], "Slicing supports positive end index");
764 array.set(1, 8);
765 deepEqual(sliced.toJS(), [8, 4], "Sliced arrays rely on shared data");
766 sliced.release();
767
768 sliced = array.slice(1, 300);
769 deepEqual(sliced.toJS(), [8, 4, 5], "Slicing supports excessive positive end index");
770 sliced.release();
771
772 sliced = array.slice(100, 300);
773 deepEqual(sliced.toJS(), [], "Slicing supports excessive positive start and end index");
774 sliced.release();
775
776 sliced = array.slice();
777 throws(() => array.length = 1, "Length of sliced array is read only");
778 throws(() => sliced.length = 1, "Length of sliced array result is read only" );
779
780 equal(array._refCount, 2, "Slicing an array increases it's refcount");
781 equal(sliced._refCount, 1, "Sliced arrays have a seperate refcount");
782 sliced.release();
783 equal(array._refCount, 1, "Releasing a sliced array decreases parent's refco unt");
784
785 array = uint32Array();
786 array.push(0, 9, 4, 5, 6, 7);
787 array.length = 4;
788 equal(array.length, 4, "Length of non-sliced array is writable");
789
714 equal(array.shift(), 0, "Shifting returns element"); 790 equal(array.shift(), 0, "Shifting returns element");
715 equal(array.shift(), 9, "Shifting returns element"); 791 equal(array.shift(), 9, "Shifting returns element");
716 deepEqual(array.toJS(), [4, 5], "Shifting by two elements succeeded"); 792 deepEqual(array.toJS(), [4, 5], "Shifting by two elements succeeded");
717 793
718 array.splice(1, 0, 1, 7); 794 array.splice(1, 0, 1, 7);
719 deepEqual(array.toJS(), [4, 1, 7, 5], "Using splice to insert elements succe eded"); 795 deepEqual(array.toJS(), [4, 1, 7, 5], "Using splice to insert elements succe eded");
720 array.splice(2, 1); 796 array.splice(2, 1);
721 deepEqual(array.toJS(), [4, 1, 5], "Using splice to remove an element succee ded"); 797 deepEqual(array.toJS(), [4, 1, 5], "Using splice to remove an element succee ded");
722 array.splice(0, 2, 9); 798 array.splice(0, 2, 9);
723 deepEqual(array.toJS(), [9, 5], "Using splice to remove two elements and ins ert one succeeded"); 799 deepEqual(array.toJS(), [9, 5], "Using splice to remove two elements and ins ert one succeeded");
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 s11.release(); 878 s11.release();
803 879
804 let s12 = string(s8, 4); 880 let s12 = string(s8, 4);
805 ok(s12, "String created as typed substring without length parameter"); 881 ok(s12, "String created as typed substring without length parameter");
806 equal(s12.length, 10, "String length set correctly"); 882 equal(s12.length, 10, "String length set correctly");
807 equal(s12.toString(), "longstring", "JavaScript representation is correct"); 883 equal(s12.toString(), "longstring", "JavaScript representation is correct");
808 s12.release(); 884 s12.release();
809 s8.release(); 885 s8.release();
810 }); 886 });
811 })(); 887 })();
OLDNEW
« no previous file with comments | « lib/typedObjects/objectTypes.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld