Updated core
[LibreOffice.git] / store / workben / t_file.cxx
blob62b7540c363bb941f2c654e4cbc9da5f1f22c72d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "sal/types.h"
22 #include "osl/thread.h"
23 #include "rtl/ustring.hxx"
25 #include "lockbyte.hxx"
27 #include <string.h>
28 #include <stdio.h>
30 #include "osl/file.h"
31 #include "osl/process.h"
33 using namespace store;
35 #define TEST_PAGESIZE 16384
37 /*========================================================================
39 * main.
41 *======================================================================*/
42 int SAL_CALL main (int argc, char **argv)
44 storeError eErrCode = store_E_None;
45 rtl::Reference<ILockBytes> xLockBytes;
47 if (argc > 1)
49 OUString aFilename (
50 argv[1], rtl_str_getLength(argv[1]),
51 osl_getThreadTextEncoding());
53 eErrCode = FileLockBytes_createInstance (
54 xLockBytes, aFilename.pData, store_AccessReadWrite);
55 if (eErrCode != store_E_None)
57 // Check reason.
58 if (eErrCode != store_E_NotExists)
60 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
61 return eErrCode;
64 // Create.
65 eErrCode = FileLockBytes_createInstance (
66 xLockBytes, aFilename.pData, store_AccessReadCreate);
67 if (eErrCode != store_E_None)
69 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
70 return eErrCode;
73 fprintf (stdout, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv[1]);
75 else
77 eErrCode = MemoryLockBytes_createInstance (xLockBytes);
78 if (eErrCode != store_E_None)
80 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
81 return eErrCode;
83 fprintf (stdout, "t_file: using MemoryLockBytes implementation.\n");
86 rtl::Reference< PageData::Allocator > xAllocator;
87 eErrCode = xLockBytes->initialize (xAllocator, TEST_PAGESIZE);
88 if (eErrCode != store_E_None)
90 fprintf (stderr, "t_file: initialize() error: %d\n", eErrCode);
91 return eErrCode;
94 sal_Char buffer[TEST_PAGESIZE];
95 memset (buffer, sal_uInt8('B'), sizeof(buffer));
97 sal_uInt32 i, k;
98 for (k = 0; k < 4; k++)
100 sal_uInt32 index = k * TEST_PAGESIZE / 4;
101 buffer[index] = 'A';
104 for (i = 0; i < 256; i++)
106 sal_uInt32 offset = i * TEST_PAGESIZE;
107 eErrCode = xLockBytes->setSize (offset + TEST_PAGESIZE);
108 if (eErrCode != store_E_None)
110 fprintf (stderr, "t_file: setSize() error: %d\n", eErrCode);
111 return eErrCode;
114 for (k = 0; k < 4; k++)
116 sal_uInt32 magic = i * 4 + k;
117 if (magic)
119 sal_uInt32 verify = 0;
120 eErrCode = xLockBytes->readAt (
121 0, &verify, sizeof(verify));
122 if (eErrCode != store_E_None)
124 fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
125 return eErrCode;
127 if (verify != magic)
129 // Failure.
130 fprintf (stderr, "Expected %ld read %ld\n", (unsigned long)(magic), (unsigned long)(verify));
134 sal_uInt32 index = k * TEST_PAGESIZE / 4;
135 eErrCode = xLockBytes->writeAt (
136 offset + index, &(buffer[index]), TEST_PAGESIZE / 4);
137 if (eErrCode != store_E_None)
139 fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
140 return eErrCode;
143 magic += 1;
144 eErrCode = xLockBytes->writeAt (
145 0, &magic, sizeof(magic));
146 if (eErrCode != store_E_None)
148 fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
149 return eErrCode;
154 eErrCode = xLockBytes->flush();
155 if (eErrCode != store_E_None)
157 fprintf (stderr, "t_file: flush() error: %d\n", eErrCode);
158 return eErrCode;
161 sal_Char verify[TEST_PAGESIZE];
162 for (i = 0; i < 256; i++)
164 sal_uInt32 offset = i * TEST_PAGESIZE;
166 eErrCode = xLockBytes->readAt (offset, verify, TEST_PAGESIZE);
167 if (eErrCode != store_E_None)
169 fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
170 return eErrCode;
173 sal_uInt32 index = 0;
174 if (offset == 0)
176 sal_uInt32 magic = 256 * 4;
177 if (memcmp (&verify[index], &magic, sizeof(magic)))
179 // Failure.
180 fprintf (stderr, "t_file: Unexpected value at 0x00000000\n");
182 index += 4;
184 if (memcmp (
185 &verify[index], &buffer[index], TEST_PAGESIZE - index))
187 // Failure.
188 fprintf (stderr, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset));
192 for (i = 0; i < 256; i++)
194 PageHolder xPage;
195 sal_uInt32 offset = i * TEST_PAGESIZE;
197 eErrCode = xLockBytes->readPageAt (xPage, offset);
198 if (eErrCode != store_E_None)
200 fprintf (stderr, "t_file: readPageAt() error: %d\n", eErrCode);
201 return eErrCode;
204 PageData * page = xPage.get();
205 (void)page; // UNUSED
208 xLockBytes.clear();
209 return 0;
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */