vfs: check userland buffers before reading them.
[haiku.git] / src / kits / tracker / Tests.cpp
blob35a59dded22404a8b2fdf4d66a0c6d75a5f1bdb7
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
36 #if DEBUG
38 #include "Tests.h"
40 #include <Debug.h>
41 #include <Locker.h>
42 #include <Path.h>
43 #include <String.h>
44 #include <Window.h>
46 #include <directories.h>
48 #include "EntryIterator.h"
49 #include "IconCache.h"
50 #include "Model.h"
51 #include "NodeWalker.h"
52 #include "StopWatch.h"
53 #include "Thread.h"
56 const char* pathsToSearch[] = {
57 // "/boot/home/config/settings/NetPositive/Bookmarks/",
58 kSystemDirectory,
59 kAppsDirectory,
60 kUserDirectory,
65 namespace BTrackerPrivate {
67 class IconSpewer : public SimpleThread {
68 public:
69 IconSpewer(bool newCache = true);
70 ~IconSpewer();
71 void SetTarget(BWindow* target)
72 { this->target = target; }
74 void Quit();
75 void Run();
77 protected:
78 void DrawSomeNew();
79 void DrawSomeOld();
80 const entry_ref* NextRef();
81 private:
82 BLocker locker;
83 bool quitting;
84 BWindow* target;
85 TNodeWalker* walker;
86 CachedEntryIterator* cachingIterator;
87 int32 searchPathIndex;
88 bigtime_t cycleTime;
89 bigtime_t lastCycleLap;
90 int32 numDrawn;
91 BStopWatch watch;
92 bool newCache;
93 BPath currentPath;
95 entry_ref ref;
99 class IconTestWindow : public BWindow {
100 public:
101 IconTestWindow();
102 bool QuitRequested();
103 private:
104 IconSpewer iconSpewer;
107 } // namespace BTrackerPrivate
110 // #pragma mark - IconSpewer
113 IconSpewer::IconSpewer(bool newCache)
115 quitting(false),
116 cachingIterator(0),
117 searchPathIndex(0),
118 cycleTime(0),
119 lastCycleLap(0),
120 numDrawn(0),
121 watch("", true),
122 newCache(newCache)
124 walker = new TNodeWalker(pathsToSearch[searchPathIndex++]);
125 if (newCache)
126 cachingIterator = new CachedEntryIterator(walker, 40);
130 IconSpewer::~IconSpewer()
132 delete walker;
133 delete cachingIterator;
137 void
138 IconSpewer::Run()
140 BStopWatch watch("", true);
141 for (;;) {
142 AutoLock<BLocker> lock(locker);
144 if (!lock || quitting)
145 break;
147 lock.Unlock();
148 if (newCache)
149 DrawSomeNew();
150 else
151 DrawSomeOld();
156 void
157 IconSpewer::Quit()
159 kill_thread(fScanThread);
160 fScanThread = -1;
164 const icon_size kIconSize = B_LARGE_ICON;
165 const int32 kRowCount = 10;
166 const int32 kColumnCount = 10;
169 void
170 IconSpewer::DrawSomeNew()
172 target->Lock();
173 BView* view = target->FindView("iconView");
174 ASSERT(view);
176 BRect bounds(target->Bounds());
177 view->SetHighColor(Color(255, 255, 255));
178 view->FillRect(bounds);
180 view->SetHighColor(Color(0, 0, 0));
181 char buffer[256];
182 if (cycleTime) {
183 sprintf(buffer, "last cycle time %" B_PRId64 " ms", cycleTime/1000);
184 view->DrawString(buffer, BPoint(20, bounds.bottom - 20));
187 if (numDrawn) {
188 sprintf(buffer, "average draw time %" B_PRId64 " us per icon",
189 watch.ElapsedTime() / numDrawn);
190 view->DrawString(buffer, BPoint(20, bounds.bottom - 30));
193 sprintf(buffer, "directory: %s", currentPath.Path());
194 view->DrawString(buffer, BPoint(20, bounds.bottom - 40));
196 target->Unlock();
198 for (int32 row = 0; row < kRowCount; row++) {
199 for (int32 column = 0; column < kColumnCount; column++) {
200 BEntry entry(NextRef());
201 Model model(&entry, true);
203 if (!target->Lock())
204 return;
206 if (model.IsDirectory())
207 entry.GetPath(&currentPath);
209 IconCache::sIconCache->Draw(&model, view,
210 BPoint(column * (kIconSize + 2), row * (kIconSize + 2)),
211 kNormalIcon, kIconSize, true);
212 target->Unlock();
213 numDrawn++;
219 bool oldIconCacheInited = false;
222 void
223 IconSpewer::DrawSomeOld()
225 #if 0
226 if (!oldIconCacheInited)
227 BIconCache::InitIconCaches();
229 target->Lock();
230 target->SetTitle("old cache");
231 BView* view = target->FindView("iconView");
232 ASSERT(view);
234 BRect bounds(target->Bounds());
235 view->SetHighColor(Color(255, 255, 255));
236 view->FillRect(bounds);
238 view->SetHighColor(Color(0, 0, 0));
239 char buffer[256];
240 if (cycleTime) {
241 sprintf(buffer, "last cycle time %Ld ms", cycleTime/1000);
242 view->DrawString(buffer, BPoint(20, bounds.bottom - 20));
244 if (numDrawn) {
245 sprintf(buffer, "average draw time %Ld us per icon",
246 watch.ElapsedTime() / numDrawn);
247 view->DrawString(buffer, BPoint(20, bounds.bottom - 30));
249 sprintf(buffer, "directory: %s", currentPath.Path());
250 view->DrawString(buffer, BPoint(20, bounds.bottom - 40));
252 target->Unlock();
254 for (int32 row = 0; row < kRowCount; row++) {
255 for (int32 column = 0; column < kColumnCount; column++) {
256 BEntry entry(NextRef());
257 BModel model(&entry, true);
259 if (!target->Lock())
260 return;
262 if (model.IsDirectory())
263 entry.GetPath(&currentPath);
265 BIconCache::LockIconCache();
266 BIconCache* iconCache
267 = BIconCache::GetIconCache(&model, kIconSize);
268 iconCache->Draw(view, BPoint(column * (kIconSize + 2),
269 row * (kIconSize + 2)), B_NORMAL_ICON, kIconSize, true);
270 BIconCache::UnlockIconCache();
272 target->Unlock();
273 numDrawn++;
276 #endif
280 const entry_ref*
281 IconSpewer::NextRef()
283 status_t result;
284 if (newCache)
285 result = cachingIterator->GetNextRef(&ref);
286 else
287 result = walker->GetNextRef(&ref);
289 if (result == B_OK)
290 return &ref;
292 delete walker;
293 if (!pathsToSearch[searchPathIndex]) {
294 bigtime_t now = watch.ElapsedTime();
295 cycleTime = now - lastCycleLap;
296 lastCycleLap = now;
297 PRINT(("**************************hit end of disk, starting over\n"));
298 searchPathIndex = 0;
301 walker = new TNodeWalker(pathsToSearch[searchPathIndex++]);
302 if (newCache) {
303 cachingIterator->SetTo(walker);
304 result = cachingIterator->GetNextRef(&ref);
305 } else
306 result = walker->GetNextRef(&ref);
308 ASSERT(result == B_OK);
309 // we don't expect and cannot deal with any problems here
310 return &ref;
314 // #pragma mark - IconTestWindow
317 IconTestWindow::IconTestWindow()
319 BWindow(BRect(100, 100, 500, 600), "icon cache test",
320 B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL, 0),
321 iconSpewer(modifiers() == 0)
323 iconSpewer.SetTarget(this);
324 BView* view = new BView(Bounds(), "iconView", B_FOLLOW_ALL, B_WILL_DRAW);
325 AddChild(view);
326 iconSpewer.Go();
330 bool
331 IconTestWindow::QuitRequested()
333 iconSpewer.Quit();
334 return true;
338 void
339 RunIconCacheTests()
341 (new IconTestWindow())->Show();
344 #endif // DEBUG