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