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 | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 20 | #include <limits.h> |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 21 | #include <math.h> |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <unistd.h> |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 26 | #include <wchar.h> |
Elliott Hughes | 91875dc | 2012-09-24 17:55:15 -0700 | [diff] [blame] | 27 | |
| 28 | TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) { |
| 29 | FILE* fp = tmpfile(); |
| 30 | ASSERT_TRUE(fp != NULL); |
| 31 | |
| 32 | int fd = fileno(fp); |
| 33 | ASSERT_NE(fd, -1); |
| 34 | |
| 35 | struct stat sb; |
| 36 | int rc = fstat(fd, &sb); |
| 37 | ASSERT_NE(rc, -1); |
| 38 | ASSERT_EQ(sb.st_mode & 0777, 0600U); |
| 39 | |
| 40 | rc = fprintf(fp, "hello\n"); |
| 41 | ASSERT_EQ(rc, 6); |
| 42 | |
| 43 | rewind(fp); |
| 44 | |
| 45 | char buf[16]; |
| 46 | char* s = fgets(buf, sizeof(buf), fp); |
| 47 | ASSERT_TRUE(s != NULL); |
| 48 | ASSERT_STREQ("hello\n", s); |
| 49 | |
| 50 | fclose(fp); |
| 51 | } |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 52 | |
| 53 | TEST(stdio, getdelim) { |
| 54 | FILE* fp = tmpfile(); |
| 55 | ASSERT_TRUE(fp != NULL); |
| 56 | |
| 57 | const char* line_written = "This is a test"; |
| 58 | int rc = fprintf(fp, "%s", line_written); |
| 59 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 60 | |
| 61 | rewind(fp); |
| 62 | |
| 63 | char* word_read = NULL; |
| 64 | size_t allocated_length = 0; |
| 65 | |
| 66 | const char* expected[] = { "This ", " ", "is ", "a ", "test" }; |
| 67 | for (size_t i = 0; i < 5; ++i) { |
| 68 | ASSERT_FALSE(feof(fp)); |
| 69 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i]))); |
| 70 | ASSERT_GE(allocated_length, strlen(expected[i])); |
| 71 | ASSERT_STREQ(word_read, expected[i]); |
| 72 | } |
| 73 | // The last read should have set the end-of-file indicator for the stream. |
| 74 | ASSERT_TRUE(feof(fp)); |
| 75 | clearerr(fp); |
| 76 | |
| 77 | // getdelim returns -1 but doesn't set errno if we're already at EOF. |
| 78 | // It should set the end-of-file indicator for the stream, though. |
| 79 | errno = 0; |
| 80 | ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 81 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 82 | ASSERT_TRUE(feof(fp)); |
| 83 | |
| 84 | free(word_read); |
| 85 | fclose(fp); |
| 86 | } |
| 87 | |
| 88 | TEST(stdio, getdelim_invalid) { |
| 89 | FILE* fp = tmpfile(); |
Elliott Hughes | 6ad8f76 | 2013-12-19 14:56:17 -0800 | [diff] [blame] | 90 | ASSERT_TRUE(fp != NULL); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 91 | |
| 92 | char* buffer = NULL; |
| 93 | size_t buffer_length = 0; |
| 94 | |
| 95 | // The first argument can't be NULL. |
| 96 | errno = 0; |
| 97 | ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 98 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 99 | |
| 100 | // The second argument can't be NULL. |
| 101 | errno = 0; |
| 102 | ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 103 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 104 | |
| 105 | // The stream can't be closed. |
| 106 | fclose(fp); |
| 107 | errno = 0; |
| 108 | ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1); |
Christopher Ferris | cbd85b9 | 2013-11-15 15:16:01 -0800 | [diff] [blame] | 109 | // glibc sometimes doesn't set errno in this particular case. |
| 110 | #if defined(__BIONIC__) |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 111 | ASSERT_EQ(EBADF, errno); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 112 | #endif // __BIONIC__ |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TEST(stdio, getline) { |
| 116 | FILE* fp = tmpfile(); |
| 117 | ASSERT_TRUE(fp != NULL); |
| 118 | |
| 119 | const char* line_written = "This is a test for getline\n"; |
| 120 | const size_t line_count = 5; |
| 121 | |
| 122 | for (size_t i = 0; i < line_count; ++i) { |
| 123 | int rc = fprintf(fp, "%s", line_written); |
| 124 | ASSERT_EQ(rc, static_cast<int>(strlen(line_written))); |
| 125 | } |
| 126 | |
| 127 | rewind(fp); |
| 128 | |
| 129 | char* line_read = NULL; |
| 130 | size_t allocated_length = 0; |
| 131 | |
| 132 | size_t read_line_count = 0; |
| 133 | ssize_t read_char_count; |
| 134 | while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) { |
| 135 | ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written))); |
| 136 | ASSERT_GE(allocated_length, strlen(line_written)); |
| 137 | ASSERT_STREQ(line_read, line_written); |
| 138 | ++read_line_count; |
| 139 | } |
| 140 | ASSERT_EQ(read_line_count, line_count); |
| 141 | |
| 142 | // The last read should have set the end-of-file indicator for the stream. |
| 143 | ASSERT_TRUE(feof(fp)); |
| 144 | clearerr(fp); |
| 145 | |
| 146 | // getline returns -1 but doesn't set errno if we're already at EOF. |
| 147 | // It should set the end-of-file indicator for the stream, though. |
| 148 | errno = 0; |
| 149 | ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 150 | ASSERT_EQ(0, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 151 | ASSERT_TRUE(feof(fp)); |
| 152 | |
| 153 | free(line_read); |
| 154 | fclose(fp); |
| 155 | } |
| 156 | |
| 157 | TEST(stdio, getline_invalid) { |
| 158 | FILE* fp = tmpfile(); |
Elliott Hughes | 6ad8f76 | 2013-12-19 14:56:17 -0800 | [diff] [blame] | 159 | ASSERT_TRUE(fp != NULL); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 160 | |
| 161 | char* buffer = NULL; |
| 162 | size_t buffer_length = 0; |
| 163 | |
| 164 | // The first argument can't be NULL. |
| 165 | errno = 0; |
| 166 | ASSERT_EQ(getline(NULL, &buffer_length, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 167 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 168 | |
| 169 | // The second argument can't be NULL. |
| 170 | errno = 0; |
| 171 | ASSERT_EQ(getline(&buffer, NULL, fp), -1); |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 172 | ASSERT_EQ(EINVAL, errno); |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 173 | |
| 174 | // The stream can't be closed. |
| 175 | fclose(fp); |
| 176 | errno = 0; |
| 177 | ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1); |
Christopher Ferris | cbd85b9 | 2013-11-15 15:16:01 -0800 | [diff] [blame] | 178 | // glibc sometimes doesn't set errno in this particular case. |
| 179 | #if defined(__BIONIC__) |
Elliott Hughes | 5e3fc43 | 2013-02-11 16:36:48 -0800 | [diff] [blame] | 180 | ASSERT_EQ(EBADF, errno); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 181 | #endif // __BIONIC__ |
Irina Tirdea | eac9eb4 | 2012-09-08 09:28:30 +0300 | [diff] [blame] | 182 | } |
Thorsten Glaser | c641caf | 2013-02-17 16:50:58 +0000 | [diff] [blame] | 183 | |
| 184 | TEST(stdio, printf_ssize_t) { |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 185 | // http://b/8253769 |
Elliott Hughes | e255642 | 2013-02-28 10:51:14 -0800 | [diff] [blame] | 186 | ASSERT_EQ(sizeof(ssize_t), sizeof(long int)); |
Elliott Hughes | b6e2248 | 2013-03-08 15:28:52 -0800 | [diff] [blame] | 187 | ASSERT_EQ(sizeof(ssize_t), sizeof(size_t)); |
| 188 | // 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] | 189 | // error: format '%zd' expects argument of type 'signed size_t', |
| 190 | // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format] |
| 191 | ssize_t v = 1; |
| 192 | char buf[32]; |
| 193 | snprintf(buf, sizeof(buf), "%zd", v); |
| 194 | } |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 195 | |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 196 | // https://code.google.com/p/android/issues/detail?id=64886 |
| 197 | TEST(stdio, snprintf_a) { |
| 198 | char buf[BUFSIZ]; |
| 199 | EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235)); |
| 200 | EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf); |
| 201 | } |
| 202 | |
| 203 | TEST(stdio, snprintf_lc) { |
| 204 | char buf[BUFSIZ]; |
| 205 | wint_t wc = L'a'; |
| 206 | EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc)); |
| 207 | EXPECT_STREQ("<a>", buf); |
| 208 | } |
| 209 | |
| 210 | TEST(stdio, snprintf_ls) { |
| 211 | char buf[BUFSIZ]; |
| 212 | wchar_t* ws = NULL; |
| 213 | EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 214 | EXPECT_STREQ("<(null)>", buf); |
| 215 | |
| 216 | wchar_t chars[] = { L'h', L'i', 0 }; |
| 217 | ws = chars; |
| 218 | EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws)); |
| 219 | EXPECT_STREQ("<hi>", buf); |
| 220 | } |
| 221 | |
| 222 | TEST(stdio, snprintf_n) { |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 223 | #if !defined(__GLIBC__) |
| 224 | // http://b/14492135 |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 225 | char buf[32]; |
Elliott Hughes | e2341d0 | 2014-05-02 18:16:32 -0700 | [diff] [blame] | 226 | int i = 1234; |
| 227 | EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i)); |
| 228 | EXPECT_EQ(1234, i); |
| 229 | EXPECT_STREQ("a n b", buf); |
| 230 | #else |
| 231 | GTEST_LOG_(INFO) << "This test does nothing.\n"; |
| 232 | #endif |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 233 | } |
Elliott Hughes | 7248a2d | 2013-09-24 18:01:33 -0700 | [diff] [blame] | 234 | |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 235 | TEST(stdio, snprintf_smoke) { |
| 236 | char buf[BUFSIZ]; |
| 237 | |
| 238 | snprintf(buf, sizeof(buf), "a"); |
| 239 | EXPECT_STREQ("a", buf); |
| 240 | |
| 241 | snprintf(buf, sizeof(buf), "%%"); |
| 242 | EXPECT_STREQ("%", buf); |
| 243 | |
| 244 | snprintf(buf, sizeof(buf), "01234"); |
| 245 | EXPECT_STREQ("01234", buf); |
| 246 | |
| 247 | snprintf(buf, sizeof(buf), "a%sb", "01234"); |
| 248 | EXPECT_STREQ("a01234b", buf); |
| 249 | |
| 250 | char* s = NULL; |
| 251 | snprintf(buf, sizeof(buf), "a%sb", s); |
| 252 | EXPECT_STREQ("a(null)b", buf); |
| 253 | |
| 254 | snprintf(buf, sizeof(buf), "aa%scc", "bb"); |
| 255 | EXPECT_STREQ("aabbcc", buf); |
| 256 | |
| 257 | snprintf(buf, sizeof(buf), "a%cc", 'b'); |
| 258 | EXPECT_STREQ("abc", buf); |
| 259 | |
| 260 | snprintf(buf, sizeof(buf), "a%db", 1234); |
| 261 | EXPECT_STREQ("a1234b", buf); |
| 262 | |
| 263 | snprintf(buf, sizeof(buf), "a%db", -8123); |
| 264 | EXPECT_STREQ("a-8123b", buf); |
| 265 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 266 | snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 267 | EXPECT_STREQ("a16b", buf); |
| 268 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 269 | snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10)); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 270 | EXPECT_STREQ("a16b", buf); |
| 271 | |
| 272 | snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL); |
| 273 | EXPECT_STREQ("a68719476736b", buf); |
| 274 | |
| 275 | snprintf(buf, sizeof(buf), "a%ldb", 70000L); |
| 276 | EXPECT_STREQ("a70000b", buf); |
| 277 | |
| 278 | snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234)); |
| 279 | EXPECT_STREQ("a0xb0001234b", buf); |
| 280 | |
| 281 | snprintf(buf, sizeof(buf), "a%xz", 0x12ab); |
| 282 | EXPECT_STREQ("a12abz", buf); |
| 283 | |
| 284 | snprintf(buf, sizeof(buf), "a%Xz", 0x12ab); |
| 285 | EXPECT_STREQ("a12ABz", buf); |
| 286 | |
| 287 | snprintf(buf, sizeof(buf), "a%08xz", 0x123456); |
| 288 | EXPECT_STREQ("a00123456z", buf); |
| 289 | |
| 290 | snprintf(buf, sizeof(buf), "a%5dz", 1234); |
| 291 | EXPECT_STREQ("a 1234z", buf); |
| 292 | |
| 293 | snprintf(buf, sizeof(buf), "a%05dz", 1234); |
| 294 | EXPECT_STREQ("a01234z", buf); |
| 295 | |
| 296 | snprintf(buf, sizeof(buf), "a%8dz", 1234); |
| 297 | EXPECT_STREQ("a 1234z", buf); |
| 298 | |
| 299 | snprintf(buf, sizeof(buf), "a%-8dz", 1234); |
| 300 | EXPECT_STREQ("a1234 z", buf); |
| 301 | |
| 302 | snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef"); |
| 303 | EXPECT_STREQ("Aabcdef Z", buf); |
| 304 | |
| 305 | snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234); |
| 306 | EXPECT_STREQ("Ahello:1234Z", buf); |
| 307 | |
| 308 | snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5); |
| 309 | EXPECT_STREQ("a005:5:05z", buf); |
| 310 | |
| 311 | void* p = NULL; |
| 312 | snprintf(buf, sizeof(buf), "a%d,%pz", 5, p); |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 313 | #if defined(__BIONIC__) |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 314 | EXPECT_STREQ("a5,0x0z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 315 | #else // __BIONIC__ |
Christopher Ferris | 1361313 | 2013-10-28 15:24:04 -0700 | [diff] [blame] | 316 | EXPECT_STREQ("a5,(nil)z", buf); |
Christopher Ferris | f04935c | 2013-12-20 18:43:21 -0800 | [diff] [blame] | 317 | #endif // __BIONIC__ |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 318 | |
| 319 | snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8); |
| 320 | EXPECT_STREQ("a68719476736,6,7,8z", buf); |
| 321 | |
| 322 | snprintf(buf, sizeof(buf), "a_%f_b", 1.23f); |
| 323 | EXPECT_STREQ("a_1.230000_b", buf); |
| 324 | |
Stephen Hines | 6c7b3cb | 2013-10-11 16:03:21 -0700 | [diff] [blame] | 325 | snprintf(buf, sizeof(buf), "a_%g_b", 3.14); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 326 | EXPECT_STREQ("a_3.14_b", buf); |
Alexander Ivchenko | edd7c2e | 2014-04-01 17:01:39 +0400 | [diff] [blame] | 327 | |
| 328 | snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice"); |
| 329 | EXPECT_STREQ("print_me_twice print_me_twice", buf); |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 332 | TEST(stdio, snprintf_f_special) { |
| 333 | char buf[BUFSIZ]; |
| 334 | snprintf(buf, sizeof(buf), "%f", nanf("")); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 335 | EXPECT_STRCASEEQ("NaN", buf); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 336 | |
| 337 | snprintf(buf, sizeof(buf), "%f", HUGE_VALF); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 338 | EXPECT_STRCASEEQ("Inf", buf); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | TEST(stdio, snprintf_g_special) { |
| 342 | char buf[BUFSIZ]; |
| 343 | snprintf(buf, sizeof(buf), "%g", nan("")); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 344 | EXPECT_STRCASEEQ("NaN", buf); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 345 | |
| 346 | snprintf(buf, sizeof(buf), "%g", HUGE_VAL); |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 347 | EXPECT_STRCASEEQ("Inf", buf); |
Elliott Hughes | 7823f32 | 2014-04-14 12:11:28 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 350 | TEST(stdio, snprintf_d_INT_MAX) { |
| 351 | char buf[BUFSIZ]; |
| 352 | snprintf(buf, sizeof(buf), "%d", INT_MAX); |
| 353 | EXPECT_STREQ("2147483647", buf); |
| 354 | } |
| 355 | |
| 356 | TEST(stdio, snprintf_d_INT_MIN) { |
| 357 | char buf[BUFSIZ]; |
| 358 | snprintf(buf, sizeof(buf), "%d", INT_MIN); |
| 359 | EXPECT_STREQ("-2147483648", buf); |
| 360 | } |
| 361 | |
| 362 | TEST(stdio, snprintf_ld_LONG_MAX) { |
| 363 | char buf[BUFSIZ]; |
| 364 | snprintf(buf, sizeof(buf), "%ld", LONG_MAX); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 365 | #if __LP64__ |
| 366 | EXPECT_STREQ("9223372036854775807", buf); |
| 367 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 368 | EXPECT_STREQ("2147483647", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 369 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | TEST(stdio, snprintf_ld_LONG_MIN) { |
| 373 | char buf[BUFSIZ]; |
| 374 | snprintf(buf, sizeof(buf), "%ld", LONG_MIN); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 375 | #if __LP64__ |
| 376 | EXPECT_STREQ("-9223372036854775808", buf); |
| 377 | #else |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 378 | EXPECT_STREQ("-2147483648", buf); |
Elliott Hughes | 925753a | 2013-10-18 13:17:18 -0700 | [diff] [blame] | 379 | #endif |
Elliott Hughes | 1d13c64 | 2013-09-23 16:02:39 -0700 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | TEST(stdio, snprintf_lld_LLONG_MAX) { |
| 383 | char buf[BUFSIZ]; |
| 384 | snprintf(buf, sizeof(buf), "%lld", LLONG_MAX); |
| 385 | EXPECT_STREQ("9223372036854775807", buf); |
| 386 | } |
| 387 | |
| 388 | TEST(stdio, snprintf_lld_LLONG_MIN) { |
| 389 | char buf[BUFSIZ]; |
| 390 | snprintf(buf, sizeof(buf), "%lld", LLONG_MIN); |
| 391 | EXPECT_STREQ("-9223372036854775808", buf); |
| 392 | } |
| 393 | |
Elliott Hughes | 4bd97ce | 2014-04-10 17:48:14 -0700 | [diff] [blame] | 394 | TEST(stdio, snprintf_e) { |
| 395 | char buf[BUFSIZ]; |
| 396 | |
| 397 | snprintf(buf, sizeof(buf), "%e", 1.5); |
| 398 | EXPECT_STREQ("1.500000e+00", buf); |
| 399 | |
| 400 | snprintf(buf, sizeof(buf), "%Le", 1.5l); |
| 401 | EXPECT_STREQ("1.500000e+00", buf); |
| 402 | } |
| 403 | |
Elliott Hughes | 6b3f49a | 2013-03-06 16:20:55 -0800 | [diff] [blame] | 404 | TEST(stdio, popen) { |
| 405 | FILE* fp = popen("cat /proc/version", "r"); |
| 406 | ASSERT_TRUE(fp != NULL); |
| 407 | |
| 408 | char buf[16]; |
| 409 | char* s = fgets(buf, sizeof(buf), fp); |
| 410 | buf[13] = '\0'; |
| 411 | ASSERT_STREQ("Linux version", s); |
| 412 | |
| 413 | ASSERT_EQ(0, pclose(fp)); |
| 414 | } |
Elliott Hughes | 6b05c8e | 2013-04-11 13:54:48 -0700 | [diff] [blame] | 415 | |
| 416 | TEST(stdio, getc) { |
| 417 | FILE* fp = fopen("/proc/version", "r"); |
| 418 | ASSERT_TRUE(fp != NULL); |
| 419 | ASSERT_EQ('L', getc(fp)); |
| 420 | ASSERT_EQ('i', getc(fp)); |
| 421 | ASSERT_EQ('n', getc(fp)); |
| 422 | ASSERT_EQ('u', getc(fp)); |
| 423 | ASSERT_EQ('x', getc(fp)); |
| 424 | fclose(fp); |
| 425 | } |
| 426 | |
| 427 | TEST(stdio, putc) { |
| 428 | FILE* fp = fopen("/proc/version", "r"); |
| 429 | ASSERT_TRUE(fp != NULL); |
| 430 | ASSERT_EQ(EOF, putc('x', fp)); |
| 431 | fclose(fp); |
| 432 | } |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 433 | |
| 434 | TEST(stdio, sscanf) { |
| 435 | char s1[123]; |
| 436 | int i1; |
| 437 | double d1; |
| 438 | char s2[123]; |
| 439 | ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2)); |
| 440 | ASSERT_STREQ("hello", s1); |
| 441 | ASSERT_EQ(123, i1); |
Christopher Ferris | f171b34 | 2014-03-17 16:40:26 -0700 | [diff] [blame] | 442 | ASSERT_DOUBLE_EQ(1.23, d1); |
Elliott Hughes | 603332f | 2014-03-12 17:10:41 -0700 | [diff] [blame] | 443 | } |
Elliott Hughes | 53b2438 | 2014-05-02 18:29:25 -0700 | [diff] [blame] | 444 | |
| 445 | TEST(stdio, cantwrite_EBADF) { |
| 446 | // If we open a file read-only... |
| 447 | FILE* fp = fopen("/proc/version", "r"); |
| 448 | |
| 449 | // ...all attempts to write to that file should return failure. |
| 450 | |
| 451 | // They should also set errno to EBADF. This isn't POSIX, but it's traditional. |
| 452 | // glibc gets the wide-character functions wrong. |
| 453 | |
| 454 | errno = 0; |
| 455 | EXPECT_EQ(EOF, putc('x', fp)); |
| 456 | EXPECT_EQ(EBADF, errno); |
| 457 | |
| 458 | errno = 0; |
| 459 | EXPECT_EQ(EOF, fprintf(fp, "hello")); |
| 460 | EXPECT_EQ(EBADF, errno); |
| 461 | |
| 462 | errno = 0; |
| 463 | EXPECT_EQ(EOF, fwprintf(fp, L"hello")); |
| 464 | #if !defined(__GLIBC__) |
| 465 | EXPECT_EQ(EBADF, errno); |
| 466 | #endif |
| 467 | |
| 468 | errno = 0; |
| 469 | EXPECT_EQ(EOF, putw(1234, fp)); |
| 470 | EXPECT_EQ(EBADF, errno); |
| 471 | |
| 472 | errno = 0; |
| 473 | EXPECT_EQ(0U, fwrite("hello", 1, 2, fp)); |
| 474 | EXPECT_EQ(EBADF, errno); |
| 475 | |
| 476 | errno = 0; |
| 477 | EXPECT_EQ(EOF, fputs("hello", fp)); |
| 478 | EXPECT_EQ(EBADF, errno); |
| 479 | |
| 480 | errno = 0; |
| 481 | EXPECT_EQ(WEOF, fputwc(L'x', fp)); |
| 482 | #if !defined(__GLIBC__) |
| 483 | EXPECT_EQ(EBADF, errno); |
| 484 | #endif |
| 485 | } |