nsprpub/pr/tests/bigfile3.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nspr.h"
michael@0 7
michael@0 8 #include <stdio.h>
michael@0 9 #include <stdlib.h>
michael@0 10 #include <string.h>
michael@0 11 #ifdef _WIN32
michael@0 12 #include <windows.h>
michael@0 13 #endif
michael@0 14
michael@0 15 #define TEST_FILE_NAME "bigfile3.txt"
michael@0 16 #ifdef WINCE
michael@0 17 #define TEST_FILE_NAME_FOR_CREATEFILE L"bigfile3.txt"
michael@0 18 #else
michael@0 19 #define TEST_FILE_NAME_FOR_CREATEFILE TEST_FILE_NAME
michael@0 20 #endif
michael@0 21
michael@0 22 #define MESSAGE "Hello world!"
michael@0 23 #define MESSAGE_SIZE 13
michael@0 24
michael@0 25 int main(int argc, char **argv)
michael@0 26 {
michael@0 27 PRFileDesc *fd;
michael@0 28 PRInt64 offset, position;
michael@0 29 PRInt32 nbytes;
michael@0 30 char buf[MESSAGE_SIZE];
michael@0 31 #ifdef _WIN32
michael@0 32 HANDLE hFile;
michael@0 33 LARGE_INTEGER li;
michael@0 34 #endif /* _WIN32 */
michael@0 35
michael@0 36 LL_I2L(offset, 1);
michael@0 37 LL_SHL(offset, offset, 32);
michael@0 38
michael@0 39 #ifdef _WIN32
michael@0 40 hFile = CreateFile(TEST_FILE_NAME_FOR_CREATEFILE, GENERIC_WRITE, 0, NULL,
michael@0 41 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
michael@0 42 if (hFile == INVALID_HANDLE_VALUE) {
michael@0 43 fprintf(stderr, "CreateFile failed\n");
michael@0 44 exit(1);
michael@0 45 }
michael@0 46 li.QuadPart = offset;
michael@0 47 li.LowPart = SetFilePointer(hFile, li.LowPart, &li.HighPart, FILE_BEGIN);
michael@0 48 if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
michael@0 49 fprintf(stderr, "SetFilePointer failed\n");
michael@0 50 exit(1);
michael@0 51 }
michael@0 52 PR_ASSERT(li.QuadPart == offset);
michael@0 53 strcpy(buf, MESSAGE);
michael@0 54 if (WriteFile(hFile, buf, sizeof(buf), &nbytes, NULL) == 0) {
michael@0 55 fprintf(stderr, "WriteFile failed\n");
michael@0 56 exit(1);
michael@0 57 }
michael@0 58 PR_ASSERT(nbytes == sizeof(buf));
michael@0 59 if (CloseHandle(hFile) == 0) {
michael@0 60 fprintf(stderr, "CloseHandle failed\n");
michael@0 61 exit(1);
michael@0 62 }
michael@0 63 #endif /* _WIN32 */
michael@0 64
michael@0 65 memset(buf, 0, sizeof(buf));
michael@0 66
michael@0 67 fd = PR_Open(TEST_FILE_NAME, PR_RDONLY, 0666);
michael@0 68 if (fd == NULL) {
michael@0 69 fprintf(stderr, "PR_Open failed\n");
michael@0 70 exit(1);
michael@0 71 }
michael@0 72 position = PR_Seek64(fd, offset, PR_SEEK_SET);
michael@0 73 if (!LL_GE_ZERO(position)) {
michael@0 74 fprintf(stderr, "PR_Seek64 failed\n");
michael@0 75 exit(1);
michael@0 76 }
michael@0 77 PR_ASSERT(LL_EQ(position, offset));
michael@0 78 nbytes = PR_Read(fd, buf, sizeof(buf));
michael@0 79 if (nbytes != sizeof(buf)) {
michael@0 80 fprintf(stderr, "PR_Read failed\n");
michael@0 81 exit(1);
michael@0 82 }
michael@0 83 if (strcmp(buf, MESSAGE)) {
michael@0 84 fprintf(stderr, "corrupt data:$%s$\n", buf);
michael@0 85 exit(1);
michael@0 86 }
michael@0 87 if (PR_Close(fd) == PR_FAILURE) {
michael@0 88 fprintf(stderr, "PR_Close failed\n");
michael@0 89 exit(1);
michael@0 90 }
michael@0 91
michael@0 92 if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) {
michael@0 93 fprintf(stderr, "PR_Delete failed\n");
michael@0 94 exit(1);
michael@0 95 }
michael@0 96
michael@0 97 printf("PASS\n");
michael@0 98 return 0;
michael@0 99 }

mercurial