vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / locale / catalogSpeed.cpp
blob3afa7356480c3bca28128790a67ebee3da6ef331
1 /*
2 ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <typeinfo>
10 #include <unistd.h>
12 #include <Application.h>
13 #include <StopWatch.h>
15 #include <Catalog.h>
16 #include <HashMapCatalog.h>
17 #include <Entry.h>
18 #include <Locale.h>
19 #include <Path.h>
20 #include <Roster.h>
22 const uint32 kNumStrings = 10000;
24 BString strs[kNumStrings];
25 BString ctxs[kNumStrings];
27 BString trls[kNumStrings];
29 const char *translated;
31 class CatalogSpeed {
32 public:
33 void TestCreation();
34 void TestLookup();
35 void TestIdCreation();
36 void TestIdLookup();
39 #define B_TRANSLATION_CONTEXT "CatalogSpeed"
41 #define catSig "x-vnd.Be.locale.catalogSpeed"
42 #define catName catSig".catalog"
45 void
46 CatalogSpeed::TestCreation()
48 for (uint32 i = 0; i < kNumStrings; i++) {
49 strs[i] << "native-string#" << 1000000+i;
50 ctxs[i] << B_TRANSLATION_CONTEXT;
51 trls[i] << "translation#" << 4000000+i;
54 BStopWatch watch("catalogSpeed", true);
56 status_t res;
57 assert(be_locale != NULL);
58 system("mkdir -p ./locale/catalogs/"catSig);
60 // create an empty catalog of default type...
61 BPrivate::EditableCatalog cat1("Default", catSig, "klingon");
62 assert(cat1.InitCheck() == B_OK);
64 // ...and populate the catalog with some data:
65 for (uint32 i = 0; i < kNumStrings; i++) {
66 cat1.SetString(strs[i].String(), trls[i].String(), ctxs[i].String());
68 watch.Suspend();
69 printf("\tadded %ld strings in %9Ld usecs\n",
70 cat1.CountItems(), watch.ElapsedTime());
72 watch.Reset();
73 watch.Resume();
74 res = cat1.WriteToFile("./locale/catalogs/"catSig"/klingon.catalog");
75 assert(res == B_OK);
76 watch.Suspend();
77 printf("\t%ld strings written to disk in %9Ld usecs\n",
78 cat1.CountItems(), watch.ElapsedTime());
82 void
83 CatalogSpeed::TestLookup()
85 BStopWatch watch("catalogSpeed", true);
87 BCatalog *cat = be_catalog = new BCatalog(catSig, "klingon");
89 assert(cat != NULL);
90 assert(cat->InitCheck() == B_OK);
91 watch.Suspend();
92 printf("\t%ld strings read from disk in %9Ld usecs\n",
93 cat->CountItems(), watch.ElapsedTime());
95 watch.Reset();
96 watch.Resume();
97 for (uint32 i = 0; i < kNumStrings; i++) {
98 translated = B_TRANSLATE(strs[i].String());
100 watch.Suspend();
101 printf("\tlooked up %lu strings in %9Ld usecs\n",
102 kNumStrings, watch.ElapsedTime());
104 delete cat;
108 void
109 CatalogSpeed::TestIdCreation()
111 BStopWatch watch("catalogSpeed", true);
112 watch.Suspend();
114 status_t res;
115 BString s("string");
116 s << "\x01" << typeid(*this).name() << "\x01";
117 //size_t hashVal = __stl_hash_string(s.String());
118 assert(be_locale != NULL);
119 system("mkdir -p ./locale/catalogs/"catSig);
121 // create an empty catalog of default type...
122 BPrivate::EditableCatalog cat1("Default", catSig, "klingon");
123 assert(cat1.InitCheck() == B_OK);
125 // ...and populate the catalog with some data:
126 for (uint32 i = 0; i < kNumStrings; i++) {
127 trls[i] = BString("id_translation#") << 6000000+i;
129 watch.Reset();
130 watch.Resume();
131 for (uint32 i = 0; i < kNumStrings; i++) {
132 cat1.SetString(i, trls[i].String());
134 watch.Suspend();
135 printf("\tadded %ld strings by id in %9Ld usecs\n",
136 cat1.CountItems(), watch.ElapsedTime());
138 watch.Reset();
139 watch.Resume();
140 res = cat1.WriteToFile("./locale/catalogs/"catSig"/klingon.catalog");
141 assert( res == B_OK);
142 watch.Suspend();
143 printf("\t%ld strings written to disk in %9Ld usecs\n",
144 cat1.CountItems(), watch.ElapsedTime());
148 void
149 CatalogSpeed::TestIdLookup()
151 BStopWatch watch("catalogSpeed", true);
153 BCatalog *cat = be_catalog = new BCatalog(catSig, "klingon");
155 assert(cat != NULL);
156 assert(cat->InitCheck() == B_OK);
157 watch.Suspend();
158 printf("\t%ld strings read from disk in %9Ld usecs\n",
159 cat->CountItems(), watch.ElapsedTime());
161 watch.Reset();
162 watch.Resume();
163 for (uint32 i = 0; i < kNumStrings; i++) {
164 translated = B_TRANSLATE_ID(i);
166 watch.Suspend();
167 printf("\tlooked up %lu strings in %9Ld usecs\n",
168 kNumStrings, watch.ElapsedTime());
170 delete cat;
175 main(int argc, char **argv)
177 BApplication* testApp
178 = new BApplication("application/"catSig);
180 // change to app-folder:
181 app_info appInfo;
182 be_app->GetAppInfo(&appInfo);
183 BEntry appEntry(&appInfo.ref);
184 BEntry appFolder;
185 appEntry.GetParent(&appFolder);
186 BPath appPath;
187 appFolder.GetPath(&appPath);
188 chdir(appPath.Path());
190 CatalogSpeed catSpeed;
191 printf("\t------------------------------------------------\n");
192 printf("\tstring-based catalog usage:\n");
193 printf("\t------------------------------------------------\n");
194 catSpeed.TestCreation();
195 catSpeed.TestLookup();
196 printf("\t------------------------------------------------\n");
197 printf("\tid-based catalog usage:\n");
198 printf("\t------------------------------------------------\n");
199 catSpeed.TestIdCreation();
200 catSpeed.TestIdLookup();
202 delete testApp;
204 return 0;