Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 1 | //===- AsmStreamerTest.cpp - Triple unit tests ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "gtest/gtest.h" |
| 11 | #include "llvm/MC/MCContext.h" |
| 12 | #include "llvm/MC/MCStreamer.h" |
Daniel Dunbar | f5e75a1 | 2009-06-24 16:05:35 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCValue.h" |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 14 | #include "llvm/Support/raw_ostream.h" |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // Helper class. |
| 20 | class StringAsmStreamer { |
| 21 | std::string Str; |
| 22 | raw_string_ostream OS; |
| 23 | MCContext Context; |
| 24 | MCStreamer *Streamer; |
| 25 | |
| 26 | public: |
| 27 | StringAsmStreamer() : OS(Str), Streamer(createAsmStreamer(Context, OS)) {} |
| 28 | ~StringAsmStreamer() { |
| 29 | delete Streamer; |
| 30 | } |
| 31 | |
| 32 | MCContext &getContext() { return Context; } |
| 33 | MCStreamer &getStreamer() { return *Streamer; } |
| 34 | |
| 35 | const std::string &getString() { |
| 36 | Streamer->Finish(); |
| 37 | return Str; |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | TEST(AsmStreamer, EmptyOutput) { |
| 42 | StringAsmStreamer S; |
| 43 | EXPECT_EQ(S.getString(), ""); |
| 44 | } |
| 45 | |
| 46 | TEST(AsmStreamer, Sections) { |
| 47 | StringAsmStreamer S; |
| 48 | MCSection *Sec0 = S.getContext().GetSection("foo"); |
| 49 | S.getStreamer().SwitchSection(Sec0); |
| 50 | EXPECT_EQ(S.getString(), ".section foo\n"); |
| 51 | } |
| 52 | |
Daniel Dunbar | f5e75a1 | 2009-06-24 16:05:35 +0000 | [diff] [blame] | 53 | TEST(AsmStreamer, Values) { |
| 54 | StringAsmStreamer S; |
| 55 | MCSection *Sec0 = S.getContext().GetSection("foo"); |
| 56 | MCSymbol *A = S.getContext().CreateSymbol(S.getContext().CreateAtom(Sec0), |
| 57 | "a"); |
| 58 | MCSymbol *B = S.getContext().CreateSymbol(S.getContext().CreateAtom(Sec0), |
| 59 | "b"); |
| 60 | S.getStreamer().SwitchSection(Sec0); |
| 61 | S.getStreamer().EmitLabel(A); |
| 62 | S.getStreamer().EmitLabel(B); |
| 63 | S.getStreamer().EmitValue(MCValue::get(A, B, 10), 1); |
| 64 | S.getStreamer().EmitValue(MCValue::get(A, B, 10), 2); |
| 65 | S.getStreamer().EmitValue(MCValue::get(A, B, 10), 4); |
| 66 | S.getStreamer().EmitValue(MCValue::get(A, B, 10), 8); |
| 67 | EXPECT_EQ(S.getString(), ".section foo\n\ |
| 68 | a:\n\ |
| 69 | b:\n\ |
| 70 | .byte a - b + 10\n\ |
| 71 | .short a - b + 10\n\ |
| 72 | .long a - b + 10\n\ |
| 73 | .quad a - b + 10\n\ |
| 74 | "); |
| 75 | } |
| 76 | |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 77 | } |