module render import math import vx11 #include "@VMODROOT/render/tests/abi.h" fn C.vx11_render_test_fill_format(&Format) fn C.vx11_render_test_fill_attributes(&PictureAttributes) fn C.vx11_render_test_fill_index(&IndexValue) fn C.vx11_render_test_fill_anim(&AnimCursor) fn C.vx11_render_test_fill_glyph(&GlyphInfo) fn C.vx11_render_test_fill_elt(&GlyphElt32) fn C.vx11_render_test_fill_trapezoid(&Trapezoid) fn C.vx11_render_test_fill_radial(&RadialGradient) fn C.vx11_render_test_fill_trap(&Trap) fn C.vx11_render_test_fill_filters(&Filters, &i16) fn C.vx11_render_test_constants(&i32) &vx11.CLong fn C.vx11_render_test_string(i32) &char const all_ones = ~vx11.CULong(0) fn test_c_long_and_short_field_widths() { mut f := Format{} C.vx11_render_test_fill_format(&f) assert f.id == PictFormat(all_ones) assert f.@type == i32(-2147483648) assert f.depth == i32(2147483647) assert f.direct.red == i16(-32768) assert f.direct.alphaMask == i16(32767) assert f.colormap == vx11.Colormap(all_ones) mut a := PictureAttributes{} C.vx11_render_test_fill_attributes(&a) assert a.alpha_map == Picture(all_ones) assert a.clip_mask == vx11.Pixmap(all_ones) assert a.dither == vx11.Atom(all_ones) assert a.component_alpha == i32(-2147483648) mut index := IndexValue{} C.vx11_render_test_fill_index(&index) assert index.pixel == all_ones assert index.alpha == u16(0xffff) mut frame := AnimCursor{} C.vx11_render_test_fill_anim(&frame) assert frame.cursor == vx11.Cursor(all_ones) assert frame.delay == all_ones mut glyph := GlyphInfo{} C.vx11_render_test_fill_glyph(&glyph) assert glyph.width == u16(0xffff) assert glyph.yOff == i16(-32768) mut elt := GlyphElt32{ chars: unsafe { nil } } C.vx11_render_test_fill_elt(&elt) assert elt.glyphset == GlyphSet(all_ones) assert elt.yOff == i32(-2147483648) mut trapezoid := Trapezoid{} C.vx11_render_test_fill_trapezoid(&trapezoid) assert trapezoid.bottom == Fixed(2147483647) assert trapezoid.right.p2.y == Fixed(-2147483648) mut radial := RadialGradient{} C.vx11_render_test_fill_radial(&radial) assert radial.outer.radius == Fixed(-2147483648) mut trap := Trap{} C.vx11_render_test_fill_trap(&trap) assert trap.bottom.y == Fixed(-2147483648) mut aliases := [i16(0)] mut filters := Filters{ filter: unsafe { nil }, alias: unsafe { nil } } C.vx11_render_test_fill_filters(&filters, aliases.data) assert filters.nfilter == i32(2147483647) assert unsafe { filters.alias[0] } == i16(-32768) } fn test_constants_match_headers() { mut count := i32(0) values := C.vx11_render_test_constants(&count) expected := [ i64(major_version), i64(minor_version), i64(x_query_version), i64(x_query_pict_formats), i64(x_query_pict_index_values), i64(x_query_dithers), i64(x_create_picture), i64(x_change_picture), i64(x_set_picture_clip_rectangles), i64(x_free_picture), i64(x_composite), i64(x_scale), i64(x_trapezoids), i64(x_triangles), i64(x_tri_strip), i64(x_tri_fan), i64(x_color_trapezoids), i64(x_color_triangles), i64(x_create_glyph_set), i64(x_reference_glyph_set), i64(x_free_glyph_set), i64(x_add_glyphs), i64(x_add_glyphs_from_picture), i64(x_free_glyphs), i64(x_composite_glyphs8), i64(x_composite_glyphs16), i64(x_composite_glyphs32), i64(x_fill_rectangles), i64(x_create_cursor), i64(x_set_picture_transform), i64(x_query_filters), i64(x_set_picture_filter), i64(x_create_anim_cursor), i64(x_add_traps), i64(x_create_solid_fill), i64(x_create_linear_gradient), i64(x_create_radial_gradient), i64(x_create_conical_gradient), i64(number_requests), i64(bad_pict_format), i64(bad_picture), i64(bad_pict_op), i64(bad_glyph_set), i64(bad_glyph), i64(number_errors), i64(pict_type_indexed), i64(pict_type_direct), i64(op_minimum), i64(op_clear), i64(op_src), i64(op_dst), i64(op_over), i64(op_over_reverse), i64(op_in), i64(op_in_reverse), i64(op_out), i64(op_out_reverse), i64(op_atop), i64(op_atop_reverse), i64(op_xor), i64(op_add), i64(op_saturate), i64(op_maximum), i64(op_disjoint_minimum), i64(op_disjoint_clear), i64(op_disjoint_src), i64(op_disjoint_dst), i64(op_disjoint_over), i64(op_disjoint_over_reverse), i64(op_disjoint_in), i64(op_disjoint_in_reverse), i64(op_disjoint_out), i64(op_disjoint_out_reverse), i64(op_disjoint_atop), i64(op_disjoint_atop_reverse), i64(op_disjoint_xor), i64(op_disjoint_maximum), i64(op_conjoint_minimum), i64(op_conjoint_clear), i64(op_conjoint_src), i64(op_conjoint_dst), i64(op_conjoint_over), i64(op_conjoint_over_reverse), i64(op_conjoint_in), i64(op_conjoint_in_reverse), i64(op_conjoint_out), i64(op_conjoint_out_reverse), i64(op_conjoint_atop), i64(op_conjoint_atop_reverse), i64(op_conjoint_xor), i64(op_conjoint_maximum), i64(op_blend_minimum), i64(op_multiply), i64(op_screen), i64(op_overlay), i64(op_darken), i64(op_lighten), i64(op_color_dodge), i64(op_color_burn), i64(op_hard_light), i64(op_soft_light), i64(op_difference), i64(op_exclusion), i64(op_hsl_hue), i64(op_hsl_saturation), i64(op_hsl_color), i64(op_hsl_luminosity), i64(op_blend_maximum), i64(poly_edge_sharp), i64(poly_edge_smooth), i64(poly_mode_precise), i64(poly_mode_imprecise), i64(cp_repeat), i64(cp_alpha_map), i64(cp_alpha_x_origin), i64(cp_alpha_y_origin), i64(cp_clip_x_origin), i64(cp_clip_y_origin), i64(cp_clip_mask), i64(cp_graphics_exposure), i64(cp_subwindow_mode), i64(cp_poly_edge), i64(cp_poly_mode), i64(cp_dither), i64(cp_component_alpha), i64(cp_last_bit), i64(filter_alias_none), i64(subpixel_unknown), i64(subpixel_horizontal_rgb), i64(subpixel_horizontal_bgr), i64(subpixel_vertical_rgb), i64(subpixel_vertical_bgr), i64(subpixel_none), i64(repeat_none), i64(repeat_normal), i64(repeat_pad), i64(repeat_reflect), i64(format_id), i64(format_type), i64(format_depth), i64(format_red), i64(format_red_mask), i64(format_green), i64(format_green_mask), i64(format_blue), i64(format_blue_mask), i64(format_alpha), i64(format_alpha_mask), i64(format_colormap), i64(standard_argb32), i64(standard_rgb24), i64(standard_a8), i64(standard_a4), i64(standard_a1), i64(standard_num), ] assert int(count) == expected.len for i, value in expected { assert value == i64(unsafe { values[i] }), 'constant ${i}' } texts := [name, filter_nearest, filter_bilinear, filter_convolution, filter_fast, filter_good, filter_best] for i, text in texts { assert text == unsafe { cstring_to_vstring(C.vx11_render_test_string(i32(i))) } } } fn test_fixed_point_conversion() { assert fixed(1.0) == 65536 assert fixed(0.5) == 32768 assert fixed(-2.0) == -131072 assert fixed(0.0) == 0 assert fixed_to_double(fixed(-3.25)) == -3.25 assert to_fixed(32767.5)! == fixed(32767.5) assert to_fixed(-32768.0)! == Fixed(-2147483648) for bad in [32768.0, -32768.5, math.inf(1), math.nan()] { if _ := to_fixed(bad) { assert false, 'accepted ${bad}' } } } fn test_transforms() { t := identity_transform() for row in 0 .. 3 { for col in 0 .. 3 { assert t.matrix[row][col] == if row == col { 65536 } else { 0 } } } s := scale_transform(0.5, 2)! assert s.matrix[0][0] == 32768 && s.matrix[1][1] == 131072 && s.matrix[2][2] == 65536 if _ := scale_transform(40000, 1) { assert false } } fn test_premultiplied_colors() { c := premultiplied(0xffff, 0x8000, 0, 0x8000) assert c.red == 0x8000 && c.green == 0x4000 && c.blue == 0 && c.alpha == 0x8000 opaque := premultiplied(0x1234, 0x5678, 0x9abc, 0xffff) assert opaque.red == 0x1234 && opaque.green == 0x5678 && opaque.blue == 0x9abc clear := premultiplied(0xffff, 0xffff, 0xffff, 0) assert clear.red == 0 && clear.alpha == 0 } fn test_operator_and_repeat_versions() { old := &Extension{ connection: unsafe { nil }, major: 0, minor: 1 } modern := &Extension{ connection: unsafe { nil }, major: 0, minor: 11 } old.check_op(op_saturate)! modern.check_op(op_hsl_luminosity)! modern.check_op(op_conjoint_xor)! for op in [op_disjoint_over, op_multiply] { if _ := old.check_op(op) { assert false, 'old server accepted ${op}' } } for op in [-1, 14, 0x1c, 0x2c, 0x3f] { if _ := modern.check_op(op) { assert false, 'accepted invalid operator ${op}' } } old.check_repeat(repeat_normal)! modern.check_repeat(repeat_reflect)! if _ := old.check_repeat(repeat_pad) { assert false } if _ := modern.check_repeat(4) { assert false } assert modern.supports(0, 10) && !old.supports(0, 2) && old.supports(0, 0) } fn test_gradient_stop_validation() { white := Color{ red: 0xffff, green: 0xffff, blue: 0xffff, alpha: 0xffff } offsets, colors := stop_arrays([ColorStop{0, white}, ColorStop{0.5, white}, ColorStop{0.5, white}, ColorStop{1, white}])! assert offsets == [Fixed(0), 32768, 32768, 65536] assert colors.len == 4 for bad in [[]ColorStop{}, [ColorStop{0.5, white}, ColorStop{0.25, white}], [ColorStop{-0.1, white}], [ColorStop{1.5, white}]] { if _, _ := stop_arrays(bad) { assert false, 'accepted ${bad.len} stops' } } } fn test_rectangle_limits() { raw := x_rectangles(unsafe { nil }, [Rectangle{-32768, 32767, 65535, 0}], -1)! assert raw[0].x == -32768 && raw[0].y == 32767 && raw[0].width == 65535 for bad in [Rectangle{32768, 0, 1, 1}, Rectangle{0, 0, -1, 1}, Rectangle{0, 0, 1, 65536}] { if _ := x_rectangles(unsafe { nil }, [bad], -1) { assert false } } } fn test_glyph_strides() { e := &Extension{ connection: unsafe { nil } } a1 := &GlyphSetHandle{ extension: e, bits_per_pixel: 1 } a8 := &GlyphSetHandle{ extension: e, bits_per_pixel: 8 } argb := &GlyphSetHandle{ extension: e, bits_per_pixel: 32 } assert a1.stride(1) == 4 && a1.stride(32) == 4 && a1.stride(33) == 8 assert a8.stride(1) == 4 && a8.stride(5) == 8 assert argb.stride(3) == 12 assert a8.stride(0) == 0 }