Refactored BWidthBuffer test into something more useful.

Looks like the class has some problems (hangs while calculating width of 
lines inside termcap, cf bug #1690


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24070 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2008-02-22 22:30:34 +00:00
parent 45ff9decad
commit 8dd5c3f974
6 changed files with 96 additions and 129 deletions

View File

@ -11,7 +11,6 @@ SEARCH_SOURCE += [ FDirName $(SUBDIR) bbitmap ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) bdeskbar ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) bpolygon ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) bregion ] ;
SEARCH_SOURCE += [ FDirName $(SUBDIR) bwidthbuffer ] ;
SEARCH_SOURCE += [ FDirName $(TOP) src kits interface ] ;
@ -48,11 +47,7 @@ UnitTestLib libinterfacetest.so
RegionInclude.cpp
RegionIntersect.cpp
RegionOffsetBy.cpp
#_BWidthBuffer_
WidthBufferTest.cpp
WidthBufferSimpleTest.cpp
: be $(TARGET_LIBSTDC++)
;
@ -128,6 +123,12 @@ Application MenuTriggerTest :
: be
;
SimpleTest WidthBufferTest :
WidthBufferTest.cpp
: be
;
SEARCH on [ FGristFiles
ScrollView.cpp CheckBox.cpp ChannelSlider.cpp ChannelControl.cpp Slider.cpp Control.cpp
] = [ FDirName $(HAIKU_TOP) src kits interface ] ;

View File

@ -0,0 +1,89 @@
/*
** Copyright 2008, Stefano Ceccherini (stefano.ceccherini@gmail.com).
** All rights reserved.
** Distributed under the terms of the Haiku License.
*/
#include <Application.h>
#include <Font.h>
#include <stdio.h>
#include <stdlib.h>
#include <WidthBuffer.h>
class App : public BApplication {
public:
App();
~App();
virtual void ReadyToRun();
private:
_BWidthBuffer_ *fWidthBuffer;
thread_id fThread;
int32 TesterFunc();
static int32 _thread(void *data);
};
int main()
{
App app;
app.Run();
return 0;
}
App::App()
:BApplication("application/x-vnd-WidthBufferTest")
{
fWidthBuffer = new _BWidthBuffer_;
fThread = spawn_thread(App::_thread, "widthbuffer tester",
B_NORMAL_PRIORITY, this);
}
void
App::ReadyToRun()
{
resume_thread(fThread);
}
App::~App()
{
status_t status;
wait_for_thread(fThread, &status);
delete fWidthBuffer;
}
int32
App::TesterFunc()
{
FILE *file = fopen("/boot/beos/etc/termcap", "r");
if (file != NULL) {
char buffer[512];
while (fgets(buffer, 512, file)) {
float width = fWidthBuffer->StringWidth(buffer, 0,
strlen(buffer), be_plain_font);
printf("string: %s, width: %f\n", buffer, width);
}
fclose(file);
}
PostMessage(B_QUIT_REQUESTED);
return 0;
}
int32
App::_thread(void *data)
{
return static_cast<App *>(data)->TesterFunc();
}

View File

@ -1,82 +0,0 @@
#include "WidthBufferSimpleTest.h"
#include "cppunit/TestCaller.h"
#include <Application.h>
#include <Font.h>
#include <stdio.h>
// Needed because it isn't available in beos headers
double round(float n)
{
int32 tmp = (int32)floor(n + 0.5);
return (double)tmp;
}
WidthBufferSimpleTest::WidthBufferSimpleTest(std::string name) :
BTestCase(name)
{
fWidthBuffer = new _BWidthBuffer_;
}
WidthBufferSimpleTest::~WidthBufferSimpleTest()
{
delete fWidthBuffer;
}
void
WidthBufferSimpleTest::PerformTest(void)
{
const char *str = "Something";
const char *str2 = "Some Text";
const char *longString = "ABCDEFGHIJKLMNOPQRSTUVYXWZ abcdefghijklmnopqrstuvwyxz";
// Some charachters which takes more than one byte
const char *str3 = "30u8g 2342 42nfrç°S£^GF°W§|£^?£ç$£!1ààà'ì";
const BFont *font = be_plain_font;
// We use round() here because the width returned by
// _BWidthBuffer_::StringWidth() isn't _exactly_ the same
// as the one returned by BFont::StringWidth();
float width = fWidthBuffer->StringWidth(str, 0, strlen(str), font);
CPPUNIT_ASSERT(round(width) == font->StringWidth(str));
width = fWidthBuffer->StringWidth(str2, 0, strlen(str2), font);
CPPUNIT_ASSERT(round(width) == font->StringWidth(str2));
width = fWidthBuffer->StringWidth(longString, 0, strlen(longString), font);
CPPUNIT_ASSERT(round(width) == font->StringWidth(longString));
// pass the first string again, it should be completely cached
// in _BWidthBuffer_'s hash table
width = fWidthBuffer->StringWidth(str, 0, strlen(str), font);
CPPUNIT_ASSERT(round(width) == font->StringWidth(str));
width = fWidthBuffer->StringWidth(str3, 0, strlen(str3), font);
CPPUNIT_ASSERT(round(width) == font->StringWidth(str3));
// choose a different font and ask the same strings as before
font = be_fixed_font;
width = fWidthBuffer->StringWidth(longString, 0, strlen(longString), font);
CPPUNIT_ASSERT(round(width) == font->StringWidth(longString));
width = fWidthBuffer->StringWidth(str, 0, strlen(str), font);
CPPUNIT_ASSERT(round(width) == font->StringWidth(str));
width = fWidthBuffer->StringWidth(str2, 0, strlen(str2), font);
CPPUNIT_ASSERT(round(width) == font->StringWidth(str2));
}
CppUnit::Test *WidthBufferSimpleTest::suite(void)
{
typedef CppUnit::TestCaller<WidthBufferSimpleTest>
WidthBufferSimpleTestCaller;
return(new WidthBufferSimpleTestCaller("_BWidthBuffer_::Simple Test", &WidthBufferSimpleTest::PerformTest));
}

View File

@ -1,19 +0,0 @@
#ifndef __WidthBufferSIMPLETest_H
#define __WidthBufferSIMPLETest_H
#include "TestCase.h"
#include <WidthBuffer.h>
class WidthBufferSimpleTest : public BTestCase
{
public:
static Test *suite(void);
void PerformTest(void);
WidthBufferSimpleTest(std::string name = "");
virtual ~WidthBufferSimpleTest();
private:
_BWidthBuffer_ *fWidthBuffer;
};
#endif

View File

@ -1,14 +0,0 @@
#include "cppunit/Test.h"
#include "cppunit/TestSuite.h"
#include "WidthBufferTest.h"
#include "WidthBufferSimpleTest.h"
CppUnit::Test *WidthBufferTestSuite()
{
CppUnit::TestSuite *testSuite = new CppUnit::TestSuite();
testSuite->addTest(WidthBufferSimpleTest::suite());
return testSuite;
}

View File

@ -1,8 +0,0 @@
#ifndef __WIDTHBUFFER_TEST_H
#define __WIDTHBUFFER_TEST_H
class CppUnit::Test;
CppUnit::Test *WidthBufferTestSuite();
#endif __WIDTHBUFFER_TEST_H