xref: /aosp_15_r20/external/executorch/runtime/core/test/span_test.cpp (revision 523fa7a60841cd1ecfb9cc4201f1ca8b03ed023a)
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <executorch/runtime/core/span.h>
10 #include <span>
11 
12 #include <gtest/gtest.h>
13 
14 using namespace ::testing;
15 using executorch::runtime::Span;
16 
TEST(SpanTest,Ctors)17 TEST(SpanTest, Ctors) {
18   int64_t x[2] = {1, 2};
19 
20   Span<int64_t> span_range = {x, x + 2};
21   Span<int64_t> span_array = {x};
22 
23   EXPECT_EQ(span_range.size(), 2);
24   EXPECT_EQ(span_array.size(), 2);
25 
26   EXPECT_EQ(span_range.begin(), x);
27   EXPECT_EQ(span_range.end(), x + 2);
28 
29   EXPECT_EQ(span_array.begin(), x);
30   EXPECT_EQ(span_array.end(), x + 2);
31 }
32 
TEST(SpanTest,MutableElements)33 TEST(SpanTest, MutableElements) {
34   int64_t x[2] = {1, 2};
35   Span<int64_t> span = {x, 2};
36   EXPECT_EQ(span.size(), 2);
37   EXPECT_EQ(span[0], 1);
38   span[0] = 2;
39   EXPECT_EQ(span[0], 2);
40 }
41 
TEST(SpanTest,Empty)42 TEST(SpanTest, Empty) {
43   int64_t x[2] = {1, 2};
44   Span<int64_t> span_full = {x, 2};
45   Span<int64_t> span_empty = {x, (size_t)0};
46 
47   EXPECT_FALSE(span_full.empty());
48   EXPECT_TRUE(span_empty.empty());
49 }
50 
TEST(SpanTest,Data)51 TEST(SpanTest, Data) {
52   int64_t x[2] = {1, 2};
53   Span<int64_t> span = {x, 2};
54   EXPECT_EQ(span.data(), x);
55 }
56 
TEST(SpanTest,TriviallyCopyable)57 TEST(SpanTest, TriviallyCopyable) {
58   int64_t x[2] = {1, 2};
59   Span<int64_t> span = {x, 2};
60   Span<int64_t> span_copy = span;
61   EXPECT_EQ(span.data(), span_copy.data());
62   EXPECT_EQ(span.size(), span_copy.size());
63   EXPECT_TRUE(std::is_trivially_copyable<Span<int64_t>>::value);
64 }
65