public boolean _validate()

in src/com/amazon/ion/impl/BlockedBuffer.java [820:905]


    public boolean _validate() {
        int pos = 0;
        int idx;
        boolean err = false;
        _validate_count++;
        if ((_validate_count % 128) != 0) return true;
        // you can change the 0 below (in from of the -2) to be the validation counter
        // which reported the failure and the test will be true when _validate() is
        // called on the last GOOD check.
        if (_validate_count == 30 -2) {
            // used to set breakpoints on particular calls for validation
            err = (_validate_count < 0);
        }
        for (idx=0; idx<this._blocks.size(); idx++) {
            bbBlock b = this._blocks.get(idx);
            if (b._idx == -1) break;
            if (b._idx != idx) {
                System.out.println("block "+idx+": index is wrong" +
                                   ", it is "+b._idx+
                                   " it should be "+idx);
                err = true;
            }
            if (b._offset != pos) {
                System.out.println("block "+idx+": starting offset is wrong"+
                                   ", it is "+b._offset+
                                   " should be "+pos);
                err = true;
            }
            // else because there's no point in using a bad reserved value to check the limit
            else if (b._limit < 0 || b._limit > b._buffer.length /* - b._reserved */ ) {
                System.out.println("block "+idx+": "+
                                   "limit is out of range"+
                                   ", it is "+b._limit+
                                   " should be between 0 and "+ (b._buffer.length /* - b._reserved */));
                err = true;
            }
            else if (b._limit == 0) {
                if ( ! (b._idx == (this._next_block_position - 1)
                     && b._offset == this._buf_limit)
                ) {
                    System.out.println("block "+idx+": "+
                                       "has a ZERO limit");
                    err = true;
                }
            }
            pos += b._limit;
        }
        if (idx != this._next_block_position) {
            System.out.println("next block position is wrong, is "+this._next_block_position+" should be "+idx);
            err = true;
        }
        for (idx++; idx<this._blocks.size(); idx++) {
            bbBlock b = this._blocks.get(idx);
            if (b._offset != -1) {
                System.out.println("block "+idx+": (in freed range) has non -1 offset, offset is "+b._offset);
                err = true;
            }
        }
        if (pos != this._buf_limit) {
            System.out.println("buffer _buf_limit: "+
                               "limit is incorrect"+
                               ", it is "+this._buf_limit+
                               " should be "+ pos);
            err = true;
        }
        if (this._next_block_position > 0) {
            bbBlock last = this._blocks.get(this._next_block_position - 1);
            if (last._offset + last._limit != this._buf_limit){
                System.out.println("last block "+last._idx+" limit isn't "+
                                   "_buf_limit ("+this._buf_limit+"): "+
                                   " calc'd last block limit is "
                                   +       last._offset +" + "+ last._limit
                                   +" = "+(last._offset + last._limit)
                                   );
                err = true;
            }
        }
        if (this._buf_limit < 0 || (this._buf_limit > 0 && this._next_block_position < 1)){
            System.out.println("this._buf_limit "+ this._buf_limit+ " is invalid");
            err = true;
        }
        if (err == true) {
            System.out.println("failed with validation count = " + _validate_count);
        }
        return err == false;  // validate is true if all is ok so that assert _validate(); works as expected
    }