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