You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2007/11/24 02:06:33 UTC
svn commit: r597779 - in /activemq/activemq-cpp/decaf/trunk/src:
main/decaf/internal/nio/ test/ test/decaf/internal/nio/
Author: tabish
Date: Fri Nov 23 17:06:32 2007
New Revision: 597779
URL: http://svn.apache.org/viewvc?rev=597779&view=rev
Log:
http://issues.apache.org/activemq/browse/AMQCPP-103
Working on the NIO package
Added:
activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.cpp
activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.h
Modified:
activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.cpp
activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.h
activemq/activemq-cpp/decaf/trunk/src/test/Makefile.am
activemq/activemq-cpp/decaf/trunk/src/test/testRegistry.cpp
Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.cpp?rev=597779&r1=597778&r2=597779&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.cpp Fri Nov 23 17:06:32 2007
@@ -20,11 +20,17 @@
using namespace decaf;
using namespace decaf::internal;
using namespace decaf::internal::nio;
+using namespace decaf::internal::util;
////////////////////////////////////////////////////////////////////////////////
-ByteArrayPerspective::ByteArrayPerspective() {
+ByteArrayPerspective::ByteArrayPerspective( int capacity ) : ByteArray( capacity ){
+ this->references = 0;
}
////////////////////////////////////////////////////////////////////////////////
-ByteArrayPerspective::~ByteArrayPerspective() {
+ByteArrayPerspective::ByteArrayPerspective( unsigned char* array, std::size_t capacity, bool own )
+ throw( lang::exceptions::NullPointerException ) :
+ ByteArray( array, capacity, own ) {
+
+ this->references = 0;
}
Modified: activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.h?rev=597779&r1=597778&r2=597779&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.h (original)
+++ activemq/activemq-cpp/decaf/trunk/src/main/decaf/internal/nio/ByteArrayPerspective.h Fri Nov 23 17:06:32 2007
@@ -24,12 +24,55 @@
namespace internal{
namespace nio{
- class ByteArrayPerspective {
+ /**
+ * This class extends ByteArray to create a reference counted byte array
+ * that can be held and used by several differen ByteBuffers and allow them
+ * to know on destruction whose job it is to delete the perspective.
+ */
+ class ByteArrayPerspective : public util::ByteArray {
private:
+
+ // Count of the number of perspectives into this array.
+ int references;
+
public:
- ByteArrayPerspective();
- virtual ~ByteArrayPerspective();
+ /**
+ * Creates a byte array object that is allocated internally and is then owned
+ * and deleted when this object is deleted. The array is initially created
+ * with all elements initialized to zero.
+ * @param capacity - size of the array, this is the limit we read and write to.
+ */
+ ByteArrayPerspective( int capacity );
+
+ /**
+ * Creates a byte array object that wraps the given array. If the own flag
+ * is set then it will delete this array when this object is deleted.
+ * @param array - array to wrap
+ * @param capacity - size of the array, this is the limit we read and write to.
+ * @param own - is this class now the owner of the pointer.
+ * @throws NullPointerException if buffer is NULL
+ */
+ ByteArrayPerspective( unsigned char* array, std::size_t capacity, bool own = false )
+ throw( lang::exceptions::NullPointerException );
+
+ virtual ~ByteArrayPerspective() {}
+
+ /**
+ * Takes a reference to this Perspective
+ */
+ void takeRef() { this->references++; }
+
+ /**
+ * Returns a reference to this Perspective, when the count is zero
+ * it should be deleted
+ */
+ void returnRef() { this->references--; }
+
+ /**
+ * @returns the current number of reference on this perspective
+ */
+ int getReferences() const { return this->references; }
};
Modified: activemq/activemq-cpp/decaf/trunk/src/test/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/test/Makefile.am?rev=597779&r1=597778&r2=597779&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/decaf/trunk/src/test/Makefile.am Fri Nov 23 17:06:32 2007
@@ -17,6 +17,7 @@
cc_sources = \
decaf/internal/util/ByteArrayTest.cpp \
+ decaf/internal/nio/ByteArrayPerspectiveTest.cpp \
decaf/lang/ByteTest.cpp \
decaf/lang/CharacterTest.cpp \
decaf/lang/BooleanTest.cpp \
@@ -57,6 +58,7 @@
h_sources = \
decaf/internal/util/ByteArrayTest.h \
+ decaf/internal/nio/ByteArrayPerspectiveTest.h \
decaf/lang/ByteTest.h \
decaf/lang/CharacterTest.h \
decaf/lang/BooleanTest.h \
Added: activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.cpp?rev=597779&view=auto
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.cpp (added)
+++ activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.cpp Fri Nov 23 17:06:32 2007
@@ -0,0 +1,265 @@
+/*
+ * 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 "ByteArrayPerspectiveTest.h"
+
+using namespace decaf;
+using namespace decaf::internal;
+using namespace decaf::internal::util;
+using namespace decaf::internal::nio;
+using namespace decaf::nio;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testReferences(){
+
+ ByteArrayPerspective array( (std::size_t)256 );
+ array.takeRef();
+ CPPUNIT_ASSERT( array.getReferences() == 1 );
+ array.takeRef();
+ CPPUNIT_ASSERT( array.getReferences() == 2 );
+ array.returnRef();
+ array.returnRef();
+ CPPUNIT_ASSERT( array.getReferences() == 0 );
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testRead(){
+
+ unsigned char* data = new unsigned char[256];
+ for( int i = 0; i < 256; ++i ) {
+ data[i] = i;
+ }
+
+ ByteArrayPerspective array( data, 256 );
+
+ CPPUNIT_ASSERT( array.getCapacity() == 256 );
+
+ unsigned char* result = new unsigned char[256];
+ array.read( result, 0, 256 );
+
+ for( int i = 0; i < 256; ++i ){
+ CPPUNIT_ASSERT( data[i] == result[i] );
+ }
+
+ delete data;
+ delete result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testWrite(){
+
+ unsigned char* data = new unsigned char[256];
+ for( int i = 0; i < 256; ++i ) {
+ data[i] = i;
+ }
+
+ ByteArrayPerspective array( (std::size_t)256 );
+
+ CPPUNIT_ASSERT( array.getCapacity() == 256 );
+
+ array.write( data, 0, 256 );
+ unsigned char* result = new unsigned char[256];
+ array.read( result, 0, 256 );
+
+ for( int i = 0; i < 256; ++i ){
+ CPPUNIT_ASSERT( data[i] == result[i] );
+ }
+
+ delete data;
+ delete result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testCtor1(){
+
+ ByteArrayPerspective array1( (std::size_t)256 );
+ CPPUNIT_ASSERT( array1.getCapacity() == 256 );
+ ByteArrayPerspective array2( (std::size_t)54 );
+ CPPUNIT_ASSERT( array2.getCapacity() == 54 );
+ ByteArrayPerspective array3( (std::size_t)5555 );
+ CPPUNIT_ASSERT( array3.getCapacity() == 5555 );
+ ByteArrayPerspective array4( (std::size_t)0 );
+ CPPUNIT_ASSERT( array4.getCapacity() == 0 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testCtor2(){
+
+ unsigned char* data1 = new unsigned char[256];
+ unsigned char* data2 = new unsigned char[999];
+ unsigned char* data3 = new unsigned char[12];
+ unsigned char* data4 = new unsigned char[0];
+
+ ByteArrayPerspective array1( data1, 256, true );
+ ByteArrayPerspective array2( data2, 999, true );
+ ByteArrayPerspective array3( data3, 10, true );
+ ByteArrayPerspective array4( data4, 0, true );
+
+ CPPUNIT_ASSERT( array1.getCapacity() == 256 );
+ CPPUNIT_ASSERT( array2.getCapacity() == 999 );
+ CPPUNIT_ASSERT( array3.getCapacity() == 10 );
+ CPPUNIT_ASSERT( array4.getCapacity() == 0 );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testClear(){
+
+ unsigned char* data = new unsigned char[256];
+ for( int i = 0; i < 256; ++i ) {
+ data[i] = i;
+ }
+
+ ByteArrayPerspective array( (std::size_t)256 );
+
+ CPPUNIT_ASSERT( array.getCapacity() == 256 );
+
+ array.write( data, 0, 256 );
+ unsigned char* result = new unsigned char[256];
+ array.read( result, 0, 256 );
+
+ for( int i = 0; i < 256; ++i ){
+ CPPUNIT_ASSERT( data[i] == result[i] );
+ }
+
+ array.clear();
+ array.read( result, 0, 256 );
+
+ for( int i = 0; i < 256; ++i ){
+ CPPUNIT_ASSERT( result[i] == 0 );
+ }
+
+ delete data;
+ delete result;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testReszie(){
+
+ unsigned char* data = new unsigned char[256];
+ for( int i = 0; i < 256; ++i ) {
+ data[i] = i;
+ }
+
+ ByteArrayPerspective array( (std::size_t)256 );
+
+ CPPUNIT_ASSERT( array.getCapacity() == 256 );
+
+ array.write( data, 0, 256 );
+ unsigned char* result = new unsigned char[256];
+ array.read( result, 0, 256 );
+
+ for( int i = 0; i < 256; ++i ){
+ CPPUNIT_ASSERT( data[i] == result[i] );
+ }
+
+ array.resize( 128 );
+ CPPUNIT_ASSERT( array.getCapacity() == 128 );
+ unsigned char* result2 = new unsigned char[128];
+ array.read( result2, 0, 128 );
+
+ for( int i = 0; i < 128; ++i ){
+ CPPUNIT_ASSERT( result[i] == data[i] );
+ }
+
+ delete result;
+ delete result2;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testOperators(){
+
+ unsigned char* data = new unsigned char[256];
+ for( int i = 0; i < 256; ++i ) {
+ data[i] = i;
+ }
+
+ ByteArrayPerspective array( data, 256 );
+
+ CPPUNIT_ASSERT( array.getCapacity() == 256 );
+
+ for( int i = 0; i < 256; ++i ){
+ CPPUNIT_ASSERT( data[i] == array[i] );
+ }
+
+ delete data;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testReadExceptions() {
+
+ unsigned char* data = new unsigned char[256];
+ for( int i = 0; i < 256; ++i ) {
+ data[i] = i;
+ }
+
+ ByteArrayPerspective array( data, 256, true );
+
+ CPPUNIT_ASSERT_THROW_MESSAGE(
+ "Should Throw NullPointerException",
+ array.read( NULL, 0, 500 ),
+ NullPointerException );
+
+ unsigned char result[5000];
+ ByteArrayPerspective array2( 256 );
+
+ CPPUNIT_ASSERT_THROW_MESSAGE(
+ "Should Throw BufferUnderflowException",
+ array.read( result, 0, 500 ),
+ BufferUnderflowException );
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testWriteExceptions() {
+
+ unsigned char* data = new unsigned char[256];
+ for( int i = 0; i < 256; ++i ) {
+ data[i] = i;
+ }
+
+ ByteArrayPerspective array( data, 256, true );
+ CPPUNIT_ASSERT_THROW_MESSAGE(
+ "Should Throw NullPointerException",
+ array.write( NULL, 0, 500 ),
+ NullPointerException );
+
+ unsigned char result[5000];
+ ByteArrayPerspective array2( 256 );
+
+ CPPUNIT_ASSERT_THROW_MESSAGE(
+ "Should Throw BufferOverflowException",
+ array.write( result, 0, 500 ),
+ BufferOverflowException );
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void ByteArrayPerspectiveTest::testOperatorsExceptions() {
+
+ unsigned char* data = new unsigned char[256];
+ for( int i = 0; i < 256; ++i ) {
+ data[i] = i;
+ }
+
+ ByteArrayPerspective array( data, 256, true );
+ CPPUNIT_ASSERT_THROW_MESSAGE(
+ "Should Throw IndexOutOfBoundsException",
+ array[9999],
+ IndexOutOfBoundsException );
+}
Added: activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.h?rev=597779&view=auto
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.h (added)
+++ activemq/activemq-cpp/decaf/trunk/src/test/decaf/internal/nio/ByteArrayPerspectiveTest.h Fri Nov 23 17:06:32 2007
@@ -0,0 +1,68 @@
+/*
+ * 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_BYTEARRAYPERSPECTIVETEST_H_
+#define _DECAF_INTERNAL_NIO_BYTEARRAYPERSPECTIVETEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <decaf/internal/nio/ByteArrayPerspective.h>
+#include <decaf/util/Config.h>
+
+namespace decaf{
+namespace internal{
+namespace nio{
+
+ class ByteArrayPerspectiveTest : public CppUnit::TestFixture {
+
+ CPPUNIT_TEST_SUITE( ByteArrayPerspectiveTest );
+ CPPUNIT_TEST( testReferences );
+ CPPUNIT_TEST( testRead );
+ CPPUNIT_TEST( testReadExceptions );
+ CPPUNIT_TEST( testWrite );
+ CPPUNIT_TEST( testWriteExceptions );
+ CPPUNIT_TEST( testCtor1 );
+ CPPUNIT_TEST( testCtor2 );
+ CPPUNIT_TEST( testClear );
+ CPPUNIT_TEST( testReszie );
+ CPPUNIT_TEST( testOperators );
+ CPPUNIT_TEST( testOperatorsExceptions );
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+
+ ByteArrayPerspectiveTest() {}
+ virtual ~ByteArrayPerspectiveTest() {}
+
+ virtual void testReferences();
+ virtual void testRead();
+ virtual void testWrite();
+ virtual void testCtor1();
+ virtual void testCtor2();
+ virtual void testClear();
+ virtual void testReszie();
+ virtual void testOperators();
+ virtual void testReadExceptions();
+ virtual void testWriteExceptions();
+ virtual void testOperatorsExceptions();
+
+ };
+
+}}}
+
+#endif /*_DECAF_INTERNAL_NIO_BYTEARRAYPERSPECTIVETEST_H_*/
Modified: activemq/activemq-cpp/decaf/trunk/src/test/testRegistry.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/decaf/trunk/src/test/testRegistry.cpp?rev=597779&r1=597778&r2=597779&view=diff
==============================================================================
--- activemq/activemq-cpp/decaf/trunk/src/test/testRegistry.cpp (original)
+++ activemq/activemq-cpp/decaf/trunk/src/test/testRegistry.cpp Fri Nov 23 17:06:32 2007
@@ -20,6 +20,8 @@
#include <decaf/internal/util/ByteArrayTest.h>
CPPUNIT_TEST_SUITE_REGISTRATION( decaf::internal::util::ByteArrayTest );
+#include <decaf/internal/nio/ByteArrayPerspectiveTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION( decaf::internal::nio::ByteArrayPerspectiveTest );
//#include <decaf/io/FilterInputStreamTest.h>
//CPPUNIT_TEST_SUITE_REGISTRATION( decaf::io::FilterInputStreamTest );