Bump version to 4.3-4
[LibreOffice.git] / store / workben / t_file.cxx
blob78127c11483b6d3f783a5aa762379d14812a937c
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 .
20 #include "sal/types.h"
21 #include "osl/thread.h"
22 #include "rtl/ustring.hxx"
24 #include "lockbyte.hxx"
26 #include <string.h>
27 #include <stdio.h>
29 #include "osl/file.h"
30 #include "osl/process.h"
32 using namespace store;
34 #define TEST_PAGESIZE 16384
36 /*========================================================================
38 * main.
40 *======================================================================*/
41 int SAL_CALL main (int argc, char **argv)
43 storeError eErrCode = store_E_None;
44 rtl::Reference<ILockBytes> xLockBytes;
46 if (argc > 1)
48 OUString aFilename (
49 argv[1], rtl_str_getLength(argv[1]),
50 osl_getThreadTextEncoding());
52 eErrCode = FileLockBytes_createInstance (
53 xLockBytes, aFilename.pData, store_AccessReadWrite);
54 if (eErrCode != store_E_None)
56 // Check reason.
57 if (eErrCode != store_E_NotExists)
59 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
60 return eErrCode;
63 // Create.
64 eErrCode = FileLockBytes_createInstance (
65 xLockBytes, aFilename.pData, store_AccessReadCreate);
66 if (eErrCode != store_E_None)
68 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
69 return eErrCode;
72 fprintf (stdout, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv[1]);
74 else
76 eErrCode = MemoryLockBytes_createInstance (xLockBytes);
77 if (eErrCode != store_E_None)
79 fprintf (stderr, "t_file: create() error: %d\n", eErrCode);
80 return eErrCode;
82 fprintf (stdout, "t_file: using MemoryLockBytes implementation.\n");
85 rtl::Reference< PageData::Allocator > xAllocator;
86 eErrCode = xLockBytes->initialize (xAllocator, TEST_PAGESIZE);
87 if (eErrCode != store_E_None)
89 fprintf (stderr, "t_file: initialize() error: %d\n", eErrCode);
90 return eErrCode;
93 sal_Char buffer[TEST_PAGESIZE];
94 memset (buffer, sal_uInt8('B'), sizeof(buffer));
96 sal_uInt32 i, k;
97 for (k = 0; k < 4; k++)
99 sal_uInt32 index = k * TEST_PAGESIZE / 4;
100 buffer[index] = 'A';
103 for (i = 0; i < 256; i++)
105 sal_uInt32 offset = i * TEST_PAGESIZE;
106 eErrCode = xLockBytes->setSize (offset + TEST_PAGESIZE);
107 if (eErrCode != store_E_None)
109 fprintf (stderr, "t_file: setSize() error: %d\n", eErrCode);
110 return eErrCode;
113 for (k = 0; k < 4; k++)
115 sal_uInt32 magic = i * 4 + k;
116 if (magic)
118 sal_uInt32 verify = 0;
119 eErrCode = xLockBytes->readAt (
120 0, &verify, sizeof(verify));
121 if (eErrCode != store_E_None)
123 fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
124 return eErrCode;
126 if (verify != magic)
128 // Failure.
129 fprintf (stderr, "Expected %ld read %ld\n", (unsigned long)(magic), (unsigned long)(verify));
133 sal_uInt32 index = k * TEST_PAGESIZE / 4;
134 eErrCode = xLockBytes->writeAt (
135 offset + index, &(buffer[index]), TEST_PAGESIZE / 4);
136 if (eErrCode != store_E_None)
138 fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
139 return eErrCode;
142 magic += 1;
143 eErrCode = xLockBytes->writeAt (
144 0, &magic, sizeof(magic));
145 if (eErrCode != store_E_None)
147 fprintf (stderr, "t_file: writeAt() error: %d\n", eErrCode);
148 return eErrCode;
153 eErrCode = xLockBytes->flush();
154 if (eErrCode != store_E_None)
156 fprintf (stderr, "t_file: flush() error: %d\n", eErrCode);
157 return eErrCode;
160 sal_Char verify[TEST_PAGESIZE];
161 for (i = 0; i < 256; i++)
163 sal_uInt32 offset = i * TEST_PAGESIZE;
165 eErrCode = xLockBytes->readAt (offset, verify, TEST_PAGESIZE);
166 if (eErrCode != store_E_None)
168 fprintf (stderr, "t_file: readAt() error: %d\n", eErrCode);
169 return eErrCode;
172 sal_uInt32 index = 0;
173 if (offset == 0)
175 sal_uInt32 magic = 256 * 4;
176 if (memcmp (&verify[index], &magic, sizeof(magic)))
178 // Failure.
179 fprintf (stderr, "t_file: Unexpected value at 0x00000000\n");
181 index += 4;
183 if (memcmp (
184 &verify[index], &buffer[index], TEST_PAGESIZE - index))
186 // Failure.
187 fprintf (stderr, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset));
191 for (i = 0; i < 256; i++)
193 PageHolder xPage;
194 sal_uInt32 offset = i * TEST_PAGESIZE;
196 eErrCode = xLockBytes->readPageAt (xPage, offset);
197 if (eErrCode != store_E_None)
199 fprintf (stderr, "t_file: readPageAt() error: %d\n", eErrCode);
200 return eErrCode;
203 PageData * page = xPage.get();
204 (void)page; // UNUSED
207 xLockBytes.clear();
208 return 0;
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */