Skip to content

Commit

Permalink
Test more corner cases of byte-array
Browse files Browse the repository at this point in the history
Make allocation algorithm terminate on 64-bit.
  • Loading branch information
ropez committed Mar 1, 2010
1 parent f649ae5 commit 9e2d259
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/pieces/byte_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ ByteArray::Data::~Data()
void ByteArray::Data::allocate(size_t size)
{
// Test for super-large size (2 GB)
if (size & 0x80000000)
if (size > 0x80000000)
throw IOException("ByteArray::Data::allocate", "Size too large");

size_t wanted = 1;
Expand Down
16 changes: 16 additions & 0 deletions src/test/test_byte_array.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "support.h"
#include <Pieces/ByteArray>
#include <Pieces/Exception>

using pcs::ByteArray;

Expand Down Expand Up @@ -302,13 +303,21 @@ CPPUNIT_TEST_SUITE_REGISTRATION(TestByteArrayExtending);
class TestByteArrayResize : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE(TestByteArrayResize);
CPPUNIT_TEST(testTrivialResize);
CPPUNIT_TEST(testIncreaseSize);
CPPUNIT_TEST(testDecreaseSize);
CPPUNIT_TEST(testIncreaseSizeAndKeepContent);
CPPUNIT_TEST(testDecreaseSizeAndKeepContent);
CPPUNIT_TEST(testSuperSize);
CPPUNIT_TEST_SUITE_END();

public:
void testTrivialResize() {
ByteArray ba("foo", 3);
ba.resize(3);
CPPUNIT_ASSERT_EQUAL(ByteArray("foo", 3), ba);
}

void testIncreaseSize() {
ByteArray ba;
ba.resize(10);
Expand Down Expand Up @@ -339,5 +348,12 @@ class TestByteArrayResize : public CppUnit::TestFixture
CPPUNIT_ASSERT_EQUAL(3ul, ba.size());
CPPUNIT_ASSERT_EQUAL(ByteArray("foo", 3), ba);
}

void testSuperSize() {
ByteArray ba;
CPPUNIT_ASSERT_NO_THROW(ba.resize(0x80000000ul));
CPPUNIT_ASSERT_THROW(ba.resize(0x100000000ull), pcs::Exception);
CPPUNIT_ASSERT_THROW(ba.resize(0xffffffff00000000ull), pcs::Exception);
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(TestByteArrayResize);

0 comments on commit 9e2d259

Please sign in to comment.