hufftree.js
/**
     * Additional tests for HuffTree's getValue(). getValue() isn't currently used by the
     * JPEG decoder, but it's a good way to experiement with the tree.
     */
Additional tests for HuffTree's getValue(). getValue() isn't currently used by the
JPEG decoder, but it's a good way to experiement with the tree.
Additional tests for HuffTree ' s getValue () . getValue () isn ' t currently used by the JPEG decoder but it ' s a good way to experiement with the tree .
static testGetValue() {
        let tree = new HuffTree(0);
        let testCnt = 0;
        let errCnt = 0;
        function runInsertTest(level, value, expected) {
            let val = tree.insertCode(level, value);
            testCnt++;
            if (val != expected) {
                console.error("Test " + testCnt + " Failed (expected " + expected + ", got " + val + ")");
                errCnt++;
            }
        }
        runInsertTest(3, 4, true);
        runInsertTest(3, 5, true);
        runInsertTest(3, 3, true);
        runInsertTest(3, 2, true);
        runInsertTest(3, 6, true);
        runInsertTest(3, 1, true);
        runInsertTest(3, 0, true);
        runInsertTest(4, 7, true);
        runInsertTest(5, 8, true);
        runInsertTest(6, 9, true);
        runInsertTest(7, 10, true);
        runInsertTest(8, 11, true);
        function runGetTest(input, expected) {
            let val = tree.getValue(input);
            testCnt++;
            if (val != expected) {
                console.error("Test " + testCnt + " Failed (expected " + expected + ", got " + val + ")");
                errCnt++;
            }
        }
        runGetTest("000", 4);
        runGetTest("001", 5);
        runGetTest("010", 3);
        runGetTest("011", 2);
        runGetTest("100", 6);
        runGetTest("101", 1);
        runGetTest("110", 0);
        runGetTest("1110", 7);
        runGetTest("11110", 8);
        runGetTest("111110", 9);
        runGetTest("1111110", 10);
        runGetTest("11111110", 11);
        runGetTest("11111111", undefined);
        const testName = "HuffTree.getValue";
        if (errCnt === 0) {
            console.log(testName + "tests passed!");
        } else {
            console.error(testName + "tests failed: " + errCnt + " errors");
        }
    }
static testGetValue ( ) { let tree = new HuffTree ( 0 ) ; let testCnt = 0 ; let errCnt = 0 ; function runInsertTest ( level , value , expected ) { let val = tree . insertCode ( level , value ) ; testCnt ++ ; if ( val != expected ) { console . error ( "Test " + testCnt + " Failed (expected " + expected + ", got " + val + ")" ) ; errCnt ++ ; } } runInsertTest ( 3 , 4 , true ) ; runInsertTest ( 3 , 5 , true ) ; runInsertTest ( 3 , 3 , true ) ; runInsertTest ( 3 , 2 , true ) ; runInsertTest ( 3 , 6 , true ) ; runInsertTest ( 3 , 1 , true ) ; runInsertTest ( 3 , 0 , true ) ; runInsertTest ( 4 , 7 , true ) ; runInsertTest ( 5 , 8 , true ) ; runInsertTest ( 6 , 9 , true ) ; runInsertTest ( 7 , 10 , true ) ; runInsertTest ( 8 , 11 , true ) ; function runGetTest ( input , expected ) { let val = tree . getValue ( input ) ; testCnt ++ ; if ( val != expected ) { console . error ( "Test " + testCnt + " Failed (expected " + expected + ", got " + val + ")" ) ; errCnt ++ ; } } runGetTest ( "000" , 4 ) ; runGetTest ( "001" , 5 ) ; runGetTest ( "010" , 3 ) ; runGetTest ( "011" , 2 ) ; runGetTest ( "100" , 6 ) ; runGetTest ( "101" , 1 ) ; runGetTest ( "110" , 0 ) ; runGetTest ( "1110" , 7 ) ; runGetTest ( "11110" , 8 ) ; runGetTest ( "111110" , 9 ) ; runGetTest ( "1111110" , 10 ) ; runGetTest ( "11111110" , 11 ) ; runGetTest ( "11111111" , undefined ) ; const testName = "HuffTree.getValue" ; if ( errCnt === 0 ) { console . log ( testName + "tests passed!" ) ; } else { console . error ( testName + "tests failed: " + errCnt + " errors" ) ; } }
Additional tests for HuffTree's getValue().
Additional tests for HuffTree ' s getValue () .
// We shouldn't be able to insert an 8th node at L3,  // but the tree currently has no way of knowing this // since nodes don't know the state of other nodes. // For this to be a problem in practice, the JPEG // file would have to be out-of-spec/corrupt //runInsertTest(3, 12, false);    // Too many nodes in tree // Sequence not in the table