Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / css / tests_easing_function.lucb

#==============================================================================================
#
#   tests_easing_function - easing functions against the reference build
#
#   DESCRIPTION:
#       Ladybird has no unit tests for CSS::EasingFunction. The test "easing functions
#       evaluate as the reference build's" builds the same dump as the local oracle
#       (luce-browser-tools/oracles/luce-browser-engine/animations/oracle.cpp, which runs the
#       reference build's LibWeb): each easing function of test_easing_texts is parsed as
#       AnimationEffect::parse_easing_string does (from_style_value, linear() canonicalization),
#       serialized, and evaluated at test_easing_inputs with and without the before flag. It
#       compares the dump with the oracle's (tests_easing_function_expected) line by line;
#       doubles are printed as their bits and may differ by 64 units in the last place, the
#       convention of the engine's float oracles (C++ may fuse multiply-adds). The printing
#       helpers and the line splitter are tests_css_values_units'.
#
#==============================================================================================

## The easing functions of the dump, as CSS text (oracle.cpp's s_easings).
let test_easing_texts: str[30] = ["linear", "ease", "ease-in", "ease-out", "ease-in-out", "cubic-bezier(0.1, 0.7, 1, 0.1)",
                                  "cubic-bezier(0.8, 0, 1, 1)", "cubic-bezier(0, 1.5, 1, -0.5)", "cubic-bezier(1, 0, 0, 1)",
                                  "cubic-bezier(0, 0, 0, 0)", "cubic-bezier(1, 1, 1, 1)", "cubic-bezier(0.3, -0.4, 0.7, 1.4)",
                                  "steps(4)", "steps(4, start)", "steps(4, end)", "steps(3, jump-none)", "steps(5, jump-both)",
                                  "steps(1, jump-start)", "steps(2, jump-end)", "step-start", "step-end", "linear(0, 0.25, 1)",
                                  "linear(0, 0.25 75%, 1)", "linear(0 0%, 1 50%, 0 100%)", "linear(0, 1 25% 75%, 0)",
                                  "linear(0.5)", "linear(0 20%, 0.5 10%, 1)", "linear(0, 0.3, 0.6 40%, 1)",
                                  "linear(0, 0.1, 0.2, 0.9 90%, 1)", "linear(-0.5, 1.5 60%, 1)"]

## The input progress values every easing function is evaluated at (oracle.cpp's s_inputs).
let test_easing_inputs: f64[16] = [-0.5, -0.1, 0.0, 0.1, 0.2, 0.25, 1.0 / 3.0, 0.4, 0.5, 0.6, 0.75, 0.9, 0.999, 1.0, 1.1, 1.5]

## How many units in the last place a double of the dump may differ from the oracle's.
let test_easing_ulps: u64 = 64

## The dump oracle.cpp prints.
func test_easing_dump(out: ak.StringBuilder*):
    for text in test_easing_texts:
        tw(out, "easing ")
        tw(out, text)
        tl(out)
        let parsed = animations_animation_effect_parse_easing_string(ak.sv(text))
        if parsed == none:
            tw(out, "invalid\n")
            continue
        let easing = parsed else trap("unreachable: checked above")
        tw(out, "string ")
        ts(out, css_easing_function_to_string(&easing))
        tl(out)
        for input in test_easing_inputs:
            for before_flag in [false, true]:
                td(out, input)
                tw(out, " 1 " if before_flag else " 0 ")
                td(out, css_easing_function_evaluate_at(&easing, input, before_flag))
                tl(out)

## The value of a 16-digit lowercase hex word (the dump's f64 bits), or none for any other word.
func test_easing_parse_f64_bits(word: str) -> u64?:
    if word.length != 16:
        return none
    var value: u64 = 0
    for c in word.bytes:
        if c >= '0' and c <= '9':
            value = value * 16 + (u64)(c - '0')
        elif c >= 'a' and c <= 'f':
            value = value * 16 + (u64)(c - 'a' + 10)
        else:
            return none
    return value

## Whether two dump lines are equal word by word, where words that are 16 hex digits (f64 bits) on both sides may
## differ by at most test_easing_ulps.
func test_easing_lines_match(actual: str, expected: str) -> bool:
    var actual_words = test_split_words(actual)
    var expected_words = test_split_words(expected)
    while true:
        let actual_word = actual_words.next()
        let expected_word = expected_words.next()
        if actual_word == none and expected_word == none:
            return true
        let a = actual_word else return false
        let e = expected_word else return false
        if a == e:
            continue
        let a_bits = test_easing_parse_f64_bits(a) else return false
        let e_bits = test_easing_parse_f64_bits(e) else return false
        let distance = a_bits - e_bits if a_bits >= e_bits else e_bits - a_bits
        if distance > test_easing_ulps:
            return false

test "easing functions evaluate as the reference build's":
    with memory.heap:
        var out = ak.string_builder_new()
        test_easing_dump(&out)

        let actual_view = ak.string_builder_string_view(&out)
        let actual = (str)ak.string_view_bytes(&actual_view)
        var actual_lines = test_split_lines(actual)
        var expected_lines = test_split_lines(test_easing_function_expected)
        var line: usize = 1
        var mismatches: usize = 0
        while true:
            let actual_line = actual_lines.next()
            let expected_line = expected_lines.next()
            if actual_line == none and expected_line == none:
                break
            let actual_text = actual_line else "(end)"
            let expected_text = expected_line else "(end)"
            if actual_text != expected_text and not test_easing_lines_match(actual_text, expected_text):
                if mismatches < 20:
                    print(f"line {line}:\n  expected: {expected_text}\n  actual:   {actual_text}")
                mismatches += 1
            line += 1
        assert(mismatches == 0, "the easing dump differs from the oracle's")

test "the keyword easing functions are the cubic-bezier() and linear() curves they name":
    let ease = css_easing_function_ease()
    match ease.variant:
        .css_cubic_bezier_easing_function(function):
            assert(function.x1 == 0.25 and function.y1 == 0.1 and function.x2 == 0.25 and function.y2 == 1.0)
        _:
            assert(false, "ease is a cubic-bezier()")
    let linear = css_easing_function_linear()
    assert(css_easing_function_evaluate_at(&linear, 0.3, false) == 0.3)
    assert(css_easing_function_evaluate_at(&linear, 1.5, false) == 1.5)