BitstreamWriterTest.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //===- BitstreamWriterTest.cpp - Tests for BitstreamWriter ----------------===//
  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. #include "llvm/Bitstream/BitstreamWriter.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/ADT/SmallString.h"
  11. #include "gtest/gtest.h"
  12. using namespace llvm;
  13. namespace {
  14. TEST(BitstreamWriterTest, emitBlob) {
  15. SmallString<64> Buffer;
  16. BitstreamWriter W(Buffer);
  17. W.emitBlob("str", /* ShouldEmitSize */ false);
  18. EXPECT_EQ(StringRef("str\0", 4), Buffer);
  19. }
  20. TEST(BitstreamWriterTest, emitBlobWithSize) {
  21. SmallString<64> Buffer;
  22. {
  23. BitstreamWriter W(Buffer);
  24. W.emitBlob("str");
  25. }
  26. SmallString<64> Expected;
  27. {
  28. BitstreamWriter W(Expected);
  29. W.EmitVBR(3, 6);
  30. W.FlushToWord();
  31. W.Emit('s', 8);
  32. W.Emit('t', 8);
  33. W.Emit('r', 8);
  34. W.Emit(0, 8);
  35. }
  36. EXPECT_EQ(StringRef(Expected), Buffer);
  37. }
  38. TEST(BitstreamWriterTest, emitBlobEmpty) {
  39. SmallString<64> Buffer;
  40. BitstreamWriter W(Buffer);
  41. W.emitBlob("", /* ShouldEmitSize */ false);
  42. EXPECT_EQ(StringRef(""), Buffer);
  43. }
  44. TEST(BitstreamWriterTest, emitBlob4ByteAligned) {
  45. SmallString<64> Buffer;
  46. BitstreamWriter W(Buffer);
  47. W.emitBlob("str0", /* ShouldEmitSize */ false);
  48. EXPECT_EQ(StringRef("str0"), Buffer);
  49. }
  50. } // end namespace