activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r652104 [23/29] - in /activemq/activemq-cpp/trunk: ./ m4/ src/examples/ src/examples/consumers/ src/main/ src/main/decaf/ src/main/decaf/internal/ src/main/decaf/internal/net/ src/main/decaf/internal/nio/ src/main/decaf/internal/util/ src/m...
Date Tue, 29 Apr 2008 20:52:37 GMT
Added: activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/CharArrayBufferTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/CharArrayBufferTest.cpp?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/CharArrayBufferTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/CharArrayBufferTest.cpp Tue Apr 29 13:52:30 2008
@@ -0,0 +1,974 @@
+/*
+ * 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 "CharArrayBufferTest.h"
+#include <decaf/lang/Integer.h>
+#include <decaf/lang/Double.h>
+#include <decaf/lang/Float.h>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::nio;
+using namespace decaf::internal::nio;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::test() {
+
+    // Check that we have setup the array and our initial assumptions on state
+    // are correct.  This is the first test run.
+    CPPUNIT_ASSERT( testBuffer1 != NULL );
+    CPPUNIT_ASSERT( testBuffer1->capacity() == testData1Size );
+    CPPUNIT_ASSERT( testBuffer1->hasRemaining() == true );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == false );
+    CPPUNIT_ASSERT( testBuffer1->toString() != "" );
+    CPPUNIT_ASSERT( testBuffer1->hasArray() == true );
+    CPPUNIT_ASSERT( testBuffer1->array() != NULL );
+    CPPUNIT_ASSERT( testBuffer1->arrayOffset() == 0 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testArray() {
+
+    testBuffer1->put( testData1, 0, testData1Size );
+    testBuffer1->position( 0 );
+
+    testBuffer1->mark();
+
+    for( std::size_t ix = 0; ix < testBuffer1->capacity(); ++ix ) {
+        CPPUNIT_ASSERT( testBuffer1->get() == testData1[ix] );
+    }
+
+    testBuffer1->reset();
+
+    char* array = testBuffer1->array();
+    CPPUNIT_ASSERT( array != NULL );
+
+    for( std::size_t ix = 0; ix < testBuffer1->capacity(); ++ix ) {
+        CPPUNIT_ASSERT( array[ix] == testData1[ix] );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testArrayOffset() {
+
+    testBuffer1->put( testData1, 0, testData1Size );
+    CPPUNIT_ASSERT( testBuffer1->arrayOffset() == 0 );
+    testBuffer1->rewind();
+    CPPUNIT_ASSERT( testBuffer1->arrayOffset() == 0 );
+
+    testBuffer1->get();
+    CharBuffer* sliced = testBuffer1->slice();
+    CPPUNIT_ASSERT( sliced->arrayOffset() == 1 );
+    delete sliced;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testReadOnlyArray() {
+
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+
+    CPPUNIT_ASSERT( readOnly != NULL );
+    CPPUNIT_ASSERT( readOnly->isReadOnly() == true );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw UnsupportedOperationException",
+        readOnly->array(),
+        UnsupportedOperationException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw UnsupportedOperationException",
+        readOnly->arrayOffset(),
+        UnsupportedOperationException );
+
+    delete readOnly;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testAsReadOnlyBuffer() {
+
+    testBuffer1->clear();
+    testBuffer1->mark();
+    testBuffer1->position( testBuffer1->limit() );
+
+    // readonly's contents should be the same as buf
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+
+    CPPUNIT_ASSERT( testBuffer1 != readOnly );
+    CPPUNIT_ASSERT( readOnly->isReadOnly() );
+    CPPUNIT_ASSERT( testBuffer1->position() == readOnly->position() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == readOnly->limit() );
+
+    CPPUNIT_ASSERT( *testBuffer1 == *readOnly );
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *readOnly ) == 0 );
+
+    // readonly's position, mark, and limit should be independent to buf
+    readOnly->reset();
+    CPPUNIT_ASSERT( readOnly->position() == 0 );
+    readOnly->clear();
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->limit() );
+    testBuffer1->reset();
+    CPPUNIT_ASSERT( testBuffer1->position() == 0);
+
+    delete readOnly;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testCompact() {
+
+    // readonly's contents should be the same as buf
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferExceptio",
+        readOnly->compact(),
+        ReadOnlyBufferException );
+
+    // case: buffer is full
+    testBuffer1->clear();
+    testBuffer1->mark();
+
+    for( std::size_t ix = 0; ix < testData1Size; ++ix ){
+        testBuffer1->put( ix, testData1[ix] );
+    }
+
+    CharBuffer& ret = testBuffer1->compact();
+
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    for( std::size_t ix = 0; ix < testBuffer1->capacity(); ix++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix ) == testData1[ix] );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a InvalidMarkException",
+        readOnly->reset(),
+        InvalidMarkException );
+
+    delete readOnly;
+
+    // case: buffer is empty
+    testBuffer1->position(0);
+    testBuffer1->limit(0);
+    testBuffer1->mark();
+    ret = testBuffer1->compact();
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    for( std::size_t ix = 0; ix < testBuffer1->capacity(); ix++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix ) == testData1[ix] );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a InvalidMarkException",
+        testBuffer1->reset(),
+        InvalidMarkException );
+
+    // case: normal
+    testBuffer1->position(1);
+    testBuffer1->limit(SMALL_TEST_LENGTH);
+    testBuffer1->mark();
+    ret = testBuffer1->compact();
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+    CPPUNIT_ASSERT( testBuffer1->position() == SMALL_TEST_LENGTH - 1  );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    for( std::size_t ix = 0; ix < SMALL_TEST_LENGTH - 1; ix++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix ) == testData1[ix + 1] );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a InvalidMarkException",
+        testBuffer1->reset(),
+        InvalidMarkException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testCompareTo() {
+
+    // compare to self
+    CPPUNIT_ASSERT( 0 == testBuffer1->compareTo( *testBuffer1 ) );
+
+    CPPUNIT_ASSERT( testBuffer1->capacity() > SMALL_TEST_LENGTH );
+    testBuffer1->clear();
+    CharBuffer* other = CharBuffer::allocate( testBuffer1->capacity() );
+
+    for( std::size_t ix = 0; ix < testData1Size; ++ix ){
+        testBuffer1->put( ix, testData1[ix] );
+    }
+
+    for( std::size_t ix = 0; ix < testData1Size; ++ix ){
+        other->put( ix, testData1[ix] );
+    }
+
+    CPPUNIT_ASSERT( 0 == testBuffer1->compareTo( *other ) );
+    CPPUNIT_ASSERT( 0 == other->compareTo( *testBuffer1 ) );
+    testBuffer1->position(1);
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) > 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) < 0 );
+    other->position( 2 );
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) < 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) > 0 );
+    testBuffer1->position( 2 );
+    other->limit(SMALL_TEST_LENGTH);
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) > 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) < 0 );
+
+    char* data = new char[21];
+    memset( data, 0, 21 );
+    CharBuffer* empty = CharBuffer::allocate(21);
+    CharBuffer* wrapped = CharBuffer::wrap( data, (std::size_t)0, (std::size_t)21 );
+
+    CPPUNIT_ASSERT( wrapped->compareTo( *empty ) == 0 );
+
+    delete empty;
+    delete wrapped;
+    delete other;
+    delete [] data;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testDuplicate() {
+
+    testBuffer1->clear();
+    testBuffer1->mark();
+    testBuffer1->position( testBuffer1->limit() );
+
+    for( std::size_t ix = 0; ix < testData1Size; ++ix ){
+        testBuffer1->put( ix, testData1[ix] );
+    }
+
+    // duplicate's contents should be the same as buf
+    CharBuffer* duplicate = testBuffer1->duplicate();
+    CPPUNIT_ASSERT( testBuffer1 != duplicate );
+    CPPUNIT_ASSERT( testBuffer1->position() == duplicate->position() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == duplicate->limit() );
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == duplicate->isReadOnly() );
+
+    for( std::size_t ix = 0; ix < testBuffer1->capacity(); ix++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix ) == duplicate->get( ix ) );
+    }
+
+    // duplicate's position, mark, and limit should be independent to buf
+    duplicate->reset();
+    CPPUNIT_ASSERT( duplicate->position() == 0 );
+    duplicate->clear();
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->limit() );
+    testBuffer1->reset();
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    delete duplicate;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testEquals() {
+
+    // equal to self
+    CPPUNIT_ASSERT( testBuffer1->equals( *testBuffer1 ) );
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    CPPUNIT_ASSERT( testBuffer1->equals( *readOnly ) );
+    CharBuffer* duplicate = testBuffer1->duplicate();
+    CPPUNIT_ASSERT( testBuffer1->equals( *duplicate ) );
+
+    CPPUNIT_ASSERT( testBuffer1->capacity() > SMALL_TEST_LENGTH );
+
+    testBuffer1->limit( testBuffer1->capacity() ).position( 0 );
+    readOnly->limit( readOnly->capacity() ).position( 1 );
+    CPPUNIT_ASSERT( !testBuffer1->equals( *readOnly ) );
+
+    testBuffer1->limit( testBuffer1->capacity() - 1 ).position( 0 );
+    duplicate->limit( duplicate->capacity() ).position( 0 );
+    CPPUNIT_ASSERT( !testBuffer1->equals( *duplicate ) );
+
+    delete readOnly;
+    delete duplicate;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testGet() {
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        CPPUNIT_ASSERT( testBuffer1->get() == testBuffer1->get(i) );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferUnderflowException",
+        testBuffer1->get(),
+        BufferUnderflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testGetbyteArray() {
+
+    std::vector<char> array;
+    array.resize( 1 );
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        CharBuffer& ret = testBuffer1->get( array );
+        CPPUNIT_ASSERT( array[0] == testBuffer1->get( i ) );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferUnderflowException",
+        testBuffer1->get( array ),
+        BufferUnderflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testGetbyteArray2() {
+
+    testBuffer1->clear();
+    char* array = new char[testBuffer1->capacity()];
+    char* array2 = new char[testBuffer1->capacity() + 1];
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferUnderflowException",
+        testBuffer1->get( array2, 0, testBuffer1->capacity() + 1 ),
+        BufferUnderflowException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    testBuffer1->get( array, testBuffer1->capacity(), 0 );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a NullPointerException",
+        testBuffer1->get( NULL, 0, 1 ),
+        NullPointerException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    testBuffer1->clear();
+    CharBuffer& ret = testBuffer1->get( array, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    for( std::size_t ix = 0; ix < testBuffer1->capacity() - 1; ix++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix ) == array[ix] );
+    }
+
+    CPPUNIT_ASSERT( &ret == testBuffer1);
+
+    delete [] array;
+    delete [] array2;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testGetWithIndex() {
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        CPPUNIT_ASSERT( testBuffer1->get() == testBuffer1->get(i) );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->get( testBuffer1->limit() ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testPutbyte() {
+
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+
+    readOnly->clear();
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        readOnly->put( 0 ),
+        ReadOnlyBufferException );
+
+    delete readOnly;
+
+    testBuffer1->clear();
+    for( size_t i = 0; i < testBuffer1->capacity(); i++) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        CharBuffer& ret = testBuffer1->put( (char)i );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (char)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        testBuffer1->put( 0 ),
+        BufferOverflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testPutbyteArray() {
+
+    std::vector<char> array;
+    array.push_back( 127 );
+
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    readOnly->clear();
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        readOnly->put( array ),
+        ReadOnlyBufferException );
+    delete readOnly;
+
+    testBuffer1->clear();
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        array[0] = (char)i;
+        CharBuffer& ret = testBuffer1->put( array );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (char)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        testBuffer1->put( array ),
+        BufferOverflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testPutbyteArray2() {
+
+    testBuffer1->clear();
+    char* array = new char[testBuffer1->capacity()];
+    char* array2 = new char[testBuffer1->capacity() + 1];
+
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    readOnly->clear();
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        readOnly->put( array, 0, testBuffer1->capacity() ),
+        ReadOnlyBufferException );
+    delete readOnly;
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        testBuffer1->put( array2, 0, testBuffer1->capacity() + 1 ),
+        BufferOverflowException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+    testBuffer1->put( array, testBuffer1->capacity(), 0 );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a NullPointerException",
+        testBuffer1->put( NULL, 2, Integer::MAX_VALUE ),
+        NullPointerException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    for( std::size_t ix = 0; ix < testData1Size; ++ix ){
+        testBuffer1->put( ix, testData1[ix] );
+    }
+
+    CharBuffer& ret = testBuffer1->put( array, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    for( std::size_t ix = 0; ix < testBuffer1->capacity() - 1; ix++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix ) == array[ix] );
+    }
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+
+    delete [] array;
+    delete [] array2;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testPutCharBuffer() {
+
+    CharBuffer* other = CharBuffer::allocate( testBuffer1->capacity() );
+
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    readOnly->clear();
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        readOnly->put( *other ),
+        ReadOnlyBufferException );
+    delete readOnly;
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IllegalArgumentException",
+        testBuffer1->put( *testBuffer1 ),
+        IllegalArgumentException );
+
+    CharBuffer* toBig = testBuffer1->allocate( testBuffer1->capacity() + 1 );
+    toBig->clear();
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        testBuffer1->put( *toBig ),
+        BufferOverflowException );
+    delete toBig;
+
+    for( std::size_t ix = 0; ix < testData1Size; ++ix ){
+        other->put( ix, testData1[ix] );
+    }
+    other->clear();
+
+    testBuffer1->clear();
+    CharBuffer& ret = testBuffer1->put( *other );
+    CPPUNIT_ASSERT( other->position() == other->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    for( std::size_t ix = 0; ix < testBuffer1->capacity() - 1; ix++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix ) == other->get( ix ) );
+    }
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+
+    delete other;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testPutIndexed() {
+
+    CharBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    readOnly->clear();
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        readOnly->put( 0, 0 ),
+        ReadOnlyBufferException );
+    delete readOnly;
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+        CharBuffer& ret = testBuffer1->put( i, (char)i );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (char)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->put( testBuffer1->limit(), 0 ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testSlice() {
+
+    CPPUNIT_ASSERT( testBuffer1->capacity() > SMALL_TEST_LENGTH );
+    testBuffer1->position( 1 );
+    testBuffer1->limit( testBuffer1->capacity() - 1 );
+
+    CharBuffer* slice = testBuffer1->slice();
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == slice->isReadOnly() );
+    CPPUNIT_ASSERT( slice->position() == 0 );
+    CPPUNIT_ASSERT( slice->limit() == testBuffer1->remaining() );
+    CPPUNIT_ASSERT( slice->capacity() == testBuffer1->remaining() );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a InvalidMarkException",
+        slice->reset(),
+        InvalidMarkException );
+
+    // slice share the same content with buf
+    for( std::size_t ix = 0; ix < slice->capacity(); ++ix ){
+        slice->put( ix, testData1[ix] );
+    }
+
+    for( std::size_t ix = 0; ix < slice->capacity(); ix++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix + 1 ) == slice->get( ix ) );
+    }
+    testBuffer1->put( 2, 100 );
+    CPPUNIT_ASSERT( slice->get(1) == 100 );
+
+    delete slice;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testToString() {
+
+    string expected = "";
+    for( std::size_t i = testBuffer1->position(); i < testBuffer1->limit(); i++ ) {
+        expected += testBuffer1->get(i);
+    }
+    string str = testBuffer1->toString();
+    CPPUNIT_ASSERT( expected == str );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testWrapNullArray() {
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a NullPointerException",
+        testBuffer1->wrap( (char*)NULL, 0, 3 ),
+        NullPointerException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testCharAt() {
+
+    for( std::size_t i = 0; i < testBuffer1->remaining(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->get( testBuffer1->position() + i ) ==
+                        testBuffer1->charAt(i) );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->charAt( testBuffer1->remaining() ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testLength() {
+    CPPUNIT_ASSERT( testBuffer1->length() == testBuffer1->remaining() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testSubSequence() {
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->subSequence(testBuffer1->length() + 1, testBuffer1->length() + 1),
+        IndexOutOfBoundsException );
+
+    CharSequence* sub1 = testBuffer1->subSequence(
+        testBuffer1->length(), testBuffer1->length() );
+    CPPUNIT_ASSERT( sub1->length() == 0 );
+    delete sub1;
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->subSequence( 1, 0 ),
+        IndexOutOfBoundsException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->subSequence( 1, testBuffer1->length() + 1 ),
+        IndexOutOfBoundsException );
+
+    CharSequence* sub2 = testBuffer1->subSequence( 0, testBuffer1->length() );
+    CPPUNIT_ASSERT( sub2->toString() == testBuffer1->toString() );
+    delete sub2;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testPutString() {
+
+    testBuffer1->clear();
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        string testString = " ";
+        testString[0] = (char)i;
+        CharBuffer& ret = testBuffer1->put( testString );
+        CPPUNIT_ASSERT( (char)testBuffer1->get(i) == (char)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        testBuffer1->put( "ASDFGHJKJKL" ),
+        BufferOverflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testPutStringWithArgs() {
+
+    testBuffer1->clear();
+    string str;
+    str.resize( testBuffer1->capacity() );
+
+    // Throw a BufferOverflowException and no character is transfered to
+    // CharBuffer
+    std::string toBig = "";
+    toBig.resize( testBuffer1->capacity() + 1 );
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        testBuffer1->put( toBig, 0, testBuffer1->capacity() + 1 ),
+        BufferOverflowException );
+
+    CPPUNIT_ASSERT( 0 == testBuffer1->position() );
+
+    testBuffer1->clear();
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->put( str, str.length() + 1, str.length() + 2 ),
+        IndexOutOfBoundsException );
+
+    testBuffer1->put( str, str.length(), str.length() );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->put( str, 2, 1 ),
+        IndexOutOfBoundsException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->put( str, 2, str.length() + 1 ),
+        IndexOutOfBoundsException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    str.resize( testBuffer1->capacity() );
+
+    CharBuffer& ret = testBuffer1->put( str, 0, str.length() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+
+    for( std::size_t ix = 0; ix < testBuffer1->capacity(); ++ix ) {
+        CPPUNIT_ASSERT( testBuffer1->get( ix ) == str.at( ix ) );
+    }
+
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testAppendSelf() {
+
+    CharBuffer* cb = CharBuffer::allocate(10);
+    CharBuffer* cb2 = cb->duplicate();
+
+    cb->append( cb );
+    CPPUNIT_ASSERT( 10 == cb->position() );
+    cb->clear();
+    CPPUNIT_ASSERT( cb2->equals( *cb ) );
+
+    delete cb2;
+    cb->put( "abc" );
+    cb2 = cb->duplicate();
+    cb->append( cb );
+    CPPUNIT_ASSERT( 10 == cb->position() );
+    cb->clear();
+    cb2->clear();
+    CPPUNIT_ASSERT( cb2->equals( *cb ) );
+
+    delete cb2;
+    cb->put( "edfg" );
+    cb->clear();
+    cb2 = cb->duplicate();
+    cb->append( cb );
+    CPPUNIT_ASSERT( 10 == cb->position() );
+    cb->clear();
+    cb2->clear();
+    CPPUNIT_ASSERT( cb->equals( *cb2 ) );
+
+    delete cb;
+    delete cb2;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testAppendOverFlow() {
+
+    CharBuffer* cb = CharBuffer::allocate( 1 );
+    CharSequence* cs = testBuffer1->subSequence( 0, 25 );
+
+    cb->put( 'A' );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        cb->append('C'),
+        BufferOverflowException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        cb->append( cs ),
+        BufferOverflowException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a BufferOverflowException",
+        cb->append( cs, 1, 2 ),
+        BufferOverflowException );
+
+    delete cb;
+    delete cs;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testReadOnlyMap() {
+
+    CharBuffer* cb = testBuffer1->asReadOnlyBuffer();
+    MyCharSequence cs( "String" );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        cb->append( 'A' ),
+        ReadOnlyBufferException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        cb->append( &cs ),
+        ReadOnlyBufferException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        cb->append( &cs, 1, 2 ),
+        ReadOnlyBufferException );
+
+    delete cb;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testAppendCNormal() {
+    CharBuffer* cb = CharBuffer::allocate(2);
+    cb->put('A');
+    CPPUNIT_ASSERT( cb == &( cb->append('B') ) );
+    CPPUNIT_ASSERT( 'B' == cb->get( 1 ) );
+    delete cb;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testAppendCharSequenceNormal() {
+
+    CharBuffer* cb = CharBuffer::allocate(10);
+    cb->put('A');
+    MyCharSequence cs( "String" );
+
+    CPPUNIT_ASSERT( cb == &( cb->append( &cs ) ) );
+    cb->flip();
+    CPPUNIT_ASSERT( MyCharSequence("AString").toString() == cb->toString() );
+    cb->append( (const lang::CharSequence*)NULL );
+    cb->flip();
+    CPPUNIT_ASSERT( cb->toString() == "null" );
+
+    delete cb;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testAppendCharSequenceIINormal() {
+
+    CharBuffer* cb = CharBuffer::allocate( 10 );
+    cb->put( 'A' );
+    MyCharSequence cs( "String" );
+
+    CPPUNIT_ASSERT( cb == &( cb->append( &cs, 1, 3 ) ) );
+    cb->flip();
+    CPPUNIT_ASSERT( "Atr" == cb->toString() );
+
+    cb->append( (const lang::CharSequence*)NULL, 0, 1 );
+    cb->flip();
+    CPPUNIT_ASSERT( "n" == cb->toString() );
+    delete cb;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testAppendCharSequenceII_IllegalArgument() {
+
+    CharBuffer* cb = CharBuffer::allocate( 10 );
+    MyCharSequence cs( "String" );
+
+    cb->append( &cs, 0, 0 );
+    cb->append( &cs, 2, 2 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        cb->append( &cs, 3, 2 ),
+        IndexOutOfBoundsException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        cb->append( &cs, 3, 0 ),
+        IndexOutOfBoundsException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        cb->append( &cs, 3, 110 ),
+        IndexOutOfBoundsException );
+
+    delete cb;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testReadCharBuffer() {
+
+    std::vector<char> buffer;
+    buffer.push_back('S');
+    buffer.push_back('t');
+    buffer.push_back('r');
+    buffer.push_back('i');
+    buffer.push_back('n');
+    buffer.push_back('g');
+
+    CharBuffer* source = CharBuffer::wrap( buffer );
+    CharBuffer* target = CharBuffer::allocate(10);
+
+    CPPUNIT_ASSERT( 6 == source->read( target ) );
+    target->flip();
+    CPPUNIT_ASSERT( "String" == target->toString() );
+
+    // return -1 when nothing to read
+    CPPUNIT_ASSERT( string::npos == source->read( target ) );
+
+    // NullPointerException
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a NullPointerException",
+        source->read( NULL ),
+        NullPointerException );
+
+    delete source;
+    delete target;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testReadReadOnly() {
+
+    CharBuffer* source = CharBuffer::wrap( testData1, 0, testData1Size );
+    CharBuffer* target = testBuffer1->asReadOnlyBuffer();
+
+    // NullPointerException
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        source->read( target ),
+        ReadOnlyBufferException );
+
+    // if target has no remaining, needn't to check the isReadOnly
+    target->flip();
+    CPPUNIT_ASSERT( 0 == source->read( target ) );
+
+    delete source;
+    delete target;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testReadOverflow() {
+
+    std::vector<char> buffer;
+    buffer.push_back('S');
+    CharBuffer* source = CharBuffer::wrap( buffer );
+    CharBuffer* target = CharBuffer::allocate( 1 );
+
+    CPPUNIT_ASSERT( 1 == source->read(target) );
+    target->flip();
+    CPPUNIT_ASSERT( "S" == target->toString() );
+    CPPUNIT_ASSERT( 1 == source->position() );
+
+    delete source;
+    delete target;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void CharArrayBufferTest::testReadSelf() {
+
+    CharBuffer* source = CharBuffer::wrap( testData1, 0, testData1Size );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IllegalArgumentException",
+        source->read( source ),
+        IllegalArgumentException );
+
+    delete source;
+}

Added: activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/CharArrayBufferTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/CharArrayBufferTest.h?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/CharArrayBufferTest.h (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/CharArrayBufferTest.h Tue Apr 29 13:52:30 2008
@@ -0,0 +1,192 @@
+/*
+ * 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.
+ */
+
+#ifndef _DECAF_INTERNAL_NIO_CHARARRAYBUFFERTEST_H_
+#define _DECAF_INTERNAL_NIO_CHARARRAYBUFFERTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <decaf/nio/CharBuffer.h>
+
+namespace decaf{
+namespace internal{
+namespace nio{
+
+    class CharArrayBufferTest : public CppUnit::TestFixture {
+
+        CPPUNIT_TEST_SUITE( CharArrayBufferTest );
+        CPPUNIT_TEST( test );
+        CPPUNIT_TEST( testArray );
+        CPPUNIT_TEST( testArrayOffset );
+        CPPUNIT_TEST( testReadOnlyArray );
+        CPPUNIT_TEST( testAsReadOnlyBuffer );
+        CPPUNIT_TEST( testCompact );
+        CPPUNIT_TEST( testCompareTo );
+        CPPUNIT_TEST( testDuplicate );
+        CPPUNIT_TEST( testEquals );
+        CPPUNIT_TEST( testGet );
+        CPPUNIT_TEST( testGetbyteArray );
+        CPPUNIT_TEST( testGetbyteArray2 );
+        CPPUNIT_TEST( testGetWithIndex );
+        CPPUNIT_TEST( testPutbyte );
+        CPPUNIT_TEST( testPutbyteArray );
+        CPPUNIT_TEST( testPutbyteArray2 );
+        CPPUNIT_TEST( testPutCharBuffer );
+        CPPUNIT_TEST( testPutIndexed );
+        CPPUNIT_TEST( testSlice );
+        CPPUNIT_TEST( testToString );
+        CPPUNIT_TEST( testWrapNullArray );
+        CPPUNIT_TEST( testCharAt );
+        CPPUNIT_TEST( testLength );
+        CPPUNIT_TEST( testSubSequence );
+        CPPUNIT_TEST( testPutString );
+        CPPUNIT_TEST( testPutStringWithArgs );
+        CPPUNIT_TEST( testAppendSelf );
+        CPPUNIT_TEST( testAppendOverFlow );
+        CPPUNIT_TEST( testReadOnlyMap );
+        CPPUNIT_TEST( testAppendCNormal );
+        CPPUNIT_TEST( testAppendCharSequenceNormal );
+        CPPUNIT_TEST( testAppendCharSequenceIINormal );
+        CPPUNIT_TEST( testAppendCharSequenceII_IllegalArgument );
+        CPPUNIT_TEST( testReadCharBuffer );
+        CPPUNIT_TEST( testReadReadOnly );
+        CPPUNIT_TEST( testReadOverflow );
+        CPPUNIT_TEST( testReadSelf );
+        CPPUNIT_TEST_SUITE_END();
+
+        decaf::nio::CharBuffer* testBuffer1;
+        char* testData1;
+
+        static const std::size_t testData1Size = 100;
+        static const std::size_t SMALL_TEST_LENGTH = 5;
+        static const std::size_t BUFFER_LENGTH = 250;
+
+        class MyCharSequence : public lang::CharSequence {
+        private:
+
+            std::string value;
+
+        public:
+
+            MyCharSequence( std::string value ) {
+                this->value = value;
+            }
+
+            virtual ~MyCharSequence() {}
+
+            virtual std::size_t length() const {
+                return this->value.length();
+            }
+
+            virtual char charAt( std::size_t index ) const
+                throw( lang::exceptions::IndexOutOfBoundsException ) {
+
+                if( index > this->value.length() ) {
+                    throw decaf::lang::exceptions::IndexOutOfBoundsException(
+                        __FILE__, __LINE__,
+                        "MyCharSequence::charAt - index is to big: %d", index );
+                }
+
+                return this->value.at( index );
+            }
+
+            virtual CharSequence* subSequence( std::size_t start, std::size_t end ) const
+                throw( lang::exceptions::IndexOutOfBoundsException ) {
+
+                if( start > end ) {
+                    throw decaf::lang::exceptions::IndexOutOfBoundsException(
+                        __FILE__, __LINE__,
+                        "CharArrayBuffer::subSequence - start > end" );
+                }
+
+                if( start > this->length() || end > this->length() ) {
+                    throw decaf::lang::exceptions::IndexOutOfBoundsException(
+                        __FILE__, __LINE__,
+                        "CharArrayBuffer::subSequence - Sequence exceed limit" );
+                }
+
+                return new MyCharSequence( this->value.substr( start, end - start ) );
+            }
+
+            virtual std::string toString() const {
+                return this->value;
+            }
+        };
+
+    public:
+
+        CharArrayBufferTest() {}
+        virtual ~CharArrayBufferTest() {}
+
+        void setUp() {
+            testBuffer1 = decaf::nio::CharBuffer::allocate( testData1Size );
+
+            testData1 = new char[testData1Size];
+            for( std::size_t i = 0; i < testData1Size; ++i ){
+                testData1[i] = (char)i;
+            }
+        }
+
+        void tearDown() {
+            delete testBuffer1;
+            delete [] testData1;
+        }
+
+        void test();
+        void testArray();
+        void testArrayOffset();
+        void testReadOnlyArray();
+        void testAsReadOnlyBuffer();
+        void testCompact();
+        void testCompareTo();
+        void testDuplicate();
+        void testEquals();
+        void testGet();
+        void testGetbyteArray();
+        void testGetbyteArray2();
+        void testGetWithIndex();
+        void testPutbyte();
+        void testPutbyteArray();
+        void testPutbyteArray2();
+        void testPutCharBuffer();
+        void testPutIndexed();
+        void testSlice();
+        void testToString();
+        void testWrapNullArray();
+        void testCharAt();
+        void testLength();
+        void testSubSequence();
+        void testPutString();
+        void testPutStringWithArgs();
+        void testAppendSelf();
+        void testAppendOverFlow();
+        void testReadOnlyMap();
+        void testAppendCNormal();
+        void testAppendCharSequenceNormal();
+        void testAppendCharSequenceIINormal();
+        void testAppendCharSequenceII_IllegalArgument();
+        void testReadCharBuffer();
+        void testReadReadOnly();
+        void testReadOverflow();
+        void testReadSelf();
+
+    };
+
+}}}
+
+#endif /*_DECAF_INTERNAL_NIO_CHARARRAYBUFFERTEST_H_*/

