blob: 196c725aa668b4be587ca18d3111cd7ce67629ef [file] [log] [blame]
Elliott Hughes91875dc2012-09-24 17:55:15 -07001/*
2 * Copyright (C) 2012 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 <gtest/gtest.h>
18
19#include <errno.h>
20#include <stdio.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <unistd.h>
24
25TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) {
26 FILE* fp = tmpfile();
27 ASSERT_TRUE(fp != NULL);
28
29 int fd = fileno(fp);
30 ASSERT_NE(fd, -1);
31
32 struct stat sb;
33 int rc = fstat(fd, &sb);
34 ASSERT_NE(rc, -1);
35 ASSERT_EQ(sb.st_mode & 0777, 0600U);
36
37 rc = fprintf(fp, "hello\n");
38 ASSERT_EQ(rc, 6);
39
40 rewind(fp);
41
42 char buf[16];
43 char* s = fgets(buf, sizeof(buf), fp);
44 ASSERT_TRUE(s != NULL);
45 ASSERT_STREQ("hello\n", s);
46
47 fclose(fp);
48}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030049
50TEST(stdio, getdelim) {
51 FILE* fp = tmpfile();
52 ASSERT_TRUE(fp != NULL);
53
54 const char* line_written = "This is a test";
55 int rc = fprintf(fp, "%s", line_written);
56 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
57
58 rewind(fp);
59
60 char* word_read = NULL;
61 size_t allocated_length = 0;
62
63 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
64 for (size_t i = 0; i < 5; ++i) {
65 ASSERT_FALSE(feof(fp));
66 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
67 ASSERT_GE(allocated_length, strlen(expected[i]));
68 ASSERT_STREQ(word_read, expected[i]);
69 }
70 // The last read should have set the end-of-file indicator for the stream.
71 ASSERT_TRUE(feof(fp));
72 clearerr(fp);
73
74 // getdelim returns -1 but doesn't set errno if we're already at EOF.
75 // It should set the end-of-file indicator for the stream, though.
76 errno = 0;
77 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080078 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030079 ASSERT_TRUE(feof(fp));
80
81 free(word_read);
82 fclose(fp);
83}
84
85TEST(stdio, getdelim_invalid) {
86 FILE* fp = tmpfile();
87
88 char* buffer = NULL;
89 size_t buffer_length = 0;
90
91 // The first argument can't be NULL.
92 errno = 0;
93 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080094 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030095
96 // The second argument can't be NULL.
97 errno = 0;
98 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080099 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300100
101 // The stream can't be closed.
102 fclose(fp);
103 errno = 0;
104 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800105 ASSERT_EQ(EBADF, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300106}
107
108TEST(stdio, getline) {
109 FILE* fp = tmpfile();
110 ASSERT_TRUE(fp != NULL);
111
112 const char* line_written = "This is a test for getline\n";
113 const size_t line_count = 5;
114
115 for (size_t i = 0; i < line_count; ++i) {
116 int rc = fprintf(fp, "%s", line_written);
117 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
118 }
119
120 rewind(fp);
121
122 char* line_read = NULL;
123 size_t allocated_length = 0;
124
125 size_t read_line_count = 0;
126 ssize_t read_char_count;
127 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
128 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
129 ASSERT_GE(allocated_length, strlen(line_written));
130 ASSERT_STREQ(line_read, line_written);
131 ++read_line_count;
132 }
133 ASSERT_EQ(read_line_count, line_count);
134
135 // The last read should have set the end-of-file indicator for the stream.
136 ASSERT_TRUE(feof(fp));
137 clearerr(fp);
138
139 // getline returns -1 but doesn't set errno if we're already at EOF.
140 // It should set the end-of-file indicator for the stream, though.
141 errno = 0;
142 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800143 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300144 ASSERT_TRUE(feof(fp));
145
146 free(line_read);
147 fclose(fp);
148}
149
150TEST(stdio, getline_invalid) {
151 FILE* fp = tmpfile();
152
153 char* buffer = NULL;
154 size_t buffer_length = 0;
155
156 // The first argument can't be NULL.
157 errno = 0;
158 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800159 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300160
161 // The second argument can't be NULL.
162 errno = 0;
163 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800164 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300165
166 // The stream can't be closed.
167 fclose(fp);
168 errno = 0;
169 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800170 ASSERT_EQ(EBADF, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300171}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000172
173TEST(stdio, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800174#if __BIONIC__
175 // http://b/8253769
176 ASSERT_EQ(sizeof(__kernel_ssize_t), sizeof(long int));
177 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
178#else
179 // TODO: add a .c file so we can test this for bionic --- our C ssize_t is fine.
180 // For our 32-bit C++ ABI, we have a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000181 // error: format '%zd' expects argument of type 'signed size_t',
182 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
183 ssize_t v = 1;
184 char buf[32];
185 snprintf(buf, sizeof(buf), "%zd", v);
Elliott Hughese2556422013-02-28 10:51:14 -0800186#endif
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000187}