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/ByteBuffer.h>
10#include <AK/Vector.h>
11
12TEST_CASE(equality_operator)
13{
14 ByteBuffer a = ByteBuffer::copy("Hello, world", 7).release_value();
15 ByteBuffer b = ByteBuffer::copy("Hello, friend", 7).release_value();
16 // `a` and `b` are both "Hello, ".
17 ByteBuffer c = ByteBuffer::copy("asdf", 4).release_value();
18 ByteBuffer d;
19 EXPECT_EQ(a == a, true);
20 EXPECT_EQ(a == b, true);
21 EXPECT_EQ(a == c, false);
22 EXPECT_EQ(a == d, false);
23 EXPECT_EQ(b == a, true);
24 EXPECT_EQ(b == b, true);
25 EXPECT_EQ(b == c, false);
26 EXPECT_EQ(b == d, false);
27 EXPECT_EQ(c == a, false);
28 EXPECT_EQ(c == b, false);
29 EXPECT_EQ(c == c, true);
30 EXPECT_EQ(c == d, false);
31 EXPECT_EQ(d == a, false);
32 EXPECT_EQ(d == b, false);
33 EXPECT_EQ(d == c, false);
34 EXPECT_EQ(d == d, true);
35}
36
37TEST_CASE(byte_buffer_vector_contains_slow_bytes)
38{
39 Vector<ByteBuffer> vector;
40 ByteBuffer a = ByteBuffer::copy("Hello, friend", 13).release_value();
41 vector.append(a);
42
43 ReadonlyBytes b = "Hello, friend"sv.bytes();
44 Bytes c = a.bytes();
45 EXPECT_EQ(vector.contains_slow(b), true);
46 EXPECT_EQ(vector.contains_slow(c), true);
47}
48
49BENCHMARK_CASE(append)
50{
51 ByteBuffer bb;
52 for (size_t i = 0; i < 1000000; ++i) {
53 bb.append(static_cast<u8>(i));
54 }
55}
56
57/*
58 * FIXME: These `negative_*` tests should cause precisely one compilation error
59 * each, and always for the specified reason. Currently we do not have a harness
60 * for that, so in order to run the test you need to set the #define to 1, compile
61 * it, and check the error messages manually.
62 */
63#define COMPILE_NEGATIVE_TESTS 0
64#if COMPILE_NEGATIVE_TESTS
65TEST_CASE(negative_operator_lt)
66{
67 ByteBuffer a = ByteBuffer::copy("Hello, world", 10).release_value();
68 ByteBuffer b = ByteBuffer::copy("Hello, friend", 10).release_value();
69 [[maybe_unused]] auto res = a < b;
70 // error: error: use of deleted function ‘bool AK::ByteBuffer::operator<(const AK::ByteBuffer&) const’
71}
72#endif /* COMPILE_NEGATIVE_TESTS */