1 //===-- Unittests for StringStream ----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/CPP/span.h"
10 #include "src/__support/CPP/stringstream.h"
11 #include "test/UnitTest/Test.h"
13 using LIBC_NAMESPACE::cpp::span
;
14 using LIBC_NAMESPACE::cpp::StringStream
;
16 TEST(LlvmLibcStringStreamTest
, Simple
) {
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");
24 StringStream
ss2(buf
);
25 ss2
<< 'a' << 'b' << 'c' << StringStream::ENDS
;
26 ASSERT_FALSE(ss2
.overflow());
27 ASSERT_STREQ(ss2
.str().data(), "abc");
30 TEST(LlvmLibcStringStreamTest
, Overflow
) {
31 constexpr size_t BUFSIZE
= 8;
34 StringStream
ss1(buf
);
35 ss1
<< "Hello, Stream - " << int(123) << StringStream::ENDS
;
36 ASSERT_TRUE(ss1
.overflow());
37 ASSERT_EQ(ss1
.str().size(), BUFSIZE
);
39 StringStream
ss2(buf
);
41 ASSERT_FALSE(ss2
.overflow());
42 ASSERT_EQ(ss2
.str().size(), size_t(7));
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
);