<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

#include "DataInputStreamTest.h"

#include <decaf/lang/Integer.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

using namespace std;
using namespace decaf;
using namespace decaf::lang;
using namespace decaf::lang::exceptions;
using namespace decaf::io;
using namespace decaf::util;

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::testConstructor() {

    try {

        os->writeChar('t');
        os->close();
        openDataInputStream();
    } catch (IOException e) {
        CPPUNIT_FAIL("IOException during constructor test : " + e.getMessage());
    }

    try {
        is->close();
    } catch (IOException e) {
        CPPUNIT_FAIL("IOException during constructor test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::testRead1() {

    try {

        std::vector<unsigned char> test( testData.begin(), testData.end() );
        os->write( test );
        os->close();
        openDataInputStream();
        std::vector<unsigned char> result;
        result.resize( testData.length() );
        is->read( result );
        CPPUNIT_ASSERT_MESSAGE( "Incorrect data read",
            string( (const char*)&result[0], result.size() ) == testData );
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during read test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::testRead2() {
    try {

        os->write( std::vector<unsigned char>( testData.begin(), testData.end() ) );
        os->close();
        openDataInputStream();
        unsigned char* result = new unsigned char[ testData.length() ];
        is->read( result, 0, testData.length() );
        CPPUNIT_ASSERT_MESSAGE("Incorrect data read",
            string( (const char*)result, testData.size() ) == testData );
        delete [] result;
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during read test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readBoolean() {

    try {
        os->writeBoolean(true);
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect boolean written", is->readBoolean() );
    } catch( IOException &e ) {
        CPPUNIT_FAIL("readBoolean test failed : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readByte() {
    try {
        os->writeByte( (unsigned char) 127);
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect byte read", is->readByte() == (unsigned char) 127);
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readByte test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readChar() {
    try {
        os->writeChar('t');
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect char read", 't' == is->readChar());
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readChar test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readDouble() {
    try {
        os->writeDouble(2345.76834720202);
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect double read",
                2345.76834720202 == is->readDouble());
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readDouble test" + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readFloat() {
    try {
        os->writeFloat(29.08764f);
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect float read", is->readFloat() == 29.08764f);
    } catch( IOException &e ) {
        CPPUNIT_FAIL("readFloat test failed : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readFully1() {
    try {
        os->write( std::vector<unsigned char>( testData.begin(), testData.end() ) );
        os->close();
        openDataInputStream();
        std::vector<unsigned char> result;
        result.resize( testData.length() );
        is->readFully( result );
        CPPUNIT_ASSERT_MESSAGE( "Incorrect data read",
            string( (const char*)&result[0], 0, result.size() ) == testData );
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readFully test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readFully2() {

    try {
        os->write( std::vector<unsigned char>( testData.begin(), testData.end() ) );
        os->close();
        openDataInputStream();
        unsigned char* rbytes = new unsigned char[ testData.length() ];
        is->readFully( rbytes, 0, testData.length() );
        CPPUNIT_ASSERT_MESSAGE("Incorrect data read",
            string( (const char*)rbytes, 0, testData.length() ) == testData );
        delete [] rbytes;
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readFully test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readFullyNullArray() {
    std::vector<unsigned char> test( 5000 );
    DataInputStream is(
        new ByteArrayInputStream( test ), true );

    unsigned char* nullByteArray = NULL;

    is.readFully( nullByteArray, 0, 0);
    is.readFully( nullByteArray, 1, 0);

    CPPUNIT_ASSERT_THROW_MESSAGE(<commit_msg>Change the code to fix an "invalid read of size 1" in valgrind.<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

#include "DataInputStreamTest.h"

#include <decaf/lang/Integer.h>

#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

using namespace std;
using namespace decaf;
using namespace decaf::lang;
using namespace decaf::lang::exceptions;
using namespace decaf::io;
using namespace decaf::util;

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::testConstructor() {

    try {

        os->writeChar('t');
        os->close();
        openDataInputStream();
    } catch (IOException e) {
        CPPUNIT_FAIL("IOException during constructor test : " + e.getMessage());
    }

    try {
        is->close();
    } catch (IOException e) {
        CPPUNIT_FAIL("IOException during constructor test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::testRead1() {

    try {

        std::vector<unsigned char> test( testData.begin(), testData.end() );
        os->write( test );
        os->close();
        openDataInputStream();
        std::vector<unsigned char> result;
        result.resize( testData.length() );
        is->read( result );
        CPPUNIT_ASSERT_MESSAGE( "Incorrect data read",
            string( (const char*)&result[0], result.size() ) == testData );
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during read test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::testRead2() {
    try {

        os->write( std::vector<unsigned char>( testData.begin(), testData.end() ) );
        os->close();
        openDataInputStream();
        unsigned char* result = new unsigned char[ testData.length() ];
        is->read( result, 0, testData.length() );
        CPPUNIT_ASSERT_MESSAGE("Incorrect data read",
            string( (const char*)result, testData.size() ) == testData );
        delete [] result;
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during read test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readBoolean() {

    try {
        os->writeBoolean(true);
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect boolean written", is->readBoolean() );
    } catch( IOException &e ) {
        CPPUNIT_FAIL("readBoolean test failed : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readByte() {
    try {
        os->writeByte( (unsigned char) 127);
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect byte read", is->readByte() == (unsigned char) 127);
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readByte test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readChar() {
    try {
        os->writeChar('t');
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect char read", 't' == is->readChar());
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readChar test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readDouble() {
    try {
        os->writeDouble(2345.76834720202);
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect double read",
                2345.76834720202 == is->readDouble());
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readDouble test" + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readFloat() {
    try {
        os->writeFloat(29.08764f);
        os->close();
        openDataInputStream();
        CPPUNIT_ASSERT_MESSAGE("Incorrect float read", is->readFloat() == 29.08764f);
    } catch( IOException &e ) {
        CPPUNIT_FAIL("readFloat test failed : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readFully1() {
    try {
        os->write( std::vector<unsigned char>( testData.begin(), testData.end() ) );
        os->close();
        openDataInputStream();
        std::vector<unsigned char> result;
        result.resize( testData.length() );
        is->readFully( result );

        string expected = "";
        for( size_t ix = 0; ix < result.size(); ++ix ) {
            expected += (char)result[ix];
        }

        CPPUNIT_ASSERT_MESSAGE("Incorrect data read", expected == testData );

    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readFully test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readFully2() {

    try {
        os->write( std::vector<unsigned char>( testData.begin(), testData.end() ) );
        os->close();
        openDataInputStream();
        unsigned char* rbytes = new unsigned char[ testData.length() ];
        is->readFully( rbytes, 0, testData.length() );

        string expected = "";
        for( size_t ix = 0; ix < testData.length(); ++ix ) {
            expected += (char)rbytes[ix];
        }

        CPPUNIT_ASSERT_MESSAGE("Incorrect data read", expected == testData );

        delete [] rbytes;
    } catch( IOException &e ) {
        CPPUNIT_FAIL("IOException during readFully test : " + e.getMessage());
    }
}

////////////////////////////////////////////////////////////////////////////////
void DataInputStreamTest::test_readFullyNullArray() {
    std::vector<unsigned char> test( 5000 );
    DataInputStream is(
        new ByteArrayInputStream( test ), true );

    unsigned char* nullByteArray = NULL;

    is.readFully( nullByteArray, 0, 0);
    is.readFully( nullByteArray, 1, 0);

    CPPUNIT_ASSERT_THROW_MESSAGE(
{'fraction_non_alphanumeric': 0.23982777179763187, 'fraction_numerical': 0.010333692142088267, 'mean_word_length': 1.8847029600496792, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6884657', 'n_tokens_mistral': 3588, 'n_tokens_neox': 3441, 'n_words': 906}