From: Michael Stapelberg Date: Mon, 10 Sep 2012 11:23:49 +0000 (+0200) Subject: tests: make cmp_float a real test instruction X-Git-Tag: 4.3~25 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=6bfbec9da3db23c5a9864ae51598845e18716705;p=i3%2Fi3 tests: make cmp_float a real test instruction --- diff --git a/testcases/lib/i3test.pm b/testcases/lib/i3test.pm index 52deebc4..86886de1 100644 --- a/testcases/lib/i3test.pm +++ b/testcases/lib/i3test.pm @@ -33,7 +33,6 @@ our @EXPORT = qw( open_floating_window get_dock_clients cmd - cmp_float sync_with_i3 does_i3_live exit_gracefully @@ -566,13 +565,7 @@ sub launch_with_config { return $i3_pid; } -# compares two floats and return true if they differ less -# then 1e-6 -sub cmp_float { - my ($a, $b) = @_; - return abs($a - $b) < 1e-6; -} package i3test::X11; use parent 'X11::XCB::Connection'; diff --git a/testcases/lib/i3test/Test.pm b/testcases/lib/i3test/Test.pm index d83de55b..4389971b 100644 --- a/testcases/lib/i3test/Test.pm +++ b/testcases/lib/i3test/Test.pm @@ -1,8 +1,12 @@ package i3test::Test; +# vim:ts=4:sw=4:expandtab use base 'Test::Builder::Module'; -our @EXPORT = qw(is_num_children); +our @EXPORT = qw( + is_num_children + cmp_float +); my $CLASS = __PACKAGE__; @@ -54,6 +58,31 @@ sub is_num_children { $tb->is_num($got_num_children, $num_children, $name); } +=head2 cmp_float($a, $b) + +Compares floating point numbers C<$a> and C<$b> and returns true if they differ +less then 1e-6. + + $tmp = fresh_workspace; + + open_window for (1..4); + + cmd 'resize grow width 10 px or 25 ppt'; + + ($nodes, $focus) = get_ws_content($tmp); + ok(cmp_float($nodes->[0]->{percent}, 0.166666666666667), 'first window got 16%'); + ok(cmp_float($nodes->[1]->{percent}, 0.166666666666667), 'second window got 16%'); + ok(cmp_float($nodes->[2]->{percent}, 0.166666666666667), 'third window got 16%'); + ok(cmp_float($nodes->[3]->{percent}, 0.50), 'fourth window got 50%'); + +=cut +sub cmp_float { + my ($a, $b, $name) = @_; + my $tb = $CLASS->builder; + + $tb->cmp_ok(abs($a - $b), '<', 1e-6, $name); +} + =head1 AUTHOR Michael Stapelberg diff --git a/testcases/t/141-resize.t b/testcases/t/141-resize.t index 20dc68f4..e769ced7 100644 --- a/testcases/t/141-resize.t +++ b/testcases/t/141-resize.t @@ -22,8 +22,8 @@ cmd 'resize grow up 10 px or 25 ppt'; my ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.25), 'top window got only 25%'); -ok(cmp_float($nodes->[1]->{percent}, 0.75), 'bottom window got 75%'); +cmp_float($nodes->[0]->{percent}, 0.25, 'top window got only 25%'); +cmp_float($nodes->[1]->{percent}, 0.75, 'bottom window got 75%'); ############################################################ @@ -34,8 +34,8 @@ cmd 'split h'; ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.25), 'top window got only 25%'); -ok(cmp_float($nodes->[1]->{percent}, 0.75), 'bottom window got 75%'); +cmp_float($nodes->[0]->{percent}, 0.25, 'top window got only 25%'); +cmp_float($nodes->[1]->{percent}, 0.75, 'bottom window got 75%'); ############################################################ # checks that resizing within stacked/tabbed cons works @@ -52,14 +52,14 @@ cmd 'split h'; cmd 'layout stacked'; ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.5), 'top window got 50%'); -ok(cmp_float($nodes->[1]->{percent}, 0.5), 'bottom window got 50%'); +cmp_float($nodes->[0]->{percent}, 0.5, 'top window got 50%'); +cmp_float($nodes->[1]->{percent}, 0.5, 'bottom window got 50%'); cmd 'resize grow up 10 px or 25 ppt'; ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.25), 'top window got 25%'); -ok(cmp_float($nodes->[1]->{percent}, 0.75), 'bottom window got 75%'); +cmp_float($nodes->[0]->{percent}, 0.25, 'top window got 25%'); +cmp_float($nodes->[1]->{percent}, 0.75, 'bottom window got 75%'); ############################################################ # Checks that resizing in the parent's parent's orientation works. @@ -79,14 +79,14 @@ $top = open_window; $bottom = open_window; ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.5), 'left window got 50%'); -ok(cmp_float($nodes->[1]->{percent}, 0.5), 'right window got 50%'); +cmp_float($nodes->[0]->{percent}, 0.5, 'left window got 50%'); +cmp_float($nodes->[1]->{percent}, 0.5, 'right window got 50%'); cmd 'resize grow left 10 px or 25 ppt'; ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.25), 'left window got 25%'); -ok(cmp_float($nodes->[1]->{percent}, 0.75), 'right window got 75%'); +cmp_float($nodes->[0]->{percent}, 0.25, 'left window got 25%'); +cmp_float($nodes->[1]->{percent}, 0.75, 'right window got 75%'); ################################################################################ # Check that the resize grow/shrink width/height syntax works. @@ -101,8 +101,8 @@ $right = open_window; cmd 'resize grow width 10 px or 25 ppt'; ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.25), 'left window got 25%'); -ok(cmp_float($nodes->[1]->{percent}, 0.75), 'right window got 75%'); +cmp_float($nodes->[0]->{percent}, 0.25, 'left window got 25%'); +cmp_float($nodes->[1]->{percent}, 0.75, 'right window got 75%'); # Now test it with four windows $tmp = fresh_workspace; @@ -112,19 +112,19 @@ open_window for (1..4); cmd 'resize grow width 10 px or 25 ppt'; ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.166666666666667), 'first window got 16%'); -ok(cmp_float($nodes->[1]->{percent}, 0.166666666666667), 'second window got 16%'); -ok(cmp_float($nodes->[2]->{percent}, 0.166666666666667), 'third window got 16%'); -ok(cmp_float($nodes->[3]->{percent}, 0.50), 'fourth window got 50%'); +cmp_float($nodes->[0]->{percent}, 0.166666666666667, 'first window got 16%'); +cmp_float($nodes->[1]->{percent}, 0.166666666666667, 'second window got 16%'); +cmp_float($nodes->[2]->{percent}, 0.166666666666667, 'third window got 16%'); +cmp_float($nodes->[3]->{percent}, 0.50, 'fourth window got 50%'); # height should be a no-op in this situation cmd 'resize grow height 10 px or 25 ppt'; ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.166666666666667), 'first window got 16%'); -ok(cmp_float($nodes->[1]->{percent}, 0.166666666666667), 'second window got 16%'); -ok(cmp_float($nodes->[2]->{percent}, 0.166666666666667), 'third window got 16%'); -ok(cmp_float($nodes->[3]->{percent}, 0.50), 'fourth window got 50%'); +cmp_float($nodes->[0]->{percent}, 0.166666666666667, 'first window got 16%'); +cmp_float($nodes->[1]->{percent}, 0.166666666666667, 'second window got 16%'); +cmp_float($nodes->[2]->{percent}, 0.166666666666667, 'third window got 16%'); +cmp_float($nodes->[3]->{percent}, 0.50, 'fourth window got 50%'); ############################################################ diff --git a/testcases/t/191-resize-levels.t b/testcases/t/191-resize-levels.t index 230a4915..9eef6c0c 100644 --- a/testcases/t/191-resize-levels.t +++ b/testcases/t/191-resize-levels.t @@ -23,7 +23,7 @@ cmd 'resize grow left 10px or 25ppt'; my ($nodes, $focus) = get_ws_content($tmp); -ok(cmp_float($nodes->[0]->{percent}, 0.25), 'left container got only 25%'); -ok(cmp_float($nodes->[1]->{percent}, 0.75), 'right container got 75%'); +cmp_float($nodes->[0]->{percent}, 0.25, 'left container got only 25%'); +cmp_float($nodes->[1]->{percent}, 0.75, 'right container got 75%'); done_testing;