1 /* Copyright (c) 2017, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <stdio.h>
16 #include <vector>
17
18 #include <gtest/gtest.h>
19
20 #include <openssl/ssl.h>
21
22 BSSL_NAMESPACE_BEGIN
23 namespace {
24
TestCtor(Span<int> s,const int * ptr,size_t size)25 static void TestCtor(Span<int> s, const int *ptr, size_t size) {
26 EXPECT_EQ(s.data(), ptr);
27 EXPECT_EQ(s.size(), size);
28 }
29
TestConstCtor(Span<const int> s,const int * ptr,size_t size)30 static void TestConstCtor(Span<const int> s, const int *ptr, size_t size) {
31 EXPECT_EQ(s.data(), ptr);
32 EXPECT_EQ(s.size(), size);
33 }
34
TEST(SpanTest,CtorEmpty)35 TEST(SpanTest, CtorEmpty) {
36 Span<int> s;
37 TestCtor(s, nullptr, 0);
38 }
39
TEST(SpanTest,CtorFromPtrAndSize)40 TEST(SpanTest, CtorFromPtrAndSize) {
41 std::vector<int> v = {7, 8, 9, 10};
42 Span<int> s(v.data(), v.size());
43 TestCtor(s, v.data(), v.size());
44 }
45
TEST(SpanTest,CtorFromVector)46 TEST(SpanTest, CtorFromVector) {
47 std::vector<int> v = {1, 2};
48 // Const ctor is implicit.
49 TestConstCtor(v, v.data(), v.size());
50 // Mutable is explicit.
51 Span<int> s(v);
52 TestCtor(s, v.data(), v.size());
53 }
54
TEST(SpanTest,CtorConstFromArray)55 TEST(SpanTest, CtorConstFromArray) {
56 int v[] = {10, 11};
57 // Array ctor is implicit for const and mutable T.
58 TestConstCtor(v, v, 2);
59 TestCtor(v, v, 2);
60 }
61
TEST(SpanTest,MakeSpan)62 TEST(SpanTest, MakeSpan) {
63 std::vector<int> v = {100, 200, 300};
64 TestCtor(MakeSpan(v), v.data(), v.size());
65 TestCtor(MakeSpan(v.data(), v.size()), v.data(), v.size());
66 TestConstCtor(MakeSpan(v.data(), v.size()), v.data(), v.size());
67 TestConstCtor(MakeSpan(v), v.data(), v.size());
68 }
69
TEST(SpanTest,MakeConstSpan)70 TEST(SpanTest, MakeConstSpan) {
71 std::vector<int> v = {100, 200, 300};
72 TestConstCtor(MakeConstSpan(v), v.data(), v.size());
73 TestConstCtor(MakeConstSpan(v.data(), v.size()), v.data(), v.size());
74 // But not:
75 // TestConstCtor(MakeSpan(v), v.data(), v.size());
76 }
77
TEST(SpanTest,Accessor)78 TEST(SpanTest, Accessor) {
79 std::vector<int> v({42, 23, 5, 101, 80});
80 Span<int> s(v);
81 for (size_t i = 0; i < s.size(); ++i) {
82 EXPECT_EQ(s[i], v[i]);
83 EXPECT_EQ(s.at(i), v.at(i));
84 }
85 EXPECT_EQ(s.begin(), v.data());
86 EXPECT_EQ(s.end(), v.data() + v.size());
87 }
88
89 } // namespace
90 BSSL_NAMESPACE_END
91