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

Tests: License headers, clang-format, clearer output

This commit is contained in:
Ben Wiederhake 2020-08-01 23:49:01 +02:00 committed by Andreas Kling
parent 29eceebdbf
commit 58240aedd9
16 changed files with 359 additions and 30 deletions

View file

@ -38,6 +38,7 @@ static const char* TEXT_ERROR = "\x1b[01;35m";
static const char* TEXT_WRONG = "\x1b[01;31m";
static const char* TEXT_OFBY1 = "\x1b[01;97m";
static const char* TEXT_RESET = "\x1b[0m";
static const long long LENIENCY = 8;
struct Testcase {
const char* test_name;
@ -307,7 +308,7 @@ bool is_strtod_close(strtod_fn_t strtod_fn, const char* test_string, const char*
long long actual_ll = cast_ll(readable.as_double);
long long off_by = expect_ll - actual_ll;
bool ofby1_hex = off_by != 0 && -8 <= off_by && off_by <= 8;
bool ofby1_hex = off_by != 0 && -LENIENCY <= off_by && off_by <= LENIENCY;
bool wrong_hex = !ofby1_hex && strcmp(expect_hex, actual_hex) != 0;
bool error_cns = !actual_consume_possible;
bool wrong_cns = !error_cns && (actual_consume != expect_consume);
@ -374,5 +375,11 @@ int main()
}
}
printf("Out of %lu tests, saw %d successes and %d fails.\n", NUM_TESTCASES, successes, fails);
if (fails != 0) {
printf("FAIL\n");
return 1;
}
printf("PASS (with leniency up to %lld ULP from the exact solution.\n", LENIENCY);
return 0;
}

View file

@ -1,6 +1,32 @@
/*
* Copyright (c) 2018-2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{

View file

@ -66,5 +66,6 @@ int main()
++i;
}
printf(failed ? "FAIL\n" : "PASS\n");
return failed ? 1 : 0;
}

View file

@ -37,7 +37,8 @@ struct SortableObject {
int m_payload;
};
int compare_sortable_object(const void* a, const void* b) {
int compare_sortable_object(const void* a, const void* b)
{
const int key1 = static_cast<const SortableObject*>(a)->m_key;
const int key2 = static_cast<const SortableObject*>(b)->m_key;
if (key1 < key2) {
@ -49,12 +50,13 @@ int compare_sortable_object(const void* a, const void* b) {
}
}
int calc_payload_for_pos(size_t pos) {
int calc_payload_for_pos(size_t pos)
{
pos *= 231;
return pos ^ (pos << 8) ^ (pos << 16) ^ (pos << 24);
}
void shuffle_vec(Vector<SortableObject> &test_objects)
void shuffle_vec(Vector<SortableObject>& test_objects)
{
for (size_t i = 0; i < test_objects.size() * 3; ++i) {
auto i1 = rand() % test_objects.size();
@ -68,7 +70,7 @@ int main()
// Generate vector of SortableObjects in sorted order, with payloads determined by their sorted positions
Vector<SortableObject> test_objects;
for (auto i = 0; i < 1024; ++i) {
test_objects.append({i * 137, calc_payload_for_pos(i)});
test_objects.append({ i * 137, calc_payload_for_pos(i) });
}
for (size_t i = 0; i < NUM_RUNS; i++) {
// Shuffle the vector, then sort it again
@ -76,8 +78,8 @@ int main()
qsort(test_objects.data(), test_objects.size(), sizeof(SortableObject), compare_sortable_object);
// Check that the objects are sorted by key
for (auto i = 0u; i + 1 < test_objects.size(); ++i) {
const auto &key1 = test_objects[i].m_key;
const auto &key2 = test_objects[i + 1].m_key;
const auto& key1 = test_objects[i].m_key;
const auto& key2 = test_objects[i + 1].m_key;
if (key1 > key2) {
printf("\x1b[01;35mTests failed: saw key %d before key %d\n", key1, key2);
return 1;
@ -93,6 +95,6 @@ int main()
}
}
}
printf("Tests succeeded\n");
printf("PASS\n");
return 0;
}