2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/errno.h"
|
2023-03-29 07:19:40 +00:00
|
|
|
#include "libc/inttypes.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/limits.h"
|
2023-11-18 10:25:36 +00:00
|
|
|
#include "libc/math.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/mem/mem.h"
|
Make improvements
- Every unit test now passes on Apple Silicon. The final piece of this
puzzle was porting our POSIX threads cancelation support, since that
works differently on ARM64 XNU vs. AMD64. Our semaphore support on
Apple Silicon is also superior now compared to AMD64, thanks to the
grand central dispatch library which lets *NSYNC locks go faster.
- The Cosmopolitan runtime is now more stable, particularly on Windows.
To do this, thread local storage is mandatory at all runtime levels,
and the innermost packages of the C library is no longer being built
using ASAN. TLS is being bootstrapped with a 128-byte TIB during the
process startup phase, and then later on the runtime re-allocates it
either statically or dynamically to support code using _Thread_local.
fork() and execve() now do a better job cooperating with threads. We
can now check how much stack memory is left in the process or thread
when functions like kprintf() / execve() etc. call alloca(), so that
ENOMEM can be raised, reduce a buffer size, or just print a warning.
- POSIX signal emulation is now implemented the same way kernels do it
with pthread_kill() and raise(). Any thread can interrupt any other
thread, regardless of what it's doing. If it's blocked on read/write
then the killer thread will cancel its i/o operation so that EINTR can
be returned in the mark thread immediately. If it's doing a tight CPU
bound operation, then that's also interrupted by the signal delivery.
Signal delivery works now by suspending a thread and pushing context
data structures onto its stack, and redirecting its execution to a
trampoline function, which calls SetThreadContext(GetCurrentThread())
when it's done.
- We're now doing a better job managing locks and handles. On NetBSD we
now close semaphore file descriptors in forked children. Semaphores on
Windows can now be canceled immediately, which means mutexes/condition
variables will now go faster. Apple Silicon semaphores can be canceled
too. We're now using Apple's pthread_yield() funciton. Apple _nocancel
syscalls are now used on XNU when appropriate to ensure pthread_cancel
requests aren't lost. The MbedTLS library has been updated to support
POSIX thread cancelations. See tool/build/runitd.c for an example of
how it can be used for production multi-threaded tls servers. Handles
on Windows now leak less often across processes. All i/o operations on
Windows are now overlapped, which means file pointers can no longer be
inherited across dup() and fork() for the time being.
- We now spawn a thread on Windows to deliver SIGCHLD and wakeup wait4()
which means, for example, that posix_spawn() now goes 3x faster. POSIX
spawn is also now more correct. Like Musl, it's now able to report the
failure code of execve() via a pipe although our approach favors using
shared memory to do that on systems that have a true vfork() function.
- We now spawn a thread to deliver SIGALRM to threads when setitimer()
is used. This enables the most precise wakeups the OS makes possible.
- The Cosmopolitan runtime now uses less memory. On NetBSD for example,
it turned out the kernel would actually commit the PT_GNU_STACK size
which caused RSS to be 6mb for every process. Now it's down to ~4kb.
On Apple Silicon, we reduce the mandatory upstream thread size to the
smallest possible size to reduce the memory overhead of Cosmo threads.
The examples directory has a program called greenbean which can spawn
a web server on Linux with 10,000 worker threads and have the memory
usage of the process be ~77mb. The 1024 byte overhead of POSIX-style
thread-local storage is now optional; it won't be allocated until the
pthread_setspecific/getspecific functions are called. On Windows, the
threads that get spawned which are internal to the libc implementation
use reserve rather than commit memory, which shaves a few hundred kb.
- sigaltstack() is now supported on Windows, however it's currently not
able to be used to handle stack overflows, since crash signals are
still generated by WIN32. However the crash handler will still switch
to the alt stack, which is helpful in environments with tiny threads.
- Test binaries are now smaller. Many of the mandatory dependencies of
the test runner have been removed. This ensures many programs can do a
better job only linking the the thing they're testing. This caused the
test binaries for LIBC_FMT for example, to decrease from 200kb to 50kb
- long double is no longer used in the implementation details of libc,
except in the APIs that define it. The old code that used long double
for time (instead of struct timespec) has now been thoroughly removed.
- ShowCrashReports() is now much tinier in MODE=tiny. Instead of doing
backtraces itself, it'll just print a command you can run on the shell
using our new `cosmoaddr2line` program to view the backtrace.
- Crash report signal handling now works in a much better way. Instead
of terminating the process, it now relies on SA_RESETHAND so that the
default SIG_IGN behavior can terminate the process if necessary.
- Our pledge() functionality has now been fully ported to AARCH64 Linux.
2023-09-19 03:44:45 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2023-08-21 23:55:29 +00:00
|
|
|
#include "libc/stdio/internal.h"
|
|
|
|
#include "libc/str/str.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/testlib/testlib.h"
|
|
|
|
|
|
|
|
#define sscanf1(STR, FMT) \
|
|
|
|
({ \
|
|
|
|
errno = 0; \
|
Import C++ Standard Template Library
You can now use the hardest fastest and most dangerous language there is
with Cosmopolitan. So far about 75% of LLVM libcxx has been added. A few
breaking changes needed to be made to help this go smoothly.
- Rename nothrow to dontthrow
- Rename nodiscard to dontdiscard
- Add some libm functions, e.g. lgamma, nan, etc.
- Change intmax_t from int128 to int64 like everything else
- Introduce %jjd formatting directive for int128_t
- Introduce strtoi128(), strtou128(), etc.
- Rename bsrmax() to bsr128()
Some of the templates that should be working currently are std::vector,
std::string, std::map, std::set, std::deque, etc.
2022-03-22 12:51:41 +00:00
|
|
|
int128_t x = 0; \
|
2020-06-15 14:18:57 +00:00
|
|
|
EXPECT_EQ(1, sscanf(STR, FMT, &x)); \
|
|
|
|
x; \
|
|
|
|
})
|
|
|
|
|
|
|
|
TEST(sscanf, testMultiple) {
|
|
|
|
int a, b, c;
|
|
|
|
ASSERT_EQ(3, sscanf("123 123 123", "%d %x %o", &a, &b, &c));
|
|
|
|
EXPECT_EQ(123, a);
|
|
|
|
EXPECT_EQ(0x123, b);
|
|
|
|
EXPECT_EQ(0123, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testDecimal) {
|
|
|
|
EXPECT_EQ(123, sscanf1("123", "%d"));
|
|
|
|
EXPECT_EQ(123, sscanf1("123", "%u"));
|
|
|
|
EXPECT_EQ((uint32_t)-123, sscanf1("-123", "%d"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testHex) {
|
|
|
|
EXPECT_EQ(0x123, sscanf1("123", "%x"));
|
|
|
|
EXPECT_EQ(0x123, sscanf1("0x123", "%x"));
|
|
|
|
EXPECT_EQ(0x123, sscanf1("0123", "%x"));
|
Import C++ Standard Template Library
You can now use the hardest fastest and most dangerous language there is
with Cosmopolitan. So far about 75% of LLVM libcxx has been added. A few
breaking changes needed to be made to help this go smoothly.
- Rename nothrow to dontthrow
- Rename nodiscard to dontdiscard
- Add some libm functions, e.g. lgamma, nan, etc.
- Change intmax_t from int128 to int64 like everything else
- Introduce %jjd formatting directive for int128_t
- Introduce strtoi128(), strtou128(), etc.
- Rename bsrmax() to bsr128()
Some of the templates that should be working currently are std::vector,
std::string, std::map, std::set, std::deque, etc.
2022-03-22 12:51:41 +00:00
|
|
|
EXPECT_EQ(INT128_MAX,
|
|
|
|
sscanf1("170141183460469231731687303715884105727", "%jjd"));
|
|
|
|
EXPECT_EQ(INT128_MIN,
|
|
|
|
sscanf1("-170141183460469231731687303715884105728", "%jjd"));
|
|
|
|
EXPECT_EQ(UINT128_MAX, sscanf1("0xffffffffffffffffffffffffffffffff", "%jjx"));
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testOctal) {
|
|
|
|
EXPECT_EQ(0123, sscanf1("123", "%o"));
|
|
|
|
EXPECT_EQ(0123, sscanf1("0123", "%o"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testNonDirectiveCharacterMatching) {
|
|
|
|
EXPECT_EQ(0, sscanf("%", "%%"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testCharacter) {
|
`*scanf()` fixes to make TeX work (#1109)
* Fix reading the same symbol twice when using `{f,}scanf()`
PR #924 appears to use `unget()` subtly incorrectly when parsing
floating point numbers. The rest of the code only uses `unget()`
immediately followed by `goto Done;` to return back the symbol that
can't possibly belong to the directive we're processing.
With floating-point, however, the ungot characters could very well
be valid for the *next* directive, so we will essentially read them
twice. It can't be seen in `sscanf()` tests because `unget()` is a
no-op there, but the test I added for `fscanf()` fails like this:
...
EXPECT_EQ(0xDEAD, i1)
need 57005 (or 0xdead) =
got 908973 (or 0x000ddead)
...
EXPECT_EQ(0xBEEF, i2)
need 48879 (or 0xbeef) =
got 769775 (or 0x000bbeef)
This means we read 0xDDEAD instead of 0xDEAD and 0xBBEEF instead of
0xBEEF. I checked that both musl and glibc read 0xDEAD/0xBEEF, as
expected.
Fix the failing test by removing the unneeded `unget()` calls.
* Don't read invalid floating-point numbers in `*scanf()`
Currently, we just ignore any errors from `strtod()`. They can
happen either because no valid float can be parsed at all, or
because the state machine recognizes only a prefix of a valid
floating-point number.
Fix this by making sure `strtod()` parses everything we recognized,
provided it's non-empty. This requires to pop the last character
off the FP buffer, which is supposed to be parsed by the next
`*scanf()` directive.
* Make `%c` parsing in `*scanf()` respect the C standard
Currently, `%c`-style directives always succeed even if there
are actually fewer characters in the input than requested.
Before the fix, the added test fails like this:
...
EXPECT_EQ(2, sscanf("ab", "%c %c %c", &c2, &c3, &c4))
need 2 (or 0x02 or '\2' or ENOENT) =
got 3 (or 0x03 or '\3' or ESRCH)
...
EXPECT_EQ(0, sscanf("abcd", "%5c", s2))
need 0 (or 0x0 or '\0') =
got 1 (or 0x01 or '\1' or EPERM)
musl and glibc pass this test.
2024-02-23 15:15:30 +00:00
|
|
|
char c1 = 0, c2 = c1, c3 = c2, c4 = c3;
|
|
|
|
char s1[32] = {0}, s2[32] = {0};
|
|
|
|
EXPECT_EQ(1, sscanf("a", "%c", &c1));
|
|
|
|
EXPECT_EQ(2, sscanf("ab", "%c %c %c", &c2, &c3, &c4));
|
|
|
|
EXPECT_EQ(1, sscanf("abcde", "%5c", s1));
|
|
|
|
EXPECT_EQ(0, sscanf("abcd", "%5c", s2));
|
|
|
|
|
|
|
|
EXPECT_EQ('a', c1);
|
|
|
|
EXPECT_EQ('a', c2);
|
|
|
|
EXPECT_EQ('b', c3);
|
|
|
|
EXPECT_STREQ("abcde", &s1[0]);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testStringBuffer) {
|
|
|
|
char s1[32], s2[32];
|
|
|
|
ASSERT_EQ(2, sscanf("abc xyz", "%s %s", s1, s2));
|
|
|
|
EXPECT_STREQ("abc", &s1[0]);
|
|
|
|
EXPECT_STREQ("xyz", &s2[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testStringBuffer_gothicUtf8ToUtf8_roundTrips) {
|
|
|
|
char s1[64], s2[64];
|
|
|
|
ASSERT_EQ(2, sscanf("𐌰𐌱𐌲𐌳 𐌴𐌵𐌶𐌷", "%63s %63s", s1, s2));
|
|
|
|
EXPECT_STREQ("𐌰𐌱𐌲𐌳", s1);
|
|
|
|
EXPECT_STREQ("𐌴𐌵𐌶𐌷", s2);
|
|
|
|
}
|
|
|
|
|
2022-08-21 04:36:07 +00:00
|
|
|
TEST(sscanf, testStringBuffer_gothicUtf8to16) {
|
2020-06-15 14:18:57 +00:00
|
|
|
char16_t s1[64], s2[64];
|
|
|
|
ASSERT_EQ(2, sscanf("𐌰𐌱𐌲𐌳 𐌴𐌵𐌶𐌷", "%63hs %63hs", s1, s2));
|
|
|
|
EXPECT_STREQ(u"𐌰𐌱𐌲𐌳", s1);
|
|
|
|
EXPECT_STREQ(u"𐌴𐌵𐌶𐌷", s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testStringBuffer_gothicUtf8ToUtf32) {
|
|
|
|
wchar_t s1[64], s2[64];
|
|
|
|
ASSERT_EQ(2, sscanf("𐌰𐌱𐌲𐌳 𐌴𐌵𐌶𐌷", "%63ls %63ls", s1, s2));
|
|
|
|
EXPECT_STREQ(L"𐌰𐌱𐌲𐌳", s1);
|
|
|
|
EXPECT_STREQ(L"𐌴𐌵𐌶𐌷", s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testStringBuffer_allocatingBehavior) {
|
|
|
|
char *s1, *s2;
|
|
|
|
ASSERT_EQ(2, sscanf("𐌰𐌱𐌲𐌳 𐌴𐌵𐌶𐌷", "%ms %ms", &s1, &s2));
|
|
|
|
EXPECT_STREQ("𐌰𐌱𐌲𐌳", s1);
|
|
|
|
EXPECT_STREQ("𐌴𐌵𐌶𐌷", s2);
|
|
|
|
free(s1);
|
|
|
|
free(s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testPracticalBusinessApplication) {
|
|
|
|
unsigned start, stop;
|
|
|
|
char width;
|
|
|
|
ASSERT_EQ(1, sscanf("0BF3..", "%x", &start));
|
|
|
|
EXPECT_EQ(0x0BF3, start);
|
|
|
|
ASSERT_EQ(3, sscanf("0BF3..0BF8;N # So [6] TAMIL DAY SIGN",
|
|
|
|
"%x..%x;%c", &start, &stop, &width));
|
|
|
|
EXPECT_EQ(0x0BF3, start);
|
|
|
|
EXPECT_EQ(0x0BF8, stop);
|
|
|
|
EXPECT_EQ('N', width);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, socketListenUri) {
|
|
|
|
char proto[4];
|
|
|
|
unsigned char ip4[4];
|
|
|
|
uint16_t port;
|
|
|
|
ASSERT_EQ(6, sscanf("tcp:127.0.0.1:31337", "%3s:%hhu.%hhu.%hhu.%hhu:%hu",
|
|
|
|
proto, &ip4[0], &ip4[1], &ip4[2], &ip4[3], &port));
|
|
|
|
ASSERT_STREQ("tcp", proto);
|
|
|
|
ASSERT_EQ(127, ip4[0]);
|
|
|
|
ASSERT_EQ(0, ip4[1]);
|
|
|
|
ASSERT_EQ(0, ip4[2]);
|
|
|
|
ASSERT_EQ(1, ip4[3]);
|
|
|
|
ASSERT_EQ(31337, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, testDiscard_notIncludedInCount) {
|
|
|
|
char buf[8];
|
|
|
|
ASSERT_EQ(1, sscanf("hello there", "%*s %8s", buf));
|
|
|
|
EXPECT_STREQ("there", buf);
|
|
|
|
}
|
2022-06-27 04:27:07 +00:00
|
|
|
|
|
|
|
TEST(sscanf, testFixedWidthFormat_Integer) {
|
2022-08-11 07:15:29 +00:00
|
|
|
int r, g, b;
|
|
|
|
ASSERT_EQ(3, sscanf("#321030", "#%2x%2b%2d", &r, &g, &b));
|
|
|
|
ASSERT_EQ(0x32, r);
|
|
|
|
ASSERT_EQ(2, g);
|
|
|
|
ASSERT_EQ(30, b);
|
2022-06-27 04:27:07 +00:00
|
|
|
}
|
2023-03-29 07:19:40 +00:00
|
|
|
|
|
|
|
TEST(sscanf, testInttypes_macros) {
|
|
|
|
int8_t i8 = (int8_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint8_t u8 = (uint8_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int16_t i16 = (int16_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint16_t u16 = (uint16_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int32_t i32 = (int32_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint32_t u32 = (uint32_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int64_t i64 = (int64_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint64_t u64 = (uint64_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
intmax_t imax = (intmax_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uintmax_t umax = (uintmax_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int_least8_t il8 = (int_least8_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint_least8_t ul8 = (uint_least8_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int_least16_t il16 = (int_least16_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint_least16_t ul16 = (uint_least16_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int_least32_t il32 = (int_least32_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint_least32_t ul32 = (uint_least32_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int_least64_t il64 = (int_least64_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint_least64_t ul64 = (uint_least64_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int_fast8_t if8 = (int_fast8_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint_fast8_t uf8 = (uint_fast8_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int_fast16_t if16 = (int_fast16_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint_fast16_t uf16 = (uint_fast16_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int_fast32_t if32 = (int_fast32_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint_fast32_t uf32 = (uint_fast32_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
int_fast64_t if64 = (int_fast64_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
uint_fast64_t uf64 = (uint_fast64_t)0xFFFFFFFFFFFFFFFF;
|
|
|
|
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNd8, &i8), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNu8, &u8), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNd16, &i16), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNu16, &u16), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNd32, &i32), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNu32, &u32), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNd64, &i64), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNu64, &u64), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdMAX, &imax), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuMAX, &umax), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdLEAST8, &il8), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuLEAST8, &ul8), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdLEAST16, &il16), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuLEAST16, &ul16), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdLEAST32, &il32), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuLEAST32, &ul32), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdLEAST64, &il64), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuLEAST64, &ul64), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdFAST8, &if8), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuFAST8, &uf8), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdFAST16, &if16), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuFAST16, &uf16), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdFAST32, &if32), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuFAST32, &uf32), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNdFAST64, &if64), 1);
|
|
|
|
ASSERT_EQ(sscanf("1", "%" SCNuFAST64, &uf64), 1);
|
|
|
|
|
|
|
|
ASSERT_EQ(i8, 1);
|
|
|
|
ASSERT_EQ(u8, 1);
|
|
|
|
ASSERT_EQ(i16, 1);
|
|
|
|
ASSERT_EQ(u16, 1);
|
|
|
|
ASSERT_EQ(i32, 1);
|
|
|
|
ASSERT_EQ(u32, 1);
|
|
|
|
ASSERT_EQ(i64, 1);
|
|
|
|
ASSERT_EQ(u64, 1);
|
|
|
|
ASSERT_EQ(imax, 1);
|
|
|
|
ASSERT_EQ(umax, 1);
|
|
|
|
ASSERT_EQ(il8, 1);
|
|
|
|
ASSERT_EQ(ul8, 1);
|
|
|
|
ASSERT_EQ(il16, 1);
|
|
|
|
ASSERT_EQ(ul16, 1);
|
|
|
|
ASSERT_EQ(il32, 1);
|
|
|
|
ASSERT_EQ(ul32, 1);
|
|
|
|
ASSERT_EQ(il64, 1);
|
|
|
|
ASSERT_EQ(ul64, 1);
|
|
|
|
ASSERT_EQ(if16, 1);
|
|
|
|
ASSERT_EQ(uf16, 1);
|
|
|
|
ASSERT_EQ(if32, 1);
|
|
|
|
ASSERT_EQ(uf32, 1);
|
|
|
|
ASSERT_EQ(if64, 1);
|
|
|
|
ASSERT_EQ(uf64, 1);
|
|
|
|
}
|
Fix scanf x specifier with string of 0 (#793)
The C standard states that, in the context of an x conversion
specifier given to scanf:
> Matches an optionally signed hexadecimal integer, whose format is
> the same as expected for the subject sequence of the strtoul
> function with the value 16 for the base argument.
- C standard, 7.23.6.2.11. The fscanf function
Cosmopolitan fails to do this, as 0 should be parsed as a 0 by such an
invocation of strtoul. Instead, cosmopolitan errors out as though such
input is invalid, which is wrong.
This means that a program such as this:
#include <stdio.h>
#undef NDEBUG
#include <assert.h>
int main()
{
int v = 0;
assert(sscanf("0", "%x", &v) == 1);
}
will not run correctly on cosmpolitan, instead failing the assertion.
This patch fixes this, along with the associated GitHub issue,
https://github.com/jart/cosmopolitan/issues/778
2023-04-15 13:25:35 +00:00
|
|
|
|
|
|
|
TEST(sscanf, test0) {
|
|
|
|
int v;
|
|
|
|
|
|
|
|
v = 0xFFFFFFFF;
|
|
|
|
ASSERT_EQ(sscanf("0", "%x", &v), 1);
|
|
|
|
ASSERT_EQ(v, 0);
|
|
|
|
|
|
|
|
v = 0xFFFFFFFF;
|
|
|
|
ASSERT_EQ(sscanf("0", "%X", &v), 1);
|
|
|
|
ASSERT_EQ(v, 0);
|
|
|
|
|
|
|
|
v = 0xFFFFFFFF;
|
|
|
|
ASSERT_EQ(sscanf("0", "%d", &v), 1);
|
|
|
|
ASSERT_EQ(v, 0);
|
|
|
|
|
|
|
|
v = 0xFFFFFFFF;
|
|
|
|
ASSERT_EQ(sscanf("0", "%o", &v), 1);
|
|
|
|
ASSERT_EQ(v, 0);
|
|
|
|
|
|
|
|
v = 0xFFFFFFFF;
|
|
|
|
ASSERT_EQ(sscanf("0", "%u", &v), 1);
|
|
|
|
ASSERT_EQ(v, 0);
|
|
|
|
|
|
|
|
v = 0xFFFFFFFF;
|
|
|
|
ASSERT_EQ(sscanf("0", "%b", &v), 1);
|
|
|
|
ASSERT_EQ(v, 0);
|
|
|
|
}
|
2023-08-21 16:00:40 +00:00
|
|
|
|
|
|
|
TEST(sscanf, n) {
|
|
|
|
int x, y;
|
|
|
|
EXPECT_EQ(1, sscanf("7 2 3 4", "%d%n", &x, &y));
|
|
|
|
EXPECT_EQ(7, x);
|
|
|
|
EXPECT_EQ(1, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, eofForNoMatching) {
|
|
|
|
int y = 666;
|
|
|
|
char x[8] = "hi";
|
|
|
|
EXPECT_EQ(-1, sscanf(" ", "%s%n", &x, &y));
|
|
|
|
EXPECT_STREQ("hi", x);
|
|
|
|
EXPECT_EQ(666, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, eofConditions) {
|
|
|
|
int x = 666;
|
|
|
|
EXPECT_EQ(-1, sscanf("", "%d", &x));
|
|
|
|
EXPECT_EQ(666, x);
|
|
|
|
EXPECT_EQ(-1, sscanf(" ", "%d", &x));
|
|
|
|
EXPECT_EQ(666, x);
|
|
|
|
EXPECT_EQ(-1, sscanf("123", "%*d%d", &x));
|
|
|
|
EXPECT_EQ(666, x);
|
|
|
|
EXPECT_EQ(-1, sscanf("123", "%*d%n", &x));
|
|
|
|
EXPECT_EQ(666, x);
|
|
|
|
}
|
2023-08-21 19:16:52 +00:00
|
|
|
|
|
|
|
TEST(sscanf, decimal) {
|
|
|
|
int x = 666;
|
|
|
|
int y = 666;
|
|
|
|
EXPECT_EQ(1, sscanf("019", "%d%d", &x, &y));
|
|
|
|
EXPECT_EQ(19, x);
|
|
|
|
EXPECT_EQ(666, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, octal) {
|
|
|
|
int x = 666;
|
|
|
|
int y = 666;
|
|
|
|
EXPECT_EQ(2, sscanf("019", "%o%d", &x, &y));
|
|
|
|
EXPECT_EQ(1, x);
|
|
|
|
EXPECT_EQ(9, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, flexdecimal_octal) {
|
|
|
|
int x = 666;
|
|
|
|
int y = 666;
|
|
|
|
EXPECT_EQ(2, sscanf("019", "%i%d", &x, &y));
|
|
|
|
EXPECT_EQ(1, x);
|
|
|
|
EXPECT_EQ(9, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, flexdecimal_decimal) {
|
|
|
|
int x = 666;
|
|
|
|
int y = 666;
|
|
|
|
EXPECT_EQ(1, sscanf("109a", "%i%d", &x, &y));
|
|
|
|
EXPECT_EQ(109, x);
|
|
|
|
EXPECT_EQ(666, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, flexdecimal_hex) {
|
|
|
|
int x = 666;
|
|
|
|
int y = 666;
|
|
|
|
EXPECT_EQ(1, sscanf("0x19a", "%i%d", &x, &y));
|
|
|
|
EXPECT_EQ(0x19a, x);
|
|
|
|
EXPECT_EQ(666, y);
|
|
|
|
}
|
|
|
|
|
2023-11-18 10:25:36 +00:00
|
|
|
TEST(sscanf, floating_point_simple) {
|
|
|
|
float x = 666.666f, y = x, z = y;
|
|
|
|
EXPECT_EQ(3, sscanf("0.3715 .3715 3715", "%f %f %f", &x, &y, &z));
|
|
|
|
EXPECT_EQ(0.3715f, x);
|
|
|
|
EXPECT_EQ(0.3715f, y);
|
|
|
|
EXPECT_EQ(3715.0f, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, floating_point_simple_double_precision) {
|
|
|
|
double x = 666.666, y = x, z = y;
|
|
|
|
EXPECT_EQ(3, sscanf("0.3715 .3715 3715", "%lf %lf %lf", &x, &y, &z));
|
|
|
|
EXPECT_EQ(0.3715, x);
|
|
|
|
EXPECT_EQ(0.3715, y);
|
|
|
|
EXPECT_EQ(3715.0, z);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, floating_point_nan) {
|
|
|
|
float a = 666.666f, b = a, c = b, d = c, e = d, f = e;
|
|
|
|
EXPECT_EQ(4, sscanf("nan -NAN nAn NaN", "%f %f %f %f", &a, &b, &c, &d));
|
|
|
|
EXPECT_EQ(2, sscanf("nan(2) -NaN(_ABCDzxcv1234_)", "%f %f", &e, &f));
|
|
|
|
EXPECT_TRUE(isnan(a));
|
|
|
|
EXPECT_TRUE(isnan(b));
|
|
|
|
EXPECT_TRUE(isnan(c));
|
|
|
|
EXPECT_TRUE(isnan(d));
|
|
|
|
EXPECT_TRUE(isnan(e));
|
|
|
|
EXPECT_TRUE(isnan(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, floating_point_nan_double_precision) {
|
|
|
|
double a = 666.666, b = a, c = b, d = c, e = d, f = e;
|
|
|
|
EXPECT_EQ(4, sscanf("nan -NAN nAn NaN", "%lf %lf %lf %lf", &a, &b, &c, &d));
|
|
|
|
EXPECT_EQ(2, sscanf("nan(2) -NAN(_ABCDzxcv1234_)", "%lf %lf", &e, &f));
|
|
|
|
EXPECT_TRUE(isnan(a));
|
|
|
|
EXPECT_TRUE(isnan(b));
|
|
|
|
EXPECT_TRUE(isnan(c));
|
|
|
|
EXPECT_TRUE(isnan(d));
|
|
|
|
EXPECT_TRUE(isnan(e));
|
|
|
|
EXPECT_TRUE(isnan(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, floating_point_infinity) {
|
|
|
|
float a = 666.666f, b = a, c = b, d = c, e = d, f = e, g = f;
|
|
|
|
EXPECT_EQ(4, sscanf("inf +INF -iNf InF", "%f %f %f %f", &a, &b, &c, &d));
|
|
|
|
EXPECT_EQ(3, sscanf("+infinity -INFINITY iNfInItY", "%f %f %f", &e, &f, &g));
|
|
|
|
EXPECT_TRUE(isinf(a));
|
|
|
|
EXPECT_TRUE(isinf(b));
|
|
|
|
EXPECT_TRUE(isinf(c));
|
|
|
|
EXPECT_TRUE(isinf(d));
|
|
|
|
EXPECT_TRUE(isinf(e));
|
|
|
|
EXPECT_TRUE(isinf(f));
|
|
|
|
EXPECT_TRUE(isinf(g));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, floating_point_infinity_double_precision) {
|
|
|
|
double a = 666.666, b = a, c = b, d = c, e = d, f = e, g = f;
|
|
|
|
EXPECT_EQ(4, sscanf("inf +INF -iNf InF", "%lf %lf %lf %lf", &a, &b, &c, &d));
|
2024-01-20 23:06:16 +00:00
|
|
|
EXPECT_EQ(3,
|
|
|
|
sscanf("+infinity -INFINITY iNfInItY", "%lf %lf %lf", &e, &f, &g));
|
2023-11-18 10:25:36 +00:00
|
|
|
EXPECT_TRUE(isinf(a));
|
|
|
|
EXPECT_TRUE(isinf(b));
|
|
|
|
EXPECT_TRUE(isinf(c));
|
|
|
|
EXPECT_TRUE(isinf(d));
|
|
|
|
EXPECT_TRUE(isinf(e));
|
|
|
|
EXPECT_TRUE(isinf(f));
|
|
|
|
EXPECT_TRUE(isinf(g));
|
|
|
|
}
|
|
|
|
|
`*scanf()` fixes to make TeX work (#1109)
* Fix reading the same symbol twice when using `{f,}scanf()`
PR #924 appears to use `unget()` subtly incorrectly when parsing
floating point numbers. The rest of the code only uses `unget()`
immediately followed by `goto Done;` to return back the symbol that
can't possibly belong to the directive we're processing.
With floating-point, however, the ungot characters could very well
be valid for the *next* directive, so we will essentially read them
twice. It can't be seen in `sscanf()` tests because `unget()` is a
no-op there, but the test I added for `fscanf()` fails like this:
...
EXPECT_EQ(0xDEAD, i1)
need 57005 (or 0xdead) =
got 908973 (or 0x000ddead)
...
EXPECT_EQ(0xBEEF, i2)
need 48879 (or 0xbeef) =
got 769775 (or 0x000bbeef)
This means we read 0xDDEAD instead of 0xDEAD and 0xBBEEF instead of
0xBEEF. I checked that both musl and glibc read 0xDEAD/0xBEEF, as
expected.
Fix the failing test by removing the unneeded `unget()` calls.
* Don't read invalid floating-point numbers in `*scanf()`
Currently, we just ignore any errors from `strtod()`. They can
happen either because no valid float can be parsed at all, or
because the state machine recognizes only a prefix of a valid
floating-point number.
Fix this by making sure `strtod()` parses everything we recognized,
provided it's non-empty. This requires to pop the last character
off the FP buffer, which is supposed to be parsed by the next
`*scanf()` directive.
* Make `%c` parsing in `*scanf()` respect the C standard
Currently, `%c`-style directives always succeed even if there
are actually fewer characters in the input than requested.
Before the fix, the added test fails like this:
...
EXPECT_EQ(2, sscanf("ab", "%c %c %c", &c2, &c3, &c4))
need 2 (or 0x02 or '\2' or ENOENT) =
got 3 (or 0x03 or '\3' or ESRCH)
...
EXPECT_EQ(0, sscanf("abcd", "%5c", s2))
need 0 (or 0x0 or '\0') =
got 1 (or 0x01 or '\1' or EPERM)
musl and glibc pass this test.
2024-02-23 15:15:30 +00:00
|
|
|
TEST(sscanf, floating_point_invalid) {
|
|
|
|
float dummy;
|
|
|
|
EXPECT_EQ(0, sscanf("junk", "%f", &dummy));
|
|
|
|
EXPECT_EQ(0, sscanf("e9", "%f", &dummy));
|
|
|
|
EXPECT_EQ(0, sscanf("-e9", "%f", &dummy));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, floating_point_invalid_double_precision) {
|
|
|
|
double dummy;
|
|
|
|
EXPECT_EQ(0, sscanf("junk", "%lf", &dummy));
|
|
|
|
EXPECT_EQ(0, sscanf("e9", "%lf", &dummy));
|
|
|
|
EXPECT_EQ(0, sscanf("-e9", "%lf", &dummy));
|
|
|
|
}
|
|
|
|
|
2023-11-18 10:25:36 +00:00
|
|
|
TEST(sscanf, floating_point_documentation_examples) {
|
2024-01-20 23:06:16 +00:00
|
|
|
float a = 666.666f, b = a, c = b, d = c, e = d, f = e, g = f, h = g, i = h,
|
|
|
|
j = i;
|
2023-11-18 10:25:36 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(2, sscanf("111.11 -2.22", "%f %f", &a, &b));
|
|
|
|
EXPECT_EQ(3, sscanf("Nan nan(2) inF", "%f %f %f", &c, &d, &e));
|
2024-01-20 23:06:16 +00:00
|
|
|
EXPECT_EQ(
|
`*scanf()` fixes to make TeX work (#1109)
* Fix reading the same symbol twice when using `{f,}scanf()`
PR #924 appears to use `unget()` subtly incorrectly when parsing
floating point numbers. The rest of the code only uses `unget()`
immediately followed by `goto Done;` to return back the symbol that
can't possibly belong to the directive we're processing.
With floating-point, however, the ungot characters could very well
be valid for the *next* directive, so we will essentially read them
twice. It can't be seen in `sscanf()` tests because `unget()` is a
no-op there, but the test I added for `fscanf()` fails like this:
...
EXPECT_EQ(0xDEAD, i1)
need 57005 (or 0xdead) =
got 908973 (or 0x000ddead)
...
EXPECT_EQ(0xBEEF, i2)
need 48879 (or 0xbeef) =
got 769775 (or 0x000bbeef)
This means we read 0xDDEAD instead of 0xDEAD and 0xBBEEF instead of
0xBEEF. I checked that both musl and glibc read 0xDEAD/0xBEEF, as
expected.
Fix the failing test by removing the unneeded `unget()` calls.
* Don't read invalid floating-point numbers in `*scanf()`
Currently, we just ignore any errors from `strtod()`. They can
happen either because no valid float can be parsed at all, or
because the state machine recognizes only a prefix of a valid
floating-point number.
Fix this by making sure `strtod()` parses everything we recognized,
provided it's non-empty. This requires to pop the last character
off the FP buffer, which is supposed to be parsed by the next
`*scanf()` directive.
* Make `%c` parsing in `*scanf()` respect the C standard
Currently, `%c`-style directives always succeed even if there
are actually fewer characters in the input than requested.
Before the fix, the added test fails like this:
...
EXPECT_EQ(2, sscanf("ab", "%c %c %c", &c2, &c3, &c4))
need 2 (or 0x02 or '\2' or ENOENT) =
got 3 (or 0x03 or '\3' or ESRCH)
...
EXPECT_EQ(0, sscanf("abcd", "%5c", s2))
need 0 (or 0x0 or '\0') =
got 1 (or 0x01 or '\1' or EPERM)
musl and glibc pass this test.
2024-02-23 15:15:30 +00:00
|
|
|
2, sscanf("0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz -0.0000000123junk junk",
|
2024-01-20 23:06:16 +00:00
|
|
|
"%f %f %f %f %f", &f, &g, &h, &i, &j));
|
2023-11-18 10:25:36 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(111.11f, a);
|
|
|
|
EXPECT_EQ(-2.22f, b);
|
|
|
|
EXPECT_TRUE(isnan(c));
|
|
|
|
EXPECT_TRUE(isnan(d));
|
|
|
|
EXPECT_TRUE(isinf(e));
|
|
|
|
EXPECT_EQ(0X1.BC70A3D70A3D7P+6f, f);
|
|
|
|
EXPECT_TRUE(isinf(g));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, floating_point_documentation_examples_double_precision) {
|
2024-01-20 23:06:16 +00:00
|
|
|
double a = 666.666, b = a, c = b, d = c, e = d, f = e, g = f, h = g, i = h,
|
|
|
|
j = i;
|
2023-11-18 10:25:36 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(2, sscanf("111.11 -2.22", "%lf %lf", &a, &b));
|
|
|
|
EXPECT_EQ(3, sscanf("Nan nan(2) inF", "%lf %lf %lf", &c, &d, &e));
|
2024-01-20 23:06:16 +00:00
|
|
|
EXPECT_EQ(
|
`*scanf()` fixes to make TeX work (#1109)
* Fix reading the same symbol twice when using `{f,}scanf()`
PR #924 appears to use `unget()` subtly incorrectly when parsing
floating point numbers. The rest of the code only uses `unget()`
immediately followed by `goto Done;` to return back the symbol that
can't possibly belong to the directive we're processing.
With floating-point, however, the ungot characters could very well
be valid for the *next* directive, so we will essentially read them
twice. It can't be seen in `sscanf()` tests because `unget()` is a
no-op there, but the test I added for `fscanf()` fails like this:
...
EXPECT_EQ(0xDEAD, i1)
need 57005 (or 0xdead) =
got 908973 (or 0x000ddead)
...
EXPECT_EQ(0xBEEF, i2)
need 48879 (or 0xbeef) =
got 769775 (or 0x000bbeef)
This means we read 0xDDEAD instead of 0xDEAD and 0xBBEEF instead of
0xBEEF. I checked that both musl and glibc read 0xDEAD/0xBEEF, as
expected.
Fix the failing test by removing the unneeded `unget()` calls.
* Don't read invalid floating-point numbers in `*scanf()`
Currently, we just ignore any errors from `strtod()`. They can
happen either because no valid float can be parsed at all, or
because the state machine recognizes only a prefix of a valid
floating-point number.
Fix this by making sure `strtod()` parses everything we recognized,
provided it's non-empty. This requires to pop the last character
off the FP buffer, which is supposed to be parsed by the next
`*scanf()` directive.
* Make `%c` parsing in `*scanf()` respect the C standard
Currently, `%c`-style directives always succeed even if there
are actually fewer characters in the input than requested.
Before the fix, the added test fails like this:
...
EXPECT_EQ(2, sscanf("ab", "%c %c %c", &c2, &c3, &c4))
need 2 (or 0x02 or '\2' or ENOENT) =
got 3 (or 0x03 or '\3' or ESRCH)
...
EXPECT_EQ(0, sscanf("abcd", "%5c", s2))
need 0 (or 0x0 or '\0') =
got 1 (or 0x01 or '\1' or EPERM)
musl and glibc pass this test.
2024-02-23 15:15:30 +00:00
|
|
|
2, sscanf("0X1.BC70A3D70A3D7P+6 1.18973e+4932zzz -0.0000000123junk junk",
|
2024-01-20 23:06:16 +00:00
|
|
|
"%lf %lf %lf %lf %lf", &f, &g, &h, &i, &j));
|
2023-11-18 10:25:36 +00:00
|
|
|
|
|
|
|
EXPECT_EQ(111.11, a);
|
|
|
|
EXPECT_EQ(-2.22, b);
|
|
|
|
EXPECT_TRUE(isnan(c));
|
|
|
|
EXPECT_TRUE(isnan(d));
|
|
|
|
EXPECT_TRUE(isinf(e));
|
|
|
|
EXPECT_EQ(0X1.BC70A3D70A3D7P+6, f);
|
|
|
|
EXPECT_TRUE(isinf(g));
|
|
|
|
}
|
|
|
|
|
2023-08-21 19:16:52 +00:00
|
|
|
TEST(sscanf, luplus) {
|
|
|
|
long x = 666;
|
|
|
|
EXPECT_EQ(1, sscanf("+123", "%lu", &x));
|
|
|
|
EXPECT_EQ(123, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(sscanf, lupluser) {
|
|
|
|
long x = 666;
|
|
|
|
EXPECT_EQ(1, sscanf("+123", "%li", &x));
|
|
|
|
EXPECT_EQ(123, x);
|
|
|
|
}
|
2023-08-21 23:55:29 +00:00
|
|
|
|
|
|
|
TEST(fscanf, stuff) {
|
|
|
|
int x;
|
|
|
|
char *s = "1 12 123\n"
|
|
|
|
"4 44\n";
|
|
|
|
FILE *f = fmemopen(s, strlen(s), "r+");
|
|
|
|
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
|
|
|
EXPECT_EQ(1, x);
|
|
|
|
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
|
|
|
EXPECT_EQ(12, x);
|
|
|
|
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
|
|
|
EXPECT_EQ(123, x);
|
|
|
|
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
|
|
|
EXPECT_EQ(4, x);
|
|
|
|
EXPECT_EQ(1, fscanf(f, "%d", &x));
|
|
|
|
EXPECT_EQ(44, x);
|
|
|
|
EXPECT_EQ(-1, fscanf(f, "%d", &x));
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(fscanf, wantDecimalButGotLetter_returnsZeroMatches) {
|
|
|
|
int x = 666;
|
|
|
|
char *s = "a1\n";
|
|
|
|
FILE *f = fmemopen(s, strlen(s), "r+");
|
|
|
|
EXPECT_EQ(0, fscanf(f, "%d", &x));
|
|
|
|
EXPECT_EQ(666, x);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2024-01-20 23:06:16 +00:00
|
|
|
|
|
|
|
TEST(scanf, n) {
|
|
|
|
int rc;
|
|
|
|
unsigned int a, b, c, d, port, len;
|
|
|
|
rc = sscanf("1.2.3.4:1848", "%u.%u.%u.%u:%u%n", &a, &b, &c, &d, &port, &len);
|
|
|
|
ASSERT_EQ(5, rc);
|
|
|
|
ASSERT_EQ(1, a);
|
|
|
|
ASSERT_EQ(2, b);
|
|
|
|
ASSERT_EQ(3, c);
|
|
|
|
ASSERT_EQ(4, d);
|
|
|
|
ASSERT_EQ(1848, port);
|
|
|
|
ASSERT_EQ(12, len);
|
|
|
|
}
|