aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/unit/fpconv.lua
blob: 6c43eda5b6f8bce225f8fc040a920851ccc1b0e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
-- fpconv tests

context("Fpconv printf functions", function()
  local ffi = require("ffi")
  local u = require "rspamd_util"
  local niter_fuzz = 100000
  local function small_double()
    return math.random()
  end
  local function large_double()
    return math.random() * math.random(2^52)
  end
  local function huge_double()
    return math.random(2^52) * math.random(2^52)
  end
  local function tiny_double()
    return math.random() / math.random(2^52)
  end
  ffi.cdef[[
int snprintf(char *str, size_t size, const char *format, ...);
long rspamd_snprintf(char *str, size_t size, const char *format, ...);
long rspamd_printf(const char *format, ...);
]]
  local benchmarks = {
    {'tiny fixed', small_double, '%f'},
    {'small fixed', tiny_double, '%f'},
    {'large fixed', large_double, '%.3f'},
    {'huge fixed', huge_double, '%.3f'},
    {'tiny scientific', small_double, '%g'},
    {'small scientific', tiny_double, '%g'},
    {'large scientific', large_double, '%g'},
    {'huge scientific', huge_double, '%g'},
  }

  local generic = {
    {0, '%f', '0'},
    {0, '%.1f', '0.0'},
    {0, '%.2f', '0.00'},
    {0, '%.32f', '0.000000000000000000000000000'}, -- max
    {0, '%.150f', '0.000000000000000000000000000'}, -- too large
    {1/3, '%f', '0.3333333333333333'},
    {1/3, '%.1f', '0.3'},
    {1/3, '%.2f', '0.33'},
    {-1/3, '%.32f', '-0.333333333333333300000000000'},
    {-1/3, '%.150f', '-0.333333333333333300000000000'},
    {-3.6817595395344857e-68, '%f', '-3.6817595395344857e-68'},
    {3.5844466002796428e+298, '%f', '3.5844466002796428e+298'},
    {9223372036854775808, '%f', '9223372036854776000'}, -- 2^63 with precision lost
    {2^50 + 0.2, '%f', '1125899906842624.3'}, -- 2^50 with precision lost
    {2^50 + 0.2, '%.2f', '1125899906842624.30'}, -- 2^50 with precision lost
    {-3.6817595395344857e-68, '%.3f', '-0.000'}, -- not enough precision
    {3.5844466002796428e+298, '%.3f', '3.5844466002796428e+298'},
    {9223372036854775808, '%.3f', '9223372036854776000.000'}, -- 2^63 with precision lost
    {math.huge, '%f', 'inf'},
    {-math.huge, '%f', '-inf'},
    {0.0/0.0, '%f', 'nan'},
    {math.huge, '%.1f', 'inf'},
    {-math.huge, '%.2f', '-inf'},
    {0.0/0.0, '%.3f', 'nan'},
    {math.huge, '%g', 'inf'},
    {-math.huge, '%g', '-inf'},
    {0.0/0.0, '%g', 'nan'},
  }

  local buf = ffi.new("char[64]")
  local buf2 = ffi.new("char[64]")

  for i,c in ipairs(generic) do
    test("Generic fp test fmt: " .. c[2] .. '; ' .. tostring(c[1]), function()
      ffi.C.rspamd_snprintf(buf, 64, c[2], c[1])
      local sbuf = ffi.string(buf)
      assert_equal(sbuf, c[3], c[3] .. " but test returned " .. sbuf)
    end)
  end
  for i,c in ipairs(benchmarks) do
    test("Fuzz fp test " .. c[1], function()
      for _=1,niter_fuzz do
        local sign = 1
        if math.random() > 0.5 then
          sign = -1
        end
        local d = c[2]() * sign
        ffi.C.snprintf(buf, 64, c[3], d)
        ffi.C.rspamd_snprintf(buf2, 64, c[3], d)

        local sbuf = ffi.string(buf)
        local sbuf2 = ffi.string(buf2)

        assert_less_than(math.abs(d -  tonumber(sbuf2))/math.abs(d),
            0.00001,
            string.format('rspamd emitted: %s, libc emitted: %s, original number: %g',
              sbuf2, sbuf, d))
      end
    end)
  end
end)
w"> ); QUnit.test( "No options, absolute", function( assert ) { assert.expect( 2 ); testHelper.shouldMove( assert, $( "#draggable2" ).draggable(), "no options, absolute" ); } ); QUnit.test( "resizable handle with complex markup (#8756 / #8757)", function( assert ) { assert.expect( 2 ); $( "#draggable1" ) .append( $( "<div>" ) .addClass( "ui-resizable-handle ui-resizable-w" ) .append( $( "<div>" ) ) ); var handle = $( ".ui-resizable-w div" ), target = $( "#draggable1" ).draggable().resizable( { handles: "all" } ); // Todo: fix resizable so it doesn't require a mouseover handle.simulate( "mouseover" ).simulate( "drag", { dx: -50 } ); assert.equal( target.width(), 250, "compare width" ); // Todo: fix resizable so it doesn't require a mouseover handle.simulate( "mouseover" ).simulate( "drag", { dx: 50 } ); assert.equal( target.width(), 200, "compare width" ); } ); QUnit.test( "#8269: Removing draggable element on drop", function( assert ) { assert.expect( 2 ); var element = $( "#draggable1" ).wrap( "<div id='wrapper' />" ).draggable( { stop: function() { assert.ok( true, "stop still called despite element being removed from DOM on drop" ); } } ), dropOffset = $( "#droppable" ).offset(); $( "#droppable" ).droppable( { drop: function() { $( "#wrapper" ).remove(); assert.ok( true, "element removed from DOM on drop" ); } } ); // Support: Opera 12.10, Safari 5.1, jQuery <1.8 if ( testHelper.unreliableContains ) { assert.ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" ); assert.ok( true, "Opera <12.14 and Safari <6.0 report wrong values for $.contains in jQuery < 1.8" ); } else { element.simulate( "drag", { handle: "corner", x: dropOffset.left, y: dropOffset.top } ); } } ); // http://bugs.jqueryui.com/ticket/7778 // drag element breaks in IE8 when its content is replaced onmousedown QUnit.test( "Stray mousemove after mousedown still drags", function( assert ) { assert.expect( 2 ); var element = $( "#draggable1" ).draggable( { scroll: false } ); // In IE8, when content is placed under the mouse (e.g. when draggable content is replaced // on mousedown), mousemove is triggered on those elements even though the mouse hasn't moved. // Support: IE <9 element.on( "mousedown", function() { $( document ).simulate( "mousemove", { button: -1 } ); } ); testHelper.shouldMove( assert, element, "element is draggable" ); } ); QUnit.test( "#6258: not following mouse when scrolled and using overflow-y: scroll", function( assert ) { assert.expect( 2 ); var element = $( "#draggable1" ).draggable( { stop: function( event, ui ) { assert.equal( ui.position.left, 1, "left position is correct despite overflow on HTML" ); assert.equal( ui.position.top, 1, "top position is correct despite overflow on HTML" ); $( "html" ) .css( "overflow-y", oldOverflowY ) .css( "overflow-x", oldOverflowX ) .scrollTop( 0 ) .scrollLeft( 0 ); } } ), oldOverflowY = $( "html" ).css( "overflow-y" ), oldOverflowX = $( "html" ).css( "overflow-x" ); testHelper.forceScrollableWindow(); $( "html" ) .css( "overflow-y", "scroll" ) .css( "overflow-x", "scroll" ) .scrollTop( 300 ) .scrollLeft( 300 ); element.simulate( "drag", { dx: 1, dy: 1, moves: 1 } ); } ); QUnit.test( "#9315: jumps down with offset of scrollbar", function( assert ) { assert.expect( 2 ); var element = $( "#draggable2" ).draggable( { stop: function( event, ui ) { assert.equal( ui.position.left, 11, "left position is correct when position is absolute" ); assert.equal( ui.position.top, 11, "top position is correct when position is absolute" ); $( "html" ).scrollTop( 0 ).scrollLeft( 0 ); } } ); testHelper.forceScrollableWindow(); $( "html" ).scrollTop( 300 ).scrollLeft( 300 ); element.simulate( "drag", { dx: 1, dy: 1, moves: 1 } ); } ); QUnit.test( "scroll offset with fixed ancestors", function( assert ) { assert.expect( 2 ); var startValue = 300, element = $( "#draggable1" ) // http://bugs.jqueryui.com/ticket/5009 // scroll not working with parent's position fixed .wrap( "<div id='wrapper' />" ) // http://bugs.jqueryui.com/ticket/9612 // abspos elements inside of fixed elements moving away from the mouse when scrolling .wrap( "<div id='wrapper2' />" ) .draggable( { drag: function() { startValue += 100; $( document ).scrollTop( startValue ).scrollLeft( startValue ); }, stop: function( event, ui ) { assert.equal( ui.position.left, 10, "left position is correct when parent position is fixed" ); assert.equal( ui.position.top, 10, "top position is correct when parent position is fixed" ); $( document ).scrollTop( 0 ).scrollLeft( 0 ); } } ); testHelper.forceScrollableWindow(); $( "#wrapper" ).css( "position", "fixed" ); $( "#wrapper2" ).css( "position", "absolute" ); element.simulate( "drag", { dx: 10, dy: 10, moves: 3 } ); } ); $( [ "hidden", "auto", "scroll" ] ).each( function() { var overflow = this; // Http://bugs.jqueryui.com/ticket/9379 - position bug in scrollable div // http://bugs.jqueryui.com/ticket/10147 - Wrong position in a parent with "overflow: hidden" QUnit.test( "position in scrollable parent with overflow: " + overflow, function( assert ) { assert.expect( 2 ); $( "#qunit-fixture" ).html( "<div id='outer'><div id='inner'></div><div id='dragged'>a</div></div>" ); $( "#inner" ).css( { position: "absolute", width: "500px", height: "500px" } ); $( "#outer" ).css( { position: "absolute", width: "300px", height: "300px" } ); $( "#dragged" ).css( { width: "10px", height: "10px" } ); var moves = 3, startValue = 0, dragDelta = 20, delta = 100, // We scroll after each drag event, so subtract 1 from number of moves for expected expected = delta + ( ( moves - 1 ) * dragDelta ), element = $( "#dragged" ).draggable( { drag: function() { startValue += dragDelta; $( "#outer" ).scrollTop( startValue ).scrollLeft( startValue ); }, stop: function( event, ui ) { assert.equal( ui.position.left, expected, "left position is correct when grandparent is scrolled" ); assert.equal( ui.position.top, expected, "top position is correct when grandparent is scrolled" ); } } ); $( "#outer" ).css( "overflow", overflow ); element.simulate( "drag", { dy: delta, dx: delta, moves: moves } ); } ); } ); QUnit.test( "#5727: draggable from iframe", function( assert ) { assert.expect( 1 ); var iframeBody, draggable1, iframe = $( "<iframe />" ).appendTo( "#qunit-fixture" ), iframeDoc = ( iframe[ 0 ].contentWindow || iframe[ 0 ].contentDocument ).document; iframeDoc.write( "<!doctype html><html><body>" ); iframeDoc.close(); iframeBody = $( iframeDoc.body ).append( "<div style='width: 2px; height: 2px;' />" ); draggable1 = iframeBody.find( "div" ); draggable1.draggable(); assert.equal( draggable1.closest( iframeBody ).length, 1 ); // TODO: fix draggable within an IFRAME to fire events on the element properly // and these testHelper.shouldMove relies on events for testing //testHelper.shouldMove( assert, draggable1, "draggable from an iframe" ); } ); QUnit.test( "#8399: A draggable should become the active element after you are finished interacting with it, but not before.", function( assert ) { assert.expect( 2 ); var element = $( "<a href='#'>link</a>" ).appendTo( "#qunit-fixture" ).draggable(); $( document ).one( "mousemove", function() { assert.notStrictEqual( document.activeElement, element.get( 0 ), "moving a draggable anchor did not make it the active element" ); } ); testHelper.move( element, 50, 50 ); assert.strictEqual( document.activeElement, element.get( 0 ), "finishing moving a draggable anchor made it the active element" ); } ); QUnit.test( "blur behavior - handle is main element", function( assert ) { var ready = assert.async(); assert.expect( 3 ); var element = $( "#draggable1" ).draggable(), focusElement = $( "<div tabindex='1'></div>" ).appendTo( element ); testHelper.onFocus( focusElement, function() { assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" ); testHelper.move( focusElement, 1, 1 ); // Http://bugs.jqueryui.com/ticket/10527 // Draggable: Can't select option in modal dialog (IE8) assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused after mousing down on itself" ); testHelper.move( element, 50, 50 ); // Http://bugs.jqueryui.com/ticket/4261 // active element should blur when mousing down on a draggable assert.notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" ); ready(); } ); } ); QUnit.test( "blur behavior - descendant of handle", function( assert ) { var ready = assert.async(); assert.expect( 2 ); var element = $( "#draggable2" ).draggable( { handle: "span" } ), // The handle is a descendant, but we also want to grab a descendant of the handle handle = element.find( "span em" ), focusElement = $( "<div tabindex='1'></div>" ).appendTo( element ); testHelper.onFocus( focusElement, function() { assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" ); testHelper.move( handle, 50, 50 ); // Elements outside of the handle should blur (#12472, #14905) assert.notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" ); ready(); } ); } ); QUnit.test( "blur behavior - off handle", function( assert ) { var ready = assert.async(); assert.expect( 3 ); var element = $( "#draggable2" ).draggable( { handle: "span" } ), focusElement = $( "<div tabindex='1'></div>" ).appendTo( element ); // Mock $.ui.safeBlur with a spy var _safeBlur = $.ui.safeBlur; var blurCalledCount = 0; $.ui.safeBlur = function() { blurCalledCount++; }; testHelper.onFocus( focusElement, function() { assert.strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" ); testHelper.move( element, 1, 1 ); assert.strictEqual( blurCalledCount, 0, "draggable doesn't blur when mousing down off handle" ); testHelper.move( element.find( "span" ), 1, 1 ); assert.strictEqual( blurCalledCount, 1, "draggable blurs when mousing down on handle" ); // Restore safeBlur $.ui.safeBlur = _safeBlur; ready(); } ); } ); QUnit.test( "ui-draggable-handle assigned to appropriate element", function( assert ) { assert.expect( 5 ); var p = $( "<p>" ).appendTo( "#qunit-fixture" ), element = $( "<div><p></p></div>" ).appendTo( "#qunit-fixture" ).draggable(); assert.hasClasses( element, "ui-draggable-handle" ); element.draggable( "option", "handle", "p" ); assert.lacksClasses( element, "ui-draggable-handle" ); assert.hasClasses( element.find( "p" ), "ui-draggable-handle", "ensure handle class name is constrained within the draggble (#10212)" ); assert.lacksClasses( p, "ui-draggable-handle" ); element.draggable( "destroy" ); assert.lacksClasses( element.find( "p" ), "ui-draggable-handle" ); } ); QUnit.test( "ui-draggable-handle managed correctly in nested draggables", function( assert ) { assert.expect( 4 ); var parent = $( "<div><div></div></div>" ).draggable().appendTo( "#qunit-fixture" ), child = parent.find( "div" ).draggable(); assert.hasClasses( parent, "ui-draggable-handle", "parent has class name on init" ); assert.hasClasses( child, "ui-draggable-handle", "child has class name on init" ); parent.draggable( "destroy" ); assert.lacksClasses( parent, "ui-draggable-handle", "parent loses class name on destroy" ); assert.hasClasses( child, "ui-draggable-handle", "child retains class name on destroy" ); } ); // Support: IE 8 only // IE 8 implements DOM Level 2 Events which only has events bubble up to the document. // We skip this test since it would be impossible for it to pass in such an environment. QUnit[ document.documentMode === 8 ? "skip" : "test" ]( "does not stop propagation to window", function( assert ) { assert.expect( 1 ); var element = $( "#draggable1" ).draggable(); var handler = function() { assert.ok( true, "mouseup propagates to window" ); }; $( window ).on( "mouseup", handler ); element.simulate( "drag", { dx: 10, dy: 10 } ); $( window ).off( "mouseup", handler ); } ); } );