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