reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
//===-- ProcessorTraceMonitorTest.cpp ------------------------- -*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "gtest/gtest.h"

#include "ProcessorTrace.h"
#include "llvm/ADT/ArrayRef.h"


using namespace lldb_private;
using namespace process_linux;

size_t ReadCylicBufferWrapper(void *buf, size_t buf_size, void *cyc_buf,
                              size_t cyc_buf_size, size_t cyc_start,
                              size_t offset) {
  llvm::MutableArrayRef<uint8_t> dst(reinterpret_cast<uint8_t *>(buf),
                                     buf_size);
  llvm::MutableArrayRef<uint8_t> src(reinterpret_cast<uint8_t *>(cyc_buf),
                                     cyc_buf_size);
  ProcessorTraceMonitor::ReadCyclicBuffer(dst, src, cyc_start, offset);
  return dst.size();
}

TEST(CyclicBuffer, EdgeCases) {
  size_t bytes_read;
  uint8_t cyclic_buffer[6] = {'l', 'i', 'c', 'c', 'y', 'c'};

  // We will always leave the last bytes untouched
  // so that string comparisions work.
  char smaller_buffer[4] = {};

  // empty buffer to read into
  bytes_read = ReadCylicBufferWrapper(smaller_buffer, 0, cyclic_buffer,
                                      sizeof(cyclic_buffer), 3, 0);
  ASSERT_EQ(0u, bytes_read);

  // empty cyclic buffer
  bytes_read = ReadCylicBufferWrapper(smaller_buffer, sizeof(smaller_buffer),
                                      cyclic_buffer, 0, 3, 0);
  ASSERT_EQ(0u, bytes_read);

  // bigger offset
  bytes_read =
      ReadCylicBufferWrapper(smaller_buffer, sizeof(smaller_buffer),
                             cyclic_buffer, sizeof(cyclic_buffer), 3, 6);
  ASSERT_EQ(0u, bytes_read);

  // wrong offset
  bytes_read =
      ReadCylicBufferWrapper(smaller_buffer, sizeof(smaller_buffer),
                             cyclic_buffer, sizeof(cyclic_buffer), 3, 7);
  ASSERT_EQ(0u, bytes_read);

  // wrong start
  bytes_read =
      ReadCylicBufferWrapper(smaller_buffer, sizeof(smaller_buffer),
                             cyclic_buffer, sizeof(cyclic_buffer), 3, 7);
  ASSERT_EQ(0u, bytes_read);
}

TEST(CyclicBuffer, EqualSizeBuffer) {
  size_t bytes_read = 0;
  uint8_t cyclic_buffer[6] = {'l', 'i', 'c', 'c', 'y', 'c'};

  char cyclic[] = "cyclic";
  for (size_t i = 0; i < sizeof(cyclic); i++) {
    // We will always leave the last bytes untouched
    // so that string comparisions work.
    char equal_size_buffer[7] = {};
    bytes_read =
        ReadCylicBufferWrapper(equal_size_buffer, sizeof(cyclic_buffer),
                               cyclic_buffer, sizeof(cyclic_buffer), 3, i);
    ASSERT_EQ((sizeof(cyclic) - i - 1), bytes_read);
    ASSERT_STREQ(equal_size_buffer, (cyclic + i));
  }
}

TEST(CyclicBuffer, SmallerSizeBuffer) {
  size_t bytes_read;
  uint8_t cyclic_buffer[6] = {'l', 'i', 'c', 'c', 'y', 'c'};

  // We will always leave the last bytes untouched
  // so that string comparisions work.
  char smaller_buffer[4] = {};
  bytes_read =
      ReadCylicBufferWrapper(smaller_buffer, (sizeof(smaller_buffer) - 1),
                             cyclic_buffer, sizeof(cyclic_buffer), 3, 0);
  ASSERT_EQ(3u, bytes_read);
  ASSERT_STREQ(smaller_buffer, "cyc");

  bytes_read =
      ReadCylicBufferWrapper(smaller_buffer, (sizeof(smaller_buffer) - 1),
                             cyclic_buffer, sizeof(cyclic_buffer), 3, 1);
  ASSERT_EQ(3u, bytes_read);
  ASSERT_STREQ(smaller_buffer, "ycl");

  bytes_read =
      ReadCylicBufferWrapper(smaller_buffer, (sizeof(smaller_buffer) - 1),
                             cyclic_buffer, sizeof(cyclic_buffer), 3, 2);
  ASSERT_EQ(3u, bytes_read);
  ASSERT_STREQ(smaller_buffer, "cli");

  bytes_read =
      ReadCylicBufferWrapper(smaller_buffer, (sizeof(smaller_buffer) - 1),
                             cyclic_buffer, sizeof(cyclic_buffer), 3, 3);
  ASSERT_EQ(3u, bytes_read);
  ASSERT_STREQ(smaller_buffer, "lic");

  {
    char smaller_buffer[4] = {};
    bytes_read =
        ReadCylicBufferWrapper(smaller_buffer, (sizeof(smaller_buffer) - 1),
                               cyclic_buffer, sizeof(cyclic_buffer), 3, 4);
    ASSERT_EQ(2u, bytes_read);
    ASSERT_STREQ(smaller_buffer, "ic");
  }
  {
    char smaller_buffer[4] = {};
    bytes_read =
        ReadCylicBufferWrapper(smaller_buffer, (sizeof(smaller_buffer) - 1),
                               cyclic_buffer, sizeof(cyclic_buffer), 3, 5);
    ASSERT_EQ(1u, bytes_read);
    ASSERT_STREQ(smaller_buffer, "c");
  }
}

TEST(CyclicBuffer, BiggerSizeBuffer) {
  size_t bytes_read = 0;
  uint8_t cyclic_buffer[6] = {'l', 'i', 'c', 'c', 'y', 'c'};

  char cyclic[] = "cyclic";
  for (size_t i = 0; i < sizeof(cyclic); i++) {
    // We will always leave the last bytes untouched
    // so that string comparisions work.
    char bigger_buffer[10] = {};
    bytes_read =
        ReadCylicBufferWrapper(bigger_buffer, (sizeof(bigger_buffer) - 1),
                               cyclic_buffer, sizeof(cyclic_buffer), 3, i);
    ASSERT_EQ((sizeof(cyclic) - i - 1), bytes_read);
    ASSERT_STREQ(bigger_buffer, (cyclic + i));
  }
}