Debian Codegraph @

mfbt/tests/gtest/TestBuffer.cpp

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #include "gtest/gtest.h"
6
7 #include "mozilla/Buffer.h"
8 #include "mozilla/Array.h"
9
10 using namespace mozilla;
11
12 TEST(Buffer, TestBufferInfallible)
13 {
14 const size_t LEN = 8;
15 Array<int32_t, LEN> arr = {1, 2, 3, 4, 5, 6, 7, 8};
16 Buffer<int32_t> buf(arr);
17
18 for (size_t i = 0; i < LEN; i++) {
19 ASSERT_EQ(buf[i], arr[i]);
20 }
21
22 auto iter = buf.begin();
23 auto end = buf.end();
24 for (size_t i = 0; i < LEN; i++) {
25 ASSERT_EQ(*iter, arr[i]);
26 iter++;
27 }
28 ASSERT_EQ(iter, end);
29
30 Span<int32_t> span = buf;
31 for (size_t i = 0; i < LEN; i++) {
32 ASSERT_EQ(span[i], arr[i]);
33 }
34
35 auto spanIter = span.begin();
36 auto spanEnd = span.end();
37 for (size_t i = 0; i < LEN; i++) {
38 ASSERT_EQ(*spanIter, arr[i]);
39 spanIter++;
40 }
41 ASSERT_EQ(spanIter, spanEnd);
42
43 span[3] = 42;
44 ASSERT_EQ(buf[3], 42);
45
46 Buffer<int32_t> another(std::move(buf));
47 ASSERT_EQ(another[3], 42);
48 ASSERT_EQ(buf.Length(), 0U);
49 }
50
51 TEST(Buffer, TestBufferFallible)
52 {
53 const size_t LEN = 8;
54 Array<int32_t, LEN> arr = {1, 2, 3, 4, 5, 6, 7, 8};
55 auto maybe = Buffer<int32_t>::CopyFrom(arr);
56 ASSERT_TRUE(maybe.isSome());
57 Buffer<int32_t> buf(std::move(*maybe));
58
59 for (size_t i = 0; i < LEN; i++) {
60 ASSERT_EQ(buf[i], arr[i]);
61 }
62
63 auto iter = buf.begin();
64 auto end = buf.end();
65 for (size_t i = 0; i < LEN; i++) {
66 ASSERT_EQ(*iter, arr[i]);
67 iter++;
68 }
69 ASSERT_EQ(iter, end);
70
71 Span<int32_t> span = buf;
72 for (size_t i = 0; i < LEN; i++) {
73 ASSERT_EQ(span[i], arr[i]);
74 }
75
76 auto spanIter = span.begin();
77 auto spanEnd = span.end();
78 for (size_t i = 0; i < LEN; i++) {
79 ASSERT_EQ(*spanIter, arr[i]);
80 spanIter++;
81 }
82 ASSERT_EQ(spanIter, spanEnd);
83
84 span[3] = 42;
85 ASSERT_EQ(buf[3], 42);
86
87 Buffer<int32_t> another(std::move(buf));
88 ASSERT_EQ(another[3], 42);
89 ASSERT_EQ(buf.Length(), 0U);
90 }
91
92 TEST(Buffer, TestBufferElements)
93 {
94 ASSERT_EQ(Buffer<int32_t>().Elements(),
95 reinterpret_cast<int32_t*>(alignof(int32_t)));
96 }