Cleanup GetFileSize test to reduce duplicated code
authorBen Pye <ben@debian>
Thu, 12 Feb 2015 22:42:43 +0000 (22:42 +0000)
committerBen Pye <ben@debian>
Thu, 12 Feb 2015 22:42:43 +0000 (22:42 +0000)
src/pal/tests/palsuite/file_io/GetFileSize/test1/GetFileSize.c

index 0e84b2d..c22c861 100644 (file)
@@ -30,17 +30,46 @@ void CleanUp(HANDLE hFile)
     }
 }
 
+void CheckFileSize(HANDLE hFile, DWORD dwOffset, DWORD dwHighOrder)
+{
+    DWORD dwRc = 0;
+    DWORD dwReturnedHighOrder = 0;
+    DWORD dwReturnedOffset = 0;
+
+    dwRc = SetFilePointer(hFile, dwOffset, (PLONG)&dwHighOrder, FILE_BEGIN);
+    if (dwRc == INVALID_SET_FILE_POINTER)
+    {
+        Trace("GetFileSize: ERROR -> Call to SetFilePointer failed with %ld.\n", 
+            GetLastError());
+        CleanUp(hFile);
+        Fail("");
+    }
+    else
+    {
+        if (!SetEndOfFile(hFile))
+        {
+            Trace("GetFileSize: ERROR -> Call to SetEndOfFile failed with %ld.\n", 
+                GetLastError());
+            CleanUp(hFile);
+            Fail("");
+        }
+        dwReturnedOffset = GetFileSize(hFile, &dwReturnedHighOrder);
+        if ((dwReturnedOffset != dwOffset) || 
+            (dwReturnedHighOrder != dwHighOrder))
+        {
+            CleanUp(hFile);
+            Fail("GetFileSize: ERROR -> File sizes do not match up.\n");
+        }
+    }
+}
+
 
 int __cdecl main(int argc, char *argv[])
 {
     HANDLE hFile = NULL;
     DWORD dwRc = 0;
     DWORD dwRc2 = 0;
-    DWORD dwError = 0;
     DWORD dwHighOrder = 0;
-    DWORD dwOffset = 0;
-    DWORD dwReturnedHighOrder = 0;
-    DWORD dwReturnedOffset = 0;
     DWORD lpNumberOfBytesWritten;
     char * data = "1234567890";
 
@@ -55,13 +84,30 @@ int __cdecl main(int argc, char *argv[])
     if (dwRc != INVALID_FILE_SIZE)
     {
         Fail("GetFileSize: ERROR -> A file size was returned for "
-            "an invalid handle.\n");
+            "a null handle.\n");
     }
+
     /* test on a null file handle using the high order option */
     dwRc = GetFileSize(hFile, &dwHighOrder);
     if (dwRc != INVALID_FILE_SIZE)
     {
         Fail("GetFileSize: ERROR -> A file size was returned for "
+            "a null handle.\n");
+    }
+
+    /* test on an invalid file handle */
+    dwRc = GetFileSize(INVALID_HANDLE_VALUE, NULL);
+    if (dwRc != INVALID_FILE_SIZE)
+    {
+        Fail("GetFileSize: ERROR -> A file size was returned for "
+            "an invalid handle.\n");
+    }
+
+    /* test on an invalid file handle using the high order option */
+    dwRc = GetFileSize(INVALID_HANDLE_VALUE, &dwHighOrder);
+    if (dwRc != INVALID_FILE_SIZE)
+    {
+        Fail("GetFileSize: ERROR -> A file size was returned for "
             "an invalid handle.\n");
     }
 
@@ -82,102 +128,14 @@ int __cdecl main(int argc, char *argv[])
     }
 
     /* give the file a size */
-    dwOffset = 256;
-    dwRc = SetFilePointer(hFile, dwOffset, NULL, FILE_BEGIN);
-    if (dwRc == INVALID_SET_FILE_POINTER)
-    {
-        Trace("GetFileSize: ERROR -> Call to SetFilePointer failed with %ld.\n", 
-            GetLastError());
-        CleanUp(hFile);
-        Fail("");
-    }
-    else
-    {
-        if (!SetEndOfFile(hFile))
-        {
-            Trace("GetFileSize: ERROR -> Call to SetEndOfFile failed with %ld.\n", 
-                GetLastError());
-            CleanUp(hFile);
-            Fail("");
-        }
-        dwReturnedOffset = GetFileSize(hFile, &dwReturnedHighOrder);
-        if ((dwReturnedOffset != dwOffset) || 
-            (dwReturnedHighOrder != dwHighOrder))
-        {
-            CleanUp(hFile);
-            Fail("GetFileSize: ERROR -> File sizes do not match up.\n");
-        }
-    }
+    CheckFileSize(hFile, 256, 0);
 
     /* make the file large using the high order option */
-    dwOffset = 256;
-    dwHighOrder = 1;
-    dwRc = SetFilePointer(hFile, dwOffset, (PLONG)&dwHighOrder, FILE_BEGIN);
-    if (dwRc == INVALID_SET_FILE_POINTER)
-    {
-        Trace("GetFileSize: ERROR -> Call to SetFilePointer failed with %ld.\n", 
-            GetLastError());
-        CleanUp(hFile);
-        Fail("");
-    }
-    else
-    {
-        if (!SetEndOfFile(hFile))
-        {
-            dwError = GetLastError();
-            CleanUp(hFile);
-            if (dwError == 112)
-            {
-                Fail("GetFileSize: ERROR -> SetEndOfFile failed due to lack of "
-                    "disk space\n");
-            }
-            else
-            {
-                Fail("GetFileSize: ERROR -> SetEndOfFile call failed "
-                    "with error %ld\n", dwError);
-            }
-        }
-        else
-        {
-            dwReturnedOffset = GetFileSize(hFile, &dwReturnedHighOrder);
-            if ((dwReturnedOffset != dwOffset) || 
-                (dwReturnedHighOrder != dwHighOrder))
-            {
-                CleanUp(hFile);
-                Fail("GetFileSize: ERROR -> File sizes do not match up.\n");
-            }
-        }
-    }
+    CheckFileSize(hFile, 256, 1);
 
 
     /* set the file size to zero */
-    dwOffset = 0;
-    dwHighOrder = 0;
-    dwRc = SetFilePointer(hFile, dwOffset, NULL, FILE_BEGIN);
-    if (dwRc == INVALID_SET_FILE_POINTER)
-    {
-        Trace("GetFileSize: ERROR -> SetEndOfFile call failed with error %ld\n",
-            GetLastError());
-        CleanUp(hFile);
-        Fail("");
-    }
-    else
-    {
-        if (!SetEndOfFile(hFile))
-        {
-            Trace("GetFileSize: ERROR -> Call to SetEndOfFile failed "
-                "with %ld.\n", GetLastError());
-            CleanUp(hFile);
-            Fail("");
-        }
-        dwReturnedOffset = GetFileSize(hFile, &dwReturnedHighOrder);
-        if ((dwReturnedOffset != dwOffset) || 
-            (dwReturnedHighOrder != dwHighOrder))
-        {
-            CleanUp(hFile);
-            Fail("GetFileSize: ERROR -> File sizes do not match up.\n");
-        }
-    }
+    CheckFileSize(hFile, 0, 0);
 
     /*  test if file size changes by writing to it. */
     /* get file size */