summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/c_runtime/_getw/test1/test1.cpp
blob: 34ce4ee7de94bdfb38cea6cb4524af3c1cadc928 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 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: Several integers are read from a previously written file
**          using _getw.  The test passes if the values read match those known to
**          be in the file.
**
**
**==========================================================================*/

#include <palsuite.h>

/*Tests _getw using a previously written data file */
int __cdecl main(int argc, char **argv)
{
    const int testValues[] =
    {
        0,
        1,
        -1,
        0x7FFFFFFF,            /* largest positive integer on 32 bit systems */
        0x80000000,            /* largest negative integer on 32 bit systems */
        0xFFFFFFFF,
        0xFFFFAAAA
    };

    int i = 0;
    int input = 0;

    const char filename[] = "test.dat";


    FILE *fp = NULL;

    /*
     * Initialize the PAL and return FAIL if this fails
     */
    if (0 != (PAL_Initialize(argc, argv)))
    {
        return FAIL;
    }

    /* write the file that we will use to test */


    /*
      Don't uncomment this code, it was used to create the data file
      initially on windows, but if it is run on all test platforms, the
      tests will always pass.

      fp = fopen(filename, "w");
      if (fp == NULL)
      {
      Fail("Unable to open file for write.\n");
      }
      for (i = 0; i < sizeof(testValues) / sizeof(testValues[0]); i++)
      {
      _putw(testValues[i], fp);
      }

      if (fclose(fp) != 0)
      {
      Fail("Error closing file after writing to it with _putw.\n");
      }
    */


    /*Now read values back from the file and see if they match.*/
    fp = fopen(filename, "r");
    if (fp == NULL)
    {
        Fail ("Unable to open file for read.\n");
    }
    for (i = 0; i < sizeof(testValues) / sizeof(testValues[0]); i++)
    {
        input = _getw(fp);
        if (VAL32(input) != testValues[i])
        {
            Fail ("_getw did not get the expected values when reading "
                    "from a file.\n");
        }
    }

    if (fclose(fp) != 0)
    {
        Fail ("Error closing file after reading from it with _getw\n");
    }
    PAL_Terminate();
    return PASS;
}