| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*===================================================================== |
| 6 | ** |
| 7 | ** Source: test2.c |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the fread function. |
| 10 | ** Open a file in READ mode, and then try to read all |
| 11 | ** the characters, more than all the characters, |
| 12 | ** 0 characters and 0 sized characters and check that |
| 13 | ** the strings read in are correct. |
| 14 | ** |
| 15 | ** Depends: |
| 16 | ** fopen |
| 17 | ** fseek |
| 18 | ** fclose |
| 19 | ** strcmp |
| 20 | ** memset |
| 21 | ** |
| 22 | ** |
| 23 | **===================================================================*/ |
| 24 | |
| 25 | /* Note: testfile should exist in the directory with 15 characters |
| 26 | in it ... something got lost if it isn't here. |
| 27 | */ |
| 28 | |
| 29 | /* Note: The behaviour in win32 is to crash if a NULL pointer is passed to |
| 30 | fread, so the test to check that it returns 0 has been removed. |
| 31 | */ |
| 32 | |
| 33 | #include <palsuite.h> |
| 34 | |
| 35 | int __cdecl main(int argc, char **argv) |
| 36 | { |
| 37 | const char filename[] = "testfile" ; |
| 38 | char buffer[128]; |
| 39 | FILE * fp = NULL; |
| 40 | |
| 41 | if (0 != PAL_Initialize(argc, argv)) |
| 42 | { |
| 43 | return FAIL; |
| 44 | } |
| 45 | |
| 46 | /* Open a file in READ mode */ |
| 47 | |
| 48 | if((fp = fopen(filename, "r" )) == NULL) |
| 49 | { |
| 50 | Fail("Unable to open a file for reading. Is the file " |
| 51 | "in the directory? It should be." ); |
| 52 | } |
| 53 | |
| 54 | /* Read 15 characters from the file. The file has exactly this many |
| 55 | in it. Then check to see that the data read in is correct. |
| 56 | Note: The 'testfile' should have "This is a test." written in it. |
| 57 | */ |
| 58 | memset(buffer,'\0',128); |
| 59 | fread(buffer,1,15,fp); |
| 60 | |
| 61 | if(strcmp(buffer,"This is a test." ) != 0) |
| 62 | { |
| 63 | Fail("ERROR: The data read in should have been " |
| 64 | "'This is a test.' but, the buffer contains '%s'." , |
| 65 | buffer); |
| 66 | } |
| 67 | |
| 68 | /* Go back to the start of the file */ |
| 69 | |
| 70 | if(fseek(fp, 0, SEEK_SET)) |
| 71 | { |
| 72 | Fail("ERROR: fseek failed, and this test depends on it." ); |
| 73 | } |
| 74 | |
| 75 | /* Attempt to read 17 characters. The same 15 characters should |
| 76 | be in the buffer. |
| 77 | */ |
| 78 | |
| 79 | memset(buffer,'\0',128); |
| 80 | fread(buffer,1,17,fp); |
| 81 | |
| 82 | if(strcmp(buffer,"This is a test." ) != 0) |
| 83 | { |
| 84 | Fail("ERROR: The data read in should have been " |
| 85 | "'This is a test.' but, the buffer contains '%s'." , |
| 86 | buffer); |
| 87 | } |
| 88 | |
| 89 | /* Back to the start of the file */ |
| 90 | |
| 91 | if(fseek(fp, 0, SEEK_SET)) |
| 92 | { |
| 93 | Fail("ERROR: fseek failed, and this test depends on it." ); |
| 94 | } |
| 95 | |
| 96 | /* Read 0 characters and ensure the buffer is empty */ |
| 97 | |
| 98 | memset(buffer,'\0',128); |
| 99 | fread(buffer,1,0,fp); |
| 100 | |
| 101 | if(strcmp(buffer,"\0" ) != 0) |
| 102 | { |
| 103 | Fail("ERROR: The data read in should have been " |
| 104 | "NULL but, the buffer contains '%s'." , |
| 105 | buffer); |
| 106 | } |
| 107 | |
| 108 | /* Read characters of 0 size and ensure the buffer is empty */ |
| 109 | |
| 110 | memset(buffer,'\0',128); |
| 111 | fread(buffer,0,5,fp); |
| 112 | |
| 113 | if(strcmp(buffer,"\0" ) != 0) |
| 114 | { |
| 115 | Fail("ERROR: The data read in should have been " |
| 116 | "NULL but, the buffer contains '%s'." , |
| 117 | buffer); |
| 118 | } |
| 119 | |
| 120 | /* Close the file */ |
| 121 | |
| 122 | if(fclose(fp)) |
| 123 | { |
| 124 | Fail("ERROR: fclose failed. Test depends on it." ); |
| 125 | } |
| 126 | |
| 127 | /* Read 5 characters of 1 size from a closed file pointer |
| 128 | and ensure the buffer is empty |
| 129 | */ |
| 130 | memset(buffer,'\0',128); |
| 131 | fread(buffer,1,5,fp); |
| 132 | if(strcmp(buffer,"\0" ) != 0) |
| 133 | { |
| 134 | Fail("ERROR: The data read in should have been " |
| 135 | "NULL but, the buffer contains '%s'." , |
| 136 | buffer); |
| 137 | } |
| 138 | |
| 139 | PAL_Terminate(); |
| 140 | return PASS; |
| 141 | } |
| 142 | |
| 143 | |
| 144 | |