Added: activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/DoubleArrayBufferTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/DoubleArrayBufferTest.cpp?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/DoubleArrayBufferTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/DoubleArrayBufferTest.cpp Tue Apr 29 13:52:30 2008
@@ -0,0 +1,559 @@
+/*
+ * 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 "DoubleArrayBufferTest.h"
+#include <decaf/lang/Integer.h>
+#include <decaf/lang/Double.h>
+#include <decaf/lang/Float.h>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::nio;
+using namespace decaf::internal::nio;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::test() {
+
+    // Check that we have setup the array and our initial assumptions on state
+    // are correct.  This is the first test run.
+    CPPUNIT_ASSERT( testBuffer1 != NULL );
+    CPPUNIT_ASSERT( testBuffer1->capacity() == testData1Size );
+    CPPUNIT_ASSERT( testBuffer1->hasRemaining() == true );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == false );
+    CPPUNIT_ASSERT( testBuffer1->toString() != "" );
+    CPPUNIT_ASSERT( testBuffer1->hasArray() == true );
+    CPPUNIT_ASSERT( testBuffer1->array() != NULL );
+    CPPUNIT_ASSERT( testBuffer1->arrayOffset() == 0 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testArray() {
+
+    double* array = testBuffer1->array();
+
+    testBuffer1->put( 0, 10.0 );
+    CPPUNIT_ASSERT( array[0] == 10.0 );
+
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+
+    loadTestData1( array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity()) ;
+
+    loadTestData2( array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+
+    loadTestData1( testBuffer1 );
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+
+    loadTestData2( testBuffer1 );
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testArrayOffset() {
+
+    double* array = testBuffer1->array();
+
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+
+    loadTestData1(array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+
+    loadTestData2(array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+
+    loadTestData1(testBuffer1);
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+
+    loadTestData2(testBuffer1);
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testReadOnlyArray() {
+
+    DoubleBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+
+    CPPUNIT_ASSERT( readOnly != NULL );
+    CPPUNIT_ASSERT( readOnly->isReadOnly() == true );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw UnsupportedOperationException",
+        readOnly->array(),
+        UnsupportedOperationException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw UnsupportedOperationException",
+        readOnly->arrayOffset(),
+        UnsupportedOperationException );
+
+    delete readOnly;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testAsReadOnlyBuffer() {
+
+    testBuffer1->clear();
+    testBuffer1->mark();
+    testBuffer1->position( testBuffer1->limit() );
+
+    // readonly's contents should be the same as testBuffer1
+    DoubleBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    CPPUNIT_ASSERT( testBuffer1 != readOnly );
+    CPPUNIT_ASSERT( readOnly->isReadOnly() );
+    CPPUNIT_ASSERT( testBuffer1->position() == readOnly->position() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == readOnly->limit() );
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); ++i ) {
+        CPPUNIT_ASSERT( testBuffer1->get( i ) == readOnly->get( i ) );
+    }
+
+    // readOnly's position, mark, and limit should be independent to testBuffer1
+    readOnly->reset();
+    CPPUNIT_ASSERT( readOnly->position() == 0 );
+    readOnly->clear();
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->limit() );
+    testBuffer1->reset();
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    delete readOnly;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testCompact() {
+
+    loadTestData1( testBuffer1 );
+
+    // case: buffer is full
+    testBuffer1->clear();
+    testBuffer1->mark();
+
+    DoubleBuffer& ret = testBuffer1->compact();
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    assertContentLikeTestData1( testBuffer1, 0, 0.0, testBuffer1->capacity() );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw InvalidMarkException",
+        testBuffer1->reset(),
+        InvalidMarkException );
+
+    // case: buffer is empty
+    testBuffer1->position(0);
+    testBuffer1->limit(0);
+    testBuffer1->mark();
+    ret = testBuffer1->compact();
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    assertContentLikeTestData1(testBuffer1, 0, 0.0, testBuffer1->capacity());
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw InvalidMarkException",
+        testBuffer1->reset(),
+        InvalidMarkException );
+
+    // case: normal
+    CPPUNIT_ASSERT( testBuffer1->capacity() > 5 );
+
+    testBuffer1->position(1);
+    testBuffer1->limit(5);
+    testBuffer1->mark();
+    ret = testBuffer1->compact();
+    CPPUNIT_ASSERT( &ret == testBuffer1);
+    CPPUNIT_ASSERT( testBuffer1->position() == 4 );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    assertContentLikeTestData1(testBuffer1, 0, 1.0, 4);
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw InvalidMarkException",
+        testBuffer1->reset(),
+        InvalidMarkException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testCompareTo() {
+
+    DoubleBuffer* other = DoubleBuffer::allocate( testBuffer1->capacity() );
+
+    loadTestData1(testBuffer1);
+    loadTestData1(other);
+
+    CPPUNIT_ASSERT( 0 == testBuffer1->compareTo( *other ) );
+    CPPUNIT_ASSERT( 0 == other->compareTo( *testBuffer1 ) );
+    testBuffer1->position(1);
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) > 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) < 0 );
+    other->position( 2 );
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) < 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) > 0 );
+    testBuffer1->position(2);
+    other->limit(5);
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) > 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) < 0 );
+
+    std::vector<double> array1( 1, Double::NaN );
+    std::vector<double> array2( 1, Double::NaN );
+    std::vector<double> array3( 1, 42.0 );
+
+    DoubleBuffer* dbuffer1 = DoubleBuffer::wrap( array1 );
+    DoubleBuffer* dbuffer2 = DoubleBuffer::wrap( array2 );
+    DoubleBuffer* dbuffer3 = DoubleBuffer::wrap( array3 );
+
+    CPPUNIT_ASSERT_MESSAGE(
+        "Failed equal comparison with NaN entry",
+        dbuffer1->compareTo( *dbuffer2 ) );
+    CPPUNIT_ASSERT_MESSAGE(
+        "Failed greater than comparison with NaN entry",
+        dbuffer3->compareTo( *dbuffer1 ) );
+    CPPUNIT_ASSERT_MESSAGE(
+        "Failed greater than comparison with NaN entry",
+        dbuffer1->compareTo( *dbuffer3 ) );
+
+    delete other;
+    delete dbuffer1;
+    delete dbuffer2;
+    delete dbuffer3;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testDuplicate() {
+    testBuffer1->clear();
+    testBuffer1->mark();
+    testBuffer1->position(testBuffer1->limit());
+
+    // duplicate's contents should be the same as testBuffer1
+    DoubleBuffer* duplicate = testBuffer1->duplicate();
+    CPPUNIT_ASSERT( testBuffer1 != duplicate );
+    CPPUNIT_ASSERT( testBuffer1->position() == duplicate->position() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == duplicate->limit() );
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == duplicate->isReadOnly() );
+    assertContentEquals( testBuffer1, duplicate );
+
+    // duplicate's position, mark, and limit should be independent to testBuffer1
+    duplicate->reset();
+    CPPUNIT_ASSERT( duplicate->position() == 0 );
+    duplicate->clear();
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->limit() );
+    testBuffer1->reset();
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    delete duplicate;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testEquals() {
+
+    // equal to self
+    CPPUNIT_ASSERT( testBuffer1->equals( *testBuffer1 ) );
+    DoubleBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    CPPUNIT_ASSERT( testBuffer1->equals( *readOnly ) );
+    DoubleBuffer* duplicate = testBuffer1->duplicate();
+    CPPUNIT_ASSERT( testBuffer1->equals( *duplicate ) );
+
+    CPPUNIT_ASSERT( testBuffer1->capacity() > 5 );
+
+    testBuffer1->limit( testBuffer1->capacity() ).position(0);
+    readOnly->limit( readOnly->capacity() ).position( 1 );
+    CPPUNIT_ASSERT( !testBuffer1->equals( *readOnly ) );
+
+    testBuffer1->limit( testBuffer1->capacity() - 1).position(0);
+    duplicate->limit( duplicate->capacity() ).position( 0 );
+    CPPUNIT_ASSERT( !testBuffer1->equals( *duplicate ) );
+
+    delete readOnly;
+    delete duplicate;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testGet() {
+
+    testBuffer1->clear();
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        CPPUNIT_ASSERT( testBuffer1->get() == testBuffer1->get(i) );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferUnderflowException",
+        testBuffer1->get(),
+        BufferUnderflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testGetDoubleArray() {
+
+    std::vector<double> array(1);
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        DoubleBuffer& ret = testBuffer1->get( array );
+        CPPUNIT_ASSERT( array[0] == testBuffer1->get(i) );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferUnderflowException",
+        testBuffer1->get( array ),
+        BufferUnderflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testGetDoubleArray2() {
+
+    testBuffer1->clear();
+    double* array = new double[testBuffer1->capacity()];
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferUnderflowException",
+        testBuffer1->get( array, 0, testBuffer1->capacity() + 1 ),
+        BufferUnderflowException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    testBuffer1->get( array, 10, 0 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferUnderflowException",
+        testBuffer1->get( array, 1, Integer::MAX_VALUE ),
+        BufferUnderflowException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    testBuffer1->clear();
+    DoubleBuffer& ret = testBuffer1->get( array, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    assertContentEquals( testBuffer1, array, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+
+    delete [] array;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testGet2() {
+
+    testBuffer1->clear();
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        CPPUNIT_ASSERT( testBuffer1->get() == testBuffer1->get(i) );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw IndexOutOfBoundsException",
+        testBuffer1->get( testBuffer1->limit() ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testHasArray() {
+    CPPUNIT_ASSERT( testBuffer1->hasArray() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testPutDouble() {
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        DoubleBuffer& ret = testBuffer1->put( (double)i );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (double)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferOverflowException",
+        testBuffer1->put( 0 ),
+        BufferOverflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testPutDoubleArray() {
+
+    double* array = new double[1];
+
+    testBuffer1->clear();
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        array[0] = (double) i;
+        DoubleBuffer& ret = testBuffer1->put( array, 0, 1 );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (double)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferOverflowException",
+        testBuffer1->put( array, 0, 1 ),
+        BufferOverflowException );
+
+    delete [] array;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testPutDoubleArray2() {
+
+    testBuffer1->clear();
+    double* array1 = new double[ testBuffer1->capacity() ];
+    double* array2 = new double[ testBuffer1->capacity() + 1 ];
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferOverflowException",
+        testBuffer1->put( array2, 0, testBuffer1->capacity() + 1 ),
+        BufferOverflowException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    testBuffer1->put( array1, testBuffer1->capacity() + 1, 0 );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    loadTestData2( array1, 0, testBuffer1->capacity() );
+    DoubleBuffer& ret = testBuffer1->put( array1, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    assertContentEquals( testBuffer1, array1, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+
+    delete [] array1;
+    delete [] array2;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testPutDoubleBuffer() {
+
+    DoubleBuffer* other = DoubleBuffer::allocate( testBuffer1->capacity() );
+    DoubleBuffer* other1 = DoubleBuffer::allocate( testBuffer1->capacity() + 1 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw IllegalArgumentException",
+        testBuffer1->put( *testBuffer1 ),
+        IllegalArgumentException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferOverflowException",
+        testBuffer1->put( *other1 ),
+        BufferOverflowException );
+
+    loadTestData2(other);
+    other->clear();
+    testBuffer1->clear();
+    DoubleBuffer& ret = testBuffer1->put( *other );
+
+    CPPUNIT_ASSERT( other->position() == other->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    assertContentEquals( other, testBuffer1 );
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+
+    delete other;
+    delete other1;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testGetWithIndex() {
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+        DoubleBuffer& ret = testBuffer1->put( i, (double)i );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (double)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw IndexOutOfBoundsException",
+        testBuffer1->put( testBuffer1->limit(), 0 ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testPutIndexed() {
+
+    DoubleBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    readOnly->clear();
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        readOnly->put( 0, 0 ),
+        ReadOnlyBufferException );
+    delete readOnly;
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+        DoubleBuffer& ret = testBuffer1->put(i, i );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->put( testBuffer1->limit(), 0 ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testSlice() {
+
+    CPPUNIT_ASSERT( testBuffer1->capacity() > 5 );
+    testBuffer1->position(1);
+    testBuffer1->limit(testBuffer1->capacity() - 1);
+
+    DoubleBuffer* slice = testBuffer1->slice();
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == slice->isReadOnly() );
+    CPPUNIT_ASSERT( slice->position() == 0 );
+    CPPUNIT_ASSERT( slice->limit() == testBuffer1->remaining() );
+    CPPUNIT_ASSERT( slice->capacity() == testBuffer1->remaining() );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw InvalidMarkException",
+        slice->reset(),
+        InvalidMarkException );
+
+    // slice share the same content with testBuffer1
+    // FIXME:
+    loadTestData1(slice);
+    assertContentLikeTestData1(testBuffer1, 1, 0, slice->capacity());
+    testBuffer1->put( 2, 500 );
+    CPPUNIT_ASSERT( slice->get(1) == 500 );
+
+    delete slice;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void DoubleArrayBufferTest::testToString() {
+
+    std::string str = testBuffer1->toString();
+    CPPUNIT_ASSERT( str.find("Double") != string::npos );
+    CPPUNIT_ASSERT( str.find( Integer::toString( testBuffer1->position() ) ) != string::npos );
+    CPPUNIT_ASSERT( str.find( Integer::toString( testBuffer1->limit() ) ) != string::npos );
+    CPPUNIT_ASSERT( str.find( Integer::toString( testBuffer1->capacity() ) ) != string::npos );
+}

Added: activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/DoubleArrayBufferTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/DoubleArrayBufferTest.h?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/DoubleArrayBufferTest.h (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/DoubleArrayBufferTest.h Tue Apr 29 13:52:30 2008
@@ -0,0 +1,165 @@
+/*
+ * 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.
+ */
+
+#ifndef _DECAF_INTERNAL_NIO_DOUBLEARRAYBUFFERTEST_H_
+#define _DECAF_INTERNAL_NIO_DOUBLEARRAYBUFFERTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <decaf/nio/DoubleBuffer.h>
+
+namespace decaf{
+namespace internal{
+namespace nio{
+
+    class DoubleArrayBufferTest : public CppUnit::TestFixture {
+
+        CPPUNIT_TEST_SUITE( DoubleArrayBufferTest );
+        CPPUNIT_TEST( test );
+        CPPUNIT_TEST( testArray );
+        CPPUNIT_TEST( testArrayOffset );
+        CPPUNIT_TEST( testReadOnlyArray );
+        CPPUNIT_TEST( testAsReadOnlyBuffer );
+        CPPUNIT_TEST( testCompact );
+        CPPUNIT_TEST( testCompareTo );
+        CPPUNIT_TEST( testDuplicate );
+        CPPUNIT_TEST( testEquals );
+        CPPUNIT_TEST( testHasArray );
+        CPPUNIT_TEST( testGet );
+        CPPUNIT_TEST( testGet2 );
+        CPPUNIT_TEST( testGetDoubleArray );
+        CPPUNIT_TEST( testGetDoubleArray2 );
+        CPPUNIT_TEST( testGetWithIndex );
+        CPPUNIT_TEST( testPutDouble );
+        CPPUNIT_TEST( testPutDoubleArray );
+        CPPUNIT_TEST( testPutDoubleArray2 );
+        CPPUNIT_TEST( testPutDoubleBuffer );
+        CPPUNIT_TEST( testPutIndexed );
+        CPPUNIT_TEST( testSlice );
+        CPPUNIT_TEST( testToString );
+        CPPUNIT_TEST_SUITE_END();
+
+        decaf::nio::DoubleBuffer* testBuffer1;
+        double* testData1;
+
+        static const std::size_t testData1Size = 100;
+        static const std::size_t SMALL_TEST_LENGTH = 5;
+        static const std::size_t BUFFER_LENGTH = 250;
+
+    public:
+
+        DoubleArrayBufferTest() {}
+        virtual ~DoubleArrayBufferTest() {}
+
+        void setUp() {
+            testBuffer1 = decaf::nio::DoubleBuffer::allocate( testData1Size );
+
+            testData1 = new double[testData1Size];
+            for( std::size_t i = 0; i < testData1Size; ++i ){
+                testData1[i] = (double)i;
+            }
+        }
+
+        void tearDown() {
+            delete testBuffer1;
+            delete [] testData1;
+        }
+
+        void test();
+        void testArray();
+        void testArrayOffset();
+        void testReadOnlyArray();
+        void testAsReadOnlyBuffer();
+        void testCompact();
+        void testCompareTo();
+        void testDuplicate();
+        void testEquals();
+        void testHasArray();
+        void testGet();
+        void testGet2();
+        void testGetDoubleArray();
+        void testGetDoubleArray2();
+        void testGetWithIndex();
+        void testPutDouble();
+        void testPutDoubleArray();
+        void testPutDoubleArray2();
+        void testPutDoubleBuffer();
+        void testPutIndexed();
+        void testSlice();
+        void testToString();
+
+    protected:
+
+        void loadTestData1( double* array, std::size_t offset, std::size_t length ) {
+            for( std::size_t i = 0; i < length; i++ ) {
+                array[offset + i] = (double)i;
+            }
+        }
+
+        void loadTestData2( double* array, std::size_t offset, std::size_t length ) {
+            for( std::size_t i = 0; i < length; i++ ) {
+                array[offset + i] = (double)length - i;
+            }
+        }
+
+        void loadTestData1( decaf::nio::DoubleBuffer* buf ) {
+            buf->clear();
+            for( std::size_t i = 0; i < buf->capacity(); i++ ) {
+                buf->put( i, (double)i );
+            }
+        }
+
+        void loadTestData2( decaf::nio::DoubleBuffer* buf ) {
+            buf->clear();
+            for( std::size_t i = 0; i < buf->capacity(); i++ ) {
+                buf->put(i, (double) buf->capacity() - i);
+            }
+        }
+
+        void assertContentEquals( decaf::nio::DoubleBuffer* buf, double* array,
+                                  std::size_t offset, std::size_t length) {
+
+            for( std::size_t i = 0; i < length; i++ ) {
+                CPPUNIT_ASSERT( buf->get(i) == array[offset + i] );
+            }
+        }
+
+        void assertContentEquals( decaf::nio::DoubleBuffer* buf,
+                                  decaf::nio::DoubleBuffer* other ) {
+            CPPUNIT_ASSERT( buf->capacity() == other->capacity() );
+            for( std::size_t i = 0; i < buf->capacity(); i++ ) {
+                CPPUNIT_ASSERT(buf->get(i) == other->get(i) );
+            }
+        }
+
+        void assertContentLikeTestData1(
+            decaf::nio::DoubleBuffer* buf, std::size_t startIndex,
+            double startValue, std::size_t length ) {
+
+            double value = startValue;
+            for( std::size_t i = 0; i < length; i++ ) {
+                CPPUNIT_ASSERT( buf->get( startIndex + i ) == value );
+                value = value + 1.0;
+            }
+        }
+
+    };
+
+}}}
+
+#endif /*_DECAF_INTERNAL_NIO_DOUBLEARRAYBUFFERTEST_H_*/

