summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/base/vlq-unittest.cc
blob: 647873410a482f6624645e0879cce0370424e15e (plain)
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
// Copyright 2021 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/base/vlq.h"

#include <cmath>
#include <limits>

#include "src/base/memory.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest-support.h"

namespace v8 {
namespace base {

int ExpectedBytesUsed(int64_t value, bool is_signed) {
  uint64_t bits = value;
  if (is_signed) {
    bits = (value < 0 ? -value : value) << 1;
  }
  int num_bits = 0;
  while (bits != 0) {
    num_bits++;
    bits >>= 1;
  }
  return std::max(1, static_cast<int>(ceil(static_cast<float>(num_bits) / 7)));
}

void TestVLQUnsignedEquals(uint32_t value) {
  std::vector<byte> buffer;
  VLQEncodeUnsigned(&buffer, value);
  byte* data_start = buffer.data();
  int index = 0;
  int expected_bytes_used = ExpectedBytesUsed(value, false);
  EXPECT_EQ(buffer.size(), static_cast<size_t>(expected_bytes_used));
  EXPECT_EQ(value, VLQDecodeUnsigned(data_start, &index));
  EXPECT_EQ(index, expected_bytes_used);
}

void TestVLQEquals(int32_t value) {
  std::vector<byte> buffer;
  VLQEncode(&buffer, value);
  byte* data_start = buffer.data();
  int index = 0;
  int expected_bytes_used = ExpectedBytesUsed(value, true);
  EXPECT_EQ(buffer.size(), static_cast<size_t>(expected_bytes_used));
  EXPECT_EQ(value, VLQDecode(data_start, &index));
  EXPECT_EQ(index, expected_bytes_used);
}

TEST(VLQ, Unsigned) {
  TestVLQUnsignedEquals(0);
  TestVLQUnsignedEquals(1);
  TestVLQUnsignedEquals(63);
  TestVLQUnsignedEquals(64);
  TestVLQUnsignedEquals(127);
  TestVLQUnsignedEquals(255);
  TestVLQUnsignedEquals(256);
}

TEST(VLQ, Positive) {
  TestVLQEquals(0);
  TestVLQEquals(1);
  TestVLQEquals(63);
  TestVLQEquals(64);
  TestVLQEquals(127);
  TestVLQEquals(255);
  TestVLQEquals(256);
}

TEST(VLQ, Negative) {
  TestVLQEquals(-1);
  TestVLQEquals(-63);
  TestVLQEquals(-64);
  TestVLQEquals(-127);
  TestVLQEquals(-255);
  TestVLQEquals(-256);
}

TEST(VLQ, LimitsUnsigned) {
  TestVLQEquals(std::numeric_limits<uint8_t>::max());
  TestVLQEquals(std::numeric_limits<uint8_t>::max() - 1);
  TestVLQEquals(std::numeric_limits<uint8_t>::max() + 1);
  TestVLQEquals(std::numeric_limits<uint16_t>::max());
  TestVLQEquals(std::numeric_limits<uint16_t>::max() - 1);
  TestVLQEquals(std::numeric_limits<uint16_t>::max() + 1);
  TestVLQEquals(std::numeric_limits<uint32_t>::max());
  TestVLQEquals(std::numeric_limits<uint32_t>::max() - 1);
}

TEST(VLQ, LimitsSigned) {
  TestVLQEquals(std::numeric_limits<int8_t>::max());
  TestVLQEquals(std::numeric_limits<int8_t>::max() - 1);
  TestVLQEquals(std::numeric_limits<int8_t>::max() + 1);
  TestVLQEquals(std::numeric_limits<int16_t>::max());
  TestVLQEquals(std::numeric_limits<int16_t>::max() - 1);
  TestVLQEquals(std::numeric_limits<int16_t>::max() + 1);
  TestVLQEquals(std::numeric_limits<int32_t>::max());
  TestVLQEquals(std::numeric_limits<int32_t>::max() - 1);
  TestVLQEquals(std::numeric_limits<int8_t>::min());
  TestVLQEquals(std::numeric_limits<int8_t>::min() - 1);
  TestVLQEquals(std::numeric_limits<int8_t>::min() + 1);
  TestVLQEquals(std::numeric_limits<int16_t>::min());
  TestVLQEquals(std::numeric_limits<int16_t>::min() - 1);
  TestVLQEquals(std::numeric_limits<int16_t>::min() + 1);
  // int32_t::min() is not supported.
  TestVLQEquals(std::numeric_limits<int32_t>::min() + 1);
}

TEST(VLQ, Random) {
  static constexpr int RANDOM_RUNS = 50;

  base::RandomNumberGenerator rng(::testing::FLAGS_gtest_random_seed);
  for (int i = 0; i < RANDOM_RUNS; ++i) {
    TestVLQUnsignedEquals(rng.NextInt(std::numeric_limits<int32_t>::max()));
  }
  for (int i = 0; i < RANDOM_RUNS; ++i) {
    TestVLQEquals(rng.NextInt());
  }
}
}  // namespace base
}  // namespace v8