1//===-- Unittests for StringStream ----------------------------------------===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "src/__support/CPP/span.h"
10#include "src/__support/CPP/stringstream.h"
11#include "test/UnitTest/Test.h"
12
13using LIBC_NAMESPACE::cpp::span;
14using LIBC_NAMESPACE::cpp::StringStream;
15
16TEST(LlvmLibcStringStreamTest, Simple) {
17 char buf[256];
18
19 StringStream ss1(buf);
20 ss1 << "Hello, Stream - " << int(123) << StringStream::ENDS;
21 ASSERT_FALSE(ss1.overflow());
22 ASSERT_STREQ(ss1.str().data(), "Hello, Stream - 123");
23
24 StringStream ss2(buf);
25 ss2 << 'a' << 'b' << 'c' << StringStream::ENDS;
26 ASSERT_FALSE(ss2.overflow());
27 ASSERT_STREQ(ss2.str().data(), "abc");
28}
29
30TEST(LlvmLibcStringStreamTest, Overflow) {
31 constexpr size_t BUFSIZE = 8;
32 char buf[BUFSIZE];
33
34 StringStream ss1(buf);
35 ss1 << "Hello, Stream - " << int(123) << StringStream::ENDS;
36 ASSERT_TRUE(ss1.overflow());
37 ASSERT_EQ(ss1.str().size(), BUFSIZE);
38
39 StringStream ss2(buf);
40 ss2 << "7777777";
41 ASSERT_FALSE(ss2.overflow());
42 ASSERT_EQ(ss2.str().size(), size_t(7));
43 ss2 << "8";
44 ASSERT_FALSE(ss2.overflow());
45 ASSERT_EQ(ss2.str().size(), size_t(8));
46 ss2 << StringStream::ENDS;
47 ASSERT_TRUE(ss2.overflow());
48 ASSERT_EQ(ss2.str().size(), BUFSIZE);
49}
50

source code of libc/test/src/__support/CPP/stringstream_test.cpp