1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: t_file.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_store.hxx"
34 #include "sal/types.h"
35 #include "osl/thread.h"
36 #include "rtl/ustring.hxx"
38 #include "lockbyte.hxx"
40 #ifndef INCLUDED_STDIO_H
42 #define INCLUDED_STDIO_H
46 #include "osl/process.h"
48 using namespace store
;
50 #define TEST_PAGESIZE 16384
52 /*========================================================================
56 *======================================================================*/
57 int SAL_CALL
main (int argc
, char **argv
)
59 storeError eErrCode
= store_E_None
;
60 rtl::Reference
<ILockBytes
> xLockBytes
;
64 rtl::OUString
aFilename (
65 argv
[1], rtl_str_getLength(argv
[1]),
66 osl_getThreadTextEncoding());
72 result
= osl_getFileURLFromSystemPath(aFilename
.pData
, &(aPath
.pData
));
73 if (result
!= osl_File_E_None
)
75 // not SystemPath, assume FileUrl.
78 if (rtl_ustr_ascii_shortenedCompare_WithLength(aPath
.pData
->buffer
, aPath
.pData
->length
, "file://", 7) != 0)
80 // not FileUrl, assume relative path.
82 (void) osl_getProcessWorkingDir (&(aBase
.pData
));
85 (void) osl_getAbsoluteFileURL(aBase
.pData
, aPath
.pData
, &(aPath
.pData
));
90 eErrCode
= FileLockBytes_createInstance (
91 xLockBytes
, aFilename
.pData
, store_AccessReadWrite
);
92 if (eErrCode
!= store_E_None
)
95 if (eErrCode
!= store_E_NotExists
)
97 fprintf (stderr
, "t_file: create() error: %d\n", eErrCode
);
102 eErrCode
= FileLockBytes_createInstance (
103 xLockBytes
, aFilename
.pData
, store_AccessReadCreate
);
104 if (eErrCode
!= store_E_None
)
106 fprintf (stderr
, "t_file: create() error: %d\n", eErrCode
);
110 fprintf (stdout
, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv
[1]);
114 eErrCode
= MemoryLockBytes_createInstance (xLockBytes
);
115 if (eErrCode
!= store_E_None
)
117 fprintf (stderr
, "t_file: create() error: %d\n", eErrCode
);
120 fprintf (stdout
, "t_file: using MemoryLockBytes implementation.\n");
123 rtl::Reference
< PageData::Allocator
> xAllocator
;
124 eErrCode
= xLockBytes
->initialize (xAllocator
, TEST_PAGESIZE
);
125 if (eErrCode
!= store_E_None
)
127 fprintf (stderr
, "t_file: initialize() error: %d\n", eErrCode
);
131 sal_Char buffer
[TEST_PAGESIZE
];
132 rtl_fillMemory (buffer
, sizeof(buffer
), sal_uInt8('B'));
135 for (k
= 0; k
< 4; k
++)
137 sal_uInt32 index
= k
* TEST_PAGESIZE
/ 4;
141 for (i
= 0; i
< 256; i
++)
143 sal_uInt32 offset
= i
* TEST_PAGESIZE
;
144 eErrCode
= xLockBytes
->setSize (offset
+ TEST_PAGESIZE
);
145 if (eErrCode
!= store_E_None
)
147 fprintf (stderr
, "t_file: setSize() error: %d\n", eErrCode
);
151 for (k
= 0; k
< 4; k
++)
153 sal_uInt32 magic
= i
* 4 + k
;
156 sal_uInt32 verify
= 0;
157 eErrCode
= xLockBytes
->readAt (
158 0, &verify
, sizeof(verify
));
159 if (eErrCode
!= store_E_None
)
161 fprintf (stderr
, "t_file: readAt() error: %d\n", eErrCode
);
167 fprintf (stderr
, "Expected %ld read %ld\n", (unsigned long)(magic
), (unsigned long)(verify
));
171 sal_uInt32 index
= k
* TEST_PAGESIZE
/ 4;
172 eErrCode
= xLockBytes
->writeAt (
173 offset
+ index
, &(buffer
[index
]), TEST_PAGESIZE
/ 4);
174 if (eErrCode
!= store_E_None
)
176 fprintf (stderr
, "t_file: writeAt() error: %d\n", eErrCode
);
181 eErrCode
= xLockBytes
->writeAt (
182 0, &magic
, sizeof(magic
));
183 if (eErrCode
!= store_E_None
)
185 fprintf (stderr
, "t_file: writeAt() error: %d\n", eErrCode
);
191 eErrCode
= xLockBytes
->flush();
192 if (eErrCode
!= store_E_None
)
194 fprintf (stderr
, "t_file: flush() error: %d\n", eErrCode
);
198 sal_Char verify
[TEST_PAGESIZE
];
199 for (i
= 0; i
< 256; i
++)
201 sal_uInt32 offset
= i
* TEST_PAGESIZE
;
203 eErrCode
= xLockBytes
->readAt (offset
, verify
, TEST_PAGESIZE
);
204 if (eErrCode
!= store_E_None
)
206 fprintf (stderr
, "t_file: readAt() error: %d\n", eErrCode
);
210 sal_uInt32 index
= 0;
213 sal_uInt32 magic
= 256 * 4;
214 if (rtl_compareMemory (&verify
[index
], &magic
, sizeof(magic
)))
217 fprintf (stderr
, "t_file: Unexpected value at 0x00000000\n");
221 if (rtl_compareMemory (
222 &verify
[index
], &buffer
[index
], TEST_PAGESIZE
- index
))
225 fprintf (stderr
, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset
));
229 for (i
= 0; i
< 256; i
++)
232 sal_uInt32 offset
= i
* TEST_PAGESIZE
;
234 eErrCode
= xLockBytes
->readPageAt (xPage
, offset
);
235 if (eErrCode
!= store_E_None
)
237 fprintf (stderr
, "t_file: readPageAt() error: %d\n", eErrCode
);
241 PageData
* page
= xPage
.get();
242 (void)page
; // UNUSED