1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 07:07:44 +00:00

AK: Prevent confusing silent misuse of ByteBuffer

Thankfully, this hasn't happened in any other code yet, but it happened
while I was trying something out. Using '==' on two ByteBuffers to check
whether they're equal seemed straight-forward, so I ran into the trap.
This commit is contained in:
Ben Wiederhake 2020-08-22 16:10:19 +02:00 committed by Andreas Kling
parent 901ed9b85d
commit 4acdb60ba3
3 changed files with 74 additions and 10 deletions

View file

@ -53,17 +53,21 @@ TEST_CASE(equality_operator)
EXPECT_EQ(d == d, true);
}
TEST_CASE(other_operators)
/*
* FIXME: These `negative_*` tests should cause precisely one compilation error
* each, and always for the specified reason. Currently we do not have a harness
* for that, so in order to run the test you need to set the #define to 1, compile
* it, and check the error messages manually.
*/
#define COMPILE_NEGATIVE_TESTS 0
#if COMPILE_NEGATIVE_TESTS
TEST_CASE(negative_operator_lt)
{
ByteBuffer a = ByteBuffer::copy("Hello, world", 7);
ByteBuffer b = ByteBuffer::copy("Hello, friend", 7);
// `a` and `b` are both "Hello, ".
ByteBuffer c = ByteBuffer::copy("asdf", 4);
ByteBuffer d;
EXPECT_EQ(a < a, true);
EXPECT_EQ(a <= b, true);
EXPECT_EQ(a >= c, false);
EXPECT_EQ(a > d, false);
ByteBuffer a = ByteBuffer::copy("Hello, world", 10);
ByteBuffer b = ByteBuffer::copy("Hello, friend", 10);
(void)(a < b);
// error: error: use of deleted function bool AK::ByteBuffer::operator<(const AK::ByteBuffer&) const
}
#endif /* COMPILE_NEGATIVE_TESTS */
TEST_MAIN(ByteBuffer)