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