01: /*
02: This source file is part of Smyle, a database library.
03: For up-to-date information, see http://www.drjava.de/smyle
04: Copyright (C) 2001 Stefan Reich (doc@drjava.de)
05:
06: This library is free software; you can redistribute it and/or
07: modify it under the terms of the GNU Lesser General Public
08: License as published by the Free Software Foundation; either
09: version 2.1 of the License, or (at your option) any later version.
10:
11: This library is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public
17: License along with this library; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19:
20: For full license text, see doc/license/lgpl.txt in this distribution
21: */
22:
23: package drjava.util.tests;
24:
25: import junit.framework.*;
26: import drjava.util.*;
27:
28: public class BenchmarkTest extends TestCase {
29: public BenchmarkTest(String name) {
30: super (name);
31: }
32:
33: class MockTimer implements Timer {
34: long time = 5; // Choose a different value than 0
35:
36: public double getMilliseconds() {
37: return time;
38: }
39: }
40:
41: MockTimer timer;
42: Benchmark bench;
43:
44: protected void setUp() {
45: timer = new MockTimer();
46: bench = new Benchmark(timer) {
47: int rep;
48:
49: public void action() {
50: spendTime(100 + (rep++));
51: }
52: };
53: }
54:
55: void spendTime(double t) {
56: timer.time += t;
57: }
58:
59: public void testSimpleBench() {
60: bench.run();
61: assertEquals(100f, bench.totalTime(), 0f);
62: }
63:
64: public void testAverage() {
65: bench.setRepetitions(11);
66: bench.run();
67: assertEquals(105f, bench.totalTime(), 0f);
68: }
69:
70: public void testToString() {
71: bench.setDescription("bla");
72: bench.run();
73: assertEquals("bla: 100 ms", bench.toString());
74: }
75:
76: public void testDone() {
77: bench = new Benchmark(timer) {
78: public void action() {
79: spendTime(33);
80: done("part 1");
81: spendTime(55);
82: done("part 2");
83: }
84: };
85: bench.run();
86: assertEquals("total: 88 ms\n" + "-part 1: 33 ms\n"
87: + "-part 2: 55 ms", bench.toString());
88: }
89: }
|