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 .
20 #include "sal/types.h"
21 #include "osl/thread.h"
22 #include "rtl/ustring.hxx"
24 #include "lockbyte.hxx"
30 #include "osl/process.h"
32 using namespace store
;
34 #define TEST_PAGESIZE 16384
36 /*========================================================================
40 *======================================================================*/
41 int SAL_CALL
main (int argc
, char **argv
)
43 storeError eErrCode
= store_E_None
;
44 rtl::Reference
<ILockBytes
> xLockBytes
;
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
)
57 if (eErrCode
!= store_E_NotExists
)
59 fprintf (stderr
, "t_file: create() error: %d\n", eErrCode
);
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
);
72 fprintf (stdout
, "t_file: using FileLockBytes(\"%s\") implementation.\n", argv
[1]);
76 eErrCode
= MemoryLockBytes_createInstance (xLockBytes
);
77 if (eErrCode
!= store_E_None
)
79 fprintf (stderr
, "t_file: create() error: %d\n", 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
);
93 sal_Char buffer
[TEST_PAGESIZE
];
94 memset (buffer
, sal_uInt8('B'), sizeof(buffer
));
97 for (k
= 0; k
< 4; k
++)
99 sal_uInt32 index
= k
* TEST_PAGESIZE
/ 4;
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
);
113 for (k
= 0; k
< 4; k
++)
115 sal_uInt32 magic
= i
* 4 + k
;
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
);
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
);
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
);
153 eErrCode
= xLockBytes
->flush();
154 if (eErrCode
!= store_E_None
)
156 fprintf (stderr
, "t_file: flush() error: %d\n", 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
);
172 sal_uInt32 index
= 0;
175 sal_uInt32 magic
= 256 * 4;
176 if (memcmp (&verify
[index
], &magic
, sizeof(magic
)))
179 fprintf (stderr
, "t_file: Unexpected value at 0x00000000\n");
184 &verify
[index
], &buffer
[index
], TEST_PAGESIZE
- index
))
187 fprintf (stderr
, "t_file: Unexpected block at 0x%08x\n", (unsigned)(offset
));
191 for (i
= 0; i
< 256; i
++)
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
);
203 PageData
* page
= xPage
.get();
204 (void)page
; // UNUSED
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */