summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/fopen/test1/test1.cpp
blob: 565b4eb77db99ae415dbac51eae7ac33c4810d25 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*=====================================================================
**
** Source:  test1.c
**
** Purpose: Tests the PAL implementation of the fopen function. 
**          This test simply attempts to open a number of files
**          with different modes.  It checks to ensure a valid
**          file pointer is returned.  It doesn't do any checking
**          to ensure the mode is really what it claims.  And checks
**          for a NULL pointer when attempts to open a directory.
**  

**
**===================================================================*/
                                 
#include <palsuite.h>

struct testCase
{
    int CorrectResult;
    char mode[20];
};

int __cdecl main(int argc, char **argv)
{
  
    FILE *fp;
    char name[128];
    int i;

    struct testCase testCases[] = 
        {
            {0,  "r"},  {1, "w"},   {1,  "a"},
            {0,  "r+"}, {1,  "w+"}, {1,  "a+"},
            {1,  "wt"}, {1,  "wb"}, {1,  "wS"},
            {1,  "w+c"}, {1,  "w+n"}, {1, "wR"},
            {1,  "wT"}, {0, "tw"} 
        };

    if (PAL_Initialize(argc, argv))
    {
        return FAIL;
    }

  
  
    for(i = 0; i < sizeof(testCases) / sizeof(struct testCase); i++)
    {
        strcpy(name,"testfiles");
        strcat(name,testCases[i].mode);
      
        fp = fopen(name,testCases[i].mode);
      
        if ((fp == 0 && testCases[i].CorrectResult != 0)  ||
            (testCases[i].CorrectResult == 0 && fp != 0) )
        {
            Fail("ERROR: fopen returned incorrectly "
                   "opening a file in %s mode.  Perhaps it opened a "
                   "read only file which didn't exist and returned a correct "
                   "pointer?",testCases[i].mode);
        }    

        memset(name, '\0', 128);
        
    }      

    /* When attempt to open a directory fopen should returned NULL */
    if ( fopen(".", "r") != NULL)
    {
        Fail("ERROR: fopen returned non-NULL when trying to open a directory"
             " the returned value was %d\n", fp);
    }
  
    PAL_Terminate();
    return PASS;
}