#include "BitmapStreamTest.h"
#include <TranslatorRoster.h>
#include <Application.h>
#include <Bitmap.h>
#include <stdio.h>
#include <string.h>
#include <cppunit/Test.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
* Default constructor - no work
*/
BitmapStreamTest::BitmapStreamTest(std::string name)
: BTestCase(name)
{
}
* Default destructor - no work
*/
BitmapStreamTest::~BitmapStreamTest()
{
}
CppUnit::Test *
BitmapStreamTest::Suite()
{
CppUnit::TestSuite *suite = new CppUnit::TestSuite("BitmapStream");
typedef CppUnit::TestCaller<BitmapStreamTest> TC;
suite->addTest(new TC("BitmapStreamTest::Constructor Test",
&BitmapStreamTest::ConstructorTest));
suite->addTest(new TC("BitmapStreamTest::DetachBitmap Test",
&BitmapStreamTest::DetachBitmapTest));
suite->addTest(new TC("BitmapStreamTest::Seek Test",
&BitmapStreamTest::SeekTest));
suite->addTest(new TC("BitmapStreamTest::SetSize Test",
&BitmapStreamTest::SetSizeTest));
suite->addTest(new TC("BitmapStreamTest::ReadWrite Test",
&BitmapStreamTest::ReadWriteTest));
return suite;
}
* Tests:
* BBitmapStream(BBitmap *map = NULL)
*/
void
BitmapStreamTest::ConstructorTest()
{
BApplication
app("application/x-vnd.Haiku-translationkit_bitmapstreamtest");
NextSubTest();
BBitmapStream streamEmpty;
BBitmap *pbits = NULL;
CPPUNIT_ASSERT(streamEmpty.Position() == 0);
CPPUNIT_ASSERT(streamEmpty.Size() == 0);
CPPUNIT_ASSERT(streamEmpty.DetachBitmap(&pbits) == B_ERROR);
CPPUNIT_ASSERT(pbits == NULL);
NextSubTest();
pbits = new BBitmap(BRect(0,0,5,5), B_RGB32);
CPPUNIT_ASSERT(pbits);
BBitmapStream *pstreamWithBits;
pstreamWithBits = new BBitmapStream(pbits);
CPPUNIT_ASSERT(pstreamWithBits);
CPPUNIT_ASSERT(pstreamWithBits->Position() == 0);
CPPUNIT_ASSERT(pstreamWithBits->Size() == 176);
BBitmap *poutbits = NULL;
CPPUNIT_ASSERT(pstreamWithBits->DetachBitmap(&poutbits) == B_OK);
CPPUNIT_ASSERT(pbits == poutbits);
delete pstreamWithBits;
pstreamWithBits = NULL;
delete pbits;
pbits = NULL;
}
* Tests:
* status_t DetachBitmap(BBitmap **outMap)
*/
void
BitmapStreamTest::DetachBitmapTest()
{
BApplication
app("application/x-vnd.Haiku-translationkit_bitmapstreamtest");
NextSubTest();
BFile file("../src/tests/kits/translation/data/images/image.jpg",
B_READ_ONLY);
CPPUNIT_ASSERT(file.InitCheck() == B_OK);
NextSubTest();
BBitmapStream *pstream = new BBitmapStream;
BBitmap *pbits = NULL;
BTranslatorRoster *proster = BTranslatorRoster::Default();
CPPUNIT_ASSERT(proster->Translate(&file, NULL, NULL, pstream,
B_TRANSLATOR_BITMAP) == B_OK);
CPPUNIT_ASSERT(pstream->DetachBitmap(&pbits) == B_NO_ERROR);
CPPUNIT_ASSERT(pbits);
CPPUNIT_ASSERT(pbits->IsValid());
NextSubTest();
CPPUNIT_ASSERT(pstream->DetachBitmap(NULL) == B_BAD_VALUE);
NextSubTest();
BBitmap *pdbits = NULL;
CPPUNIT_ASSERT(pstream->DetachBitmap(&pdbits) == B_ERROR);
NextSubTest();
delete pstream;
pstream = NULL;
CPPUNIT_ASSERT(pbits->IsValid());
CPPUNIT_ASSERT(pbits->BitsLength() > 0);
NextSubTest();
BBitmapStream *pfullstream = new BBitmapStream(pbits);
CPPUNIT_ASSERT(pfullstream->Position() == 0);
CPPUNIT_ASSERT(pfullstream->Size() ==
pbits->BitsLength() + sizeof(TranslatorBitmap));
NextSubTest();
delete pfullstream;
pfullstream = NULL;
CPPUNIT_ASSERT(pbits->BitsLength() == 0);
}
* Tests:
* ssize_t ReadAt(off_t pos, void *buffer, size_t numBytes)
* ssize_t WriteAt(off_t pos, void *buffer, size_t numBytes)
*/
void
BitmapStreamTest::ReadWriteTest()
{
NextSubTest();
BApplication
app("application/x-vnd.Haiku-translationkit_bitmapstreamtest");
char chbuf[sizeof(TranslatorBitmap)],
chheader[sizeof(TranslatorBitmap)], *pch;
TranslatorBitmap sheader;
BBitmapStream stream;
int32 width = 5, height = 5;
sheader.magic = B_TRANSLATOR_BITMAP;
sheader.bounds.left = 0;
sheader.bounds.top = 0;
sheader.bounds.right = width - 1;
sheader.bounds.bottom = height - 1;
sheader.rowBytes = width * 4;
sheader.colors = B_RGB32;
sheader.dataSize = sheader.rowBytes * height;
memcpy(&chheader, &sheader, sizeof(TranslatorBitmap));
CPPUNIT_ASSERT(swap_data(B_UINT32_TYPE, &(chheader[0]),
sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN) == B_OK);
NextSubTest();
off_t nPos;
for (nPos = 0; nPos < sizeof(TranslatorBitmap); nPos++) {
pch = (char *)(&(chheader[0])) + nPos;
CPPUNIT_ASSERT(stream.WriteAt(nPos, pch, 1) == 1);
}
NextSubTest();
CPPUNIT_ASSERT(stream.Size() ==
sizeof(TranslatorBitmap) + sheader.dataSize);
NextSubTest();
for (nPos = 0; nPos < sizeof(TranslatorBitmap); nPos++) {
pch = (char *)(&(chheader[0])) + nPos;
CPPUNIT_ASSERT(stream.ReadAt(nPos, &(chbuf[0]), 1) == 1);
CPPUNIT_ASSERT(pch[0] == chbuf[0]);
}
NextSubTest();
pch = (char *)(&(chheader[0]));
CPPUNIT_ASSERT(stream.WriteAt(0, pch,
sizeof(TranslatorBitmap)) == sizeof(TranslatorBitmap));
NextSubTest();
CPPUNIT_ASSERT(stream.Size() ==
sizeof(TranslatorBitmap) + sheader.dataSize);
NextSubTest();
CPPUNIT_ASSERT(stream.ReadAt(0, &(chbuf[0]),
sizeof(TranslatorBitmap)) == sizeof(TranslatorBitmap));
CPPUNIT_ASSERT(memcmp(pch, &(chbuf[0]), sizeof(TranslatorBitmap)) == 0);
NextSubTest();
int32 bytesLeft = sheader.dataSize;
char byt = 0xCF;
nPos = sizeof(TranslatorBitmap);
while (bytesLeft--)
CPPUNIT_ASSERT(stream.WriteAt(nPos++, &byt, 1) == 1);
NextSubTest();
CPPUNIT_ASSERT(stream.Size() ==
sizeof(TranslatorBitmap) + sheader.dataSize);
NextSubTest();
CPPUNIT_ASSERT(stream.ReadAt(stream.Size(), &(chbuf[0]), 0) == 0);
CPPUNIT_ASSERT(stream.ReadAt(sheader.dataSize + 1000, &(chbuf[0]), 0) == 0);
CPPUNIT_ASSERT(stream.ReadAt(-1, &(chbuf[0]), 0) == 0);
NextSubTest();
#if !TEST_R5
bytesLeft = sheader.dataSize;
nPos = sizeof(TranslatorBitmap);
while (bytesLeft--) {
chbuf[0] = 0x99;
ssize_t rd = stream.ReadAt(nPos++, &(chbuf[0]), 1);
CPPUNIT_ASSERT(rd == 1);
CPPUNIT_ASSERT(chbuf[0] == byt);
}
#endif
NextSubTest();
CPPUNIT_ASSERT(stream.WriteAt(0, &byt, 0) == 0);
CPPUNIT_ASSERT(stream.WriteAt(-1, &byt, 1) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.WriteAt(stream.Size(), &byt, 1) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.WriteAt(stream.Size() + 1, &byt, 1) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.WriteAt(0, NULL, 1) == B_BAD_VALUE);
NextSubTest();
CPPUNIT_ASSERT(stream.ReadAt(0, &(chbuf[0]), 0) == 0);
CPPUNIT_ASSERT(stream.ReadAt(-1, &(chbuf[0]), 1) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.ReadAt(stream.Size(), &(chbuf[0]),
1) == B_ERROR);
CPPUNIT_ASSERT(stream.ReadAt(stream.Size() + 1,
&(chbuf[0]), 1) == B_ERROR);
#if !TEST_R5
CPPUNIT_ASSERT(stream.ReadAt(0, NULL, 1) == B_BAD_VALUE);
#endif
}
* Tests:
* off_t Seek(off_t position, uint32 whence)
* off_t Position() const
*/
void
BitmapStreamTest::SeekTest()
{
BApplication
app("application/x-vnd.Haiku-translationkit_bitmapstreamtest");
NextSubTest();
BFile file("../src/tests/kits/translation/data/images/image.jpg",
B_READ_ONLY);
CPPUNIT_ASSERT(file.InitCheck() == B_OK);
NextSubTest();
BBitmapStream stream;
BTranslatorRoster *proster = BTranslatorRoster::Default();
CPPUNIT_ASSERT(proster->Translate(&file, NULL, NULL, &stream,
B_TRANSLATOR_BITMAP) == B_OK);
NextSubTest();
off_t nPos;
nPos = stream.Size();
CPPUNIT_ASSERT(stream.Seek(0, SEEK_END) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
nPos = 0;
CPPUNIT_ASSERT(stream.Seek(-stream.Size(), SEEK_END) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
nPos = stream.Size() - 15;
CPPUNIT_ASSERT(stream.Seek(-15, SEEK_END) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
CPPUNIT_ASSERT(stream.Seek(1, SEEK_END) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.Position() == nPos);
CPPUNIT_ASSERT(stream.Seek(-(stream.Size() + 1),
SEEK_END) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.Position() == nPos);
NextSubTest();
nPos = 0;
CPPUNIT_ASSERT(stream.Seek(0, SEEK_SET) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
nPos = stream.Size();
CPPUNIT_ASSERT(stream.Seek(nPos, SEEK_SET) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
nPos /= 2;
CPPUNIT_ASSERT(stream.Seek(nPos, SEEK_SET) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
CPPUNIT_ASSERT(stream.Seek(-1, SEEK_SET) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.Position() == nPos);
CPPUNIT_ASSERT(stream.Seek(stream.Size() + 1, SEEK_SET) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.Position() == nPos);
NextSubTest();
CPPUNIT_ASSERT(stream.Seek(-nPos, SEEK_CUR) == 0);
CPPUNIT_ASSERT(stream.Position() == 0);
nPos = stream.Size();
CPPUNIT_ASSERT(stream.Seek(nPos, SEEK_CUR) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
nPos -= 11;
CPPUNIT_ASSERT(stream.Seek(-11, SEEK_CUR) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
nPos += 8;
CPPUNIT_ASSERT(stream.Seek(8, SEEK_CUR) == nPos);
CPPUNIT_ASSERT(stream.Position() == nPos);
CPPUNIT_ASSERT(stream.Seek(-(stream.Position() + 1),
SEEK_CUR) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.Position() == nPos);
CPPUNIT_ASSERT(stream.Seek((stream.Size() - stream.Position()) + 1,
SEEK_CUR) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.Position() == nPos);
}
* Tests:
* status_t SetSize(off_t size) const
*/
void
BitmapStreamTest::SetSizeTest()
{
BApplication
app("application/x-vnd.Haiku-translationkit_bitmapstreamtest");
NextSubTest();
BFile file("../src/tests/kits/translation/data/images/image.jpg",
B_READ_ONLY);
CPPUNIT_ASSERT(file.InitCheck() == B_OK);
NextSubTest();
BBitmapStream stream;
BTranslatorRoster *proster = BTranslatorRoster::Default();
CPPUNIT_ASSERT(proster->Translate(&file, NULL, NULL, &stream,
B_TRANSLATOR_BITMAP) == B_OK);
NextSubTest();
CPPUNIT_ASSERT(stream.SetSize(-1) == B_BAD_VALUE);
CPPUNIT_ASSERT(stream.SetSize(stream.Size() + 1) == B_BAD_VALUE);
}