93 lines · cpp
1//===- InterleavedRangeTest.cpp - Unit tests for interleaved format -------===//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#include "llvm/Support/InterleavedRange.h"10#include "llvm/ADT/SmallVector.h"11#include "llvm/Support/raw_ostream.h"12#include "gmock/gmock.h"13#include "gtest/gtest.h"14 15using namespace llvm;16 17namespace {18 19TEST(InterleavedRangeTest, VectorInt) {20 SmallVector<int> V = {0, 1, 2, 3};21 22 // First, make sure that the raw print API works as expected.23 std::string Buff;24 raw_string_ostream OS(Buff);25 OS << interleaved(V);26 EXPECT_EQ("0, 1, 2, 3", Buff);27 Buff.clear();28 OS << interleaved_array(V);29 EXPECT_EQ("[0, 1, 2, 3]", Buff);30 31 // In the rest of the tests, use `.str()` for convenience.32 EXPECT_EQ("0, 1, 2, 3", interleaved(V).str());33 EXPECT_EQ("{{0,1,2,3}}", interleaved(V, ",", "{{", "}}").str());34 EXPECT_EQ("[0, 1, 2, 3]", interleaved_array(V).str());35 EXPECT_EQ("[0;1;2;3]", interleaved_array(V, ";").str());36 EXPECT_EQ("0;1;2;3", interleaved(V, ";").str());37}38 39TEST(InterleavedRangeTest, VectorIntEmpty) {40 SmallVector<int> V = {};41 EXPECT_EQ("", interleaved(V).str());42 EXPECT_EQ("{{}}", interleaved(V, ",", "{{", "}}").str());43 EXPECT_EQ("[]", interleaved_array(V).str());44 EXPECT_EQ("", interleaved(V, ";").str());45}46 47TEST(InterleavedRangeTest, VectorIntOneElem) {48 SmallVector<int> V = {42};49 EXPECT_EQ("42", interleaved(V).str());50 EXPECT_EQ("{{42}}", interleaved(V, ",", "{{", "}}").str());51 EXPECT_EQ("[42]", interleaved_array(V).str());52 EXPECT_EQ("42", interleaved(V, ";").str());53}54 55struct CustomPrint {56 int N;57 friend raw_ostream &operator<<(raw_ostream &OS, const CustomPrint &CP) {58 OS << "$$" << CP.N << "##";59 return OS;60 }61};62 63TEST(InterleavedRangeTest, CustomPrint) {64 CustomPrint V[] = {{3}, {4}, {5}};65 EXPECT_EQ("$$3##, $$4##, $$5##", interleaved(V).str());66 EXPECT_EQ("{{$$3##;$$4##;$$5##}}", interleaved(V, ";", "{{", "}}").str());67 EXPECT_EQ("[$$3##, $$4##, $$5##]", interleaved_array(V).str());68}69 70struct CustomDoublingOStream : raw_string_ostream {71 unsigned NumCalled = 0;72 using raw_string_ostream::raw_string_ostream;73 74 friend CustomDoublingOStream &operator<<(CustomDoublingOStream &OS, int V) {75 ++OS.NumCalled;76 static_cast<raw_string_ostream &>(OS) << (2 * V);77 return OS;78 }79};80 81TEST(InterleavedRangeTest, CustomOStream) {82 // Make sure that interleaved calls the stream operator on the derived class,83 // and that it returns a reference to the same stream type.84 int V[] = {3, 4, 5};85 std::string Buf;86 CustomDoublingOStream OS(Buf);87 OS << interleaved(V) << 22;88 EXPECT_EQ("6, 8, 1044", Buf);89 EXPECT_EQ(OS.NumCalled, 4u);90}91 92} // namespace93