1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
31 #include "osl/process.h"
33 using namespace store
;
35 #define TEST_PAGESIZE 16384
37 /*========================================================================
41 *======================================================================*/
42 int SAL_CALL
main (int argc
, char **argv
)
44 storeError eErrCode
= store_E_None
;
45 rtl::Reference
<ILockBytes
> xLockBytes
;
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
)
58 if (eErrCode
!= store_E_NotExists
)
60 fprintf (stderr
, "t_file: create() error: %d\n", eErrCode
);
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
);
73 fprintf (stdout
, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv
[1]);
77 eErrCode
= MemoryLockBytes_createInstance (xLockBytes
);
78 if (eErrCode
!= store_E_None
)
80 fprintf (stderr
, "t_file: create() error: %d\n", 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
);
94 sal_Char buffer
[TEST_PAGESIZE
];
95 memset (buffer
, sal_uInt8('B'), sizeof(buffer
));
98 for (k
= 0; k
< 4; k
++)
100 sal_uInt32 index
= k
* TEST_PAGESIZE
/ 4;
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
);
114 for (k
= 0; k
< 4; k
++)
116 sal_uInt32 magic
= i
* 4 + k
;
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
);
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
);
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
);
154 eErrCode
= xLockBytes
->flush();
155 if (eErrCode
!= store_E_None
)
157 fprintf (stderr
, "t_file: flush() error: %d\n", 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
);
173 sal_uInt32 index
= 0;
176 sal_uInt32 magic
= 256 * 4;
177 if (memcmp (&verify
[index
], &magic
, sizeof(magic
)))
180 fprintf (stderr
, "t_file: Unexpected value at 0x00000000\n");
185 &verify
[index
], &buffer
[index
], TEST_PAGESIZE
- index
))
188 fprintf (stderr
, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset
));
192 for (i
= 0; i
< 256; i
++)
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
);
204 PageData
* page
= xPage
.get();
205 (void)page
; // UNUSED
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */