1/*
2 * Copyright (C) 2008-2009 Stephan A��mus <superstippi@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT license.
4 */
5
6#include "StringTest.h"
7
8#include <stdio.h>
9
10#include <String.h>
11#include <View.h>
12
13#include "TestSupport.h"
14
15
16StringTest::StringTest()
17	: Test(),
18	  fTestDuration(0),
19	  fTestStart(-1),
20	  fGlyphsRendered(0),
21	  fGlyphsPerLine(500),
22	  fIterations(0),
23	  fMaxIterations(1500),
24
25	  fStartHeight(11.0),
26	  fLineHeight(15.0)
27{
28}
29
30
31StringTest::~StringTest()
32{
33}
34
35
36void
37StringTest::Prepare(BView* view)
38{
39//	SetupClipping(view);
40
41	font_height fh;
42	view->GetFontHeight(&fh);
43	fLineHeight = ceilf(fh.ascent) + ceilf(fh.descent)
44		+ ceilf(fh.leading);
45	fStartHeight = ceilf(fh.ascent) + ceilf(fh.descent);
46	fViewBounds = view->Bounds();
47
48	BString string;
49	string.Append('M', fGlyphsPerLine);
50	while (view->StringWidth(string.String()) < fViewBounds.Width() - 10)
51		string.Append('M', 1);
52	while (view->StringWidth(string.String()) > fViewBounds.Width() - 10)
53		string.Remove(string.Length() - 1, 1);
54
55	fGlyphsPerLine = 60; //string.Length();
56	fTestDuration = 0;
57	fGlyphsRendered = 0;
58	fIterations = 0;
59	fTestStart = system_time();
60}
61
62bool
63StringTest::RunIteration(BView* view)
64{
65	BPoint textLocation;
66	textLocation.x = 5;
67	textLocation.y = random_number_between(fStartHeight,
68		fStartHeight + fLineHeight / 2);
69
70	char buffer[fGlyphsPerLine + 1];
71	buffer[fGlyphsPerLine] = 0;
72
73	bigtime_t now = system_time();
74
75	while (true) {
76		// fill string with random chars
77		for (uint32 j = 0; j < fGlyphsPerLine; j++)
78			buffer[j] = 'A' + rand() % ('z' - 'A');
79
80		view->DrawString(buffer, textLocation);
81
82		fGlyphsRendered += fGlyphsPerLine;
83
84		// offset text location
85		textLocation.y += fLineHeight;
86		if (textLocation.y > fViewBounds.bottom)
87			break;
88	}
89
90	view->Sync();
91
92	fTestDuration += system_time() - now;
93	fIterations++;
94
95	return fIterations < fMaxIterations;
96}
97
98
99void
100StringTest::PrintResults(BView* view)
101{
102	if (fTestDuration == 0) {
103		printf("Test was not run.\n");
104		return;
105	}
106	bigtime_t timeLeak = system_time() - fTestStart - fTestDuration;
107
108	Test::PrintResults(view);
109
110	printf("Glyphs per DrawString() call: %ld\n", fGlyphsPerLine);
111	printf("Glyphs per second: %.3f\n",
112		fGlyphsRendered * 1000000.0 / fTestDuration);
113	printf("Average time between iterations: %.4f seconds.\n",
114		(float)timeLeak / fIterations / 1000000);
115}
116
117
118Test*
119StringTest::CreateTest()
120{
121	return new StringTest();
122}
123
124