Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 1 | /* |
| 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> |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 21 | #include <limits.h> |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 22 | #include <math.h> |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 27 | #include <wchar.h> |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 28 | #include <locale.h> |
| 29 | |
| 30 | #include "TemporaryFile.h" |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 31 | |
| 32 | TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) { |
| 33 | FILE* fp = tmpfile(); |
| 34 | ASSERT_TRUE(fp != NULL); |
| 35 | |
| 36 | int fd = fileno(fp); |
| 37 | ASSERT_NE(fd, -1); |
| 38 | |
| 39 | struct stat sb; |
| 40 | int rc = fstat(fd, &sb); |
| 41 | ASSERT_NE(rc, -1); |
| 42 | ASSERT_EQ(sb.st_mode & 0777, 0600U); |
| 43 | |
| 44 | rc = fprintf(fp, "hello\n"); |
| 45 | ASSERT_EQ(rc, 6); |
| 46 | |
| 47 | rewind(fp); |
| 48 | |
| 49 | char buf[16]; |
| 50 | char* s = fgets(buf, sizeof(buf), fp); |
| 51 | ASSERT_TRUE(s != NULL); |
| 52 | ASSERT_STREQ("hello\n", s); |
| 53 | |
| 54 | fclose(fp); |
| 55 | } |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 56 | |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 57 | TEST(stdio, dprintf) { |
| 58 | TemporaryFile tf; |
| 59 | |
| 60 | int rc = dprintf(tf.fd, "hello\n"); |
| 61 | ASSERT_EQ(rc, 6); |
| 62 | |
| 63 | lseek(tf.fd, SEEK_SET, 0); |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 64 | FILE* tfile = fdopen(tf.fd, "r"); |
| 65 | ASSERT_TRUE(tfile != NULL); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 66 | |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 67 | char buf[7]; |
| 68 | ASSERT_EQ(buf, fgets(buf, sizeof(buf), tfile)); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 69 | ASSERT_STREQ("hello\n", buf); |
Christopher Ferris | 9e01ea6 | 2014-05-29 12:49:35 -0700 | [diff] [blame] | 70 | // Make sure there isn't anything else in the file. |
| 71 | ASSERT_EQ(NULL, fgets(buf, sizeof(buf), tfile)); |
| 72 | fclose(tfile); |
Calin Juravle | 6afb2a9 | 2014-05-22 11:47:47 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 75 | TEST(stdio, getdelim) { |
| 76 | FILE* fp = tmpfile(); |
| 77 | ASSERT_TRUE(fp != NULL); |
| 78 | |
| 79 | const char* line_written = "This is a test"; |
| 80 | int rc = fprintf(fp, "%s", line_written); |
| 81 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 82 | |
| 83 | rewind(fp); |
| 84 | |
| 85 | char* word_read = NULL; |
| 86 | size_t allocated_length = 0; |
| 87 | |
| 88 | const char* expected[] = { "This ", " ", "is ", "a ", "test" }; |
| 89 | for (size_t i = 0; i < 5; ++i) { |
| 90 | ASSERT_FALSE(feof(fp)); |
| 91 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i]))); |
| 92 | ASSERT_GE(allocated_length, strlen(expected[i])); |
| 93 | ASSERT_STREQ(word_read, expected[i]); |
| 94 | } |
| 95 | // The last read should have set the end-of-file indicator for the stream. |
| 96 | ASSERT_TRUE(feof(fp)); |
| 97 | clearerr(fp); |
| 98 | |
| 99 | // getdelim returns -1 but doesn't set errno if we're already at EOF. |
| 100 | // It should set the end-of-file indicator for the stream, though. |
| 101 | errno = 0; |
| 102 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 103 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 104 | ASSERT_TRUE(feof(fp)); |
| 105 | |
| 106 | free(word_read); |
| 107 | fclose(fp); |
| 108 | } |
| 109 | |
| 110 | TEST(stdio, getdelim_invalid) { |
| 111 | FILE* fp = tmpfile(); |
Elliott Hughes | 6ad8f76 | 2013-12-19 14:56:17 -0800 | [diff] [blame] | 112 | ASSERT_TRUE(fp != NULL); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 113 | |
| 114 | char* buffer = NULL; |
| 115 | size_t buffer_length = 0; |
| 116 | |
| 117 | // The first argument can't be NULL. |
| 118 | errno = 0; |
| 119 | ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 120 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 121 | |
| 122 | // The second argument can't be NULL. |
| 123 | errno = 0; |
| 124 | ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 125 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 126 | |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 127 | // The underlying fd can't be closed. |
| 128 | ASSERT_EQ(0, close(fileno(fp))); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 129 | errno = 0; |
| 130 | ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 131 | ASSERT_EQ(EBADF, errno); |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 132 | fclose(fp); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | TEST(stdio, getline) { |
| 136 | FILE* fp = tmpfile(); |
| 137 | ASSERT_TRUE(fp != NULL); |
| 138 | |
| 139 | const char* line_written = "This is a test for getline\n"; |
| 140 | const size_t line_count = 5; |
| 141 | |
| 142 | for (size_t i = 0; i < line_count; ++i) { |
| 143 | int rc = fprintf(fp, "%s", line_written); |
| 144 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 145 | } |
| 146 | |
| 147 | rewind(fp); |
| 148 | |
| 149 | char* line_read = NULL; |
| 150 | size_t allocated_length = 0; |
| 151 | |
| 152 | size_t read_line_count = 0; |
| 153 | ssize_t read_char_count; |
| 154 | while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) { |
| 155 | ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written))); |
| 156 | ASSERT_GE(allocated_length, strlen(line_written)); |
| 157 | ASSERT_STREQ(line_read, line_written); |
| 158 | ++read_line_count; |
| 159 | } |
| 160 | ASSERT_EQ(read_line_count, line_count); |
| 161 | |
| 162 | // The last read should have set the end-of-file indicator for the stream. |
| 163 | ASSERT_TRUE(feof(fp)); |
| 164 | clearerr(fp); |
| 165 | |
| 166 | // getline returns -1 but doesn't set errno if we're already at EOF. |
| 167 | // It should set the end-of-file indicator for the stream, though. |
| 168 | errno = 0; |
| 169 | ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 170 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 171 | ASSERT_TRUE(feof(fp)); |
| 172 | |
| 173 | free(line_read); |
| 174 | fclose(fp); |
| 175 | } |
| 176 | |
| 177 | TEST(stdio, getline_invalid) { |
| 178 | FILE* fp = tmpfile(); |
Elliott Hughes | 6ad8f76 | 2013-12-19 14:56:17 -0800 | [diff] [blame] | 179 | ASSERT_TRUE(fp != NULL); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 180 | |
| 181 | char* buffer = NULL; |
| 182 | size_t buffer_length = 0; |
| 183 | |
| 184 | // The first argument can't be NULL. |
| 185 | errno = 0; |
| 186 | ASSERT_EQ(getline(NULL, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 187 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 188 | |
| 189 | // The second argument can't be NULL. |
| 190 | errno = 0; |
| 191 | ASSERT_EQ(getline(&buffer, NULL, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 192 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 193 | |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 194 | // The underlying fd can't be closed. |
| 195 | ASSERT_EQ(0, close(fileno(fp))); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 196 | errno = 0; |
| 197 | ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 198 | ASSERT_EQ(EBADF, errno); |
Elliott Hughes | 20f8aec | 2014-05-12 15:15:37 -0700 | [diff] [blame] | 199 | fclose(fp); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 200 | } |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 201 | |
| 202 | TEST(stdio, printf_ssize_t) { |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 203 | // http://b/8253769 |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 204 | ASSERT_EQ(sizeof(ssize_t), sizeof(long int)); |
Elliott Hughes | b6e2248 | 2013-03-08 15:28:52 -0800 | [diff] [blame] | 205 | ASSERT_EQ(sizeof(ssize_t), sizeof(size_t)); |
| 206 | // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying: |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 207 | // error: format '%zd' expects argument of type 'signed size_t', |
| 208 | // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format] |
| 209 | ssize_t v = 1; |
| 210 | char buf[32]; |
| 211 | snprintf(buf, sizeof(buf), "%zd", v); |
| 212 | } |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 213 | |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 214 | // https://code.google.com/p/android/issues/detail?id=64886 |
| 215 | TEST(stdio, snprintf_a) { |
| 216 | char buf[BUFSIZ]; |
| 217 | EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235)); |
| 218 | EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf); |
| 219 | } |
| 220 | |
| 221 | TEST(stdio, snprintf_lc) { |
| 222 | char buf[BUFSIZ]; |
| 223 | wint_t wc = L'a'; |
| 224 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc)); |
| 225 | EXPECT_STREQ("<a>", buf); |
| 226 | } |
| 227 | |
| 228 | TEST(stdio, snprintf_ls) { |
| 229 | char buf[BUFSIZ]; |
| 230 | wchar_t* ws = NULL; |
| 231 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 232 | EXPECT_STREQ("<(null)>", buf); |
| 233 | |
| 234 | wchar_t chars[] = { L'h', L'i', 0 }; |
| 235 | ws = chars; |
| 236 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 237 | EXPECT_STREQ("<hi>", buf); |
| 238 | } |
| 239 | |
| 240 | TEST(stdio, snprintf_n) { |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 241 | #if defined(__BIONIC__) |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 242 | // http://b/14492135 |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 243 | char buf[32]; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 244 | int i = 1234; |
| 245 | EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i)); |
| 246 | EXPECT_EQ(1234, i); |
| 247 | EXPECT_STREQ("a n b", buf); |
| 248 | #else |
| 249 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 250 | #endif |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 251 | } |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 252 | |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 253 | TEST(stdio, snprintf_smoke) { |
| 254 | char buf[BUFSIZ]; |
| 255 | |
| 256 | snprintf(buf, sizeof(buf), "a"); |
| 257 | EXPECT_STREQ("a", buf); |
| 258 | |
| 259 | snprintf(buf, sizeof(buf), "%%"); |
| 260 | EXPECT_STREQ("%", buf); |
| 261 | |
| 262 | snprintf(buf, sizeof(buf), "01234"); |
| 263 | EXPECT_STREQ("01234", buf); |
| 264 | |
| 265 | snprintf(buf, sizeof(buf), "a%sb", "01234"); |
| 266 | EXPECT_STREQ("a01234b", buf); |
| 267 | |
| 268 | char* s = NULL; |
| 269 | snprintf(buf, sizeof(buf), "a%sb", s); |
| 270 | EXPECT_STREQ("a(null)b", buf); |
| 271 | |
| 272 | snprintf(buf, sizeof(buf), "aa%scc", "bb"); |
| 273 | EXPECT_STREQ("aabbcc", buf); |
| 274 | |
| 275 | snprintf(buf, sizeof(buf), "a%cc", 'b'); |
| 276 | EXPECT_STREQ("abc", buf); |
| 277 | |
| 278 | snprintf(buf, sizeof(buf), "a%db", 1234); |
| 279 | EXPECT_STREQ("a1234b", buf); |
| 280 | |
| 281 | snprintf(buf, sizeof(buf), "a%db", -8123); |
| 282 | EXPECT_STREQ("a-8123b", buf); |
| 283 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 284 | snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 285 | EXPECT_STREQ("a16b", buf); |
| 286 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 287 | snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 288 | EXPECT_STREQ("a16b", buf); |
| 289 | |
| 290 | snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL); |
| 291 | EXPECT_STREQ("a68719476736b", buf); |
| 292 | |
| 293 | snprintf(buf, sizeof(buf), "a%ldb", 70000L); |
| 294 | EXPECT_STREQ("a70000b", buf); |
| 295 | |
| 296 | snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234)); |
| 297 | EXPECT_STREQ("a0xb0001234b", buf); |
| 298 | |
| 299 | snprintf(buf, sizeof(buf), "a%xz", 0x12ab); |
| 300 | EXPECT_STREQ("a12abz", buf); |
| 301 | |
| 302 | snprintf(buf, sizeof(buf), "a%Xz", 0x12ab); |
| 303 | EXPECT_STREQ("a12ABz", buf); |
| 304 | |
| 305 | snprintf(buf, sizeof(buf), "a%08xz", 0x123456); |
| 306 | EXPECT_STREQ("a00123456z", buf); |
| 307 | |
| 308 | snprintf(buf, sizeof(buf), "a%5dz", 1234); |
| 309 | EXPECT_STREQ("a 1234z", buf); |
| 310 | |
| 311 | snprintf(buf, sizeof(buf), "a%05dz", 1234); |
| 312 | EXPECT_STREQ("a01234z", buf); |
| 313 | |
| 314 | snprintf(buf, sizeof(buf), "a%8dz", 1234); |
| 315 | EXPECT_STREQ("a 1234z", buf); |
| 316 | |
| 317 | snprintf(buf, sizeof(buf), "a%-8dz", 1234); |
| 318 | EXPECT_STREQ("a1234 z", buf); |
| 319 | |
| 320 | snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef"); |
| 321 | EXPECT_STREQ("Aabcdef Z", buf); |
| 322 | |
| 323 | snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234); |
| 324 | EXPECT_STREQ("Ahello:1234Z", buf); |
| 325 | |
| 326 | snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5); |
| 327 | EXPECT_STREQ("a005:5:05z", buf); |
| 328 | |
| 329 | void* p = NULL; |
| 330 | snprintf(buf, sizeof(buf), "a%d,%pz", 5, p); |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 331 | #if defined(__BIONIC__) |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 332 | EXPECT_STREQ("a5,0x0z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 333 | #else // __BIONIC__ |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 334 | EXPECT_STREQ("a5,(nil)z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 335 | #endif // __BIONIC__ |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 336 | |
| 337 | snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8); |
| 338 | EXPECT_STREQ("a68719476736,6,7,8z", buf); |
| 339 | |
| 340 | snprintf(buf, sizeof(buf), "a_%f_b", 1.23f); |
| 341 | EXPECT_STREQ("a_1.230000_b", buf); |
| 342 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 343 | snprintf(buf, sizeof(buf), "a_%g_b", 3.14); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 344 | EXPECT_STREQ("a_3.14_b", buf); |
Alexander Ivchenko | edd7c2e | 2014-04-01 17:01:39 +0400 | [diff] [blame] | 345 | |
| 346 | snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice"); |
| 347 | EXPECT_STREQ("print_me_twice print_me_twice", buf); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 350 | TEST(stdio, snprintf_f_special) { |
| 351 | char buf[BUFSIZ]; |
| 352 | snprintf(buf, sizeof(buf), "%f", nanf("")); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 353 | EXPECT_STRCASEEQ("NaN", buf); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 354 | |
| 355 | snprintf(buf, sizeof(buf), "%f", HUGE_VALF); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 356 | EXPECT_STRCASEEQ("Inf", buf); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | TEST(stdio, snprintf_g_special) { |
| 360 | char buf[BUFSIZ]; |
| 361 | snprintf(buf, sizeof(buf), "%g", nan("")); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 362 | EXPECT_STRCASEEQ("NaN", buf); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 363 | |
| 364 | snprintf(buf, sizeof(buf), "%g", HUGE_VAL); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 365 | EXPECT_STRCASEEQ("Inf", buf); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 368 | TEST(stdio, snprintf_d_INT_MAX) { |
| 369 | char buf[BUFSIZ]; |
| 370 | snprintf(buf, sizeof(buf), "%d", INT_MAX); |
| 371 | EXPECT_STREQ("2147483647", buf); |
| 372 | } |
| 373 | |
| 374 | TEST(stdio, snprintf_d_INT_MIN) { |
| 375 | char buf[BUFSIZ]; |
| 376 | snprintf(buf, sizeof(buf), "%d", INT_MIN); |
| 377 | EXPECT_STREQ("-2147483648", buf); |
| 378 | } |
| 379 | |
| 380 | TEST(stdio, snprintf_ld_LONG_MAX) { |
| 381 | char buf[BUFSIZ]; |
| 382 | snprintf(buf, sizeof(buf), "%ld", LONG_MAX); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 383 | #if __LP64__ |
| 384 | EXPECT_STREQ("9223372036854775807", buf); |
| 385 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 386 | EXPECT_STREQ("2147483647", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 387 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | TEST(stdio, snprintf_ld_LONG_MIN) { |
| 391 | char buf[BUFSIZ]; |
| 392 | snprintf(buf, sizeof(buf), "%ld", LONG_MIN); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 393 | #if __LP64__ |
| 394 | EXPECT_STREQ("-9223372036854775808", buf); |
| 395 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 396 | EXPECT_STREQ("-2147483648", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 397 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | TEST(stdio, snprintf_lld_LLONG_MAX) { |
| 401 | char buf[BUFSIZ]; |
| 402 | snprintf(buf, sizeof(buf), "%lld", LLONG_MAX); |
| 403 | EXPECT_STREQ("9223372036854775807", buf); |
| 404 | } |
| 405 | |
| 406 | TEST(stdio, snprintf_lld_LLONG_MIN) { |
| 407 | char buf[BUFSIZ]; |
| 408 | snprintf(buf, sizeof(buf), "%lld", LLONG_MIN); |
| 409 | EXPECT_STREQ("-9223372036854775808", buf); |
| 410 | } |
| 411 | |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 412 | TEST(stdio, snprintf_e) { |
| 413 | char buf[BUFSIZ]; |
| 414 | |
| 415 | snprintf(buf, sizeof(buf), "%e", 1.5); |
| 416 | EXPECT_STREQ("1.500000e+00", buf); |
| 417 | |
| 418 | snprintf(buf, sizeof(buf), "%Le", 1.5l); |
| 419 | EXPECT_STREQ("1.500000e+00", buf); |
| 420 | } |
| 421 | |
Elliott Hughes | e77f38f | 2014-05-14 12:39:12 -0700 | [diff] [blame] | 422 | TEST(stdio, snprintf_negative_zero_5084292) { |
| 423 | char buf[BUFSIZ]; |
| 424 | |
| 425 | snprintf(buf, sizeof(buf), "%f", -0.0); |
| 426 | EXPECT_STREQ("-0.000000", buf); |
| 427 | } |
| 428 | |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 429 | TEST(stdio, snprintf_utf8_15439554) { |
| 430 | // http://b/15439554 |
| 431 | char buf[BUFSIZ]; |
| 432 | |
| 433 | // 1-byte character. |
| 434 | snprintf(buf, sizeof(buf), "%dx%d", 1, 2); |
| 435 | EXPECT_STREQ("1x2", buf); |
| 436 | // 2-byte character. |
| 437 | snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2); |
| 438 | EXPECT_STREQ("1¢2", buf); |
| 439 | // 3-byte character. |
| 440 | snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2); |
| 441 | EXPECT_STREQ("1€2", buf); |
| 442 | // 4-byte character. |
| 443 | snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2); |
| 444 | EXPECT_STREQ("1ð¤¢2", buf); |
| 445 | } |
| 446 | |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 447 | TEST(stdio, fprintf_failures_7229520) { |
Elliott Hughes | 69f05d2 | 2014-06-05 20:10:09 -0700 | [diff] [blame] | 448 | // http://b/7229520 |
Elliott Hughes | c9244bd | 2014-05-14 13:31:35 -0700 | [diff] [blame] | 449 | FILE* fp; |
| 450 | |
| 451 | // Unbuffered case where the fprintf(3) itself fails. |
| 452 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 453 | setbuf(fp, NULL); |
| 454 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 455 | ASSERT_EQ(0, close(fileno(fp))); |
| 456 | ASSERT_EQ(-1, fprintf(fp, "fail")); |
| 457 | ASSERT_EQ(-1, fclose(fp)); |
| 458 | |
| 459 | // Buffered case where we won't notice until the fclose(3). |
| 460 | // It's likely this is what was actually seen in http://b/7229520, |
| 461 | // and that expecting fprintf to fail is setting yourself up for |
| 462 | // disappointment. Remember to check fclose(3)'s return value, kids! |
| 463 | ASSERT_NE(nullptr, fp = tmpfile()); |
| 464 | ASSERT_EQ(4, fprintf(fp, "epic")); |
| 465 | ASSERT_EQ(0, close(fileno(fp))); |
| 466 | ASSERT_EQ(4, fprintf(fp, "fail")); |
| 467 | ASSERT_EQ(-1, fclose(fp)); |
| 468 | } |
| 469 | |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 470 | TEST(stdio, popen) { |
| 471 | FILE* fp = popen("cat /proc/version", "r"); |
| 472 | ASSERT_TRUE(fp != NULL); |
| 473 | |
| 474 | char buf[16]; |
| 475 | char* s = fgets(buf, sizeof(buf), fp); |
| 476 | buf[13] = '\0'; |
| 477 | ASSERT_STREQ("Linux version", s); |
| 478 | |
| 479 | ASSERT_EQ(0, pclose(fp)); |
| 480 | } |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 481 | |
| 482 | TEST(stdio, getc) { |
| 483 | FILE* fp = fopen("/proc/version", "r"); |
| 484 | ASSERT_TRUE(fp != NULL); |
| 485 | ASSERT_EQ('L', getc(fp)); |
| 486 | ASSERT_EQ('i', getc(fp)); |
| 487 | ASSERT_EQ('n', getc(fp)); |
| 488 | ASSERT_EQ('u', getc(fp)); |
| 489 | ASSERT_EQ('x', getc(fp)); |
| 490 | fclose(fp); |
| 491 | } |
| 492 | |
| 493 | TEST(stdio, putc) { |
| 494 | FILE* fp = fopen("/proc/version", "r"); |
| 495 | ASSERT_TRUE(fp != NULL); |
| 496 | ASSERT_EQ(EOF, putc('x', fp)); |
| 497 | fclose(fp); |
| 498 | } |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 499 | |
| 500 | TEST(stdio, sscanf) { |
| 501 | char s1[123]; |
| 502 | int i1; |
| 503 | double d1; |
| 504 | char s2[123]; |
| 505 | ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2)); |
| 506 | ASSERT_STREQ("hello", s1); |
| 507 | ASSERT_EQ(123, i1); |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 508 | ASSERT_DOUBLE_EQ(1.23, d1); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 509 | } |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 510 | |
| 511 | TEST(stdio, cantwrite_EBADF) { |
| 512 | // If we open a file read-only... |
| 513 | FILE* fp = fopen("/proc/version", "r"); |
| 514 | |
| 515 | // ...all attempts to write to that file should return failure. |
| 516 | |
| 517 | // They should also set errno to EBADF. This isn't POSIX, but it's traditional. |
| 518 | // glibc gets the wide-character functions wrong. |
| 519 | |
| 520 | errno = 0; |
| 521 | EXPECT_EQ(EOF, putc('x', fp)); |
| 522 | EXPECT_EQ(EBADF, errno); |
| 523 | |
| 524 | errno = 0; |
| 525 | EXPECT_EQ(EOF, fprintf(fp, "hello")); |
| 526 | EXPECT_EQ(EBADF, errno); |
| 527 | |
| 528 | errno = 0; |
| 529 | EXPECT_EQ(EOF, fwprintf(fp, L"hello")); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 530 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 531 | EXPECT_EQ(EBADF, errno); |
| 532 | #endif |
| 533 | |
| 534 | errno = 0; |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 535 | EXPECT_EQ(0U, fwrite("hello", 1, 2, fp)); |
| 536 | EXPECT_EQ(EBADF, errno); |
| 537 | |
| 538 | errno = 0; |
| 539 | EXPECT_EQ(EOF, fputs("hello", fp)); |
| 540 | EXPECT_EQ(EBADF, errno); |
| 541 | |
| 542 | errno = 0; |
| 543 | EXPECT_EQ(WEOF, fputwc(L'x', fp)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 544 | #if defined(__BIONIC__) |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 545 | EXPECT_EQ(EBADF, errno); |
| 546 | #endif |
| 547 | } |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 548 | |
| 549 | // Tests that we can only have a consistent and correct fpos_t when using |
| 550 | // f*pos functions (i.e. fpos doesn't get inside a multi byte character). |
| 551 | TEST(stdio, consistent_fpos_t) { |
| 552 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 553 | uselocale(LC_GLOBAL_LOCALE); |
| 554 | |
| 555 | FILE* fp = tmpfile(); |
| 556 | ASSERT_TRUE(fp != NULL); |
| 557 | |
| 558 | wchar_t mb_one_bytes = L'h'; |
| 559 | wchar_t mb_two_bytes = 0x00a2; |
| 560 | wchar_t mb_three_bytes = 0x20ac; |
| 561 | wchar_t mb_four_bytes = 0x24b62; |
| 562 | |
| 563 | // Write to file. |
| 564 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp))); |
| 565 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 566 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 567 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 568 | |
| 569 | rewind(fp); |
| 570 | |
| 571 | // Record each character position. |
| 572 | fpos_t pos1; |
| 573 | fpos_t pos2; |
| 574 | fpos_t pos3; |
| 575 | fpos_t pos4; |
| 576 | fpos_t pos5; |
| 577 | EXPECT_EQ(0, fgetpos(fp, &pos1)); |
| 578 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 579 | EXPECT_EQ(0, fgetpos(fp, &pos2)); |
| 580 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 581 | EXPECT_EQ(0, fgetpos(fp, &pos3)); |
| 582 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 583 | EXPECT_EQ(0, fgetpos(fp, &pos4)); |
| 584 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 585 | EXPECT_EQ(0, fgetpos(fp, &pos5)); |
| 586 | |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 587 | #if defined(__BIONIC__) |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 588 | // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD |
| 589 | // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In |
| 590 | // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE |
| 591 | // structure. |
| 592 | ASSERT_EQ(0, static_cast<off_t>(pos1)); |
| 593 | ASSERT_EQ(1, static_cast<off_t>(pos2)); |
| 594 | ASSERT_EQ(3, static_cast<off_t>(pos3)); |
| 595 | ASSERT_EQ(6, static_cast<off_t>(pos4)); |
| 596 | ASSERT_EQ(10, static_cast<off_t>(pos5)); |
| 597 | #endif |
| 598 | |
| 599 | // Exercise back and forth movements of the position. |
| 600 | ASSERT_EQ(0, fsetpos(fp, &pos2)); |
| 601 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 602 | ASSERT_EQ(0, fsetpos(fp, &pos1)); |
| 603 | ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 604 | ASSERT_EQ(0, fsetpos(fp, &pos4)); |
| 605 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 606 | ASSERT_EQ(0, fsetpos(fp, &pos3)); |
| 607 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 608 | ASSERT_EQ(0, fsetpos(fp, &pos5)); |
| 609 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 610 | |
| 611 | fclose(fp); |
| 612 | } |
| 613 | |
| 614 | // Exercise the interaction between fpos and seek. |
| 615 | TEST(stdio, fpos_t_and_seek) { |
| 616 | ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8")); |
| 617 | uselocale(LC_GLOBAL_LOCALE); |
| 618 | |
Calin Juravle | 9b95ea9 | 2014-05-14 17:07:10 +0100 | [diff] [blame] | 619 | // In glibc-2.16 fseek doesn't work properly in wide mode |
| 620 | // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is |
| 621 | // to close and re-open the file. We do it in order to make the test pass |
| 622 | // with all glibcs. |
| 623 | |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 624 | TemporaryFile tf; |
| 625 | FILE* fp = fdopen(tf.fd, "w+"); |
| 626 | ASSERT_TRUE(fp != NULL); |
| 627 | |
| 628 | wchar_t mb_two_bytes = 0x00a2; |
| 629 | wchar_t mb_three_bytes = 0x20ac; |
| 630 | wchar_t mb_four_bytes = 0x24b62; |
| 631 | |
| 632 | // Write to file. |
| 633 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp))); |
| 634 | ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp))); |
| 635 | ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp))); |
| 636 | |
| 637 | fflush(fp); |
| 638 | fclose(fp); |
| 639 | |
| 640 | fp = fopen(tf.filename, "r"); |
| 641 | ASSERT_TRUE(fp != NULL); |
| 642 | |
| 643 | // Store a valid position. |
| 644 | fpos_t mb_two_bytes_pos; |
| 645 | ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos)); |
| 646 | |
| 647 | // Move inside mb_four_bytes with fseek. |
| 648 | long offset_inside_mb = 6; |
| 649 | ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET)); |
| 650 | |
| 651 | // Store the "inside multi byte" position. |
| 652 | fpos_t pos_inside_mb; |
| 653 | ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb)); |
Elliott Hughes | 063525c | 2014-05-13 11:19:57 -0700 | [diff] [blame] | 654 | #if defined(__BIONIC__) |
| 655 | ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb)); |
| 656 | #endif |
Calin Juravle | 03e4ebe | 2014-05-08 14:42:06 +0100 | [diff] [blame] | 657 | |
| 658 | // Reading from within a byte should produce an error. |
| 659 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 660 | ASSERT_EQ(EILSEQ, errno); |
| 661 | |
| 662 | // Reverting to a valid position should work. |
| 663 | ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos)); |
| 664 | ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp))); |
| 665 | |
| 666 | // Moving withing a multi byte with fsetpos should work but reading should |
| 667 | // produce an error. |
| 668 | ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb)); |
| 669 | ASSERT_EQ(WEOF, fgetwc(fp)); |
| 670 | ASSERT_EQ(EILSEQ, errno); |
| 671 | |
| 672 | fclose(fp); |
| 673 | } |