1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/bigfile3.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nspr.h" 1.10 + 1.11 +#include <stdio.h> 1.12 +#include <stdlib.h> 1.13 +#include <string.h> 1.14 +#ifdef _WIN32 1.15 +#include <windows.h> 1.16 +#endif 1.17 + 1.18 +#define TEST_FILE_NAME "bigfile3.txt" 1.19 +#ifdef WINCE 1.20 +#define TEST_FILE_NAME_FOR_CREATEFILE L"bigfile3.txt" 1.21 +#else 1.22 +#define TEST_FILE_NAME_FOR_CREATEFILE TEST_FILE_NAME 1.23 +#endif 1.24 + 1.25 +#define MESSAGE "Hello world!" 1.26 +#define MESSAGE_SIZE 13 1.27 + 1.28 +int main(int argc, char **argv) 1.29 +{ 1.30 + PRFileDesc *fd; 1.31 + PRInt64 offset, position; 1.32 + PRInt32 nbytes; 1.33 + char buf[MESSAGE_SIZE]; 1.34 +#ifdef _WIN32 1.35 + HANDLE hFile; 1.36 + LARGE_INTEGER li; 1.37 +#endif /* _WIN32 */ 1.38 + 1.39 + LL_I2L(offset, 1); 1.40 + LL_SHL(offset, offset, 32); 1.41 + 1.42 +#ifdef _WIN32 1.43 + hFile = CreateFile(TEST_FILE_NAME_FOR_CREATEFILE, GENERIC_WRITE, 0, NULL, 1.44 + CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 1.45 + if (hFile == INVALID_HANDLE_VALUE) { 1.46 + fprintf(stderr, "CreateFile failed\n"); 1.47 + exit(1); 1.48 + } 1.49 + li.QuadPart = offset; 1.50 + li.LowPart = SetFilePointer(hFile, li.LowPart, &li.HighPart, FILE_BEGIN); 1.51 + if (li.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) { 1.52 + fprintf(stderr, "SetFilePointer failed\n"); 1.53 + exit(1); 1.54 + } 1.55 + PR_ASSERT(li.QuadPart == offset); 1.56 + strcpy(buf, MESSAGE); 1.57 + if (WriteFile(hFile, buf, sizeof(buf), &nbytes, NULL) == 0) { 1.58 + fprintf(stderr, "WriteFile failed\n"); 1.59 + exit(1); 1.60 + } 1.61 + PR_ASSERT(nbytes == sizeof(buf)); 1.62 + if (CloseHandle(hFile) == 0) { 1.63 + fprintf(stderr, "CloseHandle failed\n"); 1.64 + exit(1); 1.65 + } 1.66 +#endif /* _WIN32 */ 1.67 + 1.68 + memset(buf, 0, sizeof(buf)); 1.69 + 1.70 + fd = PR_Open(TEST_FILE_NAME, PR_RDONLY, 0666); 1.71 + if (fd == NULL) { 1.72 + fprintf(stderr, "PR_Open failed\n"); 1.73 + exit(1); 1.74 + } 1.75 + position = PR_Seek64(fd, offset, PR_SEEK_SET); 1.76 + if (!LL_GE_ZERO(position)) { 1.77 + fprintf(stderr, "PR_Seek64 failed\n"); 1.78 + exit(1); 1.79 + } 1.80 + PR_ASSERT(LL_EQ(position, offset)); 1.81 + nbytes = PR_Read(fd, buf, sizeof(buf)); 1.82 + if (nbytes != sizeof(buf)) { 1.83 + fprintf(stderr, "PR_Read failed\n"); 1.84 + exit(1); 1.85 + } 1.86 + if (strcmp(buf, MESSAGE)) { 1.87 + fprintf(stderr, "corrupt data:$%s$\n", buf); 1.88 + exit(1); 1.89 + } 1.90 + if (PR_Close(fd) == PR_FAILURE) { 1.91 + fprintf(stderr, "PR_Close failed\n"); 1.92 + exit(1); 1.93 + } 1.94 + 1.95 + if (PR_Delete(TEST_FILE_NAME) == PR_FAILURE) { 1.96 + fprintf(stderr, "PR_Delete failed\n"); 1.97 + exit(1); 1.98 + } 1.99 + 1.100 + printf("PASS\n"); 1.101 + return 0; 1.102 +}