suite.js 36.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
/*
 Copyright 2013 Daniel Wirtz <dcode@dcode.io>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

var ByteBuffer = require("../dist/bytebuffer-node.js");
var ByteBufferNode = ByteBuffer;
var ByteBufferBrowser = require("../dist/bytebuffer.min.js");
var ByteBufferBrowser_DataView = require("../dist/bytebuffer-dataview.min.js");
var pkg = require("../package.json");

/**
 * Constructs a new Sandbox for module loaders and shim testing.
 * @param {Object.<string,*>} properties Additional properties to set
 * @constructor
 */
var Sandbox = function(properties) {
    this.Int8Array = function() {};
    this.Uint8Array = function() {};
    this.Int16Array = function() {};
    this.Uint16Array = function() {};
    this.Int32Array = function() {};
    this.Uint32Array = function() {};
    this.Float32Array = function() {};
    this.Float64Array = function() {};
    this.DataView = function() {};
    for (var i in properties) {
        this[i] = properties[i];
    }
    this.console = {
        log: function(s) {
            console.log(s);
        }
    };
};

function makeSuite(ByteBuffer) {
    var type = ByteBuffer.type(), // Buffer or ArrayBuffer
        accessor = ByteBuffer.accessor(),
        Long = ByteBuffer.Long;
    var suite = {};
    
    suite.init = function(test) {
        test.ok(require("../index.js"));
        if (ByteBuffer == ByteBufferNode)
            test.log("\n\n                           --- ByteBufferNB ---\n".bold.white),
            test.log("[optional] node-memcpy is "+(ByteBuffer.memcpy ? "present" : "not present"));
        else if (ByteBuffer == ByteBufferBrowser)
            test.log("\n\n                           --- ByteBufferAB ---\n".bold.white);
        else if (ByteBuffer == ByteBufferBrowser_DataView)
            test.log("\n\n                      --- ByteBufferAB_DataView ---\n".bold.white);
        test.ok(type === Buffer || type === ArrayBuffer);
        test.ok(typeof ByteBuffer == "function");
        test.strictEqual(pkg.version, ByteBuffer.VERSION);
        test.done();
    };
    
    suite.base = {};
    
    suite.base.allocate = function(test) {
        var bb = new ByteBuffer();
        test.ok(bb.buffer instanceof type);
        test.equal(bb.offset, 0);
        test.equal(bb.limit, ByteBuffer.DEFAULT_CAPACITY);
        test.equal(bb.littleEndian, ByteBuffer.DEFAULT_ENDIAN);
        test.equal(bb.noAssert, ByteBuffer.DEFAULT_NOASSERT);
        if (type === Buffer)
            test.equal(bb.buffer.length, bb.capacity());
        else
            test.equal(bb.buffer.byteLength, bb.capacity());
        test.equal(bb.capacity(), ByteBuffer.DEFAULT_CAPACITY);
        bb = ByteBuffer.allocate(undefined, !ByteBuffer.DEFAULT_ENDIAN, !ByteBuffer.DEFAULT_NOASSERT);
        test.equal(bb.capacity(), ByteBuffer.DEFAULT_CAPACITY);
        test.equal(bb.littleEndian, !ByteBuffer.DEFAULT_ENDIAN);
        test.equal(bb.noAssert, !ByteBuffer.DEFAULT_NOASSERT);

        // __isByteBuffer__
        test.strictEqual(bb.__isByteBuffer__, true);
        bb.__isByteBuffer__ = false;
        test.strictEqual(bb.__isByteBuffer__, true);
        test.equal(ByteBuffer.isByteBuffer(bb), true);

        // Fixed set of properties
        for (var i in bb)
            if (bb.hasOwnProperty(i) && ["offset", "markedOffset", "limit", "littleEndian", "noAssert", "buffer", "view"].indexOf(i) < 0)
                test.fail("Illegal enumerable property: "+i);

        test.done();
    };

    suite.base.clone = function(test) {
        var bb = new ByteBuffer(1, true, false);
        var bb2 = bb.clone();
        test.strictEqual(bb.buffer, bb2.buffer);
        test.equal(bb.offset, bb2.offset);
        test.equal(bb.limit, bb2.limit);
        test.equal(bb.markedOffset, bb2.markedOffset);
        test.equal(bb.littleEndian, bb2.littleEndian);
        test.equal(bb.noAssert, bb2.noAssert);
        test.notStrictEqual(bb, bb2);
        test.done();
    };
    
    suite.base.assert = function(test) {
        var bb = new ByteBuffer();
        test.strictEqual(bb.noAssert, false);
        test.strictEqual(bb.assert(false), bb);
        test.strictEqual(bb.noAssert, true);
        test.strictEqual(bb.assert(true), bb);
        test.strictEqual(bb.noAssert, false);
        test.done();
    };
    
    suite.wrap = {};
    
    if (type === Buffer) {
        suite.wrap.Buffer = function(test) {
            var buf = new Buffer(1);
            buf[0] = 0x01;
            var bb = ByteBuffer.wrap(buf);
            test.strictEqual(bb.capacity(), 1);
            test.strictEqual(bb.buffer, buf);
            test.strictEqual(bb.toDebug(), "<01>");
            test.done();
        };
    }
    
    suite.wrap.ArrayBuffer = function(test) {
        var buf = new ArrayBuffer(1);
        var bb = ByteBuffer.wrap(buf);
        test.strictEqual(bb.capacity(), 1);
        if (type === ArrayBuffer)
            test.strictEqual(bb.buffer, buf);
        else
            test.ok(bb.buffer instanceof Buffer);
        test.equal(bb.offset, 0);
        test.equal(bb.limit, 1);
        test.done();
    };
    
    suite.wrap.Uint8Array = function(test) {
        // Full view
        var buf = new Uint8Array(1);
        buf[0] = 0x01;
        var bb = ByteBuffer.wrap(buf);
        test.strictEqual(bb.capacity(), 1);
        if (type === ArrayBuffer)
            test.strictEqual(bb.buffer, buf.buffer);
        else
            test.ok(bb.buffer instanceof Buffer);
        test.strictEqual(bb.toDebug(), "<01>");

        // Partial view (not on node, node copies)
        if (type === ArrayBuffer) {
            buf = new Uint8Array(3);
            buf[0] = 0x01; buf[1] = 0x02; buf[2] = 0x03;
            buf = new Uint8Array(buf.buffer, 1, 1);
            bb = ByteBuffer.wrap(buf);
            test.strictEqual(bb.capacity(), 3);
            test.strictEqual(bb.toDebug(), "01<02>03");
        }
        
        test.done();
    };
    
    suite.wrap.Array = function(test) {
        var arr = [1,255,-1];
        var bb = ByteBuffer.wrap(arr);
        test.strictEqual(bb.capacity(), 3);
        test.strictEqual(bb.toDebug(), "<01 FF FF>");
        test.done();
    };

    suite.wrap.ByteBuffer = function(test) {
        var bb2 = ByteBuffer.wrap("\x12\x34\x56\x78", "binary");
        bb2.offset = 1;
        var bb = ByteBuffer.wrap(bb2);
        test.strictEqual(bb2.offset, bb.offset);
        test.strictEqual(bb2.limit, bb.limit);
        test.strictEqual(bb2.capacity(), bb.capacity());
        test.strictEqual(bb2.toString("debug"), bb.toString("debug"));
        test.done();
    };
    
    suite.wrap.string = function(test) {
        var bb = ByteBuffer.wrap("\u0061\u0062");
        test.equal(bb.toDebug(), "<61 62>");
        test.done();
    };
       
    suite.encodings = {};
    
    suite.encodings.UTF8 = function(test) {
        ["aäöü߀b", ""].forEach(function(str) {
            var bb = ByteBuffer.wrap(str, "utf8"); // Calls ByteBuffer#fromUTF8
            test.strictEqual(bb.toUTF8(), str);
            if (str.length > 2) {
                bb.offset = 1;
                bb.limit = bb.capacity()-1;
                test.strictEqual(bb.toUTF8(), str.substring(1, str.length-1));
            }
        });
        test.done();
    };

    suite.encodings.debug = function(test) {
        ["60<61 62]63", "<60 61 62 63]", "|", "|61", "<61>", "!12"].forEach(function(str) {
            var bb = ByteBuffer.wrap(str, "debug"); // Calls ByteBuffer#fromDebug
            test.equal(bb.toDebug(), str);
        });
        test.done();
    };
    
    suite.encodings.binary = function(test) {
        ["\x61\x62\x63\x64", "", "  "].forEach(function(str) {
            var bb = ByteBuffer.wrap(str, "binary"); // Calls ByteBuffer#fromBinary
            test.strictEqual(bb.toBinary(), str);
            if (str.length > 2) {
                bb.offset = 1;
                bb.limit = bb.capacity()-1;
                test.strictEqual(bb.toBinary(), str.substring(1, str.length-1));
            }
        });
        test.done();
    };
    
    suite.encodings.hex = function(test) {
        ["61626364", "61", ""].forEach(function(str) {
            var bb = ByteBuffer.wrap(str, "hex"); // Calls ByteBuffer#fromHex
            test.strictEqual(bb.toHex(), str);
            if (str.length > 2) {
                bb.offset = 1;
                bb.limit = bb.capacity()-1;
                test.strictEqual(bb.toHex(), str.substring(2, str.length-2));
            }
        });
        test.done();
    };
    
    suite.encodings.base64 = function(test) {
        ["", "YWI=", "YWJjZGVmZw==", "YWJjZGVmZ2g=", "YWJjZGVmZ2hp"].forEach(function(str) {
            var bb = ByteBuffer.wrap(str, "base64"); // Calls ByteBuffer#fromBase64
            test.strictEqual(bb.toBase64(), str);
            if (str.length > 8) {
                bb.offset = 3;
                bb.limit = bb.offset + 3;
                test.strictEqual(bb.toBase64(), str.substr(4, 4));
            }
        });
        test.done();
    };
    
    suite.methods = {};
    
    suite.methods.concat = function(test) {
        var bbs = [
            new ArrayBuffer(1),
            ByteBuffer.fromDebug('00<01 02>'),
            ByteBuffer.fromDebug('00 01 02<03>00'),
            ByteBuffer.fromDebug('00|'),
            ByteBuffer.fromDebug('<04>'),
            type === Buffer ? new Buffer(0) : new ArrayBuffer(0),
            new Uint8Array(0),
            '05'
        ];
        var bb = ByteBuffer.concat(bbs, 'hex', !ByteBuffer.DEFAULT_ENDIAN, !ByteBuffer.DEFAULT_NOASSERT);
        test.strictEqual(bb.littleEndian, !ByteBuffer.DEFAULT_ENDIAN);
        test.strictEqual(bb.noAssert, !ByteBuffer.DEFAULT_NOASSERT);
        test.equal(bb.toDebug(), '<00 01 02 03 04 05>');
        bb = ByteBuffer.concat([]);
        test.strictEqual(bb.buffer, new ByteBuffer(0).buffer); // EMPTY_BUFFER
        test.done();
    };
    
    suite.methods.resize = function(test) {
        var bb = new ByteBuffer(1);
        bb.offset = 1;
        bb.resize(2);
        bb.fill(0, 0, 2);
        test.equal(bb.capacity(), 2);
        test.equal(bb.toDebug(), "00|00");
        test.done();
    };

    suite.methods.ensureCapacity = function(test) {
        var bb = new ByteBuffer(5);
        test.equal(bb.capacity(), 5);
        bb.ensureCapacity(6); // Doubles
        test.equal(bb.capacity(), 10);
        bb.ensureCapacity(21); // Uses 21
        test.equal(bb.capacity(), 21);
        test.done();
    };
    
    suite.methods.slice = function(test) {
        var bb = new ByteBuffer.wrap("\x12\x34\x56"),
            bb2 = bb.slice(1,2);
        test.strictEqual(bb.buffer, bb2.buffer);
        test.equal(bb.offset, 0);
        test.equal(bb.limit, 3);
        test.equal(bb2.offset, 1);
        test.equal(bb2.limit, 2);
        test.done();
    };
    
    suite.methods.flip = function(test) {
        var bb = ByteBuffer.wrap('\x12\x34\x56\x78');
        bb.offset = 4;
        test.equal(bb.offset, 4);
        test.equal(bb.limit, 4);
        bb.flip();
        test.equal(bb.offset, 0);
        test.equal(bb.limit, 4);
        test.done();
    };
    
    suite.methods.mark = function(test) {
        var bb = ByteBuffer.wrap('\x12\x34\x56\x78');
        test.equal(bb.offset, 0);
        test.equal(bb.limit, 4);
        test.equal(bb.markedOffset, -1);
        bb.mark();
        test.equal(bb.markedOffset, 0);
        test.done();
    };
    
    suite.methods.reset = function(test) {
        var bb = ByteBuffer.wrap('\x12\x34\x56\x78');
        bb.reset();
        test.equal(bb.offset, 0);
        test.equal(bb.limit, 4);
        bb.offset = 1;
        bb.mark();
        test.equal(bb.markedOffset, 1);
        bb.reset();
        test.equal(bb.offset, 1);
        test.equal(bb.markedOffset, -1);
        test.done();
    };
    
    suite.methods.copy = function(test) {
        var bb = ByteBuffer.wrap("\x01", !ByteBuffer.DEFAULT_ENDIAN),
            bb2 = bb.copy();
        test.equal(bb.offset, 0);
        test.notStrictEqual(bb, bb2);
        test.notStrictEqual(bb.buffer, bb2.buffer);
        test.equal(bb2.offset, bb.offset);
        test.equal(bb2.limit, bb.limit);
        test.equal(bb2.markedOffset, bb.markedOffset);
        test.equal(bb2.littleEndian, bb.littleEndian);
        test.equal(bb2.noAssert, bb.noAssert);
        test.done();
    };
    
    suite.methods.copyTo = function(test) {
        var bb = ByteBuffer.wrap("\x01"),
            bb2 = new ByteBuffer(2).fill(0).flip();
        test.equal(bb.toDebug(), "<01>");
        // Modifies source and target offsets
        bb.copyTo(bb2 /* all offsets omitted */);
        test.equal(bb.toDebug(), "01|"); // Read 1 byte
        test.equal(bb2.toDebug(), "01<00>"); // Written 1 byte
        bb.reset();
        test.equal(bb.toDebug(), "<01>");
        // Again, but with bb2.offset=1
        bb.copyTo(bb2 /* all offsets omitted */);
        test.equal(bb.toDebug(), "01|"); // Read 1 byte
        test.equal(bb2.toDebug(), "01 01|"); // Written 1 byte at 2
        bb.reset();
        bb2.clear().fill(0).flip();
        // Modifies source offsets only
        bb.copyTo(bb2, 0 /* source offsets omitted */);
        test.equal(bb.toDebug(), "01|"); // Read 1 byte
        test.equal(bb2.toDebug(), "<01 00>"); // Written 1 byte (no change)
        // Modifies no offsets at all
        bb.reset();
        bb2.fill(0).flip();
        bb.copyTo(bb2, 1, 0, bb.capacity() /* no offsets omitted */);
        test.equal(bb.toDebug(), "<01>"); // Read 1 byte (no change)
        test.equal(bb2.toDebug(), "<00 01>"); // Written 1 byte (no change)
        test.done();
    };
    
    suite.methods.compact = function(test) {
        var bb = ByteBuffer.wrap("\x01\x02");
        bb.limit = 1;
        bb.markedOffset = 2;
        var prevBuffer = bb.buffer,
            prevView = bb.view;
        bb.compact();
        test.notStrictEqual(bb.buffer, prevBuffer);
        if (type === ArrayBuffer) {
            test.notStrictEqual(bb.buffer, prevView);
        }
        test.equal(bb.capacity(), 1);
        test.equal(bb.offset, 0);
        test.equal(bb.limit, 1);
        test.equal(bb.markedOffset, 2); // Actually out of bounds
        
        // Empty region
        bb.offset = 1;
        prevBuffer = bb.buffer;
        bb.compact();
        test.notStrictEqual(bb.buffer, prevBuffer);
        test.strictEqual(bb.buffer, new ByteBuffer(0).buffer); // EMPTY_BUFFER
        if (type === ArrayBuffer) {
            test.strictEqual(bb.view, null);
        }
        test.equal(bb.capacity(), 0);
        test.equal(bb.offset, 0);
        test.equal(bb.limit, 0);        
        test.done();
    };
    
    suite.methods.reverse = function(test) {
        var bb = ByteBuffer.wrap("\x12\x34\x56\x78");
        bb.reverse(1, 3);
        test.equal(bb.toString("debug"), "<12 56 34 78>");
        bb.reverse();
        test.equal(bb.toString("debug"), "<78 34 56 12>");
        bb.offset = 1;
        bb.limit = 3;
        bb.reverse();
        test.equal(bb.toString("debug"), "78<56 34>12");
        bb.reverse(0, 4).clear();
        test.equal(bb.toString("debug"), "<12 34 56 78>");
        test.done();
    };
    
    suite.methods.append = function(test) {
        var bb = ByteBuffer.wrap("\x12\x34");
        var bb2 = ByteBuffer.wrap("\x56\x78");
        bb.offset = 2;
        bb.append(bb2); // Modifies offsets of both
        test.equal(bb.toString("debug"), "12 34>56 78<");
        test.equal(bb2.toString("debug"), "56 78|");
        bb2.reset();
        bb.append(bb2, 1); // Modifies offsets of bb2 only
        test.equal(bb.toString("debug"), "12 56>78 78<");
        test.equal(bb2.toString("debug"), "56 78|");
        test.done();
    };
    
    suite.methods.prepend = function(test) {
        var bb = ByteBuffer.wrap("\x12\x34"),
            bb2 = ByteBuffer.wrap("\x56\x78");
        test.strictEqual(bb.prepend(bb2), bb); // Relative prepend at 0, 2 bytes (2 overflow)
        test.equal(bb.toDebug(), "<56 78 12 34>");
        test.equal(bb2.toDebug(), "56 78|");
        bb.offset = 4;
        bb2.offset = 1;
        bb.prepend(bb2, 3); // Absolute prepend at 3, 1 byte
        test.equal(bb.toDebug(), "56 78 78 34|");
        test.equal(bb2.toDebug(), "56 78|");
        bb2.offset = 0;
        bb.prepend(bb2); // Relative prepend at 4, 2 bytes
        test.equal(bb.toDebug(), "56 78<56 78>");
        test.equal(bb2.toDebug(), "56 78|");
        bb.offset = 3;
        bb2.offset = 0;
        test.throws(function() {
            bb.prepend(bb2, 6); // Absolute out of bounds
        }, RangeError);
        bb.prepend("abcde", "utf8"); // Relative prepend at 3, 5 bytes (1 overflow)
        test.equal(bb.toDebug(), "<61 62 63 64 65 78>");
        test.done();
    };
    
    suite.methods.prependTo = function(test) {
        var bb = ByteBuffer.wrap("\x12\x34"),
            bb2 = ByteBuffer.wrap("\x56\x78");
        test.strictEqual(bb2.prependTo(bb), bb2);
        test.equal(bb.toDebug(), "<56 78 12 34>");
        test.equal(bb2.toDebug(), "56 78|");
        test.done();
    };
    
    suite.methods.remaining = function(test) {
        var bb = ByteBuffer.wrap("\x12\x34");
        test.strictEqual(bb.remaining(), 2);
        bb.offset = 2;
        test.strictEqual(bb.remaining(), 0);
        bb.offset = 3;
        test.strictEqual(bb.remaining(), -1);
        test.done();
    };
    
    suite.methods.skip = function(test) {
        var bb = ByteBuffer.wrap("\x12\x34\x56");
        test.strictEqual(bb.offset, 0);
        bb.skip(3);
        test.strictEqual(bb.offset, 3);
        test.strictEqual(bb.noAssert, false);
        test.throws(function() {
            bb.skip(1);
        });
        test.strictEqual(bb.offset, 3);
        bb.noAssert = true;
        test.doesNotThrow(function() {
            bb.skip(1);
        });
        test.strictEqual(bb.offset, 4);
        test.done();
    };
    
    suite.methods.order = function(test) {
        test.strictEqual(ByteBuffer.LITTLE_ENDIAN, true);
        test.strictEqual(ByteBuffer.BIG_ENDIAN, false);
        var bb = new ByteBuffer(2);
        test.strictEqual(bb.littleEndian, false);
        bb.writeInt32(0x12345678);
        bb.flip();
        test.strictEqual(bb.toHex(), "12345678");
        bb.clear();
        test.strictEqual(bb.LE(), bb);
        test.strictEqual(bb.littleEndian, true);
        bb.writeInt32(0x12345678);
        bb.flip();
        test.strictEqual(bb.toHex(), "78563412");
        test.strictEqual(bb.BE(), bb);
        test.strictEqual(bb.littleEndian, false);
        test.strictEqual(bb.order(ByteBuffer.LITTLE_ENDIAN), bb);
        test.strictEqual(bb.littleEndian, true);
        test.strictEqual(bb.order(ByteBuffer.BIG_ENDIAN), bb);
        test.strictEqual(bb.littleEndian, false);
        test.done();
    };
    
    var types = [
        // name          | alias   | size | input                                   | output                                  | BE representation
        ["Int8"          , "Byte"  , 1    , 0xFE                                    , -2                                      , "fe"                  ],
        ["Uint8"         , null    , 1    , -2                                      , 0xFE                                    , "fe"                  ],
        ["Int16"         , "Short" , 2    , 0xFFFE                                  , -2                                      , "fffe"                ],
        ["Uint16"        , null    , 2    , -2                                      , 0xFFFE                                  , "fffe"                ],
        ["Int32"         , "Int"   , 4    , 0xFFFFFFFE                              , -2                                      , "fffffffe"            ],
        ["Uint32"        , null    , 4    , -2                                      , 0xFFFFFFFE                              , "fffffffe"            ],
        ["Float32"       , "Float" , 4    , 0.5                                     , 0.5                                     , "3f000000"            ],
        ["Float64"       , "Double", 8    , 0.1                                     , 0.1                                     , "3fb999999999999a"    ],
        ["Int64"         , "Long"  , 8    , new Long(0xFFFFFFFE, 0xFFFFFFFF, true)  , new Long(0xFFFFFFFE, 0xFFFFFFFF, false) , "fffffffffffffffe"    ],
        ["Uint64"        , null    , 8    , new Long(0xFFFFFFFE, 0xFFFFFFFF, false) , new Long(0xFFFFFFFE, 0xFFFFFFFF, true)  , "fffffffffffffffe"    ],
        
        // name          | alias   | size | input                                   | output                                  | representation
        ["Varint32"      , null    , 5    , 0xFFFFFFFE                              , -2                                      , "feffffff0f"          ],
        ["Varint32ZigZag", null    , 1    , -1                                      , -1                                      , "01"                  ],
        ["Varint64"      , null    , 10   , new Long(0xFFFFFFFE, 0xFFFFFFFF, true)  , new Long(0xFFFFFFFE, 0xFFFFFFFF, false) , "feffffffffffffffff01"],
        ["Varint64ZigZag", null    , 1    , Long.fromNumber(-1)                     , Long.fromNumber(-1)                     , "01"                  ]
    ];
    
    suite.types = {};
    
    types.forEach(function(type) {
        var name = type[0],
            varint = name.indexOf("Varint") >= 0,
            alias = type[1],
            size = type[2],
            input = type[3],
            output = type[4],
            be = type[5],
            le = "";
        for (var i=be.length; i>0; i-=2) {
            le += be.substr(i-2, 2);
        }
        suite.types[name.toLowerCase()] = function(test) {
            var bb = new ByteBuffer(size);
            // Relative BE (always LE for varints)
            test.strictEqual(bb["write"+name](input), bb);
            bb.flip();
            var val = bb["read"+name]();
            if (output instanceof Long) {
                test.deepEqual(val, output);
            } else {
                test.strictEqual(val, output);
            }
            bb.flip();
            test.strictEqual(bb.toHex(), be);
            if (!varint) {
                // Relative LE
                bb.LE();
                bb["write"+name](input);
                bb.flip();
                val = bb["read"+name]();
                if (output instanceof Long) {
                    test.deepEqual(val, output);
                } else {
                    test.strictEqual(val, output);
                }
                bb.flip();
                test.strictEqual(bb.toHex(), le);
            }
            test.throws(function() { // OOB
                bb.offset = bb.capacity() - size + 1;
                bb["read"+name](input);
            });
            test.doesNotThrow(function() { // OOB, automatic resizing * 2
                bb["write"+name](input);
            });
            test.strictEqual(bb.capacity(), size * 2);
            // Absolute
            bb.clear();
            if (!varint)
                test.strictEqual(bb["write"+name](input, 1), bb);
            else
                test.strictEqual(bb["write"+name](input, 1), size);
            val = bb["read"+name](1);
            if (output instanceof Long) {
                if (!varint)
                    test.deepEqual(val, output);
                else
                    test.deepEqual(val, {value: output, length: size});
            } else {
                if (!varint)
                    test.strictEqual(val, output);
                else
                    test.deepEqual(val, {value: output, length: size});
            }
            // Alias
            if (alias) {
                test.strictEqual(bb["write"+name], bb["write"+alias]);
                test.strictEqual(bb["read"+name], bb["read"+alias]);
            }
            test.done();
        };
    });
    
    suite.types.bitset = function(test) {
      var bb = new ByteBuffer(2),
          arr;

      function run(data) {
        bb.reset();
        bb.writeBitSet(data);
        bb.reset();
        test.deepEqual(bb.readBitSet(),data);
      };

      run([]);
      run([true]);
      run([false]);
      run([false,true]);
      run([false,false,false,false,false,false,false,false]);
      run([true,false,true,false,true,false,true,false]);
      run([true,true,true,true,true,true,true,true]);
      run([true,false,true,false,true,false,true,false]);
      run([true,false,true,false,true,false,true,false,true]);
      
      bb.reset();
      bb.writeBitSet([,null,"",0,42,"hello world",new Date(0),{},[]]);
      bb.reset();
      test.deepEqual(bb.readBitSet(),[false,false,false,false,true,true,true,true,true]);

      test.done();
    };

    suite.types.calculateVarint = function(test) {
        test.equal(ByteBuffer.MAX_VARINT32_BYTES, 5);
        test.equal(ByteBuffer.MAX_VARINT64_BYTES, 10);
        var values = [
            [0, 1],
            [-1, 5, 10],
            [1<<7, 2],
            [1<<14, 3],
            [1<<21, 4],
            [1<<28, 5],
            [0x7FFFFFFF | 0, 5],
            [0xFFFFFFFF, 5],
            [0xFFFFFFFF | 0, 5, 10]
        ];
        for (var i=0; i<values.length; i++) {
            test.equal(ByteBuffer.calculateVarint32(values[i][0]), values[i][1]);
            test.equal(ByteBuffer.calculateVarint64(values[i][0]), values[i].length > 2 ? values[i][2] : values[i][1]);
        }
        var Long = ByteBuffer.Long;
        values = [
            [Long.fromNumber(1).shiftLeft(35), 6],
            [Long.fromNumber(1).shiftLeft(42), 7],
            [Long.fromNumber(1).shiftLeft(49), 8],
            [Long.fromNumber(1).shiftLeft(56), 9],
            [Long.fromNumber(1).shiftLeft(63), 10],
            [Long.fromNumber(1, true).shiftLeft(63), 10]
        ];
        for (i=0; i<values.length; i++) {
            test.equal(ByteBuffer.calculateVarint64(values[i][0]), values[i][1]);
        }
        test.done();
    };
    
    suite.types.zigZagVarint = function(test) {
        var Long = ByteBuffer.Long;
        var values = [
            [ 0, 0],
            [-1, 1],
            [ 1, 2],
            [-2, 3],
            [ 2, 4],
            [-3, 5],
            [ 3, 6],
            [ 2147483647, 4294967294],
            [-2147483648, 4294967295]
        ];
        for (var i=0; i<values.length; i++) {
            test.equal(ByteBuffer.zigZagEncode32(values[i][0]), values[i][1]);
            test.equal(ByteBuffer.zigZagDecode32(values[i][1]), values[i][0]);
            test.equal(ByteBuffer.zigZagEncode64(values[i][0]).toNumber(), values[i][1]);
            test.equal(ByteBuffer.zigZagDecode64(values[i][1]).toNumber(), values[i][0]);
        }
        values = [
            [Long.MAX_VALUE, Long.MAX_UNSIGNED_VALUE.subtract(Long.ONE)],
            [Long.MIN_VALUE, Long.MAX_UNSIGNED_VALUE]
        ];
        // NOTE: Even 64bit doubles from toNumber() fail for these values so we are using toString() here
        for (i=0; i<values.length; i++) {
            test.equal(ByteBuffer.zigZagEncode64(values[i][0]).toString(), values[i][1].toString());
            test.equal(ByteBuffer.zigZagDecode64(values[i][1]).toString(), values[i][0].toString());
        }
        
        // 32 bit ZZ
        values = [
            0,
            1,
            300,
            -300,
            2147483647,
            -2147483648
        ];
        bb = new ByteBuffer(10);
        for (i=0; i<values.length; i++) {
            var encLen = bb.writeVarint32ZigZag(values[i], 0);
            bb.limit = encLen;
            var dec = bb.readVarint32ZigZag(0);
            test.equal(dec['value'], values[i]);
            test.equal(encLen, dec['length']);
            bb.clear();
        }
        
        // 64 bit ZZ
        values = [
            Long.ONE, 1,
            Long.fromNumber(-3),
            Long.fromNumber(300),
            Long.fromNumber(-300),
            Long.fromNumber(0x7FFFFFFF),
            Long.fromNumber(0x8FFFFFFF),
            Long.fromNumber(0xFFFFFFFF),
            Long.fromBits(0xFFFFFFFF, 0x7FFFFFFF),
            Long.fromBits(0xFFFFFFFF, 0xFFFFFFFF)
        ];
        var bb = new ByteBuffer(10);
        for (i=0; i<values.length; i++) {
            encLen = bb.writeVarint64ZigZag(values[i], 0);
            dec = bb.readVarint64ZigZag(0);
            test.equal(values[i].toString(), dec['value'].toString());
            test.equal(encLen, dec['length']);
        }
        test.done();
    };
    
    suite.types.utf8string = function(test) {        
        var bb = new ByteBuffer(2);
        // Aliases
        test.strictEqual(bb.writeUTF8String, bb.writeString);
        test.strictEqual(bb.readUTF8String, bb.readString);
        var str = "ä☺𠜎️☁️", str2;
        // Writing 
        test.strictEqual(bb.writeUTF8String(str), bb);
        bb.flip();
        // bb.printDebug();
        // Reading
        str2 = bb.readUTF8String(ByteBuffer.calculateUTF8Chars(str), ByteBuffer.METRICS_CHARS);
        // bb.printDebug();
        test.strictEqual(str2.length, str.length);
        test.strictEqual(str2, str);
        bb.reset();
        str2 = bb.readUTF8String(bb.limit, ByteBuffer.METRICS_BYTES);
        test.strictEqual(str2, str);
        test.done();
    };

    suite.types.istring = function(test) {
        var bb = new ByteBuffer(2);
        test.strictEqual(bb.writeIString("ab"), bb); // resizes to 4+2=6
        test.strictEqual(bb.capacity(), 6);
        test.strictEqual(bb.offset, 6);
        test.strictEqual(bb.limit, 2);
        bb.flip();
        test.equal(bb.toString("debug"), "<00 00 00 02 61 62>");
        test.deepEqual(bb.readIString(0), {"string": "ab", "length": 6});
        test.strictEqual(bb.readIString(), "ab");
        bb.reset();
        test.equal(bb.toString("debug"), "<00 00 00 02 61 62>");
        test.strictEqual(bb.readIString(), "ab");
        test.equal(bb.toString("debug"), "00 00 00 02 61 62|");
        test.done();
    };
    
    suite.types.vstring = function(test) {
        var bb = new ByteBuffer(2);
        bb.writeVString("ab"); // resizes to 2*2=4
        test.strictEqual(bb.capacity(), 4);
        test.strictEqual(bb.offset, 3);
        test.strictEqual(bb.limit, 2);
        bb.flip();
        test.equal(bb.toString("debug").substr(0, 10), "<02 61 62>");
        test.deepEqual(bb.readVString(0), {"string": "ab", "length": 3});
        test.equal(bb.toString("debug").substr(0, 10), "<02 61 62>");
        test.equal(bb.readVString(), "ab");
        test.equal(bb.toString("debug").substr(0, 9), "02 61 62|");
        test.done();
    };
    
    suite.types.cstring = function(test) {
        var bb = new ByteBuffer(2);
        bb.writeCString("a");
        test.equal(bb.capacity(), 2);
        test.equal(bb.offset, 2);
        test.equal(bb.limit, 2);
        bb.offset = 1;
        bb.writeCString("b"); // resizes to 4
        test.equal(bb.capacity(), 4);
        test.equal(bb.offset, 3);
        test.equal(bb.limit, 2);
        bb.flip();
        test.equal(bb.toString("debug").substr(0, 10), "<61 62 00>");
        test.deepEqual(bb.readCString(0), {"string": "ab", "length": 3});
        test.equal(bb.toString("debug").substr(0, 10), "<61 62 00>");
        test.equal(bb.readCString(), "ab");
        test.equal(bb.toString("debug").substr(0, 9), "61 62 00|");
        test.done();
    };
    
    suite.convert = {};
    
    suite.convert.toHex = function(test) {
        var bb = new ByteBuffer(4);
        bb.writeUint16(0x1234);
        bb.writeUint8(0x56);
        bb.flip();
        test.equal(bb.toHex(), "123456");
        test.strictEqual(bb.offset, 0);
        test.equal(bb.toHex(1), "3456");
        test.equal(bb.toHex(1,2), "34");
        test.equal(bb.toHex(1,1), "");
        test.throws(function() {
            bb.toHex(1,0);
        });
        test.done();
    };
    
    suite.convert.toBase64 = function(test) {
        var bb = new ByteBuffer(8);
        bb.writeUTF8String("abcdefg"); // 7 chars
        bb.flip();
        test.equal(bb.toBase64(), "YWJjZGVmZw==");
        test.strictEqual(bb.offset, 0);
        test.equal(bb.toBase64(3), "ZGVmZw==");
        test.equal(bb.toBase64(3,6), "ZGVm");
        test.equal(bb.toBase64(3,3), "");
        test.throws(function() {
            bb.toBase64(1,0);
        });
        test.done();
    };
    
    suite.convert.toBinary = function(test) {
        var bb = new ByteBuffer(5);
        bb.writeUint32(0x001234FF);
        bb.flip();
        test.strictEqual(bb.toBinary(), "\x00\x12\x34\xFF");
        test.strictEqual(bb.offset, 0);
        test.done();
    };
    
    suite.convert.toString = function(test) {
        var bb = new ByteBuffer(3);
        bb.writeUint16(0x6162).flip();
        test.equal(bb.toString("hex"), "6162");
        test.equal(bb.toString("base64"), "YWI=");
        test.equal(bb.toString("utf8"), "ab");
        test.equal(bb.toString("debug").substr(0,7), "<61 62>");
        test.equal(bb.toString(), (type === ArrayBuffer ? (accessor === DataView ? "ByteBufferAB_DataView" : "ByteBufferAB") : "ByteBufferNB")+"(offset=0,markedOffset=-1,limit=2,capacity=3)");
        test.strictEqual(bb.offset, 0);
        test.done();
    };
    
    suite.convert.toBuffer = function(test) {
        var bb = new ByteBuffer(2);
        bb.writeUint16(0x1234).flip();
        var buf = bb.toBuffer();
        test.strictEqual(buf, bb.buffer);
        if (type === ArrayBuffer) {
            test.ok(buf instanceof ArrayBuffer);
            test.strictEqual(buf.byteLength, 2);
        } else {
            test.ok(buf instanceof Buffer);
            test.strictEqual(buf.length, 2);
        }
        bb.limit = 1;
        buf = bb.toBuffer();
        test.notStrictEqual(buf, bb.buffer);
        if (type === ArrayBuffer) {
            test.ok(buf instanceof ArrayBuffer);
            test.strictEqual(buf.byteLength, 1);
        } else {
            test.ok(buf instanceof Buffer);
            test.strictEqual(buf.length, 1);
        }
        test.done();
    };
    
    suite.convert.toArrayBuffer = function(test) {
        var bb = new ByteBuffer(3);
        if (type === ArrayBuffer) {
            test.strictEqual(bb.toArrayBuffer, bb.toBuffer);
        } else {
            test.ok(bb.buffer instanceof Buffer);
            bb.writeUint16(0x1234);
            bb.flip();
            bb.offset = 1;
            var ab = bb.toArrayBuffer();
            test.ok(ab instanceof ArrayBuffer);
            test.strictEqual(ab.byteLength, 1);
        }
        test.done();
    };

    suite.misc = {};
    
    suite.misc.pbjsi19 = function(test) {
        // test that this issue is fixed: https://github.com/dcodeIO/ProtoBuf.js/issues/19
        var bb = new ByteBuffer(9); // Trigger resize to 18 in writeVarint64
        bb.writeVarint32(16);
        bb.writeVarint32(2);
        bb.writeVarint32(24);
        bb.writeVarint32(0);
        bb.writeVarint32(32);
        bb.writeVarint64(ByteBuffer.Long.fromString("1368057600000"));
        bb.writeVarint32(40);
        bb.writeVarint64(ByteBuffer.Long.fromString("1235455123"));
        bb.flip();
        test.equal(bb.toString("debug").substr(0,52), "<10 02 18 00 20 80 B0 D9 B4 E8 27 28 93 99 8E CD 04>");
        test.done();
    };
    
    suite.misc.NaN = function(test) {
        var bb = new ByteBuffer(4);
        test.ok(isNaN(bb.writeFloat(NaN).flip().readFloat(0)));
        test.strictEqual(bb.writeFloat(+Infinity).flip().readFloat(0), +Infinity);
        test.strictEqual(bb.writeFloat(-Infinity).flip().readFloat(0), -Infinity);
        bb.resize(8);
        test.ok(isNaN(bb.writeDouble(NaN).flip().readDouble(0)));
        test.strictEqual(bb.writeDouble(+Infinity).flip().readDouble(0), +Infinity);
        test.strictEqual(bb.writeDouble(-Infinity).flip().readDouble(0), -Infinity);

        // Varints, however, always need a cast, which results in the following:
        test.strictEqual(NaN >>> 0, 0);
        test.strictEqual(NaN | 0, 0);
        test.strictEqual(Infinity >>> 0, 0);
        test.strictEqual(Infinity | 0, 0);
        test.strictEqual(-Infinity >>> 0, 0);
        test.strictEqual(-Infinity | 0, 0);

        test.done();
    };

    suite.debug = {};

    suite.debug.printDebug = function(test) {
        var bb = new ByteBuffer(3);
        function callMe() { callMe.called = true; }
        bb.printDebug(callMe);
        test.ok(callMe.called);
        test.done();
    };
    
    if (type === ArrayBuffer) {
        suite.debug.printDebugVisual = function(test) {
            var bb = ByteBuffer.wrap("Hello world! from byteBuffer.js. This is just a last visual test of ByteBuffer#printDebug.");
            console.log("");
            bb.printDebug(console.log);
            test.done();
        };
    }
    
    return suite;
}

module.exports = {
    "info": function(test) {
        test.log("Version "+ByteBuffer.VERSION+", "+new Date().toISOString()+"\n");
        test.done();
    },
    "node": makeSuite(ByteBufferNode),
    "browser": makeSuite(ByteBufferBrowser),
    "dataview": makeSuite(ByteBufferBrowser_DataView)
};