summaryrefslogtreecommitdiffstats
path: root/mfbt/tests/gtest/TestBuffer.cpp
blob: df36282be1f55a0ef4e325b991da20a022ab3f70 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"

#include "mozilla/Buffer.h"
#include "mozilla/Array.h"

using namespace mozilla;

TEST(Buffer, TestBufferInfallible)
{
  const size_t LEN = 8;
  Array<int32_t, LEN> arr = {1, 2, 3, 4, 5, 6, 7, 8};
  Buffer<int32_t> buf(arr);

  for (size_t i = 0; i < LEN; i++) {
    ASSERT_EQ(buf[i], arr[i]);
  }

  auto iter = buf.begin();
  auto end = buf.end();
  for (size_t i = 0; i < LEN; i++) {
    ASSERT_EQ(*iter, arr[i]);
    iter++;
  }
  ASSERT_EQ(iter, end);

  Span<int32_t> span = buf;
  for (size_t i = 0; i < LEN; i++) {
    ASSERT_EQ(span[i], arr[i]);
  }

  auto spanIter = span.begin();
  auto spanEnd = span.end();
  for (size_t i = 0; i < LEN; i++) {
    ASSERT_EQ(*spanIter, arr[i]);
    spanIter++;
  }
  ASSERT_EQ(spanIter, spanEnd);

  span[3] = 42;
  ASSERT_EQ(buf[3], 42);

  Buffer<int32_t> another(std::move(buf));
  ASSERT_EQ(another[3], 42);
  ASSERT_EQ(buf.Length(), 0U);
}

TEST(Buffer, TestBufferFallible)
{
  const size_t LEN = 8;
  Array<int32_t, LEN> arr = {1, 2, 3, 4, 5, 6, 7, 8};
  auto maybe = Buffer<int32_t>::CopyFrom(arr);
  ASSERT_TRUE(maybe.isSome());
  Buffer<int32_t> buf(std::move(*maybe));

  for (size_t i = 0; i < LEN; i++) {
    ASSERT_EQ(buf[i], arr[i]);
  }

  auto iter = buf.begin();
  auto end = buf.end();
  for (size_t i = 0; i < LEN; i++) {
    ASSERT_EQ(*iter, arr[i]);
    iter++;
  }
  ASSERT_EQ(iter, end);

  Span<int32_t> span = buf;
  for (size_t i = 0; i < LEN; i++) {
    ASSERT_EQ(span[i], arr[i]);
  }

  auto spanIter = span.begin();
  auto spanEnd = span.end();
  for (size_t i = 0; i < LEN; i++) {
    ASSERT_EQ(*spanIter, arr[i]);
    spanIter++;
  }
  ASSERT_EQ(spanIter, spanEnd);

  span[3] = 42;
  ASSERT_EQ(buf[3], 42);

  Buffer<int32_t> another(std::move(buf));
  ASSERT_EQ(another[3], 42);
  ASSERT_EQ(buf.Length(), 0U);
}

TEST(Buffer, TestBufferElements)
{
  ASSERT_EQ(Buffer<int32_t>().Elements(),
            reinterpret_cast<int32_t*>(alignof(int32_t)));
}