Added: activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/FloatArrayBufferTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/FloatArrayBufferTest.cpp?rev=652104&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/FloatArrayBufferTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/internal/nio/FloatArrayBufferTest.cpp Tue Apr 29 13:52:30 2008
@@ -0,0 +1,559 @@
+/*
+ * 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 "FloatArrayBufferTest.h"
+#include <decaf/lang/Integer.h>
+#include <decaf/lang/Double.h>
+#include <decaf/lang/Float.h>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::nio;
+using namespace decaf::internal::nio;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::test() {
+
+    // Check that we have setup the array and our initial assumptions on state
+    // are correct.  This is the first test run.
+    CPPUNIT_ASSERT( testBuffer1 != NULL );
+    CPPUNIT_ASSERT( testBuffer1->capacity() == testData1Size );
+    CPPUNIT_ASSERT( testBuffer1->hasRemaining() == true );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == false );
+    CPPUNIT_ASSERT( testBuffer1->toString() != "" );
+    CPPUNIT_ASSERT( testBuffer1->hasArray() == true );
+    CPPUNIT_ASSERT( testBuffer1->array() != NULL );
+    CPPUNIT_ASSERT( testBuffer1->arrayOffset() == 0 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testArray() {
+
+    float* array = testBuffer1->array();
+
+    testBuffer1->put( 0, 10.0 );
+    CPPUNIT_ASSERT( array[0] == 10.0 );
+
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+
+    loadTestData1( array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity()) ;
+
+    loadTestData2( array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+
+    loadTestData1( testBuffer1 );
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+
+    loadTestData2( testBuffer1 );
+    assertContentEquals(
+        testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testArrayOffset() {
+
+    float* array = testBuffer1->array();
+
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+
+    loadTestData1(array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+
+    loadTestData2(array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+
+    loadTestData1(testBuffer1);
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+
+    loadTestData2(testBuffer1);
+    assertContentEquals(testBuffer1, array, testBuffer1->arrayOffset(), testBuffer1->capacity());
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testReadOnlyArray() {
+
+    FloatBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+
+    CPPUNIT_ASSERT( readOnly != NULL );
+    CPPUNIT_ASSERT( readOnly->isReadOnly() == true );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw UnsupportedOperationException",
+        readOnly->array(),
+        UnsupportedOperationException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw UnsupportedOperationException",
+        readOnly->arrayOffset(),
+        UnsupportedOperationException );
+
+    delete readOnly;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testAsReadOnlyBuffer() {
+
+    testBuffer1->clear();
+    testBuffer1->mark();
+    testBuffer1->position( testBuffer1->limit() );
+
+    // readonly's contents should be the same as testBuffer1
+    FloatBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    CPPUNIT_ASSERT( testBuffer1 != readOnly );
+    CPPUNIT_ASSERT( readOnly->isReadOnly() );
+    CPPUNIT_ASSERT( testBuffer1->position() == readOnly->position() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == readOnly->limit() );
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); ++i ) {
+        CPPUNIT_ASSERT( testBuffer1->get( i ) == readOnly->get( i ) );
+    }
+
+    // readOnly's position, mark, and limit should be independent to testBuffer1
+    readOnly->reset();
+    CPPUNIT_ASSERT( readOnly->position() == 0 );
+    readOnly->clear();
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->limit() );
+    testBuffer1->reset();
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    delete readOnly;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testCompact() {
+
+    loadTestData1( testBuffer1 );
+
+    // case: buffer is full
+    testBuffer1->clear();
+    testBuffer1->mark();
+
+    FloatBuffer& ret = testBuffer1->compact();
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    assertContentLikeTestData1( testBuffer1, 0, 0.0, testBuffer1->capacity() );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw InvalidMarkException",
+        testBuffer1->reset(),
+        InvalidMarkException );
+
+    // case: buffer is empty
+    testBuffer1->position(0);
+    testBuffer1->limit(0);
+    testBuffer1->mark();
+    ret = testBuffer1->compact();
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    assertContentLikeTestData1(testBuffer1, 0, 0.0, testBuffer1->capacity());
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw InvalidMarkException",
+        testBuffer1->reset(),
+        InvalidMarkException );
+
+    // case: normal
+    CPPUNIT_ASSERT( testBuffer1->capacity() > 5 );
+
+    testBuffer1->position(1);
+    testBuffer1->limit(5);
+    testBuffer1->mark();
+    ret = testBuffer1->compact();
+    CPPUNIT_ASSERT( &ret == testBuffer1);
+    CPPUNIT_ASSERT( testBuffer1->position() == 4 );
+    CPPUNIT_ASSERT( testBuffer1->limit() == testBuffer1->capacity() );
+
+    assertContentLikeTestData1(testBuffer1, 0, 1.0, 4);
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw InvalidMarkException",
+        testBuffer1->reset(),
+        InvalidMarkException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testCompareTo() {
+
+    FloatBuffer* other = FloatBuffer::allocate( testBuffer1->capacity() );
+
+    loadTestData1(testBuffer1);
+    loadTestData1(other);
+
+    CPPUNIT_ASSERT( 0 == testBuffer1->compareTo( *other ) );
+    CPPUNIT_ASSERT( 0 == other->compareTo( *testBuffer1 ) );
+    testBuffer1->position(1);
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) > 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) < 0 );
+    other->position( 2 );
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) < 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) > 0 );
+    testBuffer1->position(2);
+    other->limit(5);
+    CPPUNIT_ASSERT( testBuffer1->compareTo( *other ) > 0 );
+    CPPUNIT_ASSERT( other->compareTo( *testBuffer1 ) < 0 );
+
+    std::vector<float> array1( 1, Float::NaN );
+    std::vector<float> array2( 1, Float::NaN );
+    std::vector<float> array3( 1, 42.0 );
+
+    FloatBuffer* dbuffer1 = FloatBuffer::wrap( array1 );
+    FloatBuffer* dbuffer2 = FloatBuffer::wrap( array2 );
+    FloatBuffer* dbuffer3 = FloatBuffer::wrap( array3 );
+
+    CPPUNIT_ASSERT_MESSAGE(
+        "Failed equal comparison with NaN entry",
+        dbuffer1->compareTo( *dbuffer2 ) );
+    CPPUNIT_ASSERT_MESSAGE(
+        "Failed greater than comparison with NaN entry",
+        dbuffer3->compareTo( *dbuffer1 ) );
+    CPPUNIT_ASSERT_MESSAGE(
+        "Failed greater than comparison with NaN entry",
+        dbuffer1->compareTo( *dbuffer3 ) );
+
+    delete other;
+    delete dbuffer1;
+    delete dbuffer2;
+    delete dbuffer3;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testDuplicate() {
+    testBuffer1->clear();
+    testBuffer1->mark();
+    testBuffer1->position(testBuffer1->limit());
+
+    // duplicate's contents should be the same as testBuffer1
+    FloatBuffer* duplicate = testBuffer1->duplicate();
+    CPPUNIT_ASSERT( testBuffer1 != duplicate );
+    CPPUNIT_ASSERT( testBuffer1->position() == duplicate->position() );
+    CPPUNIT_ASSERT( testBuffer1->limit() == duplicate->limit() );
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == duplicate->isReadOnly() );
+    assertContentEquals( testBuffer1, duplicate );
+
+    // duplicate's position, mark, and limit should be independent to testBuffer1
+    duplicate->reset();
+    CPPUNIT_ASSERT( duplicate->position() == 0 );
+    duplicate->clear();
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->limit() );
+    testBuffer1->reset();
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    delete duplicate;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testEquals() {
+
+    // equal to self
+    CPPUNIT_ASSERT( testBuffer1->equals( *testBuffer1 ) );
+    FloatBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    CPPUNIT_ASSERT( testBuffer1->equals( *readOnly ) );
+    FloatBuffer* duplicate = testBuffer1->duplicate();
+    CPPUNIT_ASSERT( testBuffer1->equals( *duplicate ) );
+
+    CPPUNIT_ASSERT( testBuffer1->capacity() > 5 );
+
+    testBuffer1->limit( testBuffer1->capacity() ).position(0);
+    readOnly->limit( readOnly->capacity() ).position( 1 );
+    CPPUNIT_ASSERT( !testBuffer1->equals( *readOnly ) );
+
+    testBuffer1->limit( testBuffer1->capacity() - 1).position(0);
+    duplicate->limit( duplicate->capacity() ).position( 0 );
+    CPPUNIT_ASSERT( !testBuffer1->equals( *duplicate ) );
+
+    delete readOnly;
+    delete duplicate;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testGet() {
+
+    testBuffer1->clear();
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        CPPUNIT_ASSERT( testBuffer1->get() == testBuffer1->get(i) );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferUnderflowException",
+        testBuffer1->get(),
+        BufferUnderflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testGetFloatArray() {
+
+    std::vector<float> array(1);
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        FloatBuffer& ret = testBuffer1->get( array );
+        CPPUNIT_ASSERT( array[0] == testBuffer1->get(i) );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferUnderflowException",
+        testBuffer1->get( array ),
+        BufferUnderflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testGetFloatArray2() {
+
+    testBuffer1->clear();
+    float* array = new float[testBuffer1->capacity()];
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferUnderflowException",
+        testBuffer1->get( array, 0, testBuffer1->capacity() + 1 ),
+        BufferUnderflowException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    testBuffer1->get( array, 10, 0 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferUnderflowException",
+        testBuffer1->get( array, 1, Integer::MAX_VALUE ),
+        BufferUnderflowException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    testBuffer1->clear();
+    FloatBuffer& ret = testBuffer1->get( array, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    assertContentEquals( testBuffer1, array, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+
+    delete [] array;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testGet2() {
+
+    testBuffer1->clear();
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        CPPUNIT_ASSERT( testBuffer1->get() == testBuffer1->get(i) );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw IndexOutOfBoundsException",
+        testBuffer1->get( testBuffer1->limit() ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testHasArray() {
+    CPPUNIT_ASSERT( testBuffer1->hasArray() );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testPutFloat() {
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        FloatBuffer& ret = testBuffer1->put( (float)i );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (float)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferOverflowException",
+        testBuffer1->put( 0 ),
+        BufferOverflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testPutFloatArray() {
+
+    float* array = new float[1];
+
+    testBuffer1->clear();
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == i );
+        array[0] = (float) i;
+        FloatBuffer& ret = testBuffer1->put( array, 0, 1 );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (float)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferOverflowException",
+        testBuffer1->put( array, 0, 1 ),
+        BufferOverflowException );
+
+    delete [] array;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testPutFloatArray2() {
+
+    testBuffer1->clear();
+    float* array1 = new float[ testBuffer1->capacity() ];
+    float* array2 = new float[ testBuffer1->capacity() + 1 ];
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferOverflowException",
+        testBuffer1->put( array2, 0, testBuffer1->capacity() + 1 ),
+        BufferOverflowException );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    testBuffer1->put( array1, testBuffer1->capacity() + 1, 0 );
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+
+    loadTestData2( array1, 0, testBuffer1->capacity() );
+    FloatBuffer& ret = testBuffer1->put( array1, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    assertContentEquals( testBuffer1, array1, 0, testBuffer1->capacity() );
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+
+    delete [] array1;
+    delete [] array2;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testPutFloatBuffer() {
+
+    FloatBuffer* other = FloatBuffer::allocate( testBuffer1->capacity() );
+    FloatBuffer* other1 = FloatBuffer::allocate( testBuffer1->capacity() + 1 );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw IllegalArgumentException",
+        testBuffer1->put( *testBuffer1 ),
+        IllegalArgumentException );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw BufferOverflowException",
+        testBuffer1->put( *other1 ),
+        BufferOverflowException );
+
+    loadTestData2(other);
+    other->clear();
+    testBuffer1->clear();
+    FloatBuffer& ret = testBuffer1->put( *other );
+
+    CPPUNIT_ASSERT( other->position() == other->capacity() );
+    CPPUNIT_ASSERT( testBuffer1->position() == testBuffer1->capacity() );
+    assertContentEquals( other, testBuffer1 );
+    CPPUNIT_ASSERT( &ret == testBuffer1 );
+
+    delete other;
+    delete other1;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testGetWithIndex() {
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+        FloatBuffer& ret = testBuffer1->put( i, (float)i );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == (float)i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw IndexOutOfBoundsException",
+        testBuffer1->put( testBuffer1->limit(), 0 ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testPutIndexed() {
+
+    FloatBuffer* readOnly = testBuffer1->asReadOnlyBuffer();
+    readOnly->clear();
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a ReadOnlyBufferException",
+        readOnly->put( 0, 0 ),
+        ReadOnlyBufferException );
+    delete readOnly;
+
+    testBuffer1->clear();
+
+    for( std::size_t i = 0; i < testBuffer1->capacity(); i++ ) {
+        CPPUNIT_ASSERT( testBuffer1->position() == 0 );
+        FloatBuffer& ret = testBuffer1->put(i, (float)i );
+        CPPUNIT_ASSERT( testBuffer1->get(i) == i );
+        CPPUNIT_ASSERT( &ret == testBuffer1 );
+    }
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw a IndexOutOfBoundsException",
+        testBuffer1->put( testBuffer1->limit(), 0 ),
+        IndexOutOfBoundsException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testSlice() {
+
+    CPPUNIT_ASSERT( testBuffer1->capacity() > 5 );
+    testBuffer1->position(1);
+    testBuffer1->limit(testBuffer1->capacity() - 1);
+
+    FloatBuffer* slice = testBuffer1->slice();
+    CPPUNIT_ASSERT( testBuffer1->isReadOnly() == slice->isReadOnly() );
+    CPPUNIT_ASSERT( slice->position() == 0 );
+    CPPUNIT_ASSERT( slice->limit() == testBuffer1->remaining() );
+    CPPUNIT_ASSERT( slice->capacity() == testBuffer1->remaining() );
+
+    CPPUNIT_ASSERT_THROW_MESSAGE(
+        "Should throw InvalidMarkException",
+        slice->reset(),
+        InvalidMarkException );
+
+    // slice share the same content with testBuffer1
+    // FIXME:
+    loadTestData1(slice);
+    assertContentLikeTestData1(testBuffer1, 1, 0, slice->capacity());
+    testBuffer1->put( 2, 500 );
+    CPPUNIT_ASSERT( slice->get(1) == 500 );
+
+    delete slice;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+void FloatArrayBufferTest::testToString() {
+
+    std::string str = testBuffer1->toString();
+    CPPUNIT_ASSERT( str.find("Float") != string::npos );
+    CPPUNIT_ASSERT( str.find( Integer::toString( testBuffer1->position() ) ) != string::npos );
+    CPPUNIT_ASSERT( str.find( Integer::toString( testBuffer1->limit() ) ) != string::npos );
+    CPPUNIT_ASSERT( str.find( Integer::toString( testBuffer1->capacity() ) ) != string::npos );
+}



Mime
View raw message