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,swprintf)25 TEST(stdio,swprintf) {
26   wchar_t wide_buff[32];
27   size_t wide_buff_len = sizeof(wide_buff) / sizeof(wchar_t);
28   EXPECT_EQ(2, swprintf(wide_buff, wide_buff_len, L"ab"));
29   EXPECT_EQ(5, swprintf(wide_buff, wide_buff_len, L"%s", "abcde"));
30   static const wchar_t kWideString[] = L"Hello\uff41 World";
31   EXPECT_EQ(12, swprintf(wide_buff, wide_buff_len, L"%ls", kWideString));
32   EXPECT_EQ(12, swprintf(wide_buff, 13, L"%ls", kWideString));
33 
34   // Unlike snprintf(), swprintf() returns -1 in case of truncation
35   // and doesn't necessarily zero-terminate the output!
36   EXPECT_EQ(-1, swprintf(wide_buff, 12, L"%ls", kWideString));
37   EXPECT_EQ(-1, swprintf(wide_buff, 1, L"%ls", kWideString));
38 }
39 
40