blob: 3dbc485a66c4e622b923b051d0c8f2e306cf87b6 [file] [log] [blame]
Elliott Hughes2b021e12014-08-19 17:00:33 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio_ext.h>
18
19#include <gtest/gtest.h>
20
21#include <errno.h>
22#include <fcntl.h>
23#include <limits.h>
24#include <math.h>
25#include <stdio.h>
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <unistd.h>
29#include <wchar.h>
30#include <locale.h>
31
32#include "TemporaryFile.h"
33
34TEST(stdio_ext, __fbufsize) {
35 FILE* fp = fopen("/proc/version", "r");
36
37 char buf[128];
38
39 ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 1));
40 ASSERT_EQ(1U, __fbufsize(fp));
41
42 ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 8));
43 ASSERT_EQ(8U, __fbufsize(fp));
44
45 fclose(fp);
46}
47
48TEST(stdio_ext, __flbf) {
49 FILE* fp = fopen("/proc/version", "r");
50
51 ASSERT_FALSE(__flbf(fp));
52
53 char buf[128];
54 ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
55
56 ASSERT_TRUE(__flbf(fp));
57
58 fclose(fp);
59}
60
61TEST(stdio_ext, __fpending) {
62 FILE* fp = fopen("/dev/null", "w");
63 ASSERT_EQ(0U, __fpending(fp));
64 ASSERT_EQ('x', fputc('x', fp));
65 ASSERT_EQ(1U, __fpending(fp));
66 ASSERT_EQ('y', fputc('y', fp));
67 ASSERT_EQ(2U, __fpending(fp));
68 fflush(fp);
69 ASSERT_EQ(0U, __fpending(fp));
70 fclose(fp);
71}
72
73TEST(stdio_ext, __fpurge) {
74 FILE* fp = tmpfile();
75
76 ASSERT_EQ('a', fputc('a', fp));
77 ASSERT_EQ(1U, __fpending(fp));
78 __fpurge(fp);
79 ASSERT_EQ(0U, __fpending(fp));
80
81 ASSERT_EQ('b', fputc('b', fp));
82 ASSERT_EQ('\n', fputc('\n', fp));
83 ASSERT_EQ(2U, __fpending(fp));
84
85 rewind(fp);
86
87 char buf[16];
88 char* s = fgets(buf, sizeof(buf), fp);
89 ASSERT_TRUE(s != NULL);
90 ASSERT_STREQ("b\n", s);
91
92 fclose(fp);
93}
94
95TEST(stdio_ext, _flushlbf) {
96 FILE* fp = fopen("/dev/null", "w");
97
98 char buf[128];
99 ASSERT_EQ(0, setvbuf(fp, buf, _IOLBF, sizeof(buf)));
100
101 ASSERT_EQ('a', fputc('a', fp));
102 ASSERT_EQ(1U, __fpending(fp));
103
104 _flushlbf();
105
106 ASSERT_EQ(0U, __fpending(fp));
107
108 fclose(fp);
109}
110
111TEST(stdio_ext, __freadable__fwritable) {
112 FILE* fp = fopen("/dev/null", "r");
113 ASSERT_TRUE(__freadable(fp));
114 ASSERT_FALSE(__fwritable(fp));
115 fclose(fp);
116
117 fp = fopen("/dev/null", "w");
118 ASSERT_FALSE(__freadable(fp));
119 ASSERT_TRUE(__fwritable(fp));
120 fclose(fp);
121
122 fp = fopen("/dev/null", "w+");
123 ASSERT_TRUE(__freadable(fp));
124 ASSERT_TRUE(__fwritable(fp));
125 fclose(fp);
126}
127
128TEST(stdio_ext, __fsetlocking) {
129 FILE* fp = fopen("/proc/version", "r");
130 // Android doesn't actually support the other modes.
131 ASSERT_EQ(FSETLOCKING_INTERNAL, __fsetlocking(fp, FSETLOCKING_QUERY));
132 fclose(fp);
133}