summaryrefslogtreecommitdiff
path: root/Tests/AK
AgeCommit message (Collapse)Author
2022-11-18AK: Add JSON object/array for-each methods for fallible callbacksTimothy Flynn
This allows the provided callback to return an ErrorOr-like type to propagate errors back to the caller.
2022-11-15Tests/AK: Re-enable `HashTable<double>` testDaniel Bertalan
The incorrect UBSan alignment check that made this test fail has been fixed in Clang 15. Closes #13614
2022-11-11AK: Add optional explicit cast to underlying type to DistinctNumericSam Atkins
2022-11-11AK+Everywhere: Replace DistinctNumeric bool parameters with named onesSam Atkins
This means that rather than this: ``` AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, true, true, false, false, false, true, FunctionAddress); ``` We now have this: ``` AK_TYPEDEF_DISTINCT_NUMERIC_GENERAL(u64, FunctionAddress, Arithmetic, Comparison, Increment); ``` Which is a lot more readable. :^) Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>
2022-11-11AK: Don't crash in HashTable::clear_with_capacity on an empty tableZaggy1024
When calling clear_with_capacity on an empty HashTable/HashMap, a null deref would occur when trying to memset() m_buckets. Checking that it has capacity before clearing fixes the issue.
2022-11-10AK: Allow Variant::downcast<OtherVariantType>()Ali Mohammad Pur
We usually give type aliases to variants, so their variant types are not always available, so make it possible to downcast to another variant type.
2022-11-03AK: Add framework for a unified floating point to string conversionDan Klishch
Currently, the floating point to string conversion is implemented several times across the codebase. This commit provides a pretty low-level function to unify all of such conversions. It converts the given double to a fixed point decimal satisfying a few correctness criteria.
2022-10-24AK: Add SplitBehavior::KeepTrailingSeparator with testsdemostanis
2022-10-24AK+Everywhere: Turn bool keep_empty to an enum in split* functionsdemostanis
2022-10-23AK: Make the JsonParser use the new double parser for numbersdavidot
Because we still support u64 and i64 (on top of i32 and u32) we do still have to parse the number ourself first. Then if we determine that the number is a floating point or is outside of the range of i64 and u64 we fallback and parse it as a double. Before JsonParser had ifdefs guarding the double computation, but it just build when we error on ifdef KERNEL so JsonParser is no longer usable in the Kernel. This can be remedied fairly easily but since it is not needed we #error on that for now.
2022-10-23AK: Add an exact and fast hex float parsing algorithmdavidot
Similar to decimal floating point parsing the current strtod hex float parsing gives a lot of incorrect results. We can use a similar technique as with decimal parsing however hex floats are much simpler as we don't need to scale with a power of 5. For hex floats we just provide the parse_first_hexfloat API as there is currently no need for a parse_hexfloat_completely API. Again the accepted input for parse_first_hexfloat is very lenient and any validation should be done before calling this method.
2022-10-23AK: Add an exact and fast floating point parsing algorithmdavidot
This is based on the paper by Daniel Lemire called "Number parsing at a Gigabyte per second", currently available at https://arxiv.org/abs/2101.11408 An implementation can be found at https://github.com/fastfloat/fast_float To support both strtod like methods and String::to_double we have two different APIs. The parse_first_floating_point gives back both the result, next character to read and the error/out of range status. Out of range here means we rounded to infinity 0. The other API, parse_floating_point_completely, will return a floating point only if the given character range contains just the floating point and nothing else. This can be much faster as we can skip actually computing the value if we notice we did not parse the whole range. Both of these APIs support a very lenient format to be usable in as many places as possible. Also it does not check for "named" values like "nan", "inf", "NAN" etc. Because this can be different for every usage. For integers and small values this new method is not faster and often even a tiny bit slower than the current strtod implementation. However the strtod implementation is wrong for a lot of values and has a much less predictable running time. For correctness this method was tested against known string -> double datasets from https://github.com/nigeltao/parse-number-fxx-test-data This method gives 100% accuracy. The old strtod gave an incorrect value in over 50% of the numbers tested.
2022-10-23AK: Make truncating UFixedBigInts constexprdavidot
Also add some tests and shift tests while we're at it.
2022-10-20AK: Do not append string bytes as code points when title-casing a stringTimothy Flynn
By appending individual bytes as code points, we were "breaking apart" multi-byte UTF-8 code points. This now behaves the same way as the invert_case() helper in StringUtils.
2022-10-11AK+Tests: Correct off-by-one error when right-trimming textSam Atkins
If the entire string you want to right-trim consists of characters you want to remove, we previously would incorrectly leave the first character there. For example: `trim("aaaaa", "a")` would return "a" instead of "". We can't use `i >= 0` in the loop since that would fail to detect underflow, so instead we keep `i` in the range `size .. 1` and then subtract 1 from it when reading the character. Added some trim() tests while I was at it. (And to confirm that this was the issue.)
2022-10-09AK+Tests: Avoid creating invalid code points from malformed UTF-8Ben Wiederhake
Instead of doing anything reasonable, Utf8CodePointIterator returned invalid code points, for example U+123456. However, many callers of this iterator assume that a code point is always at most 0x10FFFF. In fact, this is one of two reasons for the following OSS Fuzz issue: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=49184 This is probably a very old bug. In the particular case of URLParser, AK::is_url_code_point got confused: return /* ... */ || code_point >= 0xA0; If code_point is a "code point" beyond 0x10FFFF, this violates the condition given in the preceding comment, but satisfies the given condition, which eventually causes URLParser to crash. This commit fixes *only* the erroneous UTF-8 decoding, and does not fully resolve OSS-Fuzz#49184.
2022-10-04AK+Everywhere: Add AK_COMPILER_{GCC,CLANG} and use them most placesNico Weber
Doesn't use them in libc headers so that those don't have to pull in AK/Platform.h. AK_COMPILER_GCC is set _only_ for gcc, not for clang too. (__GNUC__ is defined in clang builds as well.) Using AK_COMPILER_GCC simplifies things some. AK_COMPILER_CLANG isn't as much of a win, other than that it's consistent with AK_COMPILER_GCC.
2022-09-20AK: Fix bad parsing of some file:/// URLs with base URLAndreas Kling
We were dropping the base URL path components in the resulting URL due to mistakenly determining the input URL to start with a Windows drive letter. Fix this, add a spec link, and a test.
2022-09-15AK+Tests: Don't double-destroy NoAllocationGuard in TestFixedArrayHendiadyoin1
This caused the m_allocation_enabled_previously member to be technically uninitialized when the compiler emits the implicit destructor call for stack allocated classes. This was pointed out by gcc on lagom builds, no clue how this was flying under the radar for so long and is not triggering CI.
2022-09-14Everywhere: Fix a variety of typosBrian Gianforcaro
Spelling fixes found by `codespell`.
2022-09-02AK: Allow exponents in JSON double valuesdavidot
This is required for ECMA-404 compliance, but probably not for serenity itself.
2022-08-27AK: Add `FloatingPoint.h`Jelle Raaijmakers
This is a set of functions that allow you to convert between arbitrary IEEE 754 floating point types, as long as they can be represented within 64 bits. Conversion methods between floats and doubles are provided, as well as a generic `float_to_float()`. Example usage: #include <AK/FloatingPoint.h> double val = 1.234; auto weird_f16 = convert_from_native_double<FloatingPointBits<0, 6, 10>>(val); Signed and unsigned floats are supported, and both NaN and +/-Inf are handled correctly. Values that do not fit in the target floating point type are clamped.
2022-07-22Everywhere: Prefix 'TYPEDEF_DISTINCT_NUMERIC_GENERAL' with 'AK_'Linus Groh
2022-07-14AK: Use the correct data types in bitap_bitwise()Ali Mohammad Pur
Otherwise the bit twiddling goes all wrong and breaks some boundary cases. Fixes `StringView::contains(31-chars)`.
2022-07-12Tests: Remove StringView char const* initialization testsin-ack
We now explicitly disallow this.
2022-07-12AK+Userland+Tests: Remove URL(char const*) constructorsin-ack
The StringView(char const*) constructor is being removed, and there was only a few users of this left, which are also cleaned up in this commit.
2022-07-12Everywhere: Add sv suffix to strings relying on StringView(char const*)sin-ack
Each of these strings would previously rely on StringView's char const* constructor overload, which would call __builtin_strlen on the string. Since we now have operator ""sv, we can replace these with much simpler versions. This opens the door to being able to remove StringView(char const*). No functional changes.
2022-07-12Everywhere: Explicitly specify the size in StringView constructorssin-ack
This commit moves the length calculations out to be directly on the StringView users. This is an important step towards the goal of removing StringView(char const*), as it moves the responsibility of calculating the size of the string to the user of the StringView (which will prevent naive uses causing OOB access).
2022-07-12Tests: Convert TestBase64 decode test to use StringViews directlysin-ack
Previously it would rely on the implicit StringView conversions. Now the decode_equal function will directly use StringViews.
2022-07-12Tests: Make TestSourceLocation basic_scenario specify StringView lengthsin-ack
2022-07-10AK: Treat empty string as invalid JSONLuke Wilde
Previously we would treat the empty string as `null`. This caused JavaScript like this to fail: ```js var object = {}; try { object = JSON.parse(""); } catch {} var array = object.array || []; ``` Since `JSON.parse("")` returned null instead of throwing, it would set `object` to null and then try and use it instead of using the default backup value.
2022-07-09AK: Add IPv4Address::netmask_from_cidrMaciej
2022-07-06AK: Use an enum instead of a bool for String::replace(all_occurences)DexesTTP
This commit has no behavior changes. In particular, this does not fix any of the wrong uses of the previous default parameter (which used to be 'false', meaning "only replace the first occurence in the string"). It simply replaces the default uses by String::replace(..., ReplaceMode::FirstOnly), leaving them incorrect.
2022-07-04Tests: Move sprintf test from AK/ to LibC/Daniel Bertalan
This test doesn't test AK::String, but LibC's sprintf instead, so it does not belong in `Tests/AK`. This also means this test won't be ran on Lagom using the host OS's printf implementation. Fixes a deprecated declaration warning when compiling with macOS SDK 13.
2022-06-23AK: Zero previous pointer *after* fixing the insertion list in HashTableHendiadyoin1
2022-06-22AK: Clear the previous and next pointers of deleted HashTable bucketsIdan Horowitz
Usually the values of the previous and next pointers of deleted buckets are never used, as they're not part of the main ordered bucket chain, but if an in-place rehashing is done, which results in the bucket being turned into a free bucket, the stale pointers will remain, at which point any item that is inserted into said free-bucket will have either a stale previous pointer if the HashTable was empty on insertion, or a stale next pointer, resulting in undefined behaviour. This commit also includes a new HashMap test that reproduces this issue
2022-04-11AK: Disable the HashTable<double> test until UB issue is fixedAndreas Kling
2022-04-10AK: Add hash traits for floating-point primitivesAndreas Kling
This allows us to use float and double as hash keys.
2022-04-05AK: Invalidate UTF-8 encoded code points larger than U+10ffffTimothy Flynn
On oss-fuzz, the LibJS REPL is provided a file encoded with Windows-1252 with the following contents: /ô¡°½/ The REPL assumes the input file is UTF-8. So in Windows-1252, the above is represented as [0x2f 0xf4 0xa1 0xb0 0xbd 0x2f]. The inner 4 bytes are actually a valid UTF-8 encoding if we only look at the most significant bits to parse leading/continuation bytes. However, it decodes to the code point U+121c3d, which is not a valid code point. This commit adds additional validation to ensure the decoded code point itself is also valid.
2022-04-04AK: Allow Optional<T&> to existAli Mohammad Pur
This implements Optional<T&> as a T*, whose presence has been missing since the early days of Optional. As a lot of find_foo() APIs return an Optional<T> which imposes a pointless copy on the underlying value, and can sometimes be very misleading, with this change, those APIs can return Optional<T&>.
2022-04-01Everywhere: Run clang-formatIdan Horowitz
2022-03-31Tests: Test non-trivial re-hashing in HashTablekleines Filmröllchen
This caused a system-wide crash because of a previous bug relating to non-trivial types in HashTable. Therefore, check that such types actually work under various workloads.
2022-03-31Tests: Introduce a HashTable benchmark for "table thrashing"kleines Filmröllchen
Thrashing is what I call the situations where a table is mostly filled with deleted markers, causing an increase in size (at least temporarily) when a simple re-hash would be enough to get rid of those. This happens when a hash table (especially with many elements) has a lot of deletes and re-inserts done to it, which is what this benchmark does.
2022-03-30AK: Allow printing wide characters using %ls modifiersafarp
2022-03-27AK: Add an ArbitrarySizedEnum templateLinus Groh
This is an enum-like type that works with arbitrary sized storage > u64, which is the limit for a regular enum class - which limits it to 64 members when needing bit field behavior. Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>
2022-03-18AK+Tests: Fix StringUtils::contains() being confused by repeating textSam Atkins
Previously, case-insensitively searching the haystack "Go Go Back" for the needle "Go Back" would return false: 1. Match the first three characters. "Go ". 2. Notice that 'G' and 'B' don't match. 3. Skip ahead 3 characters, plus 1 for the outer for-loop. 4. Now, the haystack is effectively "o Back", so the match fails. Reducing the skip by 1 fixes this issue. I'm not 100% convinced this fixes all cases, but I haven't been able to find any cases where it doesn't work now. :^)
2022-03-13AK: Add naive implementations of AK::timing_safe_compareBrian Gianforcaro
For security critical code we need to have some way of performing constant time buffer comparisons.
2022-03-13Tests: Rename AK/TestMemMem.cpp to AK/TestMemory.cppBrian Gianforcaro
Rename to create a new generic test group for the AK memory APIs.
2022-03-09AK: Add reverse iterator as memberFederico Guerinoni
2022-03-09AK: Implement wrapper for reverse range for loopFederico Guerinoni
Now it is possible to use range for loop in reverse mode for a container. ``` for (auto item : in_reverse(vector)) ```