1 #include <stdio.h>
2 #include <wchar.h>
3
4 #include <minitest/minitest.h>
5
TEST(stdio,snprintf)6 TEST(stdio,snprintf) {
7 char char_buff[32];
8 size_t char_buff_len = sizeof(char_buff) / sizeof(char);
9
10 EXPECT_EQ(2, snprintf(char_buff, char_buff_len, "ab"));
11 EXPECT_EQ(5, snprintf(char_buff, char_buff_len, "%s", "abcde"));
12 static const char kString[] = "Hello, World";
13 EXPECT_EQ(12, snprintf(char_buff, char_buff_len, "%s", kString));
14 EXPECT_EQ(12, snprintf(char_buff, 13, "%s", kString));
15
16 EXPECT_EQ(12, snprintf(char_buff, 12, "%s", kString));
17 EXPECT_EQ(L'\0', char_buff[11]);
18 EXPECT_EQ(12, snprintf(char_buff, 1, "%s", kString));
19 EXPECT_EQ(L'\0', char_buff[0]);
20
21 EXPECT_EQ(20, snprintf(char_buff, char_buff_len, "%a", 3.1415926535));
22 EXPECT_STREQ("0x1.921fb54411744p+1", char_buff);
23 }
24
TEST(stdio,sscanf)25 TEST(stdio,sscanf) {
26 double d;
27 float f;
28
29 EXPECT_EQ(1, sscanf("0x1p-2", "%a", &f));
30 EXPECT_EQ(0.25f, f);
31
32 EXPECT_EQ(1, sscanf("0x1p-2", "%la", &d));
33 EXPECT_EQ(0.25, f);
34 }
35
TEST(stdio,swprintf)36 TEST(stdio,swprintf) {
37 wchar_t wide_buff[32];
38 size_t wide_buff_len = sizeof(wide_buff) / sizeof(wchar_t);
39 EXPECT_EQ(2, swprintf(wide_buff, wide_buff_len, L"ab"));
40 EXPECT_EQ(5, swprintf(wide_buff, wide_buff_len, L"%s", "abcde"));
41 static const wchar_t kWideString[] = L"Hello\uff41 World";
42 EXPECT_EQ(12, swprintf(wide_buff, wide_buff_len, L"%ls", kWideString));
43 EXPECT_EQ(12, swprintf(wide_buff, 13, L"%ls", kWideString));
44
45 // Unlike snprintf(), swprintf() returns -1 in case of truncation
46 // and doesn't necessarily zero-terminate the output!
47 EXPECT_EQ(-1, swprintf(wide_buff, 12, L"%ls", kWideString));
48 EXPECT_EQ(-1, swprintf(wide_buff, 1, L"%ls", kWideString));
49 }
50
51