brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.3 KiB · 2aef1d7 Raw
70 lines · cpp
1//===-- Unittests for snprintf --------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// These tests are copies of the non-v variants of the printf functions. This is10// because these functions are identical in every way except for how the varargs11// are passed.12 13#include "src/stdio/vsnprintf.h"14 15#include "test/UnitTest/Test.h"16 17int call_vsnprintf(char *__restrict buffer, size_t buffsz,18                   const char *__restrict format, ...) {19  va_list vlist;20  va_start(vlist, format);21  int ret = LIBC_NAMESPACE::vsnprintf(buffer, buffsz, format, vlist);22  va_end(vlist);23  return ret;24}25 26// The sprintf test cases cover testing the shared printf functionality, so27// these tests will focus on snprintf exclusive features.28 29TEST(LlvmLibcVSNPrintfTest, CutOff) {30  char buff[100];31  int written;32 33  written = call_vsnprintf(buff, 16, "A simple string with no conversions.");34  EXPECT_EQ(written, 36);35  ASSERT_STREQ(buff, "A simple string");36 37  written = call_vsnprintf(buff, 5, "%s", "1234567890");38  EXPECT_EQ(written, 10);39  ASSERT_STREQ(buff, "1234");40 41  written = call_vsnprintf(buff, 67, "%-101c", 'a');42  EXPECT_EQ(written, 101);43  ASSERT_STREQ(buff, "a "44                     "        " // Each of these is 8 spaces, and there are 8.45                     "        " // In total there are 65 spaces46                     "        " // 'a' + 65 spaces + '\0' = 6747                     "        "48                     "        "49                     "        "50                     "        "51                     "        ");52 53  // passing null as the output pointer is allowed as long as buffsz is 0.54  written = call_vsnprintf(nullptr, 0, "%s and more", "1234567890");55  EXPECT_EQ(written, 19);56}57 58TEST(LlvmLibcVSNPrintfTest, NoCutOff) {59  char buff[64];60  int written;61 62  written = call_vsnprintf(buff, 37, "A simple string with no conversions.");63  EXPECT_EQ(written, 36);64  ASSERT_STREQ(buff, "A simple string with no conversions.");65 66  written = call_vsnprintf(buff, 20, "%s", "1234567890");67  EXPECT_EQ(written, 10);68  ASSERT_STREQ(buff, "1234567890");69}70