dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / css / style_values / tests_style_values_2.lucb
#==============================================================================================
#
# tests_style_values_2 - region r35's style values print what the reference build prints
#
# DESCRIPTION:
# Ladybird has no unit tests for CalculatedStyleValue, NumericType or the other style values
# of region r35. A local oracle (luce-browser-tools/oracles/luce-browser-engine/style_values_2:
# oracle.cpp, cases.txt, gen_luce_cases.py) parses each case of tests_style_values_2_cases
# with the reference build's value parser and prints the value's serializations (normal and
# resolved), its computational independence and its absolutization in an 800x600 context;
# for a CalculatedStyleValue also the calculation dump, the resolved NumericType, the
# resolves_to_* flags and every resolve_* in an empty context, a length context and a
# length context with a percentage basis (200px, 90deg, 2s or 100hz). The port prints the
# same and the dumps are compared line by line. The parsing helpers are r32's
# (tests_css_value_parsing), the contexts r34's (tests_style_values_1).
#
#==============================================================================================
# mark: Printing ================================================================================
## A bool as AK's `{:d}` prints it.
func test_sv2_flag(value: bool) -> str:
return "1" if value else "0"
## The oracle's `basis_for`: the percentage basis for a calculation whose percentages resolve as `resolve_as`.
func test_sv2_basis_for(resolve_as: CssValueType?) -> CssCalculationResolutionContextPercentageBasis:
if resolve_as == CssValueType.length:
return CssCalculationResolutionContextPercentageBasis.css_length(css_length_make_px_f64(200.0))
if resolve_as == CssValueType.angle:
return CssCalculationResolutionContextPercentageBasis.css_angle(css_angle_make_degrees(90.0))
if resolve_as == CssValueType.time:
return CssCalculationResolutionContextPercentageBasis.css_time(css_time_make_seconds(2.0))
if resolve_as == CssValueType.frequency:
return CssCalculationResolutionContextPercentageBasis.css_frequency(css_frequency_make_hertz(100.0))
return CssCalculationResolutionContextPercentageBasis.empty
## The oracle's `resolve_would_verify`: whether resolve_value() VERIFYs (traps) in this context.
func test_sv2_resolve_would_verify(calc: CssCalculatedStyleValue*, context: CssCalculationResolutionContext) -> bool:
let simplified = css_simplify_a_calculation_tree(calc.m_calculation, calc.m_context, context)
let numeric = as_if_css_numeric_calculation_node(simplified) else return false
return not css_numeric_calculation_node_is_in_canonical_unit(numeric) or (css_numeric_value_is_percentage(&numeric.m_value) and calc.m_context.percentages_resolve_as != none) or css_calculation_node_numeric_type(simplified) == none
## An optional double as the oracle prints it: `{}` or "-".
func test_sv2_opt_double(value: f64?) -> ak.String:
if let v = value:
return css_string_formatted("{}", [ak.arg_f64(v)])
return ak.string_from_str("-")
## The oracle's `resolves`: every resolve_* of `calc` in `context`.
func test_sv2_resolves(out: ak.StringBuilder*, calc: CssCalculatedStyleValue*, name: str, context: CssCalculationResolutionContext):
if test_sv2_resolve_would_verify(calc, context):
test_css_line(out, 1, css_string_formatted("{}: verifies", [ak.arg_str(name)]))
return
let number = test_sv2_opt_double(css_calculated_style_value_resolve_number(calc, context))
var integer = ak.string_from_str("-")
if let value = css_calculated_style_value_resolve_integer(calc, context):
integer = css_string_formatted("{}", [ak.arg_i64((i64)value)])
var length = ak.string_from_str("-")
if let value = css_calculated_style_value_resolve_length(calc, context):
length = css_length_to_string(&value)
let raw_length = test_sv2_opt_double(css_calculated_style_value_resolve_raw_length(calc, context))
var percentage = ak.string_from_str("-")
if let value = css_calculated_style_value_resolve_percentage(calc, context):
percentage = css_percentage_to_string(&value)
var angle = ak.string_from_str("-")
if let value = css_calculated_style_value_resolve_angle(calc, context):
angle = css_angle_to_string(&value)
var time = ak.string_from_str("-")
if let value = css_calculated_style_value_resolve_time(calc, context):
time = css_time_to_string(&value)
var frequency = ak.string_from_str("-")
if let value = css_calculated_style_value_resolve_frequency(calc, context):
frequency = css_frequency_to_string(&value)
var resolution = ak.string_from_str("-")
if let value = css_calculated_style_value_resolve_resolution(calc, context):
resolution = css_resolution_to_string(&value)
var flex = ak.string_from_str("-")
if let value = css_calculated_style_value_resolve_flex(calc, context):
flex = css_flex_to_string(&value)
test_css_line(out, 1, css_string_formatted("{}: number={} integer={} length={} raw_length={} percentage={} angle={} time={} frequency={} resolution={} flex={}", [
ak.arg_str(name),
css_arg_string(&number),
css_arg_string(&integer),
css_arg_string(&length),
css_arg_string(&raw_length),
css_arg_string(&percentage),
css_arg_string(&angle),
css_arg_string(&time),
css_arg_string(&frequency),
css_arg_string(&resolution),
css_arg_string(&flex),
]))
## The oracle's `calculated`: the dump, type, flags and resolutions of a CalculatedStyleValue.
func test_sv2_calculated(out: ak.StringBuilder*, calc: CssCalculatedStyleValue*):
let dumped = css_calculated_style_value_dump(calc)
let dumped_view = ak.string_bytes_as_string_view(&dumped)
# StringView::split_view('\n') drops empty parts.
var start: usize = 0
let bytes = ak.string_view_bytes(&dumped_view)
for i in 0..=bytes.length:
if i == bytes.length or bytes[i] == '\n':
if i > start:
test_css_line(out, 1, css_string_formatted("| {}", [ak.arg_string_view(ak.string_view_from_bytes(bytes[start..<i]))]))
start = i + 1
let type_dump = css_numeric_type_dump(&calc.m_resolved_type)
test_css_line(out, 1, css_string_formatted("type: {}", [css_arg_string(&type_dump)]))
var flags = ak.string_builder_new()
ak.string_builder_append(&flags, "flags: angle=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_angle(calc)))
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_angle_percentage(calc)))
ak.string_builder_append(&flags, " flex=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_flex(calc)))
ak.string_builder_append(&flags, " frequency=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_frequency(calc)))
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_frequency_percentage(calc)))
ak.string_builder_append(&flags, " length=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_length(calc)))
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_length_percentage(calc)))
ak.string_builder_append(&flags, " percentage=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_percentage(calc)))
ak.string_builder_append(&flags, " resolution=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_resolution(calc)))
ak.string_builder_append(&flags, " time=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_time(calc)))
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_time_percentage(calc)))
ak.string_builder_append(&flags, " number=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_number(calc)))
ak.string_builder_append(&flags, " dimension=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_resolves_to_dimension(calc)))
ak.string_builder_append(&flags, " contains_percentage=")
ak.string_builder_append(&flags, test_sv2_flag(css_calculated_style_value_contains_percentage(calc)))
test_css_line(out, 1, ak.string_builder_to_string_without_validation(&flags))
test_sv2_resolves(out, calc, "empty", CssCalculationResolutionContext())
test_sv2_resolves(out, calc, "length", CssCalculationResolutionContext(length_resolution_context = test_style_length_context()))
test_sv2_resolves(out, calc, "basis", CssCalculationResolutionContext(percentage_basis = test_sv2_basis_for(calc.m_context.percentages_resolve_as), length_resolution_context = test_style_length_context()))
## The oracle's `matrix`: a transformation's to_matrix (without a paintable box), row by row.
func test_sv2_matrix(out: ak.StringBuilder*, transformation: CssTransformationStyleValue*):
let matrix = css_transformation_style_value_to_matrix(transformation, none) catch failure:
test_css_line(out, 1, css_string_formatted("matrix: error {}", [ak.arg_str(failure.message)]))
return
var text = ak.string_builder_new()
ak.string_builder_append(&text, "matrix:")
for row in 0..<4:
for column in 0..<4:
css_appendff(&text, " {}", [ak.arg_f32(gfx.float_matrix4_x4_at_const(&matrix, usize(row), usize(column)))])
test_css_line(out, 1, ak.string_builder_to_string_without_validation(&text))
## The oracle's `independence_verifies`: a circle() or ellipse() whose position lacks an edge offset.
func test_sv2_independence_verifies(value: CssStyleValue*) -> bool:
if not css_style_value_is_basic_shape(value):
return false
var position: CssStyleValue*? = none
match css_style_value_as_basic_shape(value).m_basic_shape:
.css_circle(circle): position = circle.position
.css_ellipse(ellipse): position = ellipse.position
_: return false
let present = position else return false
return test_position_lacks_offset(css_style_value_as_position(present))
## The oracle's `value`: serializations, reparsed equality, matrices, independence, absolutization and the
## calculated details.
func test_sv2_value(out: ak.StringBuilder*, parsed: CssStyleValue*?, reparsed: CssStyleValue*? = none):
if parsed == none:
test_css_line(out, 1, ak.string_from_str("none"))
return
let value = parsed else trap("unreachable")
let normal = css_style_value_to_string(value, css_syntax.SerializationMode.normal)
test_css_line(out, 1, css_string_formatted("value: {}", [css_arg_string(&normal)]))
let resolved = css_style_value_to_string(value, css_syntax.SerializationMode.resolved_value)
test_css_line(out, 1, css_string_formatted("resolved: {}", [css_arg_string(&resolved)]))
if let other = reparsed:
test_css_line(out, 1, css_string_formatted("reparsed equal: {}", [ak.arg_str(test_sv2_flag(css_style_value_equals(value, other)))]))
if css_style_value_is_transformation(value):
test_sv2_matrix(out, css_style_value_as_transformation(value))
if css_style_value_is_value_list(value):
let items = css_style_value_list_values(css_style_value_as_value_list(value))
for item in ak.vector_span_const(&items):
if css_style_value_is_transformation(item):
test_sv2_matrix(out, css_style_value_as_transformation(item))
if css_style_value_is_counter_style_system(value):
# is_computationally_independent() VERIFYs for this class.
let system = css_style_value_as_counter_style_system(value)
var counts = ak.string_builder_new()
ak.string_builder_append(&counts, "symbol counts: ")
ak.string_builder_append(&counts, test_sv2_flag(css_counter_style_system_style_value_is_valid_symbol_count(system, 0)))
ak.string_builder_append(&counts, test_sv2_flag(css_counter_style_system_style_value_is_valid_symbol_count(system, 1)))
ak.string_builder_append(&counts, test_sv2_flag(css_counter_style_system_style_value_is_valid_symbol_count(system, 2)))
ak.string_builder_append(&counts, " additive: ")
ak.string_builder_append(&counts, test_sv2_flag(css_counter_style_system_style_value_is_valid_additive_symbol_count(system, 0)))
ak.string_builder_append(&counts, test_sv2_flag(css_counter_style_system_style_value_is_valid_additive_symbol_count(system, 1)))
test_css_line(out, 1, ak.string_builder_to_string_without_validation(&counts))
elif test_sv2_independence_verifies(value):
test_css_line(out, 1, ak.string_from_str("independent: verifies"))
else:
test_css_line(out, 1, css_string_formatted("independent: {}", [ak.arg_str(test_sv2_flag(css_style_value_is_computationally_independent(value)))]))
let context = CssComputationContext(length_resolution_context = test_style_length_context())
let absolutized = css_style_value_absolutized(value, context)
let absolutized_text = css_style_value_to_string(absolutized, css_syntax.SerializationMode.normal)
test_css_line(out, 1, css_string_formatted("absolutized: {} (same: {}, equal: {})", [css_arg_string(&absolutized_text), ak.arg_str(test_sv2_flag(absolutized == value)), ak.arg_str(test_sv2_flag(css_style_value_equals(absolutized, value)))]))
if css_style_value_is_calculated(value):
test_sv2_calculated(out, css_style_value_as_calculated(value))
if css_style_value_is_calculated(absolutized):
let absolutized_dump = css_calculated_style_value_dump(css_style_value_as_calculated(absolutized))
var replaced = ak.string_builder_new()
let absolutized_view = ak.string_bytes_as_string_view(&absolutized_dump)
for byte in ak.string_view_bytes(&absolutized_view):
if byte == '\n':
ak.string_builder_append_u8(&replaced, '/')
else:
ak.string_builder_append_u8(&replaced, byte)
test_css_line(out, 1, css_string_formatted("absolutized dump: {}", [ak.arg_string_view(ak.string_builder_string_view(&replaced))]))
# mark: Running the cases =======================================================================
## A parser over `input` in `params`, its component values and a token stream over them.
func test_sv2_parser(params: CssParsingParams, input: str) -> (CssParser, ak.Vector[css_syntax.ComponentValue]):
var parser = css_parser_create(params, ak.sv(input))
let values = css_parser_parse_as_list_of_component_values(&parser)
return (parser, values)
## Runs one oracle case (see the oracle's `run`) and returns its dump.
func test_sv2_run_case(full_mode: str, input: str) -> ak.String:
var out = ak.string_builder_new()
ak.hash_map_clear(&css_error_reporter_the().m_errors)
var params: CssParsingParams = ---
css_parsing_params_construct_css_parsing_mode(¶ms)
var mode = full_mode
var property: css_data.PropertyId? = none
while let plus = test_css_value_parsing_index_of(mode, '+'):
let option = (str)mode.bytes[0..<plus]
mode = (str)mode.bytes[plus + 1..]
if option == "svg":
css_parsing_params_construct_css_parsing_mode(¶ms, CssParsingMode.svg_presentation_attribute)
elif option.bytes.length > 5 and (str)option.bytes[0..<5] == "prop=":
property = css_data.property_id_from_string(ak.sv((str)option.bytes[5..]))
var argument = ""
if let colon = test_css_value_parsing_index_of(mode, ':'):
argument = (str)mode.bytes[colon + 1..]
mode = (str)mode.bytes[0..<colon]
var (parser, values) = test_sv2_parser(params, input)
var tokens = css_syntax.token_stream_create(values)
if let property_id = property:
ak.vector_append_t(&parser.m_value_context, CssValueParsingContext.property_id(property_id))
if mode == "type":
var (reparser, reparsed_values) = test_sv2_parser(params, input)
var reparsed_tokens = css_syntax.token_stream_create(reparsed_values)
if let property_id = property:
ak.vector_append_t(&reparser.m_value_context, CssValueParsingContext.property_id(property_id))
let reparsed = css_parser_parse_value(&reparser, test_css_value_type_named(argument), &reparsed_tokens)
test_sv2_value(&out, css_parser_parse_value(&parser, test_css_value_type_named(argument), &tokens), reparsed)
elif mode == "prop":
let id = css_data.property_id_from_string(ak.sv(argument)) else trap("unknown property")
var (reparser, reparsed_values) = test_sv2_parser(params, input)
var reparsed_tokens = css_syntax.token_stream_create(reparsed_values)
let reparsed: CssStyleValue*? = css_parser_parse_css_value(&reparser, id, &reparsed_tokens) catch:
recover none
let result: CssStyleValue*? = css_parser_parse_css_value(&parser, id, &tokens) catch:
test_css_line(&out, 1, ak.string_from_str("error"))
recover none
if let value = result:
test_sv2_value(&out, value, reparsed)
elif mode == "desc":
let slash = test_css_value_parsing_index_of(argument, '/') else trap("no descriptor")
let at_rule = css_data.AtRuleId.counter_style if (str)argument.bytes[0..<slash] == "counter-style" else css_data.AtRuleId.font_face
let descriptor = css_descriptor_name_and_id_from_name(at_rule, ak.fly_string_from_str((str)argument.bytes[slash + 1..])) else trap("unknown descriptor")
let result: CssStyleValue*? = css_parser_parse_descriptor_value(&parser, at_rule, descriptor, &tokens) catch:
test_css_line(&out, 1, ak.string_from_str("error"))
recover none
if let value = result:
test_sv2_value(&out, value)
elif mode == "ntype":
test_sv2_numeric_type_case(&out, argument, input)
else:
test_css_line(&out, 1, ak.string_from_str("unknown mode"))
test_css_dump_rest(&out, &tokens)
test_css_dump_errors(&out)
return ak.string_builder_to_string_without_validation(&out)
## How many units in the last place a printed f32 may differ from the oracle's. The oracle ran on
## macOS; transforms and calc() call the C library's tan, sin, cos, atan2, pow and exp, whose last
## bits differ between C libraries, so only macOS compares exactly (as tests_css_values_units_2 and
## luce-browser-render's gfx color tests do).
let test_sv2_libm_ulps: u64 = 0 if os.macos else 64
## Whether two printed results are equal word by word, where words that both parse as numbers may
## differ by at most test_sv2_libm_ulps as f32s.
func test_sv2_text_matches_within_libm_ulps(actual: str, expected: str) -> bool:
var a: usize = 0
var e: usize = 0
while true:
let a_start = test_sv2_skip_spaces(actual, a)
let e_start = test_sv2_skip_spaces(expected, e)
if a_start == actual.length and e_start == expected.length:
return true
if a_start == actual.length or e_start == expected.length:
return false
a = test_sv2_word_end(actual, a_start)
e = test_sv2_word_end(expected, e_start)
let a_word = (str)actual.bytes[a_start..<a]
let e_word = (str)expected.bytes[e_start..<e]
if a_word == e_word:
continue
let a_value = ak.parse_number_string_view_f32(ak.sv(a_word), ak.TrimWhitespace.no) else return false
let e_value = ak.parse_number_string_view_f32(ak.sv(e_word), ak.TrimWhitespace.no) else return false
let a_bits = (u64)a_value.bits()
let e_bits = (u64)e_value.bits()
let distance = a_bits - e_bits if a_bits >= e_bits else e_bits - a_bits
if distance > test_sv2_libm_ulps:
return false
## The first index at or after `start` that is not a space or line feed.
func test_sv2_skip_spaces(text: str, start: usize) -> usize:
var i = start
while i < text.length and (text.bytes[i] == ' ' or text.bytes[i] == '\n'):
i += 1
return i
## The end of the word that starts at `start`.
func test_sv2_word_end(text: str, start: usize) -> usize:
var i = start
while i < text.length and text.bytes[i] != ' ' and text.bytes[i] != '\n':
i += 1
return i
test "region r35's style values match the reference build on the oracle cases":
with memory.heap:
var failures = 0
for case_ in test_style_values_2_cases:
let actual = test_sv2_run_case(case_.mode, case_.input)
let actual_view = ak.string_bytes_as_string_view(&actual)
if not ak.string_view_equals(&actual_view, case_.expected) and not test_sv2_text_matches_within_libm_ulps(ak.string_view_text(actual_view), case_.expected):
failures += 1
print(f"case {case_.mode} {case_.input}\nexpected:\n{case_.expected}actual:\n{ak.string_view_text(actual_view)}")
assert(failures == 0)