Bump for 3.6-28
[LibreOffice.git] / store / workben / t_file.cxx
blob80e209b516c3abc04dfc0064fdacdc7fb1a936bd
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "sal/types.h"
31 #include "osl/thread.h"
32 #include "rtl/ustring.hxx"
34 #include "lockbyte.hxx"
36 #ifndef INCLUDED_STDIO_H
37 #include <stdio.h>
38 #define INCLUDED_STDIO_H
39 #endif
41 #include "osl/file.h"
42 #include "osl/process.h"
44 using namespace store;
46 #define TEST_PAGESIZE 16384
48 /*========================================================================
50 * main.
52 *======================================================================*/
53 int SAL_CALL main (int argc, char **argv)
55 storeError eErrCode = store_E_None;
56 rtl::Reference<ILockBytes> xLockBytes;
58 if (argc > 1)
60 rtl::OUString aFilename (
61 argv[1], rtl_str_getLength(argv[1]),
62 osl_getThreadTextEncoding());
64 eErrCode = FileLockBytes_createInstance (
65 xLockBytes, aFilename.pData, store_AccessReadWrite);
66 if (eErrCode != store_E_None)
68 // Check reason.
69 if (eErrCode != store_E_NotExists)
71 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
72 return eErrCode;
75 // Create.
76 eErrCode = FileLockBytes_createInstance (
77 xLockBytes, aFilename.pData, store_AccessReadCreate);
78 if (eErrCode != store_E_None)
80 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
81 return eErrCode;
84 fprintf (stdout, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv[1]);
86 else
88 eErrCode = MemoryLockBytes_createInstance (xLockBytes);
89 if (eErrCode != store_E_None)
91 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
92 return eErrCode;
94 fprintf (stdout, "t_file: using MemoryLockBytes implementation.\n");
97 rtl::Reference< PageData::Allocator > xAllocator;
98 eErrCode = xLockBytes->initialize (xAllocator, TEST_PAGESIZE);
99 if (eErrCode != store_E_None)
101 fprintf (stderr, "t_file: initialize() error: %d\n", eErrCode);
102 return eErrCode;
105 sal_Char buffer[TEST_PAGESIZE];
106 rtl_fillMemory (buffer, sizeof(buffer), sal_uInt8('B'));
108 sal_uInt32 i, k;
109 for (k = 0; k < 4; k++)
111 sal_uInt32 index = k * TEST_PAGESIZE / 4;
112 buffer[index] = 'A';
115 for (i = 0; i < 256; i++)
117 sal_uInt32 offset = i * TEST_PAGESIZE;
118 eErrCode = xLockBytes->setSize (offset + TEST_PAGESIZE);
119 if (eErrCode != store_E_None)
121 fprintf (stderr, "t_file: setSize() error: %d\n", eErrCode);
122 return eErrCode;
125 for (k = 0; k < 4; k++)
127 sal_uInt32 magic = i * 4 + k;
128 if (magic)
130 sal_uInt32 verify = 0;
131 eErrCode = xLockBytes->readAt (
132 0, &verify, sizeof(verify));
133 if (eErrCode != store_E_None)
135 fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
136 return eErrCode;
138 if (verify != magic)
140 // Failure.
141 fprintf (stderr, "Expected %ld read %ld\n", (unsigned long)(magic), (unsigned long)(verify));
145 sal_uInt32 index = k * TEST_PAGESIZE / 4;
146 eErrCode = xLockBytes->writeAt (
147 offset + index, &(buffer[index]), TEST_PAGESIZE / 4);
148 if (eErrCode != store_E_None)
150 fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
151 return eErrCode;
154 magic += 1;
155 eErrCode = xLockBytes->writeAt (
156 0, &magic, sizeof(magic));
157 if (eErrCode != store_E_None)
159 fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
160 return eErrCode;
165 eErrCode = xLockBytes->flush();
166 if (eErrCode != store_E_None)
168 fprintf (stderr, "t_file: flush() error: %d\n", eErrCode);
169 return eErrCode;
172 sal_Char verify[TEST_PAGESIZE];
173 for (i = 0; i < 256; i++)
175 sal_uInt32 offset = i * TEST_PAGESIZE;
177 eErrCode = xLockBytes->readAt (offset, verify, TEST_PAGESIZE);
178 if (eErrCode != store_E_None)
180 fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
181 return eErrCode;
184 sal_uInt32 index = 0;
185 if (offset == 0)
187 sal_uInt32 magic = 256 * 4;
188 if (rtl_compareMemory (&verify[index], &magic, sizeof(magic)))
190 // Failure.
191 fprintf (stderr, "t_file: Unexpected value at 0x00000000\n");
193 index += 4;
195 if (rtl_compareMemory (
196 &verify[index], &buffer[index], TEST_PAGESIZE - index))
198 // Failure.
199 fprintf (stderr, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset));
203 for (i = 0; i < 256; i++)
205 PageHolder xPage;
206 sal_uInt32 offset = i * TEST_PAGESIZE;
208 eErrCode = xLockBytes->readPageAt (xPage, offset);
209 if (eErrCode != store_E_None)
211 fprintf (stderr, "t_file: readPageAt() error: %d\n", eErrCode);
212 return eErrCode;
215 PageData * page = xPage.get();
216 (void)page; // UNUSED
219 xLockBytes.clear();
220 return 0;
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */