Commit Graph

141 Commits

Author SHA1 Message Date
Ben Wiederhake
4acdb60ba3 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.
2020-08-22 17:18:14 +02:00
Ben Wiederhake
901ed9b85d AK: Demonstrate surprising ByteBuffer behavior
This seems to be because ByteBuffer implements 'operator bool', and C++
considers bool to be an integer type. Thus, when trying to find a way to
evaluate '==', it attempts integer promotion, which in turn finds 'operator bool'.

This explains why all non-empty buffers seem to be equal, but different from the
empty one. Also, why comparison seems to be implemented.
2020-08-22 17:18:14 +02:00
asynts
d5999c3811 AK: Move include <AK/TestSuite.h> to the top.
clang-format automatically sorts include statements that are in a
'block'. Adding a whitespace prevents this. It is crutial that
<AK/TestSuite.h> is included first because it redefines some macros.
2020-08-22 10:46:56 +02:00
asynts
39b464dcfd AK: Remove test case that doesn't test anything.
Currently, there is no way to check that an assert fails. This test
passes regardless of the assert. (AK/HashTable.h:93)
2020-08-22 10:46:56 +02:00
asynts
8bbb7e25e6 LibCompress: Turn the DEFLATE implementation into a stream.
Previously, the implementation would produce one Vector<u8> which
would contain the whole decompressed data. That can be a lot and
even exhaust memory.

With these changes it is still necessary to store the whole input data
in one piece (I am working on this next,) but the output can be read
block by block. (That's not optimal either because blocks can be
arbitrarily large, but it's good for now.)
2020-08-20 16:28:31 +02:00
asynts
30abadcff9 AK: Add DuplexMemoryStream class.
This class is similar to BufferStream because it is possible to both
read and write to it. However, it differs in the following ways:

  - DuplexMemoryStream keeps a history of 64KiB and discards the rest,
    BufferStream always keeps everything around.

  - DuplexMemoryStream tracks reading and writing seperately, the
    following is valid:

        DuplexMemoryStream stream;
        stream << 42;
        int value;
        stream >> value;

    For BufferStream it would read:

        BufferStream stream;
        stream << 42;
        int value;
        stream.seek(0);
        stream >> value;

In the future I would like to replace all usages of BufferStream with
InputMemoryStream, OutputMemoryStream (doesn't exist yet) and
DuplexMemoryStream. For now I just add DuplexMemoryStream though.
2020-08-20 16:28:31 +02:00
asynts
bc332aca33 AK: Rename error() to has_error() for streams. 2020-08-20 16:28:31 +02:00
asynts
6d15318560 AK: Remove fatal() from InputStream.
Fatal errors can not be handeled and lead to an assertion error when the
stream is destroyed. It makes no sense to delay the assertion failure,
instead of setting m_fatal, an assertion should be done directly.
2020-08-20 16:28:31 +02:00
asynts
31e7f435cb AK: Rename TestMemoryStream.cpp to TestStream.cpp. 2020-08-20 16:28:31 +02:00
Ben Wiederhake
5fe6ca75ca AK: Mark compilation-unit-only functions as static
This enables a nice warning in case a function becomes dead code. Also, add forgotten
header to Base64.cpp, which would cause an issue later when we enable -Wmissing-declarations.
2020-08-12 20:40:59 +02:00
Linus Groh
62ea2c5437 AK: Add test case for duplicate JsonObject keys 2020-08-10 14:48:45 +02:00
Ben Wiederhake
cc6d5242d1 AK: Implement and test DistinctNumeric class
This template class allows for easy generation of incompatible numeric types.
This is useful whenever code has to handle heterogenous data (like meters and
seconds) but the underlying data types are compatible (like int and int).

The motivation comes from the Kernel's inconsistent use of pid_t for process and
thread IDs even though the ID spaces are incompatible, and translating forth/back
is nontrivial.

Other uses could be units (as described above), or incompatible index systems.
A popular use in real life is image manipulation, when there are multiple
coordinate systems.
2020-08-10 11:51:45 +02:00
asynts
5bfa7749c3 AK: Add InputStream abstraction and InputMemoryStream implementation. 2020-08-06 10:33:16 +02:00
asynts
42b4880653 AK: Rename Tests/Span.cpp to Tests/TestSpan.cpp. 2020-08-06 10:33:16 +02:00
Andreas Kling
3ba0164ce0 AK: Fix broken symlink used by JSON test 2020-08-06 00:05:52 +02:00
Nico Weber
ce95628b7f Unicode: Try s/codepoint/code_point/g again
This time, without trailing 's'. Ran:

    git grep -l 'codepoint' | xargs sed -ie 's/codepoint/code_point/g
2020-08-05 22:33:42 +02:00
Nico Weber
19ac1f6368 Revert "Unicode: s/codepoint/code_point/g"
This reverts commit ea9ac3155d.
It replaced "codepoint" with "code_points", not "code_point".
2020-08-05 22:33:42 +02:00
Brian Gianforcaro
125eab998f AK: Fix StringUtils tests to actually observe return value.
Noticed this while playing around with making Optional<T>
nodiscard.
2020-08-05 12:18:54 +02:00
Uma Sankar
e799da1fb6
AK: Remove an unused variable in the Span test (#2985) 2020-08-04 10:26:45 +02:00
Andreas Kling
ea9ac3155d Unicode: s/codepoint/code_point/g
Unicode calls them "code points" so let's follow their style.
2020-08-03 19:06:41 +02:00
Muhammad Zahalqa
615ba0f368
AK: Fix overflow and mixed-signedness issues in binary_search() (#2961) 2020-08-02 21:10:35 +02:00
Brian Gianforcaro
6e54d0c072 AK: Remove relative_paths test from TestLexicalPath
This test appears to be testing functionality that doesn't
exist. Just remove it.

Closes old bug #2388

CC @bugaevc
2020-08-02 20:48:37 +02:00
AnotherTest
681bb1fb23 AK: Add (more) tests for String(View)::split*
This patchset adds some tests for String(View)::split*, hopefully
documenting their behaviour in some cases.
2020-07-30 18:47:41 +02:00
Peter Elliott
e57a432118 AK: Make String::substring() return non-null for 0-length strings
This also makes String::split() give non-null strings when keep_empty is
true.
2020-07-28 17:07:22 +02:00
asynts
abe925e4b0 AK: Change the signature of AK::encode_base64() to use Span. 2020-07-27 19:58:09 +02:00
asynts
5fa0fdb219 AK: Rename Span::subspan() to Span::slice(). 2020-07-27 19:58:09 +02:00
asynts
2b57891e07 AK: Add constructors to Bytes and ReadonlyBytes that take void pointers. 2020-07-27 19:58:09 +02:00
asynts
7036a9b6f7 AK: Define conversion from Span<T> to Span<const T> correctly.
I accidently wrote `Span<RemoveConst<T>>` when I meant
`Span<RemoveConst<T>::Type>`.

Changing that wouldn't be enough though, this constructor can only be
defined if T is not const, otherwise it would redefine the copy
constructor.  This can be avoided by overloading the cast operator.
2020-07-27 19:58:09 +02:00
asynts
707d92db61 Refactor: Change the AK::binary_search signature to use AK::Span. 2020-07-26 16:49:06 +02:00
asynts
ac9c2bc492 AK: Implement Span which represents a contiguous sequence of objects.
This makes it possible to pass one object rather than pointer and length
individually.
2020-07-26 16:49:06 +02:00
Nico Weber
5ba8aba197 AK: Make encode_base64 take a ByteBuffer and return a String
That makes the interface symmetric with decode_base64 and it's
what all current callers want (except for one, which is buggy).
2020-07-22 19:22:00 +02:00
Luke
a5ecb9bd6b AK: Add case insensitive version of starts_with 2020-07-21 01:08:32 +02:00
Andreas Kling
ecd4c6718e AK: Fix JsonValue copy constructor behavior for 64-bit values
The fact that JsonValues can contain 64-bit values isn't a JavaScript
compatible behavior in the first place, but as long as we're supporting
this, we should make sure it works correctly.
2020-07-06 18:38:08 +02:00
LepkoQQ
b1c99c1891 AK: Fix JsonParser double encoding multibyte utf-8 chararcters 2020-06-20 17:04:03 +02:00
Tom Lebreux
e14c8b2707 AK: Add tests for Base64 decoder 2020-06-18 23:21:41 +02:00
Tom Lebreux
79529ffd47 AK: Add a simple and inefficient Base64 encoder
The test cases are taken from RFC 4648.
2020-06-18 23:21:41 +02:00
Hüseyin ASLITÜRK
df8fd4e8c8 Test: Fix json parse test from unicode string 2020-06-16 13:15:17 +02:00
Matthew Olsson
e8e728454c AK: JsonParser improvements
- Parsing invalid JSON no longer asserts
    Instead of asserting when coming across malformed JSON,
    JsonParser::parse now returns an Optional<JsonValue>.
- Disallow trailing commas in JSON objects and arrays
- No longer parse 'undefined', as that is a purely JS thing
- No longer allow non-whitespace after anything consumed by the initial
  parse() call. Examples of things that were valid and no longer are:
    - undefineddfz
    - {"foo": 1}abcd
    - [1,2,3]4
- JsonObject.for_each_member now iterates in original insertion order
2020-06-13 12:43:22 +02:00
Andreas Kling
fdfda6dec2 AK: Make string-to-number conversion helpers return Optional
Get rid of the weird old signature:

- int StringType::to_int(bool& ok) const

And replace it with sensible new signature:

- Optional<int> StringType::to_int() const
2020-06-12 21:28:55 +02:00
Sergey Bugaev
583108004c AK: Use unsigned int for refcount
And while fixing all the tests that look at ref_count(),
sneak in a fix for the test suite name.
2020-06-12 16:08:45 +02:00
Sergey Bugaev
602c3fdb3a AK: Rename FileSystemPath -> LexicalPath
And move canonicalized_path() to a static method on LexicalPath.

This is to make it clear that FileSystemPath/canonicalized_path() only
perform *lexical* canonicalization.
2020-05-26 14:35:10 +02:00
Brian Gianforcaro
27360f1f20 Build: Fix cmake test runner, so it knows when tests fail
The CMake runner looks at the return code if you don't set
the pattern. Since the AK test suite setup doesn't use return
codes, we were missing test failures.
2020-05-26 13:38:20 +02:00
Brian Gianforcaro
71deafe476 AK: Temporarily disable failing relative_paths tests in FileSystemPath suite
It appears this got broken in a3e4dfdf98.
I filed a tracking bug https://github.com/SerenityOS/serenity/issues/2388
2020-05-26 13:38:20 +02:00
Brian Gianforcaro
e5045b62b8 AK: Expand string tests to include ends_with case insensitivity 2020-05-26 13:17:19 +02:00
Brian Gianforcaro
332f96e7ca AK: Add case insensitive String::ends_with support
FileSystemPath::has_extension was jumping through hoops and allocating
memory to do a case insensitive comparison needlessly. Extend the
existing String::ends_with method to allow the caller to specify the
case sensitivity required.
2020-05-26 13:17:19 +02:00
AnotherTest
a4e0b585fe AK: Add a way to get the number of valid bytes in a Utf8View 2020-05-18 11:31:43 +02:00
Conrad Pankoff
b5b08fba92 AK: Make sure URL retains trailing slash if present in complete_url 2020-05-17 16:35:42 +02:00
Dominik Madarasz
4e27c58be7 Build: FreeBSD support 2020-05-16 11:01:14 +02:00
Andreas Kling
d8aa2a6997 AK: StringBuilder with 0 initial capacity shouldn't build null String
With 0 initial capacity, we don't allocate an underlying ByteBuffer
for the StringBuilder, which would then lead to a null String() being
returned from to_string().

This patch makes sure we always build a valid String.
2020-05-15 13:50:42 +02:00
Sergey Bugaev
450a2a0f9c Build: Switch to CMake :^)
Closes https://github.com/SerenityOS/serenity/issues/2080
2020-05-14 20:15:18 +02:00