Serenity Operating System
1/*
2 * Copyright (c) 2020, the SerenityOS developers.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibTest/TestCase.h>
8
9#include <AK/Checked.h>
10#include <AK/Span.h>
11#include <string.h>
12
13TEST_CASE(constexpr_default_constructor_is_empty)
14{
15 constexpr Span<int> span;
16 static_assert(span.is_empty(), "Default constructed span should be empty.");
17}
18
19TEST_CASE(implicit_conversion_to_const)
20{
21 constexpr Bytes bytes0;
22 [[maybe_unused]] constexpr ReadonlyBytes bytes2 = bytes0;
23 [[maybe_unused]] constexpr ReadonlyBytes bytes3 = static_cast<ReadonlyBytes>(bytes2);
24}
25
26TEST_CASE(span_works_with_constant_types)
27{
28 static constexpr u8 buffer[4] { 1, 2, 3, 4 };
29 constexpr ReadonlyBytes bytes { buffer, 4 };
30
31 static_assert(IsConst<RemoveReference<decltype(bytes[1])>>);
32 static_assert(bytes[2] == 3);
33}
34
35TEST_CASE(span_works_with_mutable_types)
36{
37 u8 buffer[4] { 1, 2, 3, 4 };
38 Bytes bytes { buffer, 4 };
39
40 EXPECT_EQ(bytes[2], 3);
41 ++bytes[2];
42 EXPECT_EQ(bytes[2], 4);
43}
44
45TEST_CASE(iterator_behaves_like_loop)
46{
47 u8 buffer[256];
48 for (int idx = 0; idx < 256; ++idx) {
49 buffer[idx] = static_cast<u8>(idx);
50 }
51
52 Bytes bytes { buffer, 256 };
53 size_t idx = 0;
54 for (auto iter = bytes.begin(); iter < bytes.end(); ++iter) {
55 EXPECT_EQ(*iter, buffer[idx]);
56
57 ++idx;
58 }
59}
60
61TEST_CASE(modifying_is_possible)
62{
63 int values_before[8] = { 1, 2, 3, 4, 5, 6, 7, 8 };
64 int values_after[8] = { 7, 6, 5, 4, 3, 2, 1, 0 };
65
66 Span<int> span { values_before, 8 };
67 for (auto& value : span) {
68 value = 8 - value;
69 }
70
71 for (int idx = 0; idx < 8; ++idx) {
72 EXPECT_EQ(values_before[idx], values_after[idx]);
73 }
74}
75
76TEST_CASE(at_and_index_operator_return_same_value)
77{
78 u8 buffer[256];
79 for (int idx = 0; idx < 256; ++idx) {
80 buffer[idx] = static_cast<u8>(idx);
81 }
82
83 Bytes bytes { buffer, 256 };
84 for (int idx = 0; idx < 256; ++idx) {
85 EXPECT_EQ(buffer[idx], bytes[idx]);
86 EXPECT_EQ(bytes[idx], bytes.at(idx));
87 }
88}
89
90TEST_CASE(can_subspan_whole_span)
91{
92 static constexpr u8 buffer[16] {};
93 constexpr ReadonlyBytes bytes { buffer, 16 };
94
95 constexpr auto slice = bytes.slice(0, 16);
96
97 static_assert(slice.data() == buffer);
98 static_assert(slice.size() == 16u);
99}
100
101TEST_CASE(can_subspan_as_intended)
102{
103 static constexpr u16 buffer[8] { 1, 2, 3, 4, 5, 6, 7, 8 };
104
105 constexpr ReadonlySpan<u16> span { buffer, 8 };
106 constexpr auto slice = span.slice(3, 2);
107
108 static_assert(slice.size() == 2u);
109 static_assert(slice[0] == 4);
110 static_assert(slice[1] == 5);
111}
112
113TEST_CASE(span_from_void_pointer)
114{
115 int value = 0;
116 [[maybe_unused]] Bytes bytes0 { reinterpret_cast<void*>(value), 4 };
117 [[maybe_unused]] ReadonlyBytes bytes1 { reinterpret_cast<void const*>(value), 4 };
118}
119
120TEST_CASE(span_from_c_string)
121{
122 char const* str = "Serenity";
123 [[maybe_unused]] ReadonlyBytes bytes { str, strlen(str) };
124}
125
126TEST_CASE(starts_with)
127{
128 char const* str = "HeyFriends!";
129 ReadonlyBytes bytes { str, strlen(str) };
130 char const* str_hey = "Hey";
131 ReadonlyBytes hey_bytes { str_hey, strlen(str_hey) };
132 EXPECT(bytes.starts_with(hey_bytes));
133 char const* str_nah = "Nah";
134 ReadonlyBytes nah_bytes { str_nah, strlen(str_nah) };
135 EXPECT(!bytes.starts_with(nah_bytes));
136
137 const u8 hey_array[3] = { 'H', 'e', 'y' };
138 ReadonlyBytes hey_bytes_u8 { hey_array, 3 };
139 EXPECT(bytes.starts_with(hey_bytes_u8));
140}