headers/bsd: Add sys/queue.h.
[haiku.git] / src / tests / servers / app / benchmark / Benchmark.cpp
blobe4807917a6e001bbdcce13570605f13d8e96c219
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 */
6 #include <memory>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include <Application.h>
12 #include <Screen.h>
14 #include "DrawingModeToString.h"
15 #include "TestWindow.h"
17 // tests
18 #include "HorizontalLineTest.h"
19 #include "RandomLineTest.h"
20 #include "StringTest.h"
21 #include "VerticalLineTest.h"
24 struct test_info {
25 const char* name;
26 Test* (*create)();
29 const test_info kTestInfos[] = {
30 { "HorizontalLines", HorizontalLineTest::CreateTest },
31 { "RandomLines", RandomLineTest::CreateTest },
32 { "Strings", StringTest::CreateTest },
33 { "VerticalLines", VerticalLineTest::CreateTest },
34 { NULL, NULL }
38 class Benchmark : public BApplication {
39 public:
40 Benchmark(Test* test, drawing_mode mode, bool clipping)
41 : BApplication("application/x-vnd.haiku-benchmark"),
42 fTest(test),
43 fTestWindow(NULL),
44 fDrawingMode(mode),
45 fUseClipping(clipping)
49 ~Benchmark()
51 delete fTest;
54 virtual void ReadyToRun()
56 uint32 width = 500;
57 uint32 height = 500;
58 BScreen screen;
59 BRect frame = screen.Frame();
60 frame.left = (frame.left + frame.right - width) / 2;
61 frame.top = (frame.top + frame.bottom - width) / 2;
62 frame.right = frame.left + width - 1;
63 frame.bottom = frame.top + height - 1;
65 fTestWindow = new TestWindow(frame, fTest, fDrawingMode,
66 fUseClipping, BMessenger(this));
69 virtual bool QuitRequested()
71 if (fTestWindow != NULL)
72 fTestWindow->SetAllowedToQuit(true);
73 return BApplication::QuitRequested();
76 virtual void MessageReceived(BMessage* message)
78 switch (message->what) {
79 case MSG_TEST_CANCELED:
80 printf("Test canceled early.\n");
81 // fall through
82 case MSG_TEST_FINISHED:
83 fTestWindow->Lock();
84 fTest->PrintResults(fTestWindow->View());
85 fTestWindow->Unlock();
86 PostMessage(B_QUIT_REQUESTED);
87 break;
88 default:
89 BApplication::MessageReceived(message);
90 break;
94 private:
95 Test* fTest;
96 TestWindow* fTestWindow;
97 drawing_mode fDrawingMode;
98 bool fUseClipping;
102 static void
103 print_test_list(bool error)
105 FILE* out = (error ? stderr : stdout);
107 fprintf(out, "available tests:\n");
109 for (int32 i = 0; kTestInfos[i].name; i++)
110 fprintf(out, " %s\n", kTestInfos[i].name);
115 main(int argc, char** argv)
117 // get test name
118 const char* testName;
119 if (argc < 2) {
120 fprintf(stderr, "Usage: %s <test name>\n", argv[0]);
121 print_test_list(true);
122 exit(1);
124 // skip program name
125 argc--;
126 argv++;
128 testName = argv[0];
129 bool clipping = false;
130 drawing_mode mode = B_OP_COPY;
132 while (argc > 0) {
133 drawing_mode possibleMode;
134 if (strcmp(argv[0], "--clipping") == 0 || strcmp(argv[0], "-c") == 0) {
135 clipping = true;
136 } else if (ToDrawingMode(argv[0], possibleMode)) {
137 mode = possibleMode;
139 argc--;
140 argv++;
144 // find and create the test
145 Test* test = NULL;
146 try {
147 for (int32 i = 0; kTestInfos[i].name; i++) {
148 if (strcmp(testName, kTestInfos[i].name) == 0) {
149 test = (kTestInfos[i].create)();
150 break;
153 } catch (std::bad_alloc) {
154 fprintf(stderr, "Insufficient memory to create the test. Sorry.\n");
155 exit(1);
158 if (test == NULL) {
159 fprintf(stderr, "Error: Invalid test name: \"%s\"\n", testName);
160 print_test_list(true);
161 exit(1);
164 Benchmark app(test, mode, clipping);
165 app.Run();
166 return 